2014-03-10 02:49:53 +00:00
|
|
|
var assert = require('assert')
|
2014-09-22 06:48:40 +00:00
|
|
|
var crypto = require('crypto')
|
2014-06-23 03:40:38 +00:00
|
|
|
var BIP39 = require('../index.js')
|
2014-09-22 06:48:40 +00:00
|
|
|
var sinon = require('sinon')
|
2014-03-11 08:01:14 +00:00
|
|
|
|
2014-07-04 04:33:49 +00:00
|
|
|
var wordlists = {
|
|
|
|
english: require('../wordlists/en.json'),
|
|
|
|
custom: require('./wordlist.json')
|
|
|
|
}
|
|
|
|
|
|
|
|
var vectors = require('./vectors.json')
|
2014-03-11 04:38:09 +00:00
|
|
|
|
2014-07-04 04:33:49 +00:00
|
|
|
describe('BIP39', function() {
|
2014-08-17 00:38:11 +00:00
|
|
|
describe('mnemonicToSeedHex', function() {
|
2014-07-04 04:33:49 +00:00
|
|
|
this.timeout(20000)
|
|
|
|
|
|
|
|
vectors.english.forEach(function(v, i) {
|
|
|
|
it('works for tests vector ' + i, function() {
|
2014-08-17 00:38:11 +00:00
|
|
|
assert.equal(BIP39.mnemonicToSeedHex(v[1], 'TREZOR'), v[2])
|
2014-07-04 04:33:49 +00:00
|
|
|
})
|
|
|
|
})
|
2014-06-23 07:56:08 +00:00
|
|
|
})
|
|
|
|
|
2014-08-17 01:11:28 +00:00
|
|
|
describe('mnemonicToEntropy', function() {
|
|
|
|
vectors.english.forEach(function(v, i) {
|
|
|
|
it('works for tests vector ' + i, function() {
|
|
|
|
assert.equal(BIP39.mnemonicToEntropy(v[1]), v[0])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
vectors.custom.forEach(function(v, i) {
|
|
|
|
it('works for custom test vector ' + i, function() {
|
|
|
|
assert.equal(BIP39.mnemonicToEntropy(v[1], wordlists.custom), v[0])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-07-04 04:33:49 +00:00
|
|
|
describe('entropyToMnemonic', function() {
|
|
|
|
vectors.english.forEach(function(v, i) {
|
|
|
|
it('works for tests vector ' + i, function() {
|
2014-08-17 00:37:14 +00:00
|
|
|
assert.equal(BIP39.entropyToMnemonic(v[0]), v[1])
|
2014-07-04 04:33:49 +00:00
|
|
|
})
|
|
|
|
})
|
2014-06-23 07:56:08 +00:00
|
|
|
|
2014-07-04 04:33:49 +00:00
|
|
|
vectors.custom.forEach(function(v, i) {
|
|
|
|
it('works for custom test vector ' + i, function() {
|
2014-08-17 00:37:14 +00:00
|
|
|
assert.equal(BIP39.entropyToMnemonic(v[0], wordlists.custom), v[1])
|
2014-07-04 04:33:49 +00:00
|
|
|
})
|
|
|
|
})
|
2014-06-23 07:56:08 +00:00
|
|
|
})
|
|
|
|
|
2014-07-04 04:33:49 +00:00
|
|
|
describe('generateMnemonic', function() {
|
2014-09-22 06:48:40 +00:00
|
|
|
vectors.english.forEach(function(v, i) {
|
|
|
|
it('works for tests vector ' + i, sinon.test(function() {
|
|
|
|
this.mock(crypto).expects('randomBytes')
|
|
|
|
.exactly(1)
|
|
|
|
.onCall(0).returns(new Buffer(v[0], 'hex'))
|
|
|
|
|
|
|
|
assert.equal(BIP39.generateMnemonic(), v[1])
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('can vary generated entropy bit length', function() {
|
2014-08-17 00:37:14 +00:00
|
|
|
var mnemonic = BIP39.generateMnemonic(96)
|
2014-07-04 04:33:49 +00:00
|
|
|
var words = mnemonic.split(' ')
|
|
|
|
|
|
|
|
assert.equal(words.length, 9)
|
2014-04-05 04:44:16 +00:00
|
|
|
})
|
|
|
|
|
2014-07-04 04:33:49 +00:00
|
|
|
it('allows a custom RNG to be used', function() {
|
|
|
|
var rng = function(size) {
|
|
|
|
var buffer = new Buffer(size)
|
|
|
|
buffer.fill(4) // guaranteed random
|
|
|
|
return buffer
|
|
|
|
}
|
2014-04-05 04:44:16 +00:00
|
|
|
|
2014-08-17 00:37:14 +00:00
|
|
|
var mnemonic = BIP39.generateMnemonic(64, rng)
|
2014-07-04 04:33:49 +00:00
|
|
|
assert.equal(mnemonic, 'advice cage absurd amount doctor act')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adheres to a custom wordlist', function() {
|
|
|
|
var rng = function(size) {
|
|
|
|
var buffer = new Buffer(size)
|
|
|
|
buffer.fill(4) // guaranteed random
|
|
|
|
return buffer
|
|
|
|
}
|
|
|
|
|
2014-08-17 00:37:14 +00:00
|
|
|
var mnemonic = BIP39.generateMnemonic(64, rng, wordlists.custom)
|
2014-07-04 04:33:49 +00:00
|
|
|
assert.equal(mnemonic, 'adv1c3 cag3 ab5urd am0unt d0ct0r act')
|
|
|
|
})
|
2014-04-05 04:44:16 +00:00
|
|
|
})
|
|
|
|
|
2014-08-17 00:25:12 +00:00
|
|
|
describe('validateMnemonic', function() {
|
2014-07-04 04:33:49 +00:00
|
|
|
vectors.english.forEach(function(v, i) {
|
|
|
|
|
|
|
|
it('passes check ' + i, function() {
|
2014-08-17 00:37:14 +00:00
|
|
|
assert(BIP39.validateMnemonic(v[1]))
|
2014-07-04 04:33:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with a custom wordlist', function() {
|
|
|
|
vectors.custom.forEach(function(v, i) {
|
|
|
|
|
|
|
|
it('passes custom check ' + i, function() {
|
2014-08-17 00:37:14 +00:00
|
|
|
assert(BIP39.validateMnemonic(v[1], wordlists.custom))
|
2014-07-04 04:33:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('fails for mnemonics of wrong length', function() {
|
2014-08-17 00:37:14 +00:00
|
|
|
assert(!BIP39.validateMnemonic('sleep kitten'))
|
|
|
|
assert(!BIP39.validateMnemonic('sleep kitten sleep kitten sleep kitten'))
|
2014-07-04 04:33:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('fails for mnemonics that contains words not from the word list', function() {
|
2014-08-17 00:37:14 +00:00
|
|
|
assert(!BIP39.validateMnemonic("turtle front uncle idea crush write shrug there lottery flower risky shell"))
|
2014-07-04 04:33:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('fails for mnemonics of invalid checksum', function() {
|
2014-08-17 00:37:14 +00:00
|
|
|
assert(!BIP39.validateMnemonic('sleep kitten sleep kitten sleep kitten sleep kitten sleep kitten sleep kitten'))
|
2014-07-04 04:33:49 +00:00
|
|
|
})
|
2014-04-05 04:44:16 +00:00
|
|
|
})
|
2014-09-27 16:08:00 +00:00
|
|
|
|
|
|
|
describe('utf8 passwords', function() {
|
2014-09-28 06:57:52 +00:00
|
|
|
vectors.japanese.forEach(function(v, i) {
|
2014-09-27 16:08:00 +00:00
|
|
|
it ('creates the correct seed', function() {
|
2014-09-28 06:57:52 +00:00
|
|
|
var utf8_password = "㍍ガバヴァぱばぐゞちぢ十人十色"
|
|
|
|
assert.equal(BIP39.mnemonicToSeedHex(v[1], utf8_password), v[2])
|
2014-09-27 16:08:00 +00:00
|
|
|
})
|
2014-09-28 11:56:36 +00:00
|
|
|
|
|
|
|
it ('works with already normalized password', function() {
|
|
|
|
var normalized_password = "メートルガバヴァぱばぐゞちぢ十人十色"
|
|
|
|
assert.equal(BIP39.mnemonicToSeedHex(v[1], normalized_password), v[2])
|
|
|
|
})
|
2014-09-28 06:57:52 +00:00
|
|
|
})
|
2014-09-27 16:08:00 +00:00
|
|
|
})
|
2014-04-05 04:44:16 +00:00
|
|
|
})
|