Crate walletd_bitcoin

Crate walletd_bitcoin 

Source
Expand description

§WalletD Bitcoin

Provides a wallet implementation for Bitcoin including the ability to create a new wallet or import an existing wallet, check balances, and handle transactions. It supports a hierarchical deterministic (HD) wallet structure and provides the ability to search for previously used addresses associated with the wallet as well as the creation of new addresses. It also facilitates obtaining blockchain information.

§Quickstart Guide

The BitcoinWallet struct is the main entry point to access walletD functionality for Bitcoin.

§Import from Seed

Here’s how you can access a bitcoin wallet based on a master Seed. The Seed can be derived from a Mnemonic using the Mnemonic::to_seed method.

use walletd_bitcoin::prelude::*;
use walletd_hd_key::prelude::*;
let master_seed = Seed::from_str("a2fd9c0522d84d52ee4c8533dc02d4b69b4df9b6255e1af20c9f1d4d691689f2a38637eb1ec778972bf845c32d5ae83c7536999b5666397ac32021b21e0accee")?;
let network_type = HDNetworkType::TestNet;
let master_hd_key = HDKey::new_master(master_seed, network_type)?;
let mut btc_wallet = BitcoinWallet::builder().master_hd_key(master_hd_key).build()?;

§Default Derivation Path

Deriving a BitcoinWallet is simple and uses some default settings under the hood. When using BitcoinWalletBuilder, the default settings set the AddressType as ’P2wpkh` and the corresponding default HDPurpose for the derivation path is set as HDPurpose::BIP49.

 use walletd_bitcoin::bitcoin;
   assert_eq!(btc_wallet.address_format(), bitcoin::AddressType::P2wpkh);
   assert_eq!(btc_wallet.hd_path_builder()?.purpose, Some(HDPurpose::BIP84.to_shortform_num()));

§Using Blockstream as Blockchain Connector

The BitcoinWallet struct can be used to access blockchain data through a BlockchainConnector such as Blockstream. The Blockstream instance can be used on its own to access blockchain data such as fee estimates. It can be affiliated with a BitcoinWallet to enable the BitcoinWallet to access blockchain data and send transactions.

let btc_client = Blockstream::new("https://blockstream.info/testnet/api")?;
let fee_estimates = btc_client.fee_estimates().await?;
println!("fee estimates: {:?}", fee_estimates);
btc_wallet.set_blockchain_client(btc_client);

§Sync BitcoinWallet, Load BitcoinAddresses

The BitcoinWallet struct can be used to sync the wallet with the blockchain and load the associated BitcoinAddresses.

let btc_client = Blockstream::new("https://blockstream.info/testnet/api")?;
let fee_estimates = btc_client.fee_estimates().await?;
println!("fee estimates: {:?}", fee_estimates);
btc_wallet.set_blockchain_client(btc_client);
for addr in btc_wallet.associated_info() {
    println!("address: {}, derivation path {}",
    addr.address.public_address(), addr.hd_key().derivation_path().to_string());
}
println!("next receive address: {}", btc_wallet.receive_address()?);
println!("next change address: {}", btc_wallet.next_change_address()?.public_address());



let balance = btc_wallet.balance().await?;
println!(
    "bitcoin wallet balance: {} BTC, ({} satoshi",
    balance.btc(),
    balance.satoshi()
);

Re-exports§

pub use bitcoin;

Modules§

prelude
This prelude module simplifies importing many useful items from the walletd_bitcoin crate using a glob import.

Structs§

AddressInfo
A Bitcoin address.
Bip39Mnemonic
Represents a mnemonic which follows the BIP39 standard(https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki).
BitcoinAddress
Represents a Bitcoin address. Contains the address details and network The private key and public key are optional fields.
BitcoinAmount
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).
BitcoinPrivateKey
A Bitcoin ECDSA private key
BitcoinPublicKey
A Bitcoin ECDSA public key
BitcoinWallet
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.
BitcoinWalletBuilder
Builder for BitcoinWallet that allows for the creation of a BitcoinWallet with a custom configuration
Blockstream
A blockchain connector for Bitcoin which follows the Blockstream API.
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.
HDPathBuilder
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.
Script
Bitcoin script slice.
Seed
Stores the secret value which can be used to derive a hierarchical deterministic wallet. Often associated with a mnemonic phrase.

Enums§

AddressType
The different types of addresses.
Bip39Language
The language of a Bip39 mnemonic phrase. English is the default language.
Bip39MnemonicType
Represents the different number of words that can be used for a valid Bip39Mnemonic.
Error
Custom error type for this crate.
HDNetworkType
Represents the different network types relevant to HDKey.
HDPathIndex
Represents the variants of different derivation path components.
HDPurpose
Represents the different derivation path schemes currently supported by the walletd_hd_key library.
Network
The cryptocurrency network to act on.

Traits§

BlockchainConnector
Used to connect to a blockchain and send and receive information to and from the blockchain.
CryptoAddress
The public address of a cryptocurrency.
CryptoAmount
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.
CryptoWallet
Provides common functionality for a crypto wallet. Contains functions to get the balance, send and receive transactions, and sync the wallet with the blockchain.
CryptoWalletBuilder
Provides a common interface for building a CryptoWallet.
Mnemonic
Provide a common interface for different mnemonic protocols.
MnemonicBuilder
Provides a builder pattern for creating a Mnemonic.