-
Notifications
You must be signed in to change notification settings - Fork 433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Limit ENT to 128-256, multiple of 4 (cont.) #49
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f55f62f
move helper function definitions before usage
dcousens ed79ca8
tests: add missing Error prefix
dcousens 6a8d7c9
tests: show mnemonic in test desc.
dcousens 950ce92
README/tests: isolate README tests to new file
dcousens 5bf0b64
enforce 128-256 size ENT, multiple of 4
dcousens dcf3c1b
remove inline use of parseInt
dcousens 3854893
remove double iteration of words
dcousens bb6bd4c
TypeErrors for entropy length
dcousens e908612
consistent checksumBits naming
dcousens c1e8093
check entropyBytes for ENT constraints, for readability
dcousens 59cfe5d
package: standard 9 to avoid safe-buffer issues
dcousens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,21 @@ var randomBytes = require('randombytes') | |
// use unorm until String.prototype.normalize gets better browser support | ||
var unorm = require('unorm') | ||
|
||
function lpad (str, padString, length) { | ||
while (str.length < length) str = padString + str | ||
return str | ||
} | ||
|
||
var ENGLISH_WORDLIST = require('./wordlists/english.json') | ||
var FRENCH_WORDLIST = require('./wordlists/french.json') | ||
var ITALIAN_WORDLIST = require('./wordlists/italian.json') | ||
var JAPANESE_WORDLIST = require('./wordlists/japanese.json') | ||
var SPANISH_WORDLIST = require('./wordlists/spanish.json') | ||
var DEFAULT_WORDLIST = ENGLISH_WORDLIST | ||
|
||
var INVALID_MNEMONIC = 'Invalid mnemonic' | ||
var INVALID_ENTROPY = 'Invalid entropy' | ||
|
||
function salt (password) { | ||
return 'mnemonic' + (password || '') | ||
} | ||
|
@@ -31,21 +39,19 @@ function mnemonicToEntropy (mnemonic, wordlist) { | |
wordlist = wordlist || DEFAULT_WORDLIST | ||
|
||
var words = unorm.nfkd(mnemonic).split(' ') | ||
if (words.length % 3 !== 0) throw new Error('Invalid mnemonic') | ||
if (words.length % 3 !== 0) throw new Error(INVALID_MNEMONIC) | ||
if (words.some(function (word) { | ||
return wordlist.indexOf(word) === -1 | ||
})) throw new Error('Invalid mnemonic') | ||
})) throw new Error(INVALID_MNEMONIC) | ||
|
||
// convert word indices to 11 bit binary strings | ||
var bits = words.map(function (word) { | ||
var index = wordlist.indexOf(word) | ||
return lpad(index.toString(2), '0', 11) | ||
}).join('') | ||
|
||
// max entropy is 1024; (1024×8)+((1024×8)÷32) = 8448 | ||
if (bits.length > 8448) { | ||
throw new Error('Invalid mnemonic') | ||
} | ||
if (bits.length < 128) throw new Error(INVALID_MNEMONIC) | ||
if (bits.length > 264) throw new Error(INVALID_MNEMONIC) | ||
|
||
// split the binary string into ENT/CS | ||
var dividerIndex = Math.floor(bits.length / 33) * 32 | ||
|
@@ -56,25 +62,28 @@ function mnemonicToEntropy (mnemonic, wordlist) { | |
var entropyBytes = entropy.match(/(.{1,8})/g).map(function (bin) { | ||
return parseInt(bin, 2) | ||
}) | ||
if (entropy.length % 4 !== 0) throw new TypeError(INVALID_ENTROPY) | ||
var entropyBuffer = new Buffer(entropyBytes) | ||
var newChecksum = checksumBits(entropyBuffer) | ||
|
||
var newChecksum = checksumBits(entropyBuffer) | ||
if (newChecksum !== checksum) throw new Error('Invalid mnemonic checksum') | ||
|
||
return entropyBuffer.toString('hex') | ||
} | ||
|
||
function entropyToMnemonic (entropy, wordlist) { | ||
function entropyToMnemonic (entropyHex, wordlist) { | ||
wordlist = wordlist || DEFAULT_WORDLIST | ||
|
||
var entropyBuffer = new Buffer(entropy, 'hex') | ||
// 128 <= ENT <= 256 | ||
if (entropyHex.length < 32) throw new Error(INVALID_ENTROPY) | ||
if (entropyHex.length > 64) throw new Error(INVALID_ENTROPY) | ||
|
||
if (entropyBuffer.length === 0 || entropyBuffer.length > 1024 || entropyBuffer.length % 4 !== 0) { | ||
throw new Error('Invalid entropy') | ||
} | ||
// multiple of 4 | ||
if (entropyHex.length % 8 !== 0) throw new Error(INVALID_ENTROPY) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. boo hex |
||
|
||
var entropyBits = bytesToBinary([].slice.call(entropyBuffer)) | ||
var checksum = checksumBits(entropyBuffer) | ||
var entropy = new Buffer(entropyHex, 'hex') | ||
var entropyBits = bytesToBinary([].slice.call(entropy)) | ||
var checksum = checksumBits(entropy) | ||
|
||
var bits = entropyBits + checksum | ||
var chunks = bits.match(/(.{1,11})/g) | ||
|
@@ -90,6 +99,7 @@ function entropyToMnemonic (entropy, wordlist) { | |
|
||
function generateMnemonic (strength, rng, wordlist) { | ||
strength = strength || 128 | ||
if (strength % 32 !== 0) throw new TypeError(INVALID_ENTROPY) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not really needed here, would be caught by |
||
rng = rng || randomBytes | ||
|
||
var hex = rng(strength / 8).toString('hex') | ||
|
@@ -106,6 +116,12 @@ function validateMnemonic (mnemonic, wordlist) { | |
return true | ||
} | ||
|
||
function bytesToBinary (bytes) { | ||
return bytes.map(function (x) { | ||
return lpad(x.toString(2), '0', 8) | ||
}).join('') | ||
} | ||
|
||
function checksumBits (entropyBuffer) { | ||
var hash = createHash('sha256').update(entropyBuffer).digest() | ||
|
||
|
@@ -116,19 +132,6 @@ function checksumBits (entropyBuffer) { | |
return bytesToBinary([].slice.call(hash)).slice(0, CS) | ||
} | ||
|
||
// =========== helper methods from bitcoinjs-lib ======== | ||
|
||
function bytesToBinary (bytes) { | ||
return bytes.map(function (x) { | ||
return lpad(x.toString(2), '0', 8) | ||
}).join('') | ||
} | ||
|
||
function lpad (str, padString, length) { | ||
while (str.length < length) str = padString + str | ||
return str | ||
} | ||
|
||
module.exports = { | ||
mnemonicToSeed: mnemonicToSeed, | ||
mnemonicToSeedHex: mnemonicToSeedHex, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
var bip39 = require('../') | ||
var proxyquire = require('proxyquire') | ||
var test = require('tape') | ||
|
||
test('README example 1', function (t) { | ||
// defaults to BIP39 English word list | ||
var entropy = 'ffffffffffffffffffffffffffffffff' | ||
var mnemonic = bip39.entropyToMnemonic(entropy) | ||
|
||
t.plan(2) | ||
t.equal(mnemonic, 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong') | ||
|
||
// reversible | ||
t.equal(bip39.mnemonicToEntropy(mnemonic), entropy) | ||
}) | ||
|
||
test('README example 2', function (t) { | ||
var stub = { | ||
randombytes: function (size) { | ||
return new Buffer('qwertyuiopasdfghjklzxcvbnm[];,./'.slice(0, size)) | ||
} | ||
} | ||
var proxiedbip39 = proxyquire('../', stub) | ||
|
||
// mnemonic strength defaults to 128 bits | ||
var 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) { | ||
var mnemonic = 'basket actual' | ||
var seed = bip39.mnemonicToSeed(mnemonic) | ||
var seedHex = bip39.mnemonicToSeedHex(mnemonic) | ||
|
||
t.plan(3) | ||
t.equal(seed.toString('hex'), seedHex) | ||
t.equal(seedHex, '5cf2d4a8b0355e90295bdfc565a022a409af063d5365bb57bf74d9528f494bfa4400f53d8349b80fdae44082d7f9541e1dba2b003bcfec9d0d53781ca676651f') | ||
t.equal(bip39.validateMnemonic(mnemonic), false) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rubensayshi I haven't got time to check, but why does a 24-word mnemonic decode to 264 bits of ENT bits?