mnemonic: fix an error for certain languages.

This commit is contained in:
Christopher Jeffrey 2017-08-09 17:44:06 -07:00
parent ed4ce0f032
commit 9f3a040758
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 27 additions and 8 deletions

View File

@ -245,7 +245,7 @@ Mnemonic.prototype.fromPhrase = function fromPhrase(phrase) {
// Rebuild entropy bytes.
for (let i = 0; i < words.length; i++) {
const word = words[i];
const index = util.binarySearch(wordlist, word, util.strcmp);
const index = wordlist.indexOf(word);
if (index === -1)
throw new Error('Could not find word.');
@ -338,7 +338,7 @@ Mnemonic.fromEntropy = function fromEntropy(entropy, lang) {
Mnemonic.getLanguage = function getLanguage(word) {
for (const lang of Mnemonic.languages) {
const wordlist = Mnemonic.getWordlist(lang);
if (util.binarySearch(wordlist, word, util.strcmp) !== -1)
if (wordlist.indexOf(word) !== -1)
return lang;
}

View File

@ -13,8 +13,8 @@ const tests = {
};
describe('Mnemonic', function() {
for (const lang of Object.keys(tests)) {
const test = tests[lang];
for (const language of Object.keys(tests)) {
const test = tests[language];
let i = 0;
for (const data of test) {
@ -23,19 +23,38 @@ describe('Mnemonic', function() {
const passphrase = data[2];
const seed = Buffer.from(data[3], 'hex');
const xpriv = data[4];
it(`should create a ${lang} mnemonic (${i++})`, () => {
it(`should create a ${language} mnemonic from entropy (${i})`, () => {
const mnemonic = new Mnemonic({
language: lang,
entropy: entropy,
passphrase: passphrase
language,
entropy,
passphrase
});
assert.strictEqual(mnemonic.getPhrase(), phrase);
assert.deepStrictEqual(mnemonic.getEntropy(), entropy);
assert.deepStrictEqual(mnemonic.toSeed(), seed);
const key = HDPrivateKey.fromMnemonic(mnemonic);
assert.strictEqual(key.toBase58(), xpriv);
});
it(`should create a ${language} mnemonic from phrase (${i})`, () => {
const mnemonic = new Mnemonic({
language,
phrase,
passphrase
});
assert.strictEqual(mnemonic.getPhrase(), phrase);
assert.deepStrictEqual(mnemonic.getEntropy(), entropy);
assert.deepStrictEqual(mnemonic.toSeed(), seed);
const key = HDPrivateKey.fromMnemonic(mnemonic);
assert.strictEqual(key.toBase58(), xpriv);
});
i += 1;
}
}