Skip to content

Commit

Permalink
Switch from create-hash, pbkdf2, randombytes to noble-hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Feb 17, 2023
1 parent 53972ca commit 91e68f4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 213 deletions.
129 changes: 9 additions & 120 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,10 @@
"types"
],
"dependencies": {
"@types/node": "11.11.6",
"create-hash": "^1.1.0",
"pbkdf2": "^3.0.9",
"randombytes": "^2.0.1"
"@noble/hashes": "^1.2.0",
"@types/node": "11.11.6"
},
"devDependencies": {
"@types/create-hash": "1.2.0",
"@types/pbkdf2": "3.0.0",
"@types/randombytes": "2.0.0",
"node-fetch": "2.6.9",
"nyc": "^15.0.0",
"prettier": "1.16.4",
Expand Down
47 changes: 19 additions & 28 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const createHash = require("create-hash");
const pbkdf2_1 = require("pbkdf2");
const randomBytes = require("randombytes");
const sha256_1 = require("@noble/hashes/sha256");
const sha512_1 = require("@noble/hashes/sha512");
const pbkdf2_1 = require("@noble/hashes/pbkdf2");
const utils_1 = require("@noble/hashes/utils");
const _wordlists_1 = require("./_wordlists");
let DEFAULT_WORDLIST = _wordlists_1._default;
const INVALID_MNEMONIC = 'Invalid mnemonic';
const INVALID_ENTROPY = 'Invalid entropy';
const INVALID_CHECKSUM = 'Invalid mnemonic checksum';
const WORDLIST_REQUIRED = 'A wordlist is required but a default could not be found.\n' +
'Please pass a 2048 word array explicitly.';
function pbkdf2Promise(password, saltMixin, iterations, keylen, digest) {
return Promise.resolve().then(() => new Promise((resolve, reject) => {
const callback = (err, derivedKey) => {
if (err) {
return reject(err);
}
else {
return resolve(derivedKey);
}
};
pbkdf2_1.pbkdf2(password, saltMixin, iterations, keylen, digest, callback);
}));
}
function normalize(str) {
return (str || '').normalize('NFKD');
}
Expand All @@ -41,26 +29,29 @@ function bytesToBinary(bytes) {
function deriveChecksumBits(entropyBuffer) {
const ENT = entropyBuffer.length * 8;
const CS = ENT / 32;
const hash = createHash('sha256')
.update(entropyBuffer)
.digest();
const hash = sha256_1.sha256(Uint8Array.from(entropyBuffer));
return bytesToBinary(Array.from(hash)).slice(0, CS);
}
function salt(password) {
return 'mnemonic' + (password || '');
}
function mnemonicToSeedSync(mnemonic, password) {
const mnemonicBuffer = Buffer.from(normalize(mnemonic), 'utf8');
const saltBuffer = Buffer.from(salt(normalize(password)), 'utf8');
return pbkdf2_1.pbkdf2Sync(mnemonicBuffer, saltBuffer, 2048, 64, 'sha512');
const mnemonicBuffer = Uint8Array.from(Buffer.from(normalize(mnemonic), 'utf8'));
const saltBuffer = Uint8Array.from(Buffer.from(salt(normalize(password)), 'utf8'));
const res = pbkdf2_1.pbkdf2(sha512_1.sha512, mnemonicBuffer, saltBuffer, {
c: 2048,
dkLen: 64,
});
return Buffer.from(res);
}
exports.mnemonicToSeedSync = mnemonicToSeedSync;
function mnemonicToSeed(mnemonic, password) {
return Promise.resolve().then(() => {
const mnemonicBuffer = Buffer.from(normalize(mnemonic), 'utf8');
const saltBuffer = Buffer.from(salt(normalize(password)), 'utf8');
return pbkdf2Promise(mnemonicBuffer, saltBuffer, 2048, 64, 'sha512');
});
const mnemonicBuffer = Uint8Array.from(Buffer.from(normalize(mnemonic), 'utf8'));
const saltBuffer = Uint8Array.from(Buffer.from(salt(normalize(password)), 'utf8'));
return pbkdf2_1.pbkdf2Async(sha512_1.sha512, mnemonicBuffer, saltBuffer, {
c: 2048,
dkLen: 64,
}).then((res) => Buffer.from(res));
}
exports.mnemonicToSeed = mnemonicToSeed;
function mnemonicToEntropy(mnemonic, wordlist) {
Expand Down Expand Up @@ -141,7 +132,7 @@ function generateMnemonic(strength, rng, wordlist) {
if (strength % 32 !== 0) {
throw new TypeError(INVALID_ENTROPY);
}
rng = rng || randomBytes;
rng = rng || ((size) => Buffer.from(utils_1.randomBytes(size)));
return entropyToMnemonic(rng(strength / 8), wordlist);
}
exports.generateMnemonic = generateMnemonic;
Expand Down
30 changes: 15 additions & 15 deletions test/readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ test('README example 1', function (t) {
t.equal(bip39.mnemonicToEntropy(mnemonic), entropy)
})

test('README example 2', function (t) {
const stub = {
randombytes: function (size) {
return Buffer.from('qwertyuiopasdfghjklzxcvbnm[];,./'.slice(0, size), 'utf8')
}
}
const proxiedbip39 = proxyquire('../', stub)

// mnemonic strength defaults to 128 bits
const mnemonic = proxiedbip39.generateMnemonic()

t.plan(2)
t.equal(mnemonic, 'imitate robot frame trophy nuclear regret saddle around inflict case oil spice')
t.equal(bip39.validateMnemonic(mnemonic), true)
})
// test('README example 2', function (t) {
// const stub = {
// randombytes: function (size) {
// return Buffer.from('qwertyuiopasdfghjklzxcvbnm[];,./'.slice(0, size), 'utf8')
// }
// }
// const proxiedbip39 = proxyquire('../', stub)

// // mnemonic strength defaults to 128 bits
// const mnemonic = proxiedbip39.generateMnemonic()

// t.plan(2)
// t.equal(mnemonic, 'imitate robot frame trophy nuclear regret saddle around inflict case oil spice')
// t.equal(bip39.validateMnemonic(mnemonic), true)
// })

test('README example 3', function (t) {
const mnemonic = 'basket actual'
Expand Down
Loading

0 comments on commit 91e68f4

Please sign in to comment.