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..]); 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))
}