cosmos-rust icon indicating copy to clipboard operation
cosmos-rust copied to clipboard

cosmrs: better document BIP-32 `SigningKey` derivation

Open 0xForerunner opened this issue 3 years ago • 8 comments

As it stands there is very little documentation surrounding the SigningKey trait and the way you are supposed to use it with Bip32. I think a very common use case would be generating this from a mnemonic string, and as it stands there is no obvious way to go about that. An example of doing that would likely be very helpful for many.

0xForerunner avatar Oct 27 '22 17:10 0xForerunner

This is the relevant method: https://docs.rs/cosmrs/latest/cosmrs/crypto/secp256k1/struct.SigningKey.html#method.derive_from_path

tony-iqlusion avatar Oct 27 '22 17:10 tony-iqlusion

This is the relevant method: https://docs.rs/cosmrs/latest/cosmrs/crypto/secp256k1/struct.SigningKey.html#method.derive_from_path

Is this not the correct way to do it?

        let mnemonic = bip32::Mnemonic::new(chain.private_key.clone(), Language::English).unwrap();
        let seed = mnemonic.to_seed("password");
        let xprv = XPrv::new(&seed)?;
        let signing_key: SigningKey = xprv.into();

0xForerunner avatar Oct 27 '22 17:10 0xForerunner

@ewoolsey that's the correct way to get the seed, however you have no derivation path in your example.

You can pass the seed into derive_from_path, but you need a derivation path like m/44'/118'/0'/0

tony-iqlusion avatar Oct 27 '22 18:10 tony-iqlusion

Ahhh so something like this?

        let mnemonic = bip32::Mnemonic::new(chain.mnemonic.clone(), Language::English).unwrap();
        let seed = mnemonic.to_seed("password");
        let child_path = "m/0/2147483647'/1/2147483646'";
        let child_xprv = XPrv::derive_from_path(&seed, &child_path.parse()?)?;        
        let signing_key: SigningKey = child_xprv.into();

0xForerunner avatar Oct 27 '22 18:10 0xForerunner

The last two lines need to be:

let signing_key = cosmrs::crypto::secp256k1::SigningKey::derive_from_path(&seed, child_path.parse()?)?);

tony-iqlusion avatar Oct 27 '22 18:10 tony-iqlusion

Gotcha! Thanks for the help! Appreciate it!

0xForerunner avatar Oct 27 '22 18:10 0xForerunner

Is this the only place for one to be able to get this knowledge? There is still no good docs for this and I had to come here..

meekteek avatar Jan 17 '24 19:01 meekteek

@meekteek this issue is still open to track updating the documentation because it hasn't been done yet.

Contributions to improve the documentation would be welcome.

tony-iqlusion avatar Feb 01 '24 16:02 tony-iqlusion