mirror of https://github.com/status-im/bip39.git
index: avoid BIP39 instantiation
This commit is contained in:
parent
fda008ad25
commit
713e5c107d
32
index.js
32
index.js
|
@ -4,16 +4,14 @@ var secureRandom = require('secure-random')
|
||||||
|
|
||||||
var DEFAULT_WORDLIST = require('./wordlists/en.json')
|
var DEFAULT_WORDLIST = require('./wordlists/en.json')
|
||||||
|
|
||||||
function BIP39(wordlist) {
|
function mnemonicToSeed(mnemonic, password) {
|
||||||
this.wordlist = wordlist || DEFAULT_WORDLIST
|
var options = { iterations: 2048, hasher: CryptoJS.algo.SHA512, keySize: 512/32 }
|
||||||
}
|
|
||||||
|
|
||||||
BIP39.prototype.mnemonicToSeed = function(mnemonic, password) {
|
|
||||||
var options = {iterations: 2048, hasher: CryptoJS.algo.SHA512, keySize: 512/32}
|
|
||||||
return CryptoJS.PBKDF2(mnemonic, salt(password), options).toString(CryptoJS.enc.Hex)
|
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 entropyBuffer = new Buffer(entropy, 'hex')
|
||||||
var entropyBits = bytesToBinary([].slice.call(entropyBuffer))
|
var entropyBits = bytesToBinary([].slice.call(entropyBuffer))
|
||||||
var checksum = checksumBits(entropyBuffer)
|
var checksum = checksumBits(entropyBuffer)
|
||||||
|
@ -24,26 +22,27 @@ BIP39.prototype.entropyToMnemonic = function(entropy) {
|
||||||
var words = chunks.map(function(binary) {
|
var words = chunks.map(function(binary) {
|
||||||
var index = parseInt(binary, 2)
|
var index = parseInt(binary, 2)
|
||||||
|
|
||||||
return this.wordlist[index]
|
return wordlist[index]
|
||||||
}, this)
|
})
|
||||||
|
|
||||||
return words.join(' ')
|
return words.join(' ')
|
||||||
}
|
}
|
||||||
|
|
||||||
BIP39.prototype.generateMnemonic = function(strength, rng) {
|
function generateMnemonic(strength, rng, wordlist) {
|
||||||
strength = strength || 128
|
strength = strength || 128
|
||||||
rng = rng || secureRandom.randomBuffer
|
rng = rng || secureRandom.randomBuffer
|
||||||
|
|
||||||
var hex = rng(strength / 8).toString('hex')
|
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(' ')
|
var words = mnemonic.split(' ')
|
||||||
|
|
||||||
if (words.length % 3 !== 0) return false
|
if (words.length % 3 !== 0) return false
|
||||||
|
|
||||||
var wordlist = this.wordlist
|
|
||||||
var belongToList = words.every(function(word) {
|
var belongToList = words.every(function(word) {
|
||||||
return wordlist.indexOf(word) > -1
|
return wordlist.indexOf(word) > -1
|
||||||
})
|
})
|
||||||
|
@ -102,4 +101,9 @@ function lpad(str, padString, length) {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = BIP39
|
module.exports = {
|
||||||
|
mnemonicToSeed: mnemonicToSeed,
|
||||||
|
entropyToMnemonic: entropyToMnemonic,
|
||||||
|
generateMnemonic: generateMnemonic,
|
||||||
|
validateMnemonic: validateMnemonic
|
||||||
|
}
|
||||||
|
|
|
@ -9,29 +9,12 @@ var wordlists = {
|
||||||
var vectors = require('./vectors.json')
|
var vectors = require('./vectors.json')
|
||||||
|
|
||||||
describe('BIP39', function() {
|
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() {
|
describe('mnemonicToSeed', function() {
|
||||||
this.timeout(20000)
|
this.timeout(20000)
|
||||||
|
|
||||||
var bip39
|
|
||||||
beforeEach(function() {
|
|
||||||
bip39 = new BIP39()
|
|
||||||
})
|
|
||||||
|
|
||||||
vectors.english.forEach(function(v, i) {
|
vectors.english.forEach(function(v, i) {
|
||||||
it('works for tests vector ' + i, function() {
|
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() {
|
describe('entropyToMnemonic', function() {
|
||||||
vectors.english.forEach(function(v, i) {
|
vectors.english.forEach(function(v, i) {
|
||||||
it('works for tests vector ' + i, function() {
|
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) {
|
vectors.custom.forEach(function(v, i) {
|
||||||
it('works for custom test vector ' + i, function() {
|
it('works for custom test vector ' + i, function() {
|
||||||
var bip39 = new BIP39(wordlists.custom)
|
assert.equal(BIP39.entropyToMnemonic(v[0], wordlists.custom), v[1])
|
||||||
assert.equal(bip39.entropyToMnemonic(v[0]), v[1])
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('generateMnemonic', function() {
|
describe('generateMnemonic', function() {
|
||||||
it('generates a mnemonic', function() {
|
it('generates a mnemonic', function() {
|
||||||
var bip39 = new BIP39()
|
var mnemonic = BIP39.generateMnemonic(96)
|
||||||
var mnemonic = bip39.generateMnemonic(96)
|
|
||||||
var words = mnemonic.split(' ')
|
var words = mnemonic.split(' ')
|
||||||
|
|
||||||
assert.equal(words.length, 9)
|
assert.equal(words.length, 9)
|
||||||
|
@ -67,9 +47,8 @@ describe('BIP39', function() {
|
||||||
buffer.fill(4) // guaranteed random
|
buffer.fill(4) // guaranteed random
|
||||||
return buffer
|
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')
|
assert.equal(mnemonic, 'advice cage absurd amount doctor act')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -80,45 +59,39 @@ describe('BIP39', function() {
|
||||||
return buffer
|
return buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
var bip39 = new BIP39(wordlists.custom)
|
var mnemonic = BIP39.generateMnemonic(64, rng, wordlists.custom)
|
||||||
var mnemonic = bip39.generateMnemonic(64, rng)
|
|
||||||
assert.equal(mnemonic, 'adv1c3 cag3 ab5urd am0unt d0ct0r act')
|
assert.equal(mnemonic, 'adv1c3 cag3 ab5urd am0unt d0ct0r act')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('validateMnemonic', function() {
|
describe('validateMnemonic', function() {
|
||||||
vectors.english.forEach(function(v, i) {
|
vectors.english.forEach(function(v, i) {
|
||||||
var bip39 = new BIP39()
|
|
||||||
|
|
||||||
it('passes check ' + i, function() {
|
it('passes check ' + i, function() {
|
||||||
assert(bip39.validateMnemonic(v[1]))
|
assert(BIP39.validateMnemonic(v[1]))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('with a custom wordlist', function() {
|
describe('with a custom wordlist', function() {
|
||||||
vectors.custom.forEach(function(v, i) {
|
vectors.custom.forEach(function(v, i) {
|
||||||
var bip39 = new BIP39(wordlists.custom)
|
|
||||||
|
|
||||||
it('passes custom check ' + i, function() {
|
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() {
|
it('fails for mnemonics of wrong length', function() {
|
||||||
var bip39 = new BIP39()
|
assert(!BIP39.validateMnemonic('sleep kitten'))
|
||||||
assert(!bip39.validateMnemonic('sleep kitten'))
|
assert(!BIP39.validateMnemonic('sleep kitten sleep kitten sleep kitten'))
|
||||||
assert(!bip39.validateMnemonic('sleep kitten sleep kitten sleep kitten'))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('fails for mnemonics that contains words not from the word list', function() {
|
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() {
|
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'))
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue