Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 5c3ee93

Browse files
authored
Merge pull request #81 from ethereumjs/upgrade-crypto-deps
Upgrade crypto deps
2 parents 6de4676 + 2f93a76 commit 5c3ee93

File tree

4 files changed

+32
-11789
lines changed

4 files changed

+32
-11789
lines changed

.travis.yml

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,16 @@ branches:
44
only:
55
- master
66

7-
env:
8-
- CXX=g++-4.8
9-
107
node_js:
8+
- "14"
9+
- "13"
10+
- "12"
11+
- "10"
1112
- "8"
12-
- "7"
13-
- "6"
14-
- "5"
15-
- "4"
16-
17-
addons:
18-
apt:
19-
sources:
20-
- ubuntu-toolchain-r-test
21-
packages:
22-
- gcc-4.8
23-
- g++-4.8
2413

2514
before_script:
2615
- npm install
2716

2817
script:
2918
- npm run lint
30-
- istanbul cover -x **/lib/** ./node_modules/mocha/bin/_mocha test/keys.js --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage
19+
- istanbul cover ./node_modules/mocha/bin/_mocha test/keys.js --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage

index.js

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var sjcl = require("sjcl");
1111
var uuid = require("uuid");
1212
var secp256k1 = require("secp256k1/elliptic");
1313
var createKeccakHash = require("keccak/js");
14+
var scrypt = require("scrypt-js");
1415

1516
function isFunction(f) {
1617
return typeof f === "function";
@@ -152,7 +153,9 @@ module.exports = {
152153
privateKeyBuffer
153154
]);
154155
}
155-
publicKey = secp256k1.publicKeyCreate(privateKeyBuffer, false).slice(1);
156+
publicKey = Buffer.from(
157+
secp256k1.publicKeyCreate(privateKeyBuffer, false).slice(1)
158+
);
156159
return "0x" + keccak256(publicKey).slice(-20).toString("hex");
157160
},
158161

@@ -177,44 +180,21 @@ module.exports = {
177180
/**
178181
* Used internally.
179182
*/
180-
deriveKeyUsingScryptInNode: function (password, salt, options, cb) {
181-
if (!isFunction(cb)) return this.deriveKeyUsingScryptInBrowser(password, salt, options);
182-
require("scrypt").hash(password, {
183-
N: options.kdfparams.n || this.constants.scrypt.n,
184-
r: options.kdfparams.r || this.constants.scrypt.r,
185-
p: options.kdfparams.p || this.constants.scrypt.p
186-
}, options.kdfparams.dklen || this.constants.scrypt.dklen, salt).then(cb).catch(cb);
187-
},
188-
189-
/**
190-
* Used internally.
191-
*/
192-
deriveKeyUsingScryptInBrowser: function (password, salt, options, cb) {
193-
var self = this;
194-
if (this.scrypt === null) this.scrypt = require("./lib/scrypt");
195-
if (isFunction(this.scrypt)) {
196-
this.scrypt = this.scrypt(options.kdfparams.memory || this.constants.scrypt.memory);
197-
}
198-
if (!isFunction(cb)) {
199-
return Buffer.from(this.scrypt.to_hex(this.scrypt.crypto_scrypt(
200-
password,
201-
salt,
202-
options.kdfparams.n || this.constants.scrypt.n,
203-
options.kdfparams.r || this.constants.scrypt.r,
204-
options.kdfparams.p || this.constants.scrypt.p,
205-
options.kdfparams.dklen || this.constants.scrypt.dklen
206-
)), "hex");
183+
deriveKeyUsingScrypt: function (password, salt, options, cb) {
184+
var n = options.kdfparams.n || this.constants.scrypt.n;
185+
var r = options.kdfparams.r || this.constants.scrypt.r;
186+
var p = options.kdfparams.p || this.constants.scrypt.p;
187+
var dklen = options.kdfparams.dklen || this.constants.scrypt.dklen;
188+
if (isFunction(cb)) {
189+
scrypt
190+
.scrypt(password, salt, n, r, p, dklen)
191+
.then(function (key) {
192+
cb(Buffer.from(key));
193+
})
194+
.catch(cb);
195+
} else {
196+
return Buffer.from(scrypt.syncScrypt(password, salt, n, r, p, dklen));
207197
}
208-
setTimeout(function () {
209-
cb(Buffer.from(self.scrypt.to_hex(self.scrypt.crypto_scrypt(
210-
password,
211-
salt,
212-
options.kdfparams.n || self.constants.scrypt.n,
213-
options.kdfparams.r || self.constants.scrypt.r,
214-
options.kdfparams.p || self.constants.scrypt.p,
215-
options.kdfparams.dklen || self.constants.scrypt.dklen
216-
)), "hex"));
217-
}, 0);
218198
},
219199

220200
/**
@@ -242,8 +222,7 @@ module.exports = {
242222

243223
// use scrypt as key derivation function
244224
if (options.kdf === "scrypt") {
245-
if (!this.browser) return this.deriveKeyUsingScryptInNode(password, salt, options, cb);
246-
return this.deriveKeyUsingScryptInBrowser(password, salt, options, cb);
225+
return this.deriveKeyUsingScrypt(password, salt, options, cb);
247226
}
248227

249228
// use default key derivation function (PBKDF2)

0 commit comments

Comments
 (0)