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
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?

@DaughterOfMars DaughterOfMars merged commit b3c59fd into sdk-bindings Nov 3, 2025
14 checks passed
@DaughterOfMars DaughterOfMars deleted the feat/pk-from-mnemonic branch November 3, 2025 08:00
Alex6323 pushed a commit that referenced this pull request Nov 3, 2025
* feat(crypto): Add ability to create a private key from a mnemonic

* clippy

* allow passing password and path

* fix mnemonic parsing and add example

* bindings examples

* clippy

* add password and path to example

* remove impls from `SimpleKeypair`

* add tests and fix secp256 generation

* split mnemonic methods

* fix python example

* use constant arrays
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