Example: mnemonic_signer
Example
To run this example:
- Clone the examples repository:
git clone git@github.com:alloy-rs/examples.git
- Run:
cargo run --example mnemonic_signer
//! Example of using `MnemonicBuilder` to access a wallet from a mnemonic phrase.
use alloy::signers::local::{coins_bip39::English, MnemonicBuilder};
use eyre::Result;
#[tokio::main]
async fn main() -> Result<()> {
let phrase = "work man father plunge mystery proud hollow address reunion sauce theory bonus";
let index = 0u32;
let password = "TREZOR123";
// Access mnemonic phrase with password.
// Child key at derivation path: m/44'/60'/0'/0/{index}.
let wallet = MnemonicBuilder::<English>::default()
.phrase(phrase)
.index(index)?
// Use this if your mnemonic is encrypted.
.password(password)
.build()?;
println!("Wallet: {}", wallet.address());
// Generate a random wallet (24 word phrase) at custom derivation path.
let wallet = MnemonicBuilder::<English>::default()
.word_count(24)
.derivation_path("m/44'/60'/0'/2/1")?
// Optionally add this if you want the generated mnemonic to be written
// to a file `.write_to(path)`.
.build_random()?;
println!("Random wallet: {}", wallet.address());
Ok(())
}
Find the source code on Github here.