diff --git a/src/_wordlists.js b/src/_wordlists.js index 9f2f8b2..134cc71 100644 --- a/src/_wordlists.js +++ b/src/_wordlists.js @@ -3,49 +3,28 @@ Object.defineProperty(exports, "__esModule", { value: true }); // browserify by default only pulls in files that are hard coded in requires // In order of last to first in this file, the default wordlist will be chosen // based on what is present. (Bundles may remove wordlists they don't need) +const wordlistFilenames = [ + 'chinese_simplified', + 'chinese_traditional', + 'korean', + 'french', + 'italian', + 'spanish', + 'japanese', + 'english', +]; const wordlists = {}; exports.wordlists = wordlists; let _default; exports._default = _default; -try { - exports._default = _default = require('./wordlists/chinese_simplified.json'); - wordlists.chinese_simplified = _default; -} -catch (err) { } -try { - exports._default = _default = require('./wordlists/chinese_traditional.json'); - wordlists.chinese_traditional = _default; -} -catch (err) { } -try { - exports._default = _default = require('./wordlists/korean.json'); - wordlists.korean = _default; -} -catch (err) { } -try { - exports._default = _default = require('./wordlists/french.json'); - wordlists.french = _default; -} -catch (err) { } -try { - exports._default = _default = require('./wordlists/italian.json'); - wordlists.italian = _default; -} -catch (err) { } -try { - exports._default = _default = require('./wordlists/spanish.json'); - wordlists.spanish = _default; -} -catch (err) { } -try { - exports._default = _default = require('./wordlists/japanese.json'); - wordlists.japanese = _default; - wordlists.JA = _default; -} -catch (err) { } -try { - exports._default = _default = require('./wordlists/english.json'); - wordlists.english = _default; - wordlists.EN = _default; -} -catch (err) { } +wordlistFilenames.forEach(lang => { + try { + exports._default = _default = require('./wordlists/' + lang + '.json'); + wordlists[lang] = _default; + if (lang === 'japanese') + wordlists.JA = _default; + if (lang === 'english') + wordlists.EN = _default; + } + catch (err) { } +}); diff --git a/src/index.js b/src/index.js index 51aa2dd..e244341 100644 --- a/src/index.js +++ b/src/index.js @@ -152,7 +152,19 @@ function setDefaultWordlist(language) { const result = _wordlists_1.wordlists[language]; if (result) DEFAULT_WORDLIST = result; + else + throw new Error('Could not find wordlist for language "' + language + '"'); } exports.setDefaultWordlist = setDefaultWordlist; +function getDefaultWordlist() { + if (!DEFAULT_WORDLIST) + throw new Error('No Default Wordlist set'); + return Object.keys(_wordlists_1.wordlists).filter(lang => { + if (lang === 'JA' || lang === 'EN') + return false; + return _wordlists_1.wordlists[lang].every((word, index) => word === DEFAULT_WORDLIST[index]); + })[0]; +} +exports.getDefaultWordlist = getDefaultWordlist; var _wordlists_2 = require("./_wordlists"); exports.wordlists = _wordlists_2.wordlists; diff --git a/test/index.js b/test/index.js index bb8058d..005c0d2 100644 --- a/test/index.js +++ b/test/index.js @@ -34,6 +34,46 @@ vectors.english.forEach(function (v, i) { testVector('English', undefined, 'TREZ 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) }) +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) +}) + test('invalid entropy', function (t) { t.plan(3) diff --git a/ts_src/_wordlists.ts b/ts_src/_wordlists.ts index 746467d..242533d 100644 --- a/ts_src/_wordlists.ts +++ b/ts_src/_wordlists.ts @@ -1,42 +1,26 @@ // browserify by default only pulls in files that are hard coded in requires // In order of last to first in this file, the default wordlist will be chosen // based on what is present. (Bundles may remove wordlists they don't need) +const wordlistFilenames: string[] = [ + 'chinese_simplified', + 'chinese_traditional', + 'korean', + 'french', + 'italian', + 'spanish', + 'japanese', + 'english', // Last language available in list will be the default. +]; const wordlists: { [index: string]: string[] } = {}; let _default: string[] | undefined; -try { - _default = require('./wordlists/chinese_simplified.json'); - wordlists.chinese_simplified = _default as string[]; -} catch (err) {} -try { - _default = require('./wordlists/chinese_traditional.json'); - wordlists.chinese_traditional = _default as string[]; -} catch (err) {} -try { - _default = require('./wordlists/korean.json'); - wordlists.korean = _default as string[]; -} catch (err) {} -try { - _default = require('./wordlists/french.json'); - wordlists.french = _default as string[]; -} catch (err) {} -try { - _default = require('./wordlists/italian.json'); - wordlists.italian = _default as string[]; -} catch (err) {} -try { - _default = require('./wordlists/spanish.json'); - wordlists.spanish = _default as string[]; -} catch (err) {} -try { - _default = require('./wordlists/japanese.json'); - wordlists.japanese = _default as string[]; - wordlists.JA = _default as string[]; -} catch (err) {} -try { - _default = require('./wordlists/english.json'); - wordlists.english = _default as string[]; - wordlists.EN = _default as string[]; -} catch (err) {} +wordlistFilenames.forEach(lang => { + try { + _default = require('./wordlists/' + lang + '.json'); + wordlists[lang] = _default as string[]; + if (lang === 'japanese') wordlists.JA = _default as string[]; + if (lang === 'english') wordlists.EN = _default as string[]; + } catch (err) {} +}); // Last one to overwrite wordlist gets to be default. export { wordlists, _default }; diff --git a/ts_src/index.ts b/ts_src/index.ts index 62dbb66..39102f9 100644 --- a/ts_src/index.ts +++ b/ts_src/index.ts @@ -185,6 +185,18 @@ export function validateMnemonic( export function setDefaultWordlist(language: string): void { const result = wordlists[language]; if (result) DEFAULT_WORDLIST = result; + else + throw new Error('Could not find wordlist for language "' + language + '"'); +} + +export function getDefaultWordlist(): string { + if (!DEFAULT_WORDLIST) throw new Error('No Default Wordlist set'); + return Object.keys(wordlists).filter(lang => { + if (lang === 'JA' || lang === 'EN') return false; + return wordlists[lang].every( + (word, index) => word === DEFAULT_WORDLIST![index], + ); + })[0]; } export { wordlists } from './_wordlists'; diff --git a/types/index.d.ts b/types/index.d.ts index 4bdf33a..e4701a7 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -8,4 +8,5 @@ export declare function entropyToMnemonic(entropy: Buffer | string, wordlist?: s export declare function generateMnemonic(strength?: number, rng?: (size: number) => Buffer, wordlist?: string[]): string; export declare function validateMnemonic(mnemonic: string, wordlist?: string[]): boolean; export declare function setDefaultWordlist(language: string): void; +export declare function getDefaultWordlist(): string; export { wordlists } from './_wordlists';