bitcoinjs / bip39

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

Passphrase Usage Support #83

Closed itsMikeLowrey closed 6 years ago

itsMikeLowrey commented 6 years ago

I was wondering if this library supported passphrase protecting mnemonic phrases. It is described in the "From mnemonic to seed" section of the bip39 white paper. I am looking for the type of functionality found in Ian Coleman Web Wallet , BIP39 Passphrase (optional) section. Thank you for the help.

itsMikeLowrey commented 6 years ago

I was able to find it on this page https://github.com/bitcoinjs/bip39/blob/v2.5.0/index.js#L50-L59. I am not sure why this functionality isn't well documented on the read me page. I will close this issue.

lacksfish commented 6 years ago

It seems that mnemonicToSeed() returns a Buffer that contains "too much" entropy? https://github.com/bitcoinjs/bip39/blob/v2.5.0/index.js#L99 will then throw an Exception when I try to feed that entropy seed back into entropyToMnemonic()

Below I share the code with which I tried to create a password derived mnemonic with a passphrase based on the optional BIP39 spec with the bip39.js library. I also add a code snippet of how to achieve the encryption using bitcore-mnemonic

// bip39.js
var mnemonic = bip39.generateMnemonic(256);
var seedFromFirstMnemonic = bip39.mnemonicToSeed(mnemonic, "password");
// next line throws
var encryptedSecondMnemonic = bip39.entropyToMnemonic(seedFromFirstMnemonic);
// bitcore-mnemonic
var mnemonic = new Mnemonic(256, Mnemonic.Words.ENGLISH);
var seed = mnemonic.toSeed("password");
var encryptedMnemonic = Mnemonic.fromSeed(seed, Mnemonic.Words.ENGLISH);

The Exception thrown is

lacksfish@computer:~/tmp/path$ node mnemonic-crypt.js
/tmp/path/bip39/index.js:99
  if (entropy.length > 32) throw new TypeError(INVALID_ENTROPY)
                           ^

TypeError: Invalid entropy
    at Object.entropyToMnemonic (/tmp/path/bip39/index.js:99:34)
    at Object.<anonymous> (/tmp/path/mnemonic-crypt.js:7:37)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3

Most likely I'm doing something wrong. Although what is confusing is that bitcore-mnemonic seems to be using the seed/entropy Buffer the way I'd have expected... ( see https://github.com/bitpay/bitcore-mnemonic/blob/master/lib/mnemonic.js#L71)

dcousens commented 6 years ago

@lacksfish this is probably a new issue. What environment are you using?

lacksfish commented 6 years ago

Ok. Opened a new issue (please excuse the copypasta) and included my environment in #89

dcousens commented 6 years ago

Added a README note in https://github.com/bitcoinjs/bip39/pull/90