mnemonic: fix an error for certain languages.
This commit is contained in:
parent
ed4ce0f032
commit
9f3a040758
@ -245,7 +245,7 @@ Mnemonic.prototype.fromPhrase = function fromPhrase(phrase) {
|
|||||||
// Rebuild entropy bytes.
|
// Rebuild entropy bytes.
|
||||||
for (let i = 0; i < words.length; i++) {
|
for (let i = 0; i < words.length; i++) {
|
||||||
const word = words[i];
|
const word = words[i];
|
||||||
const index = util.binarySearch(wordlist, word, util.strcmp);
|
const index = wordlist.indexOf(word);
|
||||||
|
|
||||||
if (index === -1)
|
if (index === -1)
|
||||||
throw new Error('Could not find word.');
|
throw new Error('Could not find word.');
|
||||||
@ -338,7 +338,7 @@ Mnemonic.fromEntropy = function fromEntropy(entropy, lang) {
|
|||||||
Mnemonic.getLanguage = function getLanguage(word) {
|
Mnemonic.getLanguage = function getLanguage(word) {
|
||||||
for (const lang of Mnemonic.languages) {
|
for (const lang of Mnemonic.languages) {
|
||||||
const wordlist = Mnemonic.getWordlist(lang);
|
const wordlist = Mnemonic.getWordlist(lang);
|
||||||
if (util.binarySearch(wordlist, word, util.strcmp) !== -1)
|
if (wordlist.indexOf(word) !== -1)
|
||||||
return lang;
|
return lang;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,8 +13,8 @@ const tests = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
describe('Mnemonic', function() {
|
describe('Mnemonic', function() {
|
||||||
for (const lang of Object.keys(tests)) {
|
for (const language of Object.keys(tests)) {
|
||||||
const test = tests[lang];
|
const test = tests[language];
|
||||||
let i = 0;
|
let i = 0;
|
||||||
|
|
||||||
for (const data of test) {
|
for (const data of test) {
|
||||||
@ -23,19 +23,38 @@ describe('Mnemonic', function() {
|
|||||||
const passphrase = data[2];
|
const passphrase = data[2];
|
||||||
const seed = Buffer.from(data[3], 'hex');
|
const seed = Buffer.from(data[3], 'hex');
|
||||||
const xpriv = data[4];
|
const xpriv = data[4];
|
||||||
it(`should create a ${lang} mnemonic (${i++})`, () => {
|
|
||||||
|
it(`should create a ${language} mnemonic from entropy (${i})`, () => {
|
||||||
const mnemonic = new Mnemonic({
|
const mnemonic = new Mnemonic({
|
||||||
language: lang,
|
language,
|
||||||
entropy: entropy,
|
entropy,
|
||||||
passphrase: passphrase
|
passphrase
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.strictEqual(mnemonic.getPhrase(), phrase);
|
assert.strictEqual(mnemonic.getPhrase(), phrase);
|
||||||
|
assert.deepStrictEqual(mnemonic.getEntropy(), entropy);
|
||||||
assert.deepStrictEqual(mnemonic.toSeed(), seed);
|
assert.deepStrictEqual(mnemonic.toSeed(), seed);
|
||||||
|
|
||||||
const key = HDPrivateKey.fromMnemonic(mnemonic);
|
const key = HDPrivateKey.fromMnemonic(mnemonic);
|
||||||
assert.strictEqual(key.toBase58(), xpriv);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user