bitcoinjs / bip39

JavaScript implementation of Bitcoin BIP39: Mnemonic code for generating deterministic keys
ISC License
1.11k stars 447 forks source link

Reduce code duplication with normalize function #123

Closed lukechilds closed 5 years ago

lukechilds commented 5 years ago

I noticed when reading through this that there's quite a bit of code duplication for string normalization. Everywhere a string is normalized it's first evaluated as a boolean, then set as an empty string if false, then the resulting string is normalized with .normalize('NFKD').

This PR DRYs up the code with a simpler normalize(str?) function that always ensures those actions are taken.

I think it makes the code quite a bit more readable, especially considering the line length wrapping we enforce.

e.g this:

const mnemonicBuffer = Buffer.from(
  (mnemonic || '').normalize('NFKD'),
  'utf8',
);
const saltBuffer = Buffer.from(
  salt((password || '').normalize('NFKD')),
  'utf8',
);

becomes this:

const mnemonicBuffer = Buffer.from(normalize(mnemonic), 'utf8');
const saltBuffer = Buffer.from(salt(normalize(password)), 'utf8');