Expand description
§WalletD Ethereum Library
Provides a wallet implementation for Ethereum and blockchain-specific functionality.
§Quickstart Guide
Use the EthereumWallet struct as a good starting point to access the functionalities for Ethereum that walletD provides.
Each EthereumWallet is associated with one public address. An EthereumWallet could be instantiated without a EthereumPrivateKey affiliated with it, but it would not be able to sign transactions. If an EthereumWallet is created with an EthereumPublicKey but no EthereumPrivateKey, it will be able to verify signatures but not sign or send transactions.
§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 mutable so that we can modify ethereum_wallet
later.
By importing the ethereum_wallet
fom a master_hd_key
will both the EthereumPrivateKey and the EthereumPublicKey will be provided to the ethereum_wallet
.
use walletd_ethereum::prelude::*;
use walletd_hd_key::prelude::*;
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.
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 and 60’ corresponding to the coin type for Ethereum.
We need to add a blockchain connector to our ethereum wallet to be able to interact with the Ethereum blockchain.
§Adding a Blockchain Connector
Here’s an example of how to add an instance of EthClient to our ethereum_wallet
.
EthClient currently supports any Ethereum endpoint that conforms to the Ethereum JSON-RPC standard for accessing Ethereum blockchain data.
We recommend using Infura, or Alchemy. 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).
let ethclient_url = "https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161";
let eth_client = EthClient::new(ethclient_url)?;
ethereum_wallet.set_blockchain_client(eth_client);
§Using EthClient to Access Blockchain Data
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.
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);
§Balance of Ethereum Wallet on Blockchain
When the ethereum_wallet
is connected to the blockchain, we can find the balance of the wallet.
let balance = ethereum_wallet.balance().await?;
println!("ethereum wallet balance: {} ETH, ({} wei)", balance.eth(), balance.wei());
Re-exports§
pub use web3;
Modules§
- prelude
- This prelude module simplifies importing many useful items from the walletd_ethereum crate using a glob import.
Structs§
- Bip39
Mnemonic - Represents a mnemonic which follows the
BIP39 standard
(https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki). - EthClient
- A blockchain connector for Ethereum which contains a
web3 instance
using a HTTP transport. - Ethereum
Amount - Contains a field representing the amount of wei in the amount. Also has functions to convert to and from the main unit (ETH) and the smallest unit (wei).
- Ethereum
Private Key - Represents a private key for an Ethereum wallet, wraps a SecretKey from the secp256k1 crate
- Ethereum
Public Key - Represents an EthereumPublicKey, wraps a PublicKey from the secp256k1 crate
- Ethereum
Wallet - Contains the information needed to interact with an Ethereum wallet with a single public address associated with it.
- Ethereum
Wallet Builder - Builder for EthereumWallet, allows for specification of options for the ethereum wallet
- HDKey
- Represents a master or a derived child HD (Hierarchical Deterministic) key.
- HDPath
- Contains a vector of HDPathIndex to represent a derivation path for a HDKey and relevant helper functions.
- HDPath
Builder - A builder for the HDPath struct, it allows specification of the standard full path and also which components are hardened. The default implementation uses the standard format for the full path.
- Seed
- Stores the secret value which can be used to derive a hierarchical deterministic wallet. Often associated with a mnemonic phrase.
Enums§
- Bip39
Language - The language of a Bip39 mnemonic phrase. English is the default language.
- Bip39
Mnemonic Type - Represents the different number of words that can be used for a valid Bip39Mnemonic.
- Error
- Custom error type for this crate.
- Ethereum
Format - Represents the format of an Ethereum address (checksummed or non-checksummed)
- HDNetwork
Type - Represents the different network types relevant to HDKey.
- HDPath
Index - Represents the variants of different derivation path components.
- HDPurpose
- Represents the different derivation path schemes currently supported by the walletd_hd_key library.
Traits§
- Blockchain
Connector - Used to connect to a blockchain and send and receive information to and from the blockchain.
- Crypto
Address - The public address of a cryptocurrency.
- Crypto
Amount - Provides a common interface for handling amounts of a cryptocurrency. Has functions to convert to and from the main unit and the smallest unit of the coin.
- Crypto
Wallet - Provides common functionality for a crypto wallet. Contains functions to get the balance, send and receive transactions, and sync the wallet with the blockchain.
- Crypto
Wallet Builder - Provides a common interface for building a CryptoWallet.
- Mnemonic
- Provide a common interface for different mnemonic protocols.
- Mnemonic
Builder - Provides a builder pattern for creating a Mnemonic.