Bitcoin

WalletD features the ability to create and work with Bitcoin wallets. This resource provides documentation related to Bitcoin in WalletD.

BitcoinWallet Struct

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.

BitcoinWallet Struct Methods

  • add(&mut self, associated: &AssociatedAddress)

    • Adds an associated Bitcoin address to the wallet if it is not already associated to it while also keeping track of the associated Bitcoin address's derived HD key.
  • associated_info(&self) -> &[AssociatedAddress]

    • Returns the associated info: info on the associated Bitcoin addresses paired with their derived HD key.
  • addresses(&self) -> Vec<BitcoinAddress>

    • Returns a vector of the BitcoinAddress objects associated with the wallet.
  • coin_type_id(&self) -> Result<u32, Error>

    • Returns the coin type id num based on the Bitcoin network. Returns an error if the network is not supported.
  • default_hd_purpose(&self) -> Result<HDPurpose, Error>

    • Returns the default HDPurpose based on the address format Returns an error if the address format is not currently supported
  • address_format(&self) -> AddressType

    • Discovers previously used addresses by searching in sequential order based on master HDKey and a derivation type, stopping discovery when gap limit (n consecutive addresses without transaction history) has been met. Only considers change index = 0 (the receiving/external chain) when considering the gap limit but if there is transaction history with change index = 1 it is added as an associated address. If the account discovery setting is false, it will only search for addresses in the first account (account_index = 0). Returns the address format
  • master_hd_key(&self) -> Result<HDKey, Error>

    • Returns the master HDKey, if it exists otherwise returns an error
  • network(&self) -> Result<Network, Error>

    • Returns the network based on the master HDKey
  • next_address(&self) -> Result<BitcoinAddress, Error>

    • Returns a BitcoinAddress object on the the next available address on the first account (account_index = 0).

    Returns an error with details if it encounters a problem while deriving the next address

  • next_change_address(&self) -> Result<BitcoinAddress, Error>

    • Considering only account 0, returns the next change address corresponding to 1 + the max existing change address index.

    Change addresses are used for sending change back to the wallet and have a value of 1 (internal chain) instead of 0 (external chain) in the derivation path for the change index.

  • set_gap_limit(&mut self, gap_limit: usize)

    • Set the gap limit to use when searching for addresses, if not set, the default gap limit of 20 is used.
  • set_account_discovery(&mut self, account_discovery: bool)

    • Set the account discovery flag, if set to true, the wallet will search for addresses on all accounts, if set to false, the wallet will only search for addresses on the first account.

    If not set, the default value is true to enable account discovery.

  • set_hd_path_builder(&mut self, hd_path_builder: HDPathBuilder)

    • Set the HDPathBuilder to use when deriving addresses, if not set, the default HDPathBuilder is used.
  • gap_limit(&self) -> usize

    • Returns the gap limit that is being used when searching for addresses with this wallet.
  • account_discovery(&self) -> bool

    • Returns the account discovery flag that is being used when searching for addresses with this wallet.
  • hd_path_builder(&self) -> Result<HDPathBuilder, Error>

    • Returns the HDPathBuilder that is being used when deriving addresses with this wallet If no HDPathBuilder has been set, the default HDPathBuilder that is being used is returned
  • signature_sighashall_for_transaction_hash() -> Result<String, Error>

    • This function is used to calculate the signature as a hex encoded string with the option sighashall for a given transaction hash using a provided private key.
  • sign_tx() -> Result<BTransaction, Error>

    • Signs a transaction with the provided private keys and returns the signed transaction.
  • choose_inputs_and_set_fee() -> Result<(Vec<Input>, BitcoinAmount, Vec<usize>), Error>

    • Goal is to find a combination of the fewest inputs that is bigger than what we need - close to twice the send amount while not producing a change amount that is smaller than what the fee would be to spend that amount.
  • estimate_fee_with_default_sizes() -> Result<u64, Error>

    • Estimates the fee for a transaction with the given number of inputs and outputs given the fee per byte, makes use of default sizes to estimate the size of the transaction and the corresponding fee.
  • prepare_transaction() -> Result<(BTransaction, Vec<usize>), Error>

    • Prepares a transaction to be signed and broadcasted.

BitcoinWallet's Implementation of the CryptoWallet Trait

  • set_blockchain_client(&mut self, client: Self::BlockchainClient)

    • Associates a particular blockchain client with the CryptoWallet
  • blockchain_client(&self) -> Result<&Self::BlockchainClient, Self::ErrorType>

    • Returns the blockchain client that is associated with the CryptoWallet if it exists, otherwise returns an error
  • balance(&self) -> Result<Self::CryptoAmount, Self::ErrorType>

    • Returns the balance of the CryptoWallet as a CryptoAmount
  • transfer(&self, send_amount: &Self::CryptoAmount, public_address: &str) -> Result<String, Self::ErrorType>

    • Sends a transaction from the CryptoWallet to a given public address with a given amount
  • sync(&mut self) -> Result<(), Self::ErrorType>

    • Syncs the CryptoWallet with the blockchain
  • receive_address(&self) -> Result<String, Self::ErrorType>

    • Returns the receive address of the CryptoWallet, this is the address that is used to receive transactions
  • builder() -> Self::WalletBuilder

    • Returns a builder for the CryptoWallet that can be used to build a CryptoWallet with custom options

BitcoinWalletBuilder Struct

Builder for BitcoinWallet that allows for the creation of a BitcoinWallet with a custom configuration

BitcoinWalletBuilder Struct Methods

  • gap_limit(&mut self, gap_limit: usize) -> &mut Self

    • Allows specification of the gap limit to use for the wallet
  • account_discovery(&mut self) -> &mut Self

    • Enable account discovery, will search consecutive accounts until the gap limit is reached and an account is found with no transactions
  • no_account_discovery(&mut self) -> &mut Self

    • Specifies that account discovery should not be disabled, will not search past the first account
  • default_hd_purpose(&self) -> Result<HDPurpose, Error>

    • Returns the default HDPurpose based on the address format
  • coin_type_id(&self) -> Result<u32, Error>

    • Returns the coin type id num based on the network

BitcoinWalletBuilder's Implementation of the CryptoWalletBuilder Trait

  • new() -> Self

    • Constructs a new CryptoWalletBuilder
  • build(&self) -> Result<T, T::ErrorType>

    • Builds a CryptoWallet from the CryptoWalletBuilder
  • master_hd_key(&mut self, master_hd_key: HDKey) -> &mut Self

    • Specifies the master HD key (HDKey) for the CryptoWalletBuilder.
  • mnemonic_seed(&mut self, mnemonic_seed: Seed) -> &mut Self

    • Specifies the mnemonic seed (Seed) for the CryptoWalletBuilder.
  • address_format(&mut self, address_format: T::AddressFormat) -> &mut Self

    • Specifies the address format (CryptoWallet::AddressFormat) for the CryptoWalletBuilder.
  • hd_path_builder(&mut self, hd_path_builder: HDPathBuilder) -> &mut Self

    • Specifies the HD path builder (HDPathBuilder) for the CryptoWalletBuilder.
  • network_type(&mut self, network_type: T::NetworkType) -> &mut Self

    • Specifies the network type CryptoWallet::NetworkType for the CryptoWalletBuilder.

BitcoinAmount 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).

BitcoinAmount Struct Methods

  • from_btc(btc_amount: f64) -> Self

    • The number of satoshis (the smallest unit of the bitcoin currency) in the amount.

    There are 100 million satoshis per bitcoin (BTC). Returns a BitcoinAmount struct from a decimal value representing the amount in BTC.

  • from_satoshi(satoshi_amount: u64) -> Self

    • Returns a BitcoinAmount struct from an integer value representing the amount in satoshis.
  • btc(&self) -> f64

    • Returns the amount in BTC as a f64.
  • satoshi(&self) -> u64

    • Returns the amount in satoshis as a u64.