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