fix exports

This commit is contained in:
d-yokoi 2019-03-23 22:46:53 +09:00
parent 834389c038
commit b91260e9f4
No known key found for this signature in database
GPG Key ID: 49EAF81BC6A0D19A
3 changed files with 66 additions and 52 deletions

View File

@ -53,9 +53,11 @@ function mnemonicToSeed(mnemonic, password) {
const saltBuffer = Buffer.from(salt(unorm.nfkd(password)), 'utf8');
return pbkdf2_1.pbkdf2Sync(mnemonicBuffer, saltBuffer, 2048, 64, 'sha512');
}
exports.mnemonicToSeed = mnemonicToSeed;
function mnemonicToSeedHex(mnemonic, password) {
return mnemonicToSeed(mnemonic, password).toString('hex');
}
exports.mnemonicToSeedHex = mnemonicToSeedHex;
function mnemonicToSeedAsync(mnemonic, password) {
return new Promise((resolve, reject) => {
try {
@ -73,12 +75,14 @@ function mnemonicToSeedAsync(mnemonic, password) {
}
});
}
exports.mnemonicToSeedAsync = mnemonicToSeedAsync;
function mnemonicToSeedHexAsync(mnemonic, password) {
return __awaiter(this, void 0, void 0, function* () {
const buf = yield mnemonicToSeedAsync(mnemonic, password);
return buf.toString('hex');
});
}
exports.mnemonicToSeedHexAsync = mnemonicToSeedHexAsync;
function mnemonicToEntropy(mnemonic, wordlist) {
wordlist = wordlist || DEFAULT_WORDLIST;
const words = unorm.nfkd(mnemonic).split(' ');
@ -111,6 +115,7 @@ function mnemonicToEntropy(mnemonic, wordlist) {
throw new Error(INVALID_CHECKSUM);
return entropy.toString('hex');
}
exports.mnemonicToEntropy = mnemonicToEntropy;
function entropyToMnemonic(entropy, wordlist) {
if (!Buffer.isBuffer(entropy))
entropy = Buffer.from(entropy, 'hex');
@ -134,6 +139,7 @@ function entropyToMnemonic(entropy, wordlist) {
? words.join('\u3000')
: words.join(' ');
}
exports.entropyToMnemonic = entropyToMnemonic;
function generateMnemonic(strength, rng, wordlist) {
strength = strength || 128;
if (strength % 32 !== 0)
@ -141,6 +147,7 @@ function generateMnemonic(strength, rng, wordlist) {
rng = rng || randomBytes;
return entropyToMnemonic(rng(strength / 8), wordlist);
}
exports.generateMnemonic = generateMnemonic;
function validateMnemonic(mnemonic, wordlist) {
try {
mnemonicToEntropy(mnemonic, wordlist);
@ -150,25 +157,16 @@ function validateMnemonic(mnemonic, wordlist) {
}
return true;
}
module.exports = {
mnemonicToSeed,
mnemonicToSeedAsync,
mnemonicToSeedHex,
mnemonicToSeedHexAsync,
mnemonicToEntropy,
entropyToMnemonic,
generateMnemonic,
validateMnemonic,
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,
},
exports.validateMnemonic = validateMnemonic;
exports.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,
};

View File

@ -47,18 +47,18 @@ function salt(password?: string): string {
return 'mnemonic' + (password || '');
}
function mnemonicToSeed(mnemonic: string, password: string): Buffer {
export function mnemonicToSeed(mnemonic: string, password: string): Buffer {
const mnemonicBuffer = Buffer.from(unorm.nfkd(mnemonic), 'utf8');
const saltBuffer = Buffer.from(salt(unorm.nfkd(password)), 'utf8');
return pbkdf2(mnemonicBuffer, saltBuffer, 2048, 64, 'sha512');
}
function mnemonicToSeedHex(mnemonic: string, password: string): string {
export function mnemonicToSeedHex(mnemonic: string, password: string): string {
return mnemonicToSeed(mnemonic, password).toString('hex');
}
function mnemonicToSeedAsync(
export function mnemonicToSeedAsync(
mnemonic: string,
password: string,
): Promise<Buffer> {
@ -85,7 +85,7 @@ function mnemonicToSeedAsync(
);
}
async function mnemonicToSeedHexAsync(
export async function mnemonicToSeedHexAsync(
mnemonic: string,
password: string,
): Promise<string> {
@ -93,7 +93,10 @@ async function mnemonicToSeedHexAsync(
return buf.toString('hex');
}
function mnemonicToEntropy(mnemonic: string, wordlist: string[]): string {
export function mnemonicToEntropy(
mnemonic: string,
wordlist?: string[],
): string {
wordlist = wordlist || DEFAULT_WORDLIST;
const words = unorm.nfkd(mnemonic).split(' ');
@ -102,7 +105,7 @@ function mnemonicToEntropy(mnemonic: string, wordlist: string[]): string {
// convert word indices to 11 bit binary strings
const bits = words
.map(word => {
const index = wordlist.indexOf(word);
const index = wordlist!.indexOf(word);
if (index === -1) throw new Error(INVALID_MNEMONIC);
return lpad(index.toString(2), '0', 11);
@ -127,7 +130,7 @@ function mnemonicToEntropy(mnemonic: string, wordlist: string[]): string {
return entropy.toString('hex');
}
function entropyToMnemonic(
export function entropyToMnemonic(
entropy: Buffer | string,
wordlist?: string[],
): string {
@ -154,7 +157,7 @@ function entropyToMnemonic(
: words.join(' ');
}
function generateMnemonic(
export function generateMnemonic(
strength?: number,
rng?: (size: number) => Buffer,
wordlist?: string[],
@ -166,7 +169,10 @@ function generateMnemonic(
return entropyToMnemonic(rng(strength / 8), wordlist);
}
function validateMnemonic(mnemonic: string, wordlist: string[]): boolean {
export function validateMnemonic(
mnemonic: string,
wordlist?: string[],
): boolean {
try {
mnemonicToEntropy(mnemonic, wordlist);
} catch (e) {
@ -176,26 +182,16 @@ function validateMnemonic(mnemonic: string, wordlist: string[]): boolean {
return true;
}
module.exports = {
mnemonicToSeed,
mnemonicToSeedAsync,
mnemonicToSeedHex,
mnemonicToSeedHexAsync,
mnemonicToEntropy,
entropyToMnemonic,
generateMnemonic,
validateMnemonic,
wordlists: {
EN: ENGLISH_WORDLIST,
JA: JAPANESE_WORDLIST,
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,
},
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,
};

22
types/index.d.ts vendored
View File

@ -1 +1,21 @@
export {};
/// <reference types="node" />
export declare function mnemonicToSeed(mnemonic: string, password: string): Buffer;
export declare function mnemonicToSeedHex(mnemonic: string, password: string): string;
export declare function mnemonicToSeedAsync(mnemonic: string, password: string): Promise<Buffer>;
export declare function mnemonicToSeedHexAsync(mnemonic: string, password: string): Promise<string>;
export declare function mnemonicToEntropy(mnemonic: string, wordlist?: string[]): string;
export declare function entropyToMnemonic(entropy: Buffer | string, wordlist?: string[]): string;
export declare function generateMnemonic(strength?: number, rng?: (size: number) => Buffer, wordlist?: string[]): string;
export declare function validateMnemonic(mnemonic: string, wordlist?: string[]): boolean;
export declare const wordlists: {
EN: string[];
JA: string[];
chinese_simplified: string[];
chinese_traditional: string[];
english: string[];
french: string[];
italian: string[];
japanese: string[];
korean: string[];
spanish: string[];
};