Allow for wordlists in JS bip39 functions (#1805)

This commit is contained in:
Jaco
2023-04-13 09:14:45 +03:00
committed by GitHub
parent f0f561e243
commit efe4ce8d9d
12 changed files with 78 additions and 29 deletions
@@ -1,11 +0,0 @@
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
export { default as en } from './en.js';
export { default as es } from './es.js';
export { default as fr } from './fr.js';
export { default as it } from './it.js';
export { default as jp } from './jp.js';
export { default as ko } from './ko.js';
export { default as zhs } from './zh-s.js';
export { default as zht } from './zh-t.js';
@@ -3,29 +3,78 @@
/// <reference types="@polkadot/dev-test/globals.d.ts" />
import * as lists from './bip39-lang/index.js';
import { entropyToMnemonic, mnemonicToEntropy } from './bip39.js';
import * as lists from './wordlists/index.js';
import { entropyToMnemonic, generateMnemonic, mnemonicToEntropy, validateMnemonic } from './bip39.js';
describe('wordlists', (): void => {
for (const [k, v] of Object.entries(lists)) {
describe(`language ${k}`, (): void => {
for (const [lang, words] of Object.entries(lists)) {
describe(`language ${lang}`, (): void => {
it('has the correct number of words', (): void => {
expect(v).toHaveLength(2048);
expect(words).toHaveLength(2048);
});
it('has no empty words', (): void => {
expect(v.some((n) => !n.length || n.trim() !== n)).toEqual(false);
expect(
words.some((w) => !w.length || w.trim() !== w)
).toEqual(false);
});
});
}
});
describe('bip39', (): void => {
it('has a two-way entropy <-> mnemonic', (): void => {
const m = 'seed sock milk update focus rotate barely fade car face mechanic mercy';
const m = 'seed sock milk update focus rotate barely fade car face mechanic mercy';
const e = mnemonicToEntropy(m);
it('generates a known entropy', (): void => {
expect(e).toEqual(
new Uint8Array([194, 249, 194, 50, 119, 69, 163, 120, 68, 162, 142, 34, 74, 50, 40, 197])
);
});
it('has a two-way entropy <-> mnemonic (default wordlist)', (): void => {
expect(
entropyToMnemonic(mnemonicToEntropy(m))
entropyToMnemonic(mnemonicToEntropy(entropyToMnemonic(e)))
).toEqual(m);
});
for (const [lang, words] of Object.entries(lists)) {
const isUsingList = (test: string, length = 12): void => {
const split = test.split(' ');
expect(
split
).toHaveLength(length);
expect(
split.some((w) => words.indexOf(w) === -1)
).toEqual(false);
};
describe(`language ${lang}`, (): void => {
it('has a two-way entropy <-> mnemonic', (): void => {
const test = entropyToMnemonic(e, words);
const entr = mnemonicToEntropy(test, words);
isUsingList(test);
expect(
entr
).toEqual(e);
expect(
entropyToMnemonic(entr, words)
).toEqual(test);
});
it('generates a valid mnemonic', (): void => {
const test = generateMnemonic(24, words);
isUsingList(test, 24);
expect(
validateMnemonic(test, words)
).toEqual(true);
});
});
}
});
+9 -9
View File
@@ -19,7 +19,7 @@ import { stringToU8a, u8aToU8a } from '@polkadot/util';
import { pbkdf2Encode } from '../pbkdf2/index.js';
import { randomAsU8a } from '../random/index.js';
import { sha256AsU8a } from '../sha/index.js';
import DEFAULT_WORDLIST from './bip39-lang/en.js';
import DEFAULT_WORDLIST from './wordlists/en.js';
const INVALID_MNEMONIC = 'Invalid mnemonic';
const INVALID_ENTROPY = 'Invalid entropy';
@@ -50,7 +50,7 @@ export function mnemonicToSeedSync (mnemonic: string, password?: string): Uint8A
).password;
}
export function mnemonicToEntropy (mnemonic: string): Uint8Array {
export function mnemonicToEntropy (mnemonic: string, wordlist: string[] = DEFAULT_WORDLIST): Uint8Array {
const words = normalize(mnemonic).split(' ');
if (words.length % 3 !== 0) {
@@ -60,7 +60,7 @@ export function mnemonicToEntropy (mnemonic: string): Uint8Array {
// convert word indices to 11 bit binary strings
const bits = words
.map((word): string => {
const index = DEFAULT_WORDLIST.indexOf(word);
const index = wordlist.indexOf(word);
if (index === -1) {
throw new Error(INVALID_MNEMONIC);
@@ -92,14 +92,14 @@ export function mnemonicToEntropy (mnemonic: string): Uint8Array {
return entropy;
}
export function entropyToMnemonic (entropy: Uint8Array): string {
export function entropyToMnemonic (entropy: Uint8Array, wordlist: string[] = DEFAULT_WORDLIST): string {
// 128 <= ENT <= 256
if ((entropy.length % 4 !== 0) || (entropy.length < 16) || (entropy.length > 32)) {
throw new Error(INVALID_ENTROPY);
}
const matched = `${bytesToBinary(Array.from(entropy))}${deriveChecksumBits(entropy)}`.match(/(.{1,11})/g);
const mapped = matched && matched.map((binary) => DEFAULT_WORDLIST[binaryToByte(binary)]);
const mapped = matched && matched.map((b) => wordlist[binaryToByte(b)]);
if (!mapped || (mapped.length < 12)) {
throw new Error('Unable to map entropy to mnemonic');
@@ -108,13 +108,13 @@ export function entropyToMnemonic (entropy: Uint8Array): string {
return mapped.join(' ');
}
export function generateMnemonic (numWords: 12 | 15 | 18 | 21 | 24): string {
return entropyToMnemonic(randomAsU8a((numWords / 3) * 4));
export function generateMnemonic (numWords: 12 | 15 | 18 | 21 | 24, wordlist?: string[]): string {
return entropyToMnemonic(randomAsU8a((numWords / 3) * 4), wordlist);
}
export function validateMnemonic (mnemonic: string): boolean {
export function validateMnemonic (mnemonic: string, wordlist?: string[]): boolean {
try {
mnemonicToEntropy(mnemonic);
mnemonicToEntropy(mnemonic, wordlist);
} catch {
return false;
}
@@ -0,0 +1,11 @@
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
export { default as english } from './en.js';
export { default as spanish } from './es.js';
export { default as french } from './fr.js';
export { default as italian } from './it.js';
export { default as japanese } from './jp.js';
export { default as korean } from './ko.js';
export { default as chineseSimplified } from './zh-s.js';
export { default as chineseTraditional } from './zh-t.js';