Skip to content

Commit

Permalink
fix exports
Browse files Browse the repository at this point in the history
  • Loading branch information
d-yokoi committed Mar 23, 2019
1 parent 834389c commit b91260e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 53 deletions.
40 changes: 19 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(' ');
Expand Down Expand Up @@ -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');
Expand All @@ -134,13 +139,15 @@ function entropyToMnemonic(entropy, wordlist) {
? words.join('\u3000')
: words.join(' ');
}
exports.entropyToMnemonic = entropyToMnemonic;
function generateMnemonic(strength, rng, wordlist) {
strength = strength || 128;
if (strength % 32 !== 0)
throw new TypeError(INVALID_ENTROPY);
rng = rng || randomBytes;
return entropyToMnemonic(rng(strength / 8), wordlist);
}
exports.generateMnemonic = generateMnemonic;
function validateMnemonic(mnemonic, wordlist) {
try {
mnemonicToEntropy(mnemonic, wordlist);
Expand All @@ -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,
};
58 changes: 27 additions & 31 deletions ts_src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -85,15 +85,18 @@ function mnemonicToSeedAsync(
);
}

async function mnemonicToSeedHexAsync(
export async function mnemonicToSeedHexAsync(
mnemonic: string,
password: string,
): Promise<string> {
const buf = await mnemonicToSeedAsync(mnemonic, password);
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(' ');
Expand All @@ -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);
Expand All @@ -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 {
Expand All @@ -154,7 +157,7 @@ function entropyToMnemonic(
: words.join(' ');
}

function generateMnemonic(
export function generateMnemonic(
strength?: number,
rng?: (size: number) => Buffer,
wordlist?: string[],
Expand All @@ -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) {
Expand All @@ -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,

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 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,
};
22 changes: 21 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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[];
};

0 comments on commit b91260e

Please sign in to comment.