-2

I'm new to rust and currently exploring ethers.rs library. I want to print wallet's private key on the console for this first I'm building a mnemonic using.

let mnemonic = Mnemonic::<English>::new(&mut rand::thread_rng());

now using this mnemonic I want to extract the private key. I know a mnemonic can have multiple private keys but I want to extract the first one. For this I did something like this.

let key = mnemonic.derive_key("m/44'/60'/0'/0/0", None).unwrap()

this gives me output of type XPriv. I am unable to understand how can I work with this type as it does not contain any function to give me private key in string form so I can print it to the console. Any help would be appreciated.

here's my entire function

async fn test() {
    let mnemonic = Mnemonic::<English>::new(&mut rand::thread_rng());

    println!("mnemonic: {}", mnemonic.to_phrase());

    let key = mnemonic
        .derive_key("m/44'/60'/0'/0/0", None)
        .expect("Failed to derive pkey");
}

here's my cargo.toml

name = "rust-basics"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ethers="2.0.7"
tokio = { version = "1", features = ["full"] }
bip39 = "2.0"
rand = "0.8"
dialoguer = "0.10.4"
serde = "1.0.164"
serde_json = "1.0.97"
password-hash = "0.5.0"
bcrypt = "0.14.0"
  • 2
    Why? Private keys are supposed to be secrets. Printing them on a console is a seriously bad idea. – user207421 Jun 25 '23 at 10:12
  • The package is supposed to run locally on a users machine I want to encrypt the private key using a password and then save locally in a file. But for that first I need private key in string form. – Muhammad Waleed Jun 25 '23 at 10:15
  • Private keys are usually stored in PCKS#11 keystores, which already have passwords and are already secure. This technology already exists. Printing it to the console does not constitute encryption, and you don't need to convert anything to a string to encrypt it. – user207421 Jun 25 '23 at 10:19
  • I don't know a lot about cryptography, so probably listen to @user207421. But just for reference, there is an [Encoder trait](https://docs.rs/coins-bip32/latest/coins_bip32/enc/trait.XKeyEncoder.html#tymethod.write_xpriv). – isaactfa Jun 25 '23 at 10:24
  • @user207421 I'm trying to implement Metamask wallet-type functionality. Metamask is a browser wallet. I want to do the same thing as a cli wallet. For this, I need to save user's information either their mnemonic or their private keys. So they can come later and by selecting an account and providing the password for that account they can make transactions. – Muhammad Waleed Jun 25 '23 at 11:54
  • OK but you still don't need to convert anything to a string to encrypt it. – user207421 Jun 25 '23 at 12:02
  • @user207421 What is the other solution then? Can you guide me? – Muhammad Waleed Jun 25 '23 at 12:22

1 Answers1

1

I did not find the Mnemonic, but there is the MnemonicBuilder: https://github.com/gakonst/ethers-rs/blob/master/examples/wallets/examples/mnemonic.rs

After you get the wallet, you can get the private key using following code.

let private_key = wallet
    .signer()
    .to_bytes()
    .iter()
    .map(|&i|format!("{:X}", i))
    .collect::<Vec<String>>()
    .join("");

Hope it helps.

ligulfzhou
  • 26
  • 2