Skip to content

Conversation

@DaughterOfMars
Copy link
Contributor

@DaughterOfMars DaughterOfMars commented Oct 27, 2025

Description

Allow creating private keys from mnemonics. While working on this, it made sense to separate the PrivateKeyExt trait into several smaller traits.

Closes #266

@DaughterOfMars DaughterOfMars linked an issue Oct 27, 2025 that may be closed by this pull request
Comment on lines 150 to 156
&[
crate::DERIVATION_PATH_PURPOSE_ED25519,
crate::DERIVATION_PATH_COIN_TYPE,
0,
0,
0,
],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine to have this as default, but we should also allow other paths

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole path or just the last three?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm not sure about this, the wallets we have only increase the account index for new accounts, maybe we should only allow this then so it's compatible
People that want something else can use a bip39 library directly to derive a private key with other bip32 paths

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thibault-martinez thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this question still relevant?

but we should also allow other paths

I see we are taking a path as parameter?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part not, as @DaughterOfMars added the path now, but the question is whether we want the full path or just an account index

Copy link
Member

@thibault-martinez thibault-martinez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this deserves a new example or an addition to an existing one?

println!("Public Key With Flag: {flagged_public_key}");
println!("Address: {address}");

let private_key = Secp256r1PrivateKey::from_mnemonic(MNEMONIC, None, None)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe at least one example with a password and/or path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

/// Defines the scheme of a private key
pub trait PrivateKeyScheme {
Copy link
Member

@thibault-martinez thibault-martinez Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splitting this in 2 feels over the top to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well only some types can have a const scheme

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok but you can't really implement scheme() if you don't have one? Or what am I missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SimpleKeypair can impl PrivateKeyScheme but not the const version

}

/// Defines a type which can be constructed from bytes
pub trait FromBytes {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these 2 traits when ToFromBech32 is one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because some types cannot impl FromBytes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like what?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SimpleKeypair


let path = path.into().unwrap_or_else(|| {
format!(
"m/{}'/{}'/0'/0'/0'",
Copy link
Contributor

@Alex6323 Alex6323 Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am asking this because I am unsure about this myself, but are you sure about the hardening in this path? AFAIK this scheme allows safe non-hardened derivation as compared to ED25519. So just checking whether you've thought about this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not thought about it and I do not know what the best way is

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

monorepo has this

/// Secp256k1 follows BIP-32/44 using path where the first 3 levels are
/// hardened: m/54'/4218'/0'/0/{index} Secp256r1 follows BIP-32/44 using path
/// where the first 3 levels are hardened: m/74'/4218'/0'/0/{index}.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still wonder whether we should take the whole path. Maybe two functions?

Comment on lines 121 to 122
scheme: SignatureScheme,
phrase: &str,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe the phrase should come first, then the scheme?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the scheme is the most important thing because it determines the type of key, which is why I put it first

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with scheme being first since it's being matched on and the rest of the params are just being forwarded

Copy link
Contributor

@Alex6323 Alex6323 Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't the method called from_mnemonic which is represented more by the phrase than by the scheme?

Actually, I would even prefer having it in last position (even if its the the most important internally), reason being that the argument list should be consistent with the other methods. Wouldn't those signatures be better overall from a user standpoint?

.from_mnemonic(phrase, password, path)
.from_menmonic(phrase, password, path, scheme)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I would also be fine with simply removing this method and the impls on SimpleKeypair. You can just create the inner keys and construct SimpleKeypair from them 🤷‍♀️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I would also be fine with simply removing this method and the impls on SimpleKeypair. You can just create the inner keys and construct SimpleKeypair from them 🤷‍♀️

I think I agree with that, we also don't have a method to create a random SimpleKeypair with one of the schemes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow to create private key from mnemonic

5 participants