Written by LXDAO Buidler: 0xhardman
This guide demonstrates how to convert a mnemonic phrase to an Ethereum address using Rust, offering blockchain developers deeper insights into cryptographic principles and system architecture.
Introduction
Your Ethereum journey likely began with creating a mnemonic phrase via MetaMask. But how does that 12-word phrase transform into a functional ETH address? This article demystifies the process step-by-step while answering key questions:
- Can you create a mnemonic from a dictionary?
- Why are mnemonics sensitive?
- Can an ETH private key generate a BTC address?
- How do 12 words become a private key and address?
Why Rust?
Rust’s reliability and efficiency make it ideal for blockchain development. Ethereum ecosystem projects like Foundry (development toolkit) and Reth (protocol implementation) already leverage Rust’s capabilities.
Step-by-Step Conversion Process
Step 1: Generate Mnemonic Phrase
- Mnemonics derive from entropy (random binary data) and include a checksum for validation.
- BIP39 wordlists support multiple languages (English, Chinese, Japanese, etc.).
- Key Insight: Mnemonics aren’t fully random—the last word is a checksum calculated from preceding words.
fn step1_generate_mnemonic() {
let entropy = &[0x33, 0xE4, 0x6B, 0xB1, ...]; // 128-bit entropy
let mnemonic = Mnemonic::from_entropy(entropy, Language::English).unwrap();
println!("Mnemonic: {}", mnemonic.phrase());
}Step 2: Mnemonic to Seed
- Use PBKDF2 with HMAC-SHA512 to hash the mnemonic (2048 iterations).
- Output: 512-bit seed (64 bytes).
fn step2_mnemonic_to_seed(mnemonic: &Mnemonic) -> String {
let seed = Seed::new(mnemonic, "");
hex::encode(seed.as_bytes()) // Returns 64-byte hex string
}Step 3: Seed to Master Key
- Apply HMAC-SHA512 to the seed with key
"Bitcoin seed". - Split output into master private key (32 bytes) and chain code (32 bytes).
fn step3_seed_to_master_key(seed_hex: &String) -> (String, String) {
let key = hmac::Key::new(hmac::HMAC_SHA512, b"Bitcoin seed");
let tag = hmac::sign(&key, &hex::decode(seed_hex).unwrap());
let (il, ir) = tag.as_ref().split_at(32);
(hex::encode(il), hex::encode(ir)) // (master_key, chain_code)
}Step 4: Derive Private Key via BIP32 Path
- Use BIP32 hierarchical paths (e.g.,
m/44’/60’/0’/0/0for ETH). - Hardened derivation (apostrophes) enhances security.
fn derive_with_path(master_key: SecretKey, chain_code: [u8; 32], path: &[u32; 5]) -> SecretKey {
let mut private_key = master_key;
for &i in path {
(private_key, chain_code) = derive_child_key(i, private_key, chain_code);
}
private_key
}Step 5: Private Key → Public Key
- Multiply private key by the elliptic curve generator point (secp256k1).
fn step5_private_key_to_public_key(private_key_hex: String) -> String {
let private_key = SecretKey::from_slice(&hex::decode(private_key_hex).unwrap()).unwrap();
hex::encode(serialize_curve_point(curve_point_from_int(private_key)))
}Step 6: Public Key → ETH Address
- Keccak-256 hash of the public key’s last 64 bytes.
- Take the final 20 bytes as the address.
fn step6_public_key_to_address(pub_key_hex: String) -> String {
let public_key_bytes = &PublicKey::from_slice(&hex::decode(pub_key_hex).unwrap()).unwrap().serialize_uncompressed()[1..];
let mut output = [0u8; 32];
Keccak::v256().update(public_key_bytes).finalize(&mut output);
hex::encode(&output[12..]) // Last 20 bytes
}FAQs
Q1: Can I create a custom mnemonic from a dictionary?
No. Mnemonics require valid entropy + checksum per BIP39 standards.
Q2: Why is my mnemonic phrase sensitive?
It generates all derived private keys. Exposure risks asset theft across linked wallets.
Q3: Can an ETH private key generate a BTC address?
No. Different coins use unique BIP32 paths (e.g., Bitcoin: m/44’/0’/0’, ETH: m/44’/60’/0’/0).
Q4: How do 12 words become an address?
- Entropy → Mnemonic (BIP39).
- Mnemonic + PBKDF2 → Seed.
- Seed + HMAC-SHA512 → Master Key.
- Master Key + BIP32 path → Private Key.
- Private Key → Public Key → ETH Address (Keccak-256).
Recommended Tools
References
- Ethereum 201: Mnemonics – Medium Article
- BIP32/BIP44 Specifications – GitHub
- Converting Mnemonics to Keys – MDRZA Guide