cosmos / cosmjs

The Swiss Army knife to power JavaScript based client solutions ranging from Web apps/explorers over browser extensions to server-side clients like faucets/scrapers.
https://cosmos.github.io/cosmjs/
Apache License 2.0
652 stars 341 forks source link

Request for Bip 39 passphrase functionality #736

Closed puneet2019 closed 3 years ago

puneet2019 commented 3 years ago

DirectSecp256k1HdWallet.fromMnemonic should take a passphrase parameter as well, it is used in creating bip39.mnemonicToSeed.

webmaster128 commented 3 years ago

Are you using this passphrase? It is not supported here because I never saw it being used in the wild. It would be quite some pain to design an API for that because the method already has two optional arguments.

puneet2019 commented 3 years ago

yes, I was using cosmosjs of cosmostaion, they give this option and I am thinking of migrating to cosmjs because I am already using stargate for offline signer. Will reduce an extra dependency.

webmaster128 commented 3 years ago

Alright, let's see how this can be done.

In the meantime what you can do is using thr non-HD wallet DirectSecp256k1Wallet

async function myHelper(/* ... , */password: string): Promise<DirectSecp256k1Wallet> {
  const mnemonicChecked = new EnglishMnemonic(mnemonic);
  const seed = await Bip39.mnemonicToSeed(mnemonicChecked, password);
  const { privkey } = Slip10.derivePath(Slip10Curve.Secp256k1, seed, hdPath);
  return DirectSecp256k1Wallet.fromKey(privkey, prefix);
}
puneet2019 commented 3 years ago

Yes, this works, Thank you.

webmaster128 commented 3 years ago

@willclarktech What do you think about changing DirectSecp256k1HdWallet.fromMnemonic to only take the mnemonic as a positional argument and the test goes into and optional options object (path, prefix, password)? I would like to avoid an growing list of positional optional arguments.

willclarktech commented 3 years ago

@webmaster128 I like that. Actually there are probably a few other places which could benefit from this kind of shuffle.