Skip to content
This repository was archived by the owner on May 26, 2023. It is now read-only.

Commit 3bc32a6

Browse files
authored
Merge pull request #281 from Zilliqa/fix/nodejs-crypto-scrypt
fix/nodejs-crypto-scrypt-undefined-error
2 parents e88d379 + f879c75 commit 3bc32a6

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

examples/webpack/html/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<!-- Please generate zilliqa-min.js using yarn build:web-->>
88
<script src="zilliqa.min.js"></script>
99
<script src="index.js"></script>
10+
<script src="load-keystore-demo.js"></script>
1011
</head>
1112
<body>
1213
</body>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
async function test() {
2+
const zilliqa = new Zilliqa.Zilliqa('https://dev-api.zilliqa.com');
3+
const keystore = `{"address":"0x3591c6b333776a5b6c885Ada53b4462dEe8c67B6","crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"1b23d4a09fa241b9cbe176fbc7d9567f"},"ciphertext":"40cce0a0b4eee4e4258341e18d880ff4b12ed25887252b90b7de4b6be86da5d5","kdf":"scrypt","kdfparams":{"salt":"4997f389a370424072967f6faeb709c238ff3cc3606a4101fc3dcbdd74b67108","n":8192,"c":262144,"r":8,"p":1,"dklen":32},"mac":"97f0677a9f0ad0c6052896e1b5ae94f86025047b9fe2617c1fce220af0db0e82"},"id":"39626239-3338-4138-b662-363663333030","version":3}`;
4+
const keystoreAddress = await zilliqa.wallet.addByKeystore(keystore, "strong_password");
5+
const exportedKeystore = await zilliqa.wallet.export("0x3591c6b333776a5b6c885Ada53b4462dEe8c67B6", "strong_password", 'scrypt');
6+
7+
console.log("keystore loaded - address: %o", keystoreAddress);
8+
console.log("exported keystore: %o", exportedKeystore);
9+
}
10+
11+
test();

packages/zilliqa-js-crypto/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"hmac-drbg": "^1.0.1",
3434
"pbkdf2": "^3.0.16",
3535
"randombytes": "^2.0.6",
36+
"scrypt-js": "^3.0.1",
3637
"scryptsy": "^2.1.0",
3738
"sodium-native": "^3.2.0",
3839
"uuid": "^3.3.2"

packages/zilliqa-js-crypto/src/keystore.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import aes from 'aes-js';
1919
import hashjs from 'hash.js';
2020
import { pbkdf2Sync } from 'pbkdf2';
21-
import crypto from 'crypto';
21+
import scrypt from 'scrypt-js';
2222
import uuid from 'uuid';
2323

2424
import { bytes } from '@zilliqa-js/util';
@@ -60,7 +60,8 @@ async function getDerivedKey(
6060

6161
if (kdf === 'scrypt') {
6262
const { n, r, p, dklen } = params as ScryptParams;
63-
return crypto.scryptSync(key, salt, dklen, { N: n, r: r, p: p });
63+
const derivedKeyInt8Array = scrypt.syncScrypt(key, salt, n, r, p, dklen);
64+
return Buffer.from(derivedKeyInt8Array);
6465
}
6566

6667
throw new Error('Only pbkdf2 and scrypt are supported');

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8940,6 +8940,11 @@ schema-utils@^1.0.0:
89408940
ajv-errors "^1.0.0"
89418941
ajv-keywords "^3.1.0"
89428942

8943+
scrypt-js@^3.0.1:
8944+
version "3.0.1"
8945+
resolved "https://registry.npmjs.org/scrypt-js/-/scrypt-js-3.0.1.tgz#d314a57c2aef69d1ad98a138a21fe9eafa9ee312"
8946+
integrity sha512-cdwTTnqPu0Hyvf5in5asVdZocVDTNRmR7XEcJuIzMjJeSHybHl7vpB66AzwTaIg6CLSbtjcxc8fqcySfnTkccA==
8947+
89438948
scryptsy@^2.1.0:
89448949
version "2.1.0"
89458950
resolved "https://registry.npmjs.org/scryptsy/-/scryptsy-2.1.0.tgz#8d1e8d0c025b58fdd25b6fa9a0dc905ee8faa790"

0 commit comments

Comments
 (0)