Ethereum Wallet
WalletD features the ability to create and work with Ethereum wallets. In this section, we'll guide you on how to use WalletD to handle Ethereum wallets.
Imports needed for this section
use walletd::walletd_ethereum::prelude::*;
use walletd::walletd_hd_key::prelude::*;
Import from Seed
Here's how you can import an Ethereum wallet based on a master HDKey
. We will use the mut
keyword to make the ethereum_wallet
.
By importing the ethereum_wallet
fom a master_hd_key
, both the EthereumPrivateKey
and the EthereumPublicKey
will be provided to the ethereum_wallet
.
Example
let master_seed = Seed::from_str("a2fd9c0522d84d52ee4c8533dc02d4b69b4df9b6255e1af20c9f1d4d691689f2a38637eb1ec778972bf845c32d5ae83c7536999b5666397ac32021b21e0accee")?;
let master_hd_key = HDKey::new_master(master_seed, HDNetworkType::TestNet)?;
let mut ethereum_wallet = EthereumWallet::builder().master_hd_key(master_hd_key).build()?;
let public_address = ethereum_wallet.public_address();
println!("ethereum wallet public address: {}", public_address);
Default Derivation Path
Deriving an EthereumWallet
is simple and uses some default settings under the hood.
We can inspect our ethereum_wallet
to see that a child derived_hd_key
was derived from the master master_hd_key
and see the derivation path HDPath
that was used by default.
Example
let derived_hd_key = ethereum_wallet.derived_hd_key()?;
let address_derivation_path = derived_hd_key.derivation_path;
println!("address derivation path: {}", address_derivation_path);
We see that by default the Ethereum wallet uses the derivation path "m/44'/60'/0'/0/" corresponding to BIP44 for the purpose value (HDPurpose::BIP44
).
Using EthClient
The EthClient
struct can be used to access blockchain data through a struct that implements the BlockchainConnector
trait such as EthClient
.
The EthClient
instance can be used on its own to access blockchain data such as the current block number and the gas price.
It can be affiliated with a EthereumWallet
to enable the EthereumWallet
to access blockchain data and send transactions.
EthClient
currently supports any Ethereum endpoint that conforms to the Ethereum JSON-RPC standard for accessing Ethereum blockchain data.
We recommend using Infura(https://www.infura.io/), or Alchemy(https://www.alchemy.com/pricing). Both services provide generous free plans that you can use.
Note that the url used to connect needs to match the network type being used (pay attention to the difference between testnet networks (used for testing and development purposes) and the mainnet network (where coins have actual value)).
Here's an example of creating an instance of EthClient
.
Example
let ethclient_url = "https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161";
let eth_client = EthClient::new(ethclient_url)?;
The blockchain client ethclient
can be used separately from the ethereum_wallet
to access blockchain data such as details of a transaction given a tx hash, the current block number, or the current gas price.
Example
let tx_hash = "0xe4216d69bf935587b82243e68189de7ade0aa5b6f70dd0de8636b8d643431c0b";
let tx = eth_client.transaction_data_from_hash(tx_hash).await?;
let block_number = eth_client.current_block_number().await;
let gas_price = eth_client.gas_price().await;
println!("transaction data: {:?}", tx);
We can associate the ethclient
with the ethereum_wallet
using the set_blockchain_client
method.
ethereum_wallet.set_blockchain_client(eth_client);
Check Balance
When the ethereum_wallet
is connected to the blockchain, we can find the balance of the wallet as well as access other functions such as transfer
required for the CryptoWallet
trait.
Example
let balance = ethereum_wallet.balance().await?;
println!("ethereum wallet balance: {} ETH, ({} wei)", balance.eth(), balance.wei());