index: avoid BIP39 instantiation

This commit is contained in:
Daniel Cousens 2014-08-17 10:37:14 +10:00
parent fda008ad25
commit 713e5c107d
2 changed files with 30 additions and 53 deletions

View File

@ -4,16 +4,14 @@ var secureRandom = require('secure-random')
var DEFAULT_WORDLIST = require('./wordlists/en.json')
function BIP39(wordlist) {
this.wordlist = wordlist || DEFAULT_WORDLIST
}
BIP39.prototype.mnemonicToSeed = function(mnemonic, password) {
var options = {iterations: 2048, hasher: CryptoJS.algo.SHA512, keySize: 512/32}
function mnemonicToSeed(mnemonic, password) {
var options = { iterations: 2048, hasher: CryptoJS.algo.SHA512, keySize: 512/32 }
return CryptoJS.PBKDF2(mnemonic, salt(password), options).toString(CryptoJS.enc.Hex)
}
BIP39.prototype.entropyToMnemonic = function(entropy) {
function entropyToMnemonic(entropy, wordlist) {
wordlist = wordlist || DEFAULT_WORDLIST
var entropyBuffer = new Buffer(entropy, 'hex')
var entropyBits = bytesToBinary([].slice.call(entropyBuffer))
var checksum = checksumBits(entropyBuffer)
@ -24,26 +22,27 @@ BIP39.prototype.entropyToMnemonic = function(entropy) {
var words = chunks.map(function(binary) {
var index = parseInt(binary, 2)
return this.wordlist[index]
}, this)
return wordlist[index]
})
return words.join(' ')
}
BIP39.prototype.generateMnemonic = function(strength, rng) {
function generateMnemonic(strength, rng, wordlist) {
strength = strength || 128
rng = rng || secureRandom.randomBuffer
var hex = rng(strength / 8).toString('hex')
return this.entropyToMnemonic(hex)
return entropyToMnemonic(hex, wordlist)
}
BIP39.prototype.validateMnemonic = function(mnemonic) {
function validateMnemonic(mnemonic, wordlist) {
wordlist = wordlist || DEFAULT_WORDLIST
var words = mnemonic.split(' ')
if (words.length % 3 !== 0) return false
var wordlist = this.wordlist
var belongToList = words.every(function(word) {
return wordlist.indexOf(word) > -1
})
@ -102,4 +101,9 @@ function lpad(str, padString, length) {
return str;
}
module.exports = BIP39
module.exports = {
mnemonicToSeed: mnemonicToSeed,
entropyToMnemonic: entropyToMnemonic,
generateMnemonic: generateMnemonic,
validateMnemonic: validateMnemonic
}

View File

@ -9,29 +9,12 @@ var wordlists = {
var vectors = require('./vectors.json')
describe('BIP39', function() {
describe('constructor', function() {
it('defaults language to english', function() {
var bip39 = new BIP39()
assert.deepEqual(bip39.wordlist, wordlists.english)
})
it('accepts a custom wordlist', function() {
var bip39 = new BIP39(wordlists.custom)
assert.deepEqual(bip39.wordlist, wordlists.custom)
})
})
describe('mnemonicToSeed', function() {
this.timeout(20000)
var bip39
beforeEach(function() {
bip39 = new BIP39()
})
vectors.english.forEach(function(v, i) {
it('works for tests vector ' + i, function() {
assert.equal(bip39.mnemonicToSeed(v[1], 'TREZOR'), v[2])
assert.equal(BIP39.mnemonicToSeed(v[1], 'TREZOR'), v[2])
})
})
})
@ -39,23 +22,20 @@ describe('BIP39', function() {
describe('entropyToMnemonic', function() {
vectors.english.forEach(function(v, i) {
it('works for tests vector ' + i, function() {
var bip39 = new BIP39()
assert.equal(bip39.entropyToMnemonic(v[0]), v[1])
assert.equal(BIP39.entropyToMnemonic(v[0]), v[1])
})
})
vectors.custom.forEach(function(v, i) {
it('works for custom test vector ' + i, function() {
var bip39 = new BIP39(wordlists.custom)
assert.equal(bip39.entropyToMnemonic(v[0]), v[1])
assert.equal(BIP39.entropyToMnemonic(v[0], wordlists.custom), v[1])
})
})
})
describe('generateMnemonic', function() {
it('generates a mnemonic', function() {
var bip39 = new BIP39()
var mnemonic = bip39.generateMnemonic(96)
var mnemonic = BIP39.generateMnemonic(96)
var words = mnemonic.split(' ')
assert.equal(words.length, 9)
@ -67,9 +47,8 @@ describe('BIP39', function() {
buffer.fill(4) // guaranteed random
return buffer
}
var bip39 = new BIP39()
var mnemonic = bip39.generateMnemonic(64, rng)
var mnemonic = BIP39.generateMnemonic(64, rng)
assert.equal(mnemonic, 'advice cage absurd amount doctor act')
})
@ -80,45 +59,39 @@ describe('BIP39', function() {
return buffer
}
var bip39 = new BIP39(wordlists.custom)
var mnemonic = bip39.generateMnemonic(64, rng)
var mnemonic = BIP39.generateMnemonic(64, rng, wordlists.custom)
assert.equal(mnemonic, 'adv1c3 cag3 ab5urd am0unt d0ct0r act')
})
})
describe('validateMnemonic', function() {
vectors.english.forEach(function(v, i) {
var bip39 = new BIP39()
it('passes check ' + i, function() {
assert(bip39.validateMnemonic(v[1]))
assert(BIP39.validateMnemonic(v[1]))
})
})
describe('with a custom wordlist', function() {
vectors.custom.forEach(function(v, i) {
var bip39 = new BIP39(wordlists.custom)
it('passes custom check ' + i, function() {
assert(bip39.validateMnemonic(v[1]))
assert(BIP39.validateMnemonic(v[1], wordlists.custom))
})
})
})
it('fails for mnemonics of wrong length', function() {
var bip39 = new BIP39()
assert(!bip39.validateMnemonic('sleep kitten'))
assert(!bip39.validateMnemonic('sleep kitten sleep kitten sleep kitten'))
assert(!BIP39.validateMnemonic('sleep kitten'))
assert(!BIP39.validateMnemonic('sleep kitten sleep kitten sleep kitten'))
})
it('fails for mnemonics that contains words not from the word list', function() {
var bip39 = new BIP39()
assert(!bip39.validateMnemonic("turtle front uncle idea crush write shrug there lottery flower risky shell"))
assert(!BIP39.validateMnemonic("turtle front uncle idea crush write shrug there lottery flower risky shell"))
})
it('fails for mnemonics of invalid checksum', function() {
var bip39 = new BIP39()
assert(!bip39.validateMnemonic('sleep kitten sleep kitten sleep kitten sleep kitten sleep kitten sleep kitten'))
assert(!BIP39.validateMnemonic('sleep kitten sleep kitten sleep kitten sleep kitten sleep kitten sleep kitten'))
})
})
})