2019-03-24 11:49:42 +00:00
var bip39 = require ( '../' )
2017-03-15 06:57:19 +00:00
var download = require ( '../util/wordlists' ) . download
2016-10-05 14:02:59 +00:00
var WORDLISTS = {
2019-03-23 13:06:58 +00:00
english : require ( '../src/wordlists/english.json' ) ,
japanese : require ( '../src/wordlists/japanese.json' ) ,
2014-07-04 04:33:49 +00:00
custom : require ( './wordlist.json' )
}
var vectors = require ( './vectors.json' )
2016-10-05 14:02:59 +00:00
var test = require ( 'tape' )
2014-03-11 04:38:09 +00:00
2016-10-05 14:02:59 +00:00
function testVector ( description , wordlist , password , v , i ) {
var ventropy = v [ 0 ]
var vmnemonic = v [ 1 ]
var vseedHex = v [ 2 ]
2014-07-04 04:33:49 +00:00
2017-05-12 02:31:52 +00:00
test ( 'for ' + description + '(' + i + '), ' + ventropy , function ( t ) {
2019-03-05 07:43:14 +00:00
t . plan ( 6 )
2014-06-23 07:56:08 +00:00
2016-10-05 14:02:59 +00:00
t . equal ( bip39 . mnemonicToEntropy ( vmnemonic , wordlist ) , ventropy , 'mnemonicToEntropy returns ' + ventropy . slice ( 0 , 40 ) + '...' )
2019-04-02 09:03:44 +00:00
t . equal ( bip39 . mnemonicToSeedSync ( vmnemonic , password ) . toString ( 'hex' ) , vseedHex , 'mnemonicToSeedSync returns ' + vseedHex . slice ( 0 , 40 ) + '...' )
bip39 . mnemonicToSeed ( vmnemonic , password ) . then ( function ( asyncSeed ) {
t . equal ( asyncSeed . toString ( 'hex' ) , vseedHex , 'mnemonicToSeed returns ' + vseedHex . slice ( 0 , 40 ) + '...' )
2019-03-05 07:43:14 +00:00
} )
2016-10-05 14:02:59 +00:00
t . equal ( bip39 . entropyToMnemonic ( ventropy , wordlist ) , vmnemonic , 'entropyToMnemonic returns ' + vmnemonic . slice ( 0 , 40 ) + '...' )
2014-06-23 07:56:08 +00:00
2017-05-23 05:04:19 +00:00
function rng ( ) { return Buffer . from ( ventropy , 'hex' ) }
2016-10-05 14:02:59 +00:00
t . equal ( bip39 . generateMnemonic ( undefined , rng , wordlist ) , vmnemonic , 'generateMnemonic returns RNG entropy unmodified' )
t . equal ( bip39 . validateMnemonic ( vmnemonic , wordlist ) , true , 'validateMnemonic returns true' )
2014-04-05 04:44:16 +00:00
} )
2016-10-05 14:02:59 +00:00
}
2014-04-05 04:44:16 +00:00
2016-10-05 14:02:59 +00:00
vectors . english . forEach ( function ( v , i ) { testVector ( 'English' , undefined , 'TREZOR' , v , i ) } )
vectors . japanese . forEach ( function ( v , i ) { testVector ( 'Japanese' , WORDLISTS . japanese , '㍍ガバヴァぱばぐゞちぢ十人十色' , v , i ) } )
vectors . custom . forEach ( function ( v , i ) { testVector ( 'Custom' , WORDLISTS . custom , undefined , v , i ) } )
2019-03-28 08:21:54 +00:00
test ( 'getDefaultWordlist returns "english"' , function ( t ) {
t . plan ( 1 )
const english = bip39 . getDefaultWordlist ( )
t . equal ( english , 'english' )
// TODO: Test that Error throws when called if no wordlists are compiled with bip39
} )
test ( 'setDefaultWordlist changes default wordlist' , function ( t ) {
t . plan ( 4 )
const english = bip39 . getDefaultWordlist ( )
t . equal ( english , 'english' )
bip39 . setDefaultWordlist ( 'italian' )
const italian = bip39 . getDefaultWordlist ( )
t . equal ( italian , 'italian' )
const phraseItalian = bip39 . entropyToMnemonic ( '00000000000000000000000000000000' )
t . equal ( phraseItalian . slice ( 0 , 5 ) , 'abaco' )
bip39 . setDefaultWordlist ( 'english' )
const phraseEnglish = bip39 . entropyToMnemonic ( '00000000000000000000000000000000' )
t . equal ( phraseEnglish . slice ( 0 , 7 ) , 'abandon' )
} )
test ( 'setDefaultWordlist throws on unknown wordlist' , function ( t ) {
t . plan ( 2 )
const english = bip39 . getDefaultWordlist ( )
t . equal ( english , 'english' )
try {
bip39 . setDefaultWordlist ( 'abcdefghijklmnop' )
} catch ( error ) {
t . equal ( error . message , 'Could not find wordlist for language "abcdefghijklmnop"' )
return
}
t . assert ( false )
} )
2016-10-05 14:34:04 +00:00
test ( 'invalid entropy' , function ( t ) {
t . plan ( 3 )
t . throws ( function ( ) {
2017-05-23 05:04:19 +00:00
bip39 . entropyToMnemonic ( Buffer . from ( '' , 'hex' ) )
2017-05-12 02:57:37 +00:00
} , / ^ T y p e E r r o r : I n v a l i d e n t r o p y $ / , ' t h r o w s f o r e m p t y e n t r o p y ' )
2016-10-05 14:34:04 +00:00
t . throws ( function ( ) {
2017-05-23 05:04:19 +00:00
bip39 . entropyToMnemonic ( Buffer . from ( '000000' , 'hex' ) )
2017-05-12 02:57:37 +00:00
} , /^TypeError: Invalid entropy$/ , 'throws for entropy that\'s not a multitude of 4 bytes' )
2016-10-05 14:34:04 +00:00
t . throws ( function ( ) {
2017-05-23 05:04:19 +00:00
bip39 . entropyToMnemonic ( Buffer . from ( new Array ( 1028 + 1 ) . join ( '00' ) , 'hex' ) )
2017-05-12 02:57:37 +00:00
} , /^TypeError: Invalid entropy$/ , 'throws for entropy that is larger than 1024' )
2016-10-05 14:34:04 +00:00
} )
2016-10-05 14:02:59 +00:00
test ( 'UTF8 passwords' , function ( t ) {
t . plan ( vectors . japanese . length * 2 )
2014-09-27 16:08:00 +00:00
2016-10-05 14:02:59 +00:00
vectors . japanese . forEach ( function ( v ) {
var vmnemonic = v [ 1 ]
var vseedHex = v [ 2 ]
var password = '㍍ガバヴァぱばぐゞちぢ十人十色'
var normalizedPassword = 'メートルガバヴァぱばぐゞちぢ十人十色'
2019-04-02 09:03:44 +00:00
t . equal ( bip39 . mnemonicToSeedSync ( vmnemonic , password ) . toString ( 'hex' ) , vseedHex , 'mnemonicToSeedSync normalizes passwords' )
t . equal ( bip39 . mnemonicToSeedSync ( vmnemonic , normalizedPassword ) . toString ( 'hex' ) , vseedHex , 'mnemonicToSeedSync leaves normalizes passwords as-is' )
2014-09-27 16:08:00 +00:00
} )
2016-10-05 14:02:59 +00:00
} )
2014-12-09 19:23:15 +00:00
2016-10-05 14:02:59 +00:00
test ( 'generateMnemonic can vary entropy length' , function ( t ) {
2017-05-12 02:11:48 +00:00
var words = bip39 . generateMnemonic ( 160 ) . split ( ' ' )
2014-12-09 19:23:15 +00:00
2016-10-05 14:02:59 +00:00
t . plan ( 1 )
2017-05-12 02:11:48 +00:00
t . equal ( words . length , 15 , 'can vary generated entropy bit length' )
2016-10-05 14:02:59 +00:00
} )
2015-10-01 14:30:01 +00:00
2017-05-12 02:11:48 +00:00
test ( 'generateMnemonic requests the exact amount of data from an RNG' , function ( t ) {
2016-10-05 14:02:59 +00:00
t . plan ( 1 )
2015-08-17 00:36:58 +00:00
2017-05-12 02:11:48 +00:00
bip39 . generateMnemonic ( 160 , function ( size ) {
t . equal ( size , 160 / 8 )
2017-05-23 05:04:19 +00:00
return Buffer . allocUnsafe ( size )
2015-08-17 00:36:58 +00:00
} )
2014-04-05 04:44:16 +00:00
} )
2016-10-05 14:02:59 +00:00
test ( 'validateMnemonic' , function ( t ) {
2016-10-05 14:34:04 +00:00
t . plan ( 5 )
2016-10-05 14:02:59 +00:00
t . equal ( bip39 . validateMnemonic ( 'sleep kitten' ) , false , 'fails for a mnemonic that is too short' )
t . equal ( bip39 . validateMnemonic ( 'sleep kitten sleep kitten sleep kitten' ) , false , 'fails for a mnemonic that is too short' )
2016-10-05 14:34:04 +00:00
t . equal ( bip39 . validateMnemonic ( ' abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon aband
2016-10-05 14:02:59 +00:00
t . equal ( bip39 . validateMnemonic ( 'turtle front uncle idea crush write shrug there lottery flower risky shell' ) , false , 'fails if mnemonic words are not in the word list' )
t . equal ( bip39 . validateMnemonic ( 'sleep kitten sleep kitten sleep kitten sleep kitten sleep kitten sleep kitten' ) , false , 'fails for invalid checksum' )
} )
test ( 'exposes standard wordlists' , function ( t ) {
t . plan ( 2 )
t . same ( bip39 . wordlists . EN , WORDLISTS . english )
t . equal ( bip39 . wordlists . EN . length , 2048 )
} )
2017-03-15 06:57:19 +00:00
test ( 'verify wordlists from https://github.com/bitcoin/bips/blob/master/bip-0039/bip-0039-wordlists.md' , function ( t ) {
download ( ) . then ( function ( wordlists ) {
Object . keys ( wordlists ) . forEach ( function ( name ) {
t . same ( bip39 . wordlists [ name ] , wordlists [ name ] )
} )
t . end ( )
} )
} )