wallet-wizard 0.2.0

Embark on a cryptographic journey with wallet-wizard, a Rust library that opens portals to the blockchain realm. This mystical tool harnesses the ancient art of BIP-39 mnemonics to generate secure wallets. Whether you're a seasoned sorcerer of the blockchain world or a novice in the cryptographic universe, wallet-wizard offers a seamless and secure way to create wallets. Perfect for applications needing robust wallet functionality, it's your go-to spellbook for generating, managing, and utilizing wallets in your Rust applications.
Documentation
use bip39::Mnemonic;
use hdkey::HDKey;
use secp256k1::Secp256k1;
use secp256k1::SecretKey;
use std::str::FromStr;
use tiny_keccak::{Hasher, Keccak};

pub fn generate_ethereum_wallet(
    mnemonic: &str,
    index: u32,
) -> Result<(String, String), Box<dyn std::error::Error>> {
    let mnemonic = Mnemonic::from_str(mnemonic)?;

    let seed = mnemonic.to_seed("");
    let hd_wallet = HDKey::from_master_seed(&seed, None)?;

    let derivation_path = format!("m/44'/60'/0'/0/{}", index);

    let derived_key = hd_wallet.derive(&derivation_path)?;

    let private_key = derived_key.private_key().unwrap();

    let secp = Secp256k1::new();

    let secret_key = SecretKey::from_slice(&private_key)?;
    let public_key = secp256k1::PublicKey::from_secret_key(&secp, &secret_key);
    let serialized_public_key = public_key.serialize_uncompressed();

    let mut hasher = Keccak::v256();
    hasher.update(&serialized_public_key[1..]); // Skip the first byte (0x04)
    let mut hash = [0u8; 32];
    hasher.finalize(&mut hash);

    let address = &hash[12..];
    let address_hex = format!("0x{}", hex::encode(address));

    let private_key_hex = hex::encode(private_key);

    Ok((address_hex, private_key_hex))
}