Expand description
WalletD
A cryptocurrency wallet library that encapsulates Rust-based functionality for various different cryptocurrencies and blockchains. Contains features to handle creating and importing wallets, checking balances, and sending and receiving transactions. Provides a common interface for interacting with different cryptocurrencies and blockchains. Built to facilitate and simplify development and implementation of multi-cryptocurrency non-custodial wallets.
Quickstart Guide
A good way to access many of the different features of this walletD library is through the use of the KeyPair struct which can enable a user to create a HD (Hierarchical Deterministic) wallet from a mnemonic phrase that could be used with multiple cryptocurrencies.
The KeyPairBuilder struct which can be accessed with default settings through KeyPair::builder()
is a versatile way to specify options for and build a KeyPair struct.
You can use the KeyPairBuilder to specify the mnemonic phrase, mnemonic seed, passphrase, network type, and the mnemonic key pair type.
The default specifications for the KeyPairBuilder are: no mnemonic phrase, no mnemonic seed, no passphrase, mainnet network type, and BIP39 mnemonic key pair type.
To use the testnet network, specify the network type as HDNetworkType::TestNet using the network_type method
on a KeyPairBuilder object.
You need to either specify a mnemonic seed or a mnemonic phrase to build a KeyPair struct. If a mnemonic phrase is specified, the mnemonic seed is derived from the mnemonic phrase and the optional passphrase.
If the mnemonic seed is specified, any specifications for the mnemonic phrase and passphrase are ignored when deriving a HD wallet key, but any specifications given for these attributes are stored on the KeyPair struct.
Warning: The information in the KeyPair struct should be treated as sensitive information and should be stored and handled securely, especially if it is being used to store real funds.
The KeyPair struct contains the mnemonic phrase, mnemonic seed, and passphrase if specified, as well as the network type and the mnemonic key pair type.
Create HD KeyPair from Bip39 Mnemonic
Here’s how you can create a KeyPair from a Bip39Mnemonic using the KeyPairBuilder.
use walletd::prelude::*;
use walletd_bip39::prelude::*;
let mnemonic_phrase = "outer ride neither foil glue number place usage ball shed dry point";
let bip39_mnemonic = Bip39Mnemonic::builder().mnemonic_phrase(mnemonic_phrase).build()?;
let seed = bip39_mnemonic.to_seed();
println!("seed_hex: {:x}", seed);
let master_hd_key = HDKey::new_master(seed, HDNetworkType::TestNet)?;
let keypair = KeyPair::builder().mnemonic_phrase(mnemonic_phrase.into()).network_type(HDNetworkType::TestNet).build()?;
assert_eq!(keypair.to_master_key(), master_hd_key);
Derive Wallets
The KeyPair::derive_wallet
method can be used to derive a cryptowallet for a specific cryptocurrency from a KeyPair.
You can specify a concrete struct that implements the CryptoWallet trait such as BitcoinWallet or EthereumWallet to derive a cryptowallet from the keypair
of the specified concrete type.
use walletd_bitcoin::prelude::*;
use walletd_ethereum::prelude::*;
let mut btc_wallet = keypair.derive_wallet::<BitcoinWallet>()?;
let mut eth_wallet = keypair.derive_wallet::<EthereumWallet>()?;
Specify Blockchain Connectors
A valid blockchain client is a concrete instance of a struct that implements the BlockchainConnector trait.
You can setup a Blockstream blockchain client to access the Bitcoin blockchain and an EthClient blockchain client to access the Ethereum blockchain.
Specifying a valid endpoint url is required for the Blockstream and EthClient blockchain clients.
To associate an existing instance of a cryptowallet with a blockchain client, use the set_blockchain_client
method on the cryptowallet object.
btc_wallet.set_blockchain_client(Blockstream::new("https://blockstream.info/testnet/api")?);
eth_wallet.set_blockchain_client(EthClient::new("https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161")?);
Use the CryptoWallets
Once you have a cryptowallet object associated with a [blockchain client] you can use the cryptowallet to access blockchain data.
Any object that implements the CryptoWallet trait must implement functions within the trait which include balance
, and transfer
.
btc_wallet.sync().await?;
println!("btc_wallet balance: {} BTC", btc_wallet.balance().await?.btc());
let mut eth_wallet = keypair.derive_wallet::<EthereumWallet>()?;
print!("eth_wallet public address: {}", eth_wallet.public_address());
let eth_client = EthClient::new("https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161")?;
eth_wallet.set_blockchain_client(eth_client);
println!("eth_wallet balance: {} ETH", eth_wallet.balance().await?.eth());
Re-exports
pub use walletd_bip39;
pub use walletd_bitcoin;
pub use walletd_coin_core;
pub use walletd_ethereum;
pub use walletd_hd_key;
pub use walletd_mnemonics_core;
Modules
- This prelude module simplifies importing many useful items from the walletd crate using a glob import.
Structs
- Represents a mnemonic which follows the
BIP39 standard
(https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki). - Implements the builder pattern for creating a Bip39Mnemonic struct.
- Contains a field representing the amount of satoshis in the amount. Has functions to convert to and from the main unit (BTC) and the smallest unit (satoshi).
- Represents a Hierarchical Deterministic (HD) Bitcoin wallet which can have multiple BitcoinAddress structs associated with it which are derived from a single master HD key.
- A builder that can be used to build a BlockchainConnector with custom options.
- A blockchain connector for Bitcoin which follows
the Blockstream API
. - A blockchain connector for Ethereum which contains a
web3 instance
using a HTTP transport. - 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).
- Contains the information needed to interact with an Ethereum wallet with a single public address associated with it.
- Represents a master or a derived child HD (Hierarchical Deterministic) key.
- Contains a vector of HDPathIndex to represent a derivation path for a HDKey and relevant helper functions.
- 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.
- Holds info about a mnemonic type and the associated seed and phrase as well as the network type. Enables the creation of a HD wallet from a mnemonic phrase that could be used with multiple cryptocurrencies.
- Stores the secret value which can be used to derive a hierarchical deterministic wallet. Often associated with a mnemonic phrase.
Enums
- The language of a Bip39 mnemonic phrase. English is the default language.
- Represents the different number of words that can be used for a valid Bip39Mnemonic.
- Represents the type of blockchain connector that is being used.
- Represents the different crypto coins that are currently supported by walletD
- Custom error type for the walletD crate.
- Represents the different network types relevant to HDKey.
- Represents the variants of different derivation path components.
- Represents the different derivation path schemes currently supported by the walletd_hd_key library.
- Used to specify the type of mnemonic phrase associated with a KeyPair struct.
Traits
- Used to connect to a blockchain and send and receive information to and from the blockchain.
- The public address of a cryptocurrency.
- 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.
- Provides common functionality for a crypto wallet. Contains functions to get the balance, send and receive transactions, and sync the wallet with the blockchain.
- Provides a common interface for building a CryptoWallet.
- The Language trait is used to provide a common interface for the different Language implementations in different walletD mnemonic libraries.
- Provide a common interface for different mnemonic protocols.
- Provides a builder pattern for creating a Mnemonic.