New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow excluding wordlists when building for browserify #105
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"use strict"; | ||
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; | ||
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) { } | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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; | ||
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 }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,18 @@ | ||
import createHash = require('create-hash'); | ||
import * as createHash from 'create-hash'; | ||
import { pbkdf2 as pbkdf2Async, pbkdf2Sync as pbkdf2 } from 'pbkdf2'; | ||
import randomBytes = require('randombytes'); | ||
|
||
import * as randomBytes from 'randombytes'; | ||
// use unorm until String.prototype.normalize gets better browser support | ||
import unorm = require('unorm'); | ||
|
||
import CHINESE_SIMPLIFIED_WORDLIST = require('./wordlists/chinese_simplified.json'); | ||
import CHINESE_TRADITIONAL_WORDLIST = require('./wordlists/chinese_traditional.json'); | ||
import ENGLISH_WORDLIST = require('./wordlists/english.json'); | ||
import FRENCH_WORDLIST = require('./wordlists/french.json'); | ||
import ITALIAN_WORDLIST = require('./wordlists/italian.json'); | ||
import JAPANESE_WORDLIST = require('./wordlists/japanese.json'); | ||
import KOREAN_WORDLIST = require('./wordlists/korean.json'); | ||
import SPANISH_WORDLIST = require('./wordlists/spanish.json'); | ||
const DEFAULT_WORDLIST = ENGLISH_WORDLIST; | ||
import * as unorm from 'unorm'; | ||
import { _default as _DEFAULT_WORDLIST, wordlists } from './_wordlists'; | ||
|
||
let DEFAULT_WORDLIST: string[] | undefined = _DEFAULT_WORDLIST; | ||
|
||
const INVALID_MNEMONIC = 'Invalid mnemonic'; | ||
const INVALID_ENTROPY = 'Invalid entropy'; | ||
const INVALID_CHECKSUM = 'Invalid mnemonic checksum'; | ||
const WORDLIST_REQUIRED = | ||
'A wordlist is required but a default could not be found.\n' + | ||
'Please explicitly pass a 2048 word array explicitly.'; | ||
|
||
function lpad(str: string, padString: string, length: number): string { | ||
while (str.length < length) str = padString + str; | ||
|
@@ -97,6 +92,9 @@ export function mnemonicToEntropy( | |
wordlist?: string[], | ||
): string { | ||
wordlist = wordlist || DEFAULT_WORDLIST; | ||
if (!wordlist) { | ||
throw new Error(WORDLIST_REQUIRED); | ||
} | ||
|
||
const words = unorm.nfkd(mnemonic).split(' '); | ||
if (words.length % 3 !== 0) throw new Error(INVALID_MNEMONIC); | ||
|
@@ -135,6 +133,9 @@ export function entropyToMnemonic( | |
): string { | ||
if (!Buffer.isBuffer(entropy)) entropy = Buffer.from(entropy, 'hex'); | ||
wordlist = wordlist || DEFAULT_WORDLIST; | ||
if (!wordlist) { | ||
throw new Error(WORDLIST_REQUIRED); | ||
} | ||
|
||
// 128 <= ENT <= 256 | ||
if (entropy.length < 16) throw new TypeError(INVALID_ENTROPY); | ||
|
@@ -151,7 +152,7 @@ export function entropyToMnemonic( | |
return wordlist![index]; | ||
}); | ||
|
||
return wordlist === JAPANESE_WORDLIST | ||
return wordlist[0] === '\u3042\u3044\u3053\u304f\u3057\u3093' // Japanese wordlist | ||
? words.join('\u3000') | ||
: words.join(' '); | ||
} | ||
|
@@ -181,16 +182,21 @@ export function validateMnemonic( | |
return true; | ||
} | ||
|
||
export const wordlists = { | ||
EN: ENGLISH_WORDLIST, | ||
JA: JAPANESE_WORDLIST, | ||
|
||
chinese_simplified: CHINESE_SIMPLIFIED_WORDLIST, | ||
chinese_traditional: CHINESE_TRADITIONAL_WORDLIST, | ||
english: ENGLISH_WORDLIST, | ||
french: FRENCH_WORDLIST, | ||
italian: ITALIAN_WORDLIST, | ||
japanese: JAPANESE_WORDLIST, | ||
korean: KOREAN_WORDLIST, | ||
spanish: SPANISH_WORDLIST, | ||
}; | ||
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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this should just return null...? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't you export There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do you mean? |
||
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'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
declare const wordlists: { | ||
[index: string]: string[]; | ||
}; | ||
declare let _default: string[] | undefined; | ||
export { wordlists, _default }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
declare const wordlists: { | ||
[index: string]: string[]; | ||
}; | ||
declare let _default: string[] | undefined; | ||
export { wordlists, _default }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expect this function to throw error if the wordlist does not exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition, why don't we provide
getDefaultWordlist
as well.