@ -45,9 +45,44 @@ exports.WordsetNamesByWordsetName = wordsetNamesByWordsetName
exports . AllWordsetNames = allWordsetNames
//
//
// Mnemonic wordset utilities - Comparison
// TODO: perhaps move this to mnemonic.js
function AreEqualMnemonics (
a ,
b ,
a _ _wordsetName ,
b _ _wordsetName
) {
if ( a _ _wordsetName !== b _ _wordsetName ) {
return false
}
const wordsetName = a _ _wordsetName
const wordset = mnemonic . mn _words [ wordsetName ]
const prefix _len = wordset . prefix _len
// since mnemonics can be entered with only the first N letters, we must check equality of mnemonics by prefix
let a _ _mnemonicString _words = a . split ( " " )
let b _ _mnemonicString _words = b . split ( " " )
if ( a _ _mnemonicString _words . length != b _ _mnemonicString _words . length ) {
return false
}
let numberOf _mnemonicString _words = a _ _mnemonicString _words . length
for ( var i = 0 ; i < numberOf _mnemonicString _words ; i ++ ) {
let a _ _word = a _ _mnemonicString _words [ i ]
let b _ _word = b _ _mnemonicString _words [ i ]
// ... We're assuming that a and b are already valid mneminics
const a _prefix = a _ _word . slice ( 0 , prefix _len )
const b _prefix = b _ _word . slice ( 0 , prefix _len )
if ( a _prefix !== b _prefix ) {
return false
}
}
return true
}
exports . AreEqualMnemonics = AreEqualMnemonics
//
////////////////////////////////////////////////////////////////////////////////
// Mnemonic wordset utilities - Wordset name detection by mnemonic contents
//
// TODO: perhaps move this to mnemonic.js
function WordsetNameAccordingToMnemonicString ( mnemonicString ) // throws
{
const mnemonicString _words = mnemonicString . split ( ' ' )
@ -61,12 +96,22 @@ function WordsetNameAccordingToMnemonicString(mnemonicString) // throws
if ( wordsetName === 'electrum' ) {
continue // skip because it conflicts with 'english'
}
const wordsetWords = mnemonic . mn _words [ wordsetName ] . words
if ( wordsetWords . indexOf ( mnemonicString _word ) !== - 1 ) {
thisWordIsInWordsetNamed = wordsetName
break // done looking
const wordset = mnemonic . mn _words [ wordsetName ]
const prefix _len = wordset . prefix _len
if ( mnemonicString _word . length < prefix _len ) {
throw "Please enter more than " + ( prefix _len - 1 ) + " letters per word"
}
const wordsetWords = wordset . words
for ( let wordsetWord of wordsetWords ) {
if ( wordsetWord . indexOf ( mnemonicString _word ) == 0 ) { // we can safely check prefix b/c we've checked mnemonicString_word is of at least min length
thisWordIsInWordsetNamed = wordsetName
break // done looking; exit interior then exterior loops
}
}
if ( thisWordIsInWordsetNamed != null ) { // just found
break // also exit
}
// haven't found it yet
// haven't found it yet ; keep looking
}
if ( thisWordIsInWordsetNamed === null ) { // didn't find this word in any of the mnemonic wordsets
throw "Unrecognized mnemonic language"