Page MenuHomePhabricator

[ecash-lib] Add mnemonic functionality
AcceptedPublic

Authored by tobias_ruck on Thu, Jan 30, 13:34.

Details

Reviewers
bytesofman
Group Reviewers
Restricted Project
Summary

Adds entropyToMnemonic, mnemonicToEntropy and mnemonicToSeed, which is important to build a wallet.

The wordlist is included in the bundle but has to be imported manually to give users maximum flexibilty without forcing everyone to blow up their bundle size.

Depends on D17610.

Test Plan

npm test

Event Timeline

very nice to have this in our own lib but we do lose the empirical validation of the bip39 lib, i.e. this has been used for years in wallets without any known issues in "truly random" seed generation.

how does this approach differ from the legacy libs it would replace? Is it just modernization / replacing js with ts?

modules/ecash-lib/src/mnemonic.ts
13

why not

return entropyLength / 4

would also be nice to use defined consts instead of hardcoded numbers; we have magic number 8 all over this file, could be a const defined after BITS_PER_WORD

This revision now requires changes to proceed.Thu, Jan 30, 17:59

very nice to have this in our own lib but we do lose the empirical validation of the bip39 lib, i.e. this has been used for years in wallets without any known issues in "truly random" seed generation.

There's no random generation here, it's all deterministic. The user has to provide entropy at the moment. In the future, we can add a generate function.

how does this approach differ from the legacy libs it would replace? Is it just modernization / replacing js with ts?

The approach is very similar, the few differences are:

  • Wordlist is to be imported separately, gives more flexibility when using this library without having to use IgnorePlugin in webpack
  • We use Uint8Array to calculate the mnemonic from/to entropy, bip39 uses strings of "1" and "0", which tbh seems a bit goofy to me
  • Better error messages, bip39 just says "Invalid mnemonic", here we say something like "Invalid word: hans"
  • (IMO) cleaner code, e.g. we don't match on the first word of the wordlist to see if we're Japanese (to find the word separator), instead this is provided as part of the wordlist.
modules/ecash-lib/src/mnemonic.ts
13

well this is from the original code, I guess it converts to bits first and then the number bits for each checksum bit is 32.

This revision is now accepted and ready to land.Thu, Jan 30, 22:43