bip39/test/index.js

112 lines
3.2 KiB
JavaScript
Raw Normal View History

2014-03-10 02:49:53 +00:00
var assert = require('assert')
2014-06-23 03:40:38 +00:00
var BIP39 = require('../index.js')
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() {
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() {
assert.equal(BIP39.mnemonicToSeedHex(v[1], 'TREZOR'), v[2])
2014-07-04 04:33:49 +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-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-07-04 04:33:49 +00:00
describe('generateMnemonic', function() {
it('generates a mnemonic', 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
})
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
})
})