使用ether.rs将私钥转换为字符串。

huangapple go评论72阅读模式
英文:

Convert private key to string using ether.rs

问题

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

[package]
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"
英文:

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::&lt;English&gt;::new(&amp;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(&quot;m/44&#39;/60&#39;/0&#39;/0/0&quot;, 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::&lt;English&gt;::new(&amp;mut rand::thread_rng());

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

    let key = mnemonic
        .derive_key(&quot;m/44&#39;/60&#39;/0&#39;/0/0&quot;, None)
        .expect(&quot;Failed to derive pkey&quot;);
}

here's my cargo.toml

name = &quot;rust-basics&quot;
version = &quot;0.1.0&quot;
edition = &quot;2021&quot;

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

[dependencies]
ethers=&quot;2.0.7&quot;
tokio = { version = &quot;1&quot;, features = [&quot;full&quot;] }
bip39 = &quot;2.0&quot;
rand = &quot;0.8&quot;
dialoguer = &quot;0.10.4&quot;
serde = &quot;1.0.164&quot;
serde_json = &quot;1.0.97&quot;
password-hash = &quot;0.5.0&quot;
bcrypt = &quot;0.14.0&quot;

答案1

得分: 1

我没有找到Mnemonic,但有MnemonicBuilder:https://github.com/gakonst/ethers-rs/blob/master/examples/wallets/examples/mnemonic.rs

在获得wallet之后,您可以使用以下代码获取私钥。

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

希望对您有所帮助。

英文:

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(|&amp;i|format!(&quot;{:X}&quot;, i))
    .collect::&lt;Vec&lt;String&gt;&gt;()
    .join(&quot;&quot;);

Hope it helps.

huangapple
  • 本文由 发表于 2023年6月25日 18:09:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76549870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定