pub struct Mnemonic { /* private fields */ }mnemonic only.Expand description
A BIP-39 mnemonic phrase for key derivation.
§Example
use aptos_sdk::account::Mnemonic;
// Generate a new mnemonic
let mnemonic = Mnemonic::generate(24).unwrap();
println!("Mnemonic: {}", mnemonic.phrase());
// Parse an existing mnemonic
let mnemonic = Mnemonic::from_phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about").unwrap();Implementations§
Source§impl Mnemonic
impl Mnemonic
Sourcepub fn generate(word_count: usize) -> AptosResult<Self>
pub fn generate(word_count: usize) -> AptosResult<Self>
Sourcepub fn from_phrase(phrase: &str) -> AptosResult<Self>
pub fn from_phrase(phrase: &str) -> AptosResult<Self>
Creates a mnemonic from an existing phrase.
§Errors
Returns an error if the phrase is not a valid BIP-39 mnemonic.
Sourcepub fn to_seed(&self) -> AptosResult<[u8; 64]>
pub fn to_seed(&self) -> AptosResult<[u8; 64]>
Derives the seed from this mnemonic.
Uses an empty passphrase by default.
§Errors
Returns an error if the mnemonic cannot be re-parsed (should not happen since the phrase was validated during construction).
Sourcepub fn to_seed_with_passphrase(&self, passphrase: &str) -> AptosResult<[u8; 64]>
pub fn to_seed_with_passphrase(&self, passphrase: &str) -> AptosResult<[u8; 64]>
Derives the seed from this mnemonic with a passphrase.
§Errors
Returns an error if the mnemonic phrase cannot be re-parsed. This should never happen because the phrase is validated during construction, but returning an error is safer than panicking.
Sourcepub fn derive_ed25519_key(&self, index: u32) -> AptosResult<Ed25519PrivateKey>
Available on crate feature ed25519 only.
pub fn derive_ed25519_key(&self, index: u32) -> AptosResult<Ed25519PrivateKey>
ed25519 only.Derives an Ed25519 private key using the Aptos default path
m/44'/637'/0'/0'/{address_index}', where index selects the
5th (address) component. Index 0 yields m/44'/637'/0'/0'/0'.
§Errors
Returns an error if key derivation fails or the derived key is invalid.
Sourcepub fn derive_ed25519_key_at_path(
&self,
path: &DerivationPath,
) -> AptosResult<Ed25519PrivateKey>
Available on crate feature ed25519 only.
pub fn derive_ed25519_key_at_path( &self, path: &DerivationPath, ) -> AptosResult<Ed25519PrivateKey>
ed25519 only.Derives an Ed25519 private key at a custom BIP-44 path.
All components of path must be hardened, matching SLIP-0010 (Ed25519
has no non-hardened child derivation).
§Errors
Returns an error if the path contains a non-hardened component, if HMAC/SLIP-0010 derivation fails, or if the resulting key bytes do not form a valid Ed25519 private key.
Sourcepub fn derive_secp256k1_key(
&self,
index: u32,
) -> AptosResult<Secp256k1PrivateKey>
Available on crate feature secp256k1 only.
pub fn derive_secp256k1_key( &self, index: u32, ) -> AptosResult<Secp256k1PrivateKey>
secp256k1 only.Derives a Secp256k1 private key using the Aptos default path
m/44'/637'/0'/0/{address_index}, where index selects the 5th
(address) component. Index 0 yields m/44'/637'/0'/0/0.
The last two indices are non-hardened by convention, matching the TypeScript SDK.
§Errors
Returns an error if key derivation fails or the derived scalar is invalid (probability ~2^-127 per step, effectively never).
Sourcepub fn derive_secp256k1_key_at_path(
&self,
path: &DerivationPath,
) -> AptosResult<Secp256k1PrivateKey>
Available on crate feature secp256k1 only.
pub fn derive_secp256k1_key_at_path( &self, path: &DerivationPath, ) -> AptosResult<Secp256k1PrivateKey>
secp256k1 only.Derives a Secp256k1 private key at a custom BIP-32 path.
Supports hardened and non-hardened components per BIP-32.
§Errors
Returns an error if HMAC fails, if any intermediate scalar is invalid (zero or >= curve order – vanishingly rare), or if the final 32 bytes do not form a valid Secp256k1 private key.