crypto_wallet_gen/wallets/
ethereum.rs

1use anyhow::Result;
2use failure::Fail;
3use secp256k1_17::key::SecretKey;
4use wagyu_ethereum::format::EthereumFormat;
5use wagyu_ethereum::private_key::EthereumPrivateKey;
6use wagyu_model::PrivateKey;
7
8use super::Wallet;
9use crate::bip32::HDPrivKey;
10
11pub struct EthereumWallet {
12    private_key: EthereumPrivateKey,
13}
14
15impl EthereumWallet {
16    pub fn private_key(&self) -> String {
17        self.private_key.to_string()
18    }
19
20    pub fn public_key(&self) -> String {
21        self.private_key.to_public_key().to_string()
22    }
23
24    pub fn address(&self) -> Result<String> {
25        Ok(self
26            .private_key
27            .to_address(&EthereumFormat::Standard)
28            .map_err(|err| err.compat())?
29            .to_string())
30    }
31}
32
33impl Wallet for EthereumWallet {
34    fn from_hd_key(private_key: HDPrivKey) -> Result<Self> {
35        let secp_key = SecretKey::from_slice(private_key.key_part().to_bytes())?;
36        Ok(Self {
37            private_key: EthereumPrivateKey::from_secp256k1_secret_key(secp_key),
38        })
39    }
40}