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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"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 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) { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// 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 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) {} | ||
|
||
// Last one to overwrite wordlist gets to be default. | ||
export { wordlists, _default }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,9 @@ 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; | ||
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 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 commentThe reason will be displayed to describe this comment to others. Learn more. In addition, why don't we provide |
||
} | ||
|
||
export { wordlists } from './_wordlists'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think it would be even better if you implement this using loop.