mirror of
https://github.com/status-im/bip39.git
synced 2025-02-16 14:36:28 +00:00
Reduce code duplication with normalize function
This commit is contained in:
parent
07c06c0911
commit
f79c9df3c2
13
src/index.js
13
src/index.js
@ -10,6 +10,9 @@ 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 normalize(str) {
|
||||
return (str || '').normalize('NFKD');
|
||||
}
|
||||
function lpad(str, padString, length) {
|
||||
while (str.length < length)
|
||||
str = padString + str;
|
||||
@ -33,16 +36,16 @@ function salt(password) {
|
||||
return 'mnemonic' + (password || '');
|
||||
}
|
||||
function mnemonicToSeedSync(mnemonic, password) {
|
||||
const mnemonicBuffer = Buffer.from((mnemonic || '').normalize('NFKD'), 'utf8');
|
||||
const saltBuffer = Buffer.from(salt((password || '').normalize('NFKD')), 'utf8');
|
||||
const mnemonicBuffer = Buffer.from(normalize(mnemonic), 'utf8');
|
||||
const saltBuffer = Buffer.from(salt(normalize(password)), 'utf8');
|
||||
return pbkdf2_1.pbkdf2Sync(mnemonicBuffer, saltBuffer, 2048, 64, 'sha512');
|
||||
}
|
||||
exports.mnemonicToSeedSync = mnemonicToSeedSync;
|
||||
function mnemonicToSeed(mnemonic, password) {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const mnemonicBuffer = Buffer.from((mnemonic || '').normalize('NFKD'), 'utf8');
|
||||
const saltBuffer = Buffer.from(salt((password || '').normalize('NFKD')), 'utf8');
|
||||
const mnemonicBuffer = Buffer.from(normalize(mnemonic), 'utf8');
|
||||
const saltBuffer = Buffer.from(salt(normalize(password)), 'utf8');
|
||||
pbkdf2_1.pbkdf2(mnemonicBuffer, saltBuffer, 2048, 64, 'sha512', (err, data) => {
|
||||
if (err)
|
||||
return reject(err);
|
||||
@ -61,7 +64,7 @@ function mnemonicToEntropy(mnemonic, wordlist) {
|
||||
if (!wordlist) {
|
||||
throw new Error(WORDLIST_REQUIRED);
|
||||
}
|
||||
const words = (mnemonic || '').normalize('NFKD').split(' ');
|
||||
const words = normalize(mnemonic).split(' ');
|
||||
if (words.length % 3 !== 0)
|
||||
throw new Error(INVALID_MNEMONIC);
|
||||
// convert word indices to 11 bit binary strings
|
||||
|
@ -12,6 +12,10 @@ const WORDLIST_REQUIRED =
|
||||
'A wordlist is required but a default could not be found.\n' +
|
||||
'Please explicitly pass a 2048 word array explicitly.';
|
||||
|
||||
function normalize(str?: string): string {
|
||||
return (str || '').normalize('NFKD');
|
||||
}
|
||||
|
||||
function lpad(str: string, padString: string, length: number): string {
|
||||
while (str.length < length) str = padString + str;
|
||||
return str;
|
||||
@ -43,14 +47,8 @@ export function mnemonicToSeedSync(
|
||||
mnemonic: string,
|
||||
password?: string,
|
||||
): Buffer {
|
||||
const mnemonicBuffer = Buffer.from(
|
||||
(mnemonic || '').normalize('NFKD'),
|
||||
'utf8',
|
||||
);
|
||||
const saltBuffer = Buffer.from(
|
||||
salt((password || '').normalize('NFKD')),
|
||||
'utf8',
|
||||
);
|
||||
const mnemonicBuffer = Buffer.from(normalize(mnemonic), 'utf8');
|
||||
const saltBuffer = Buffer.from(salt(normalize(password)), 'utf8');
|
||||
|
||||
return pbkdf2Sync(mnemonicBuffer, saltBuffer, 2048, 64, 'sha512');
|
||||
}
|
||||
@ -62,14 +60,8 @@ export function mnemonicToSeed(
|
||||
return new Promise(
|
||||
(resolve, reject): void => {
|
||||
try {
|
||||
const mnemonicBuffer = Buffer.from(
|
||||
(mnemonic || '').normalize('NFKD'),
|
||||
'utf8',
|
||||
);
|
||||
const saltBuffer = Buffer.from(
|
||||
salt((password || '').normalize('NFKD')),
|
||||
'utf8',
|
||||
);
|
||||
const mnemonicBuffer = Buffer.from(normalize(mnemonic), 'utf8');
|
||||
const saltBuffer = Buffer.from(salt(normalize(password)), 'utf8');
|
||||
pbkdf2(mnemonicBuffer, saltBuffer, 2048, 64, 'sha512', (err, data) => {
|
||||
if (err) return reject(err);
|
||||
else return resolve(data);
|
||||
@ -90,7 +82,7 @@ export function mnemonicToEntropy(
|
||||
throw new Error(WORDLIST_REQUIRED);
|
||||
}
|
||||
|
||||
const words = (mnemonic || '').normalize('NFKD').split(' ');
|
||||
const words = normalize(mnemonic).split(' ');
|
||||
if (words.length % 3 !== 0) throw new Error(INVALID_MNEMONIC);
|
||||
|
||||
// convert word indices to 11 bit binary strings
|
||||
|
Loading…
x
Reference in New Issue
Block a user