Struct walletd_bitcoin::BitcoinWallet
source · pub struct BitcoinWallet { /* private fields */ }
Expand description
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.
Implementations§
source§impl BitcoinWallet
impl BitcoinWallet
sourcepub fn add(&mut self, associated: &AssociatedAddress)
pub fn 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.
sourcepub fn associated_info(&self) -> &[AssociatedAddress]
pub fn associated_info(&self) -> &[AssociatedAddress]
Returns the associated info: info on the associated Bitcoin addresses paired with their derived HD key.
sourcepub fn addresses(&self) -> Vec<BitcoinAddress>
pub fn addresses(&self) -> Vec<BitcoinAddress>
Returns a vector of the BitcoinAddress objects associated with the wallet.
sourcepub fn coin_type_id(&self) -> Result<u32, Error>
pub fn 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.
sourcepub fn default_hd_purpose(&self) -> Result<HDPurpose, Error>
pub fn 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
If the address format is AddressType::P2pkh the default purpose is HDPurpose::BIP44 If the address format is AddressType::P2sh the default purpose is HDPurpose::BIP49 If the address format is AddressType::P2wpkh the default purpose is HDPurpose::BIP84 Other address formats are currently not supported and will return an error
sourcepub async fn add_previously_used_addresses(&mut self) -> Result<(), Error>
pub async fn add_previously_used_addresses(&mut self) -> Result<(), Error>
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).
sourcepub fn address_format(&self) -> AddressType
pub fn address_format(&self) -> AddressType
Returns the address format
sourcepub fn master_hd_key(&self) -> Result<HDKey, Error>
pub fn master_hd_key(&self) -> Result<HDKey, Error>
Returns the master HDKey, if it exists otherwise returns an error
sourcepub fn add_address_index(&mut self, address_index: u32) -> Result<(), Error>
pub fn add_address_index(&mut self, address_index: u32) -> Result<(), Error>
Adds an address on the first account (account_index = 0) with the specified address index to the BitcoinWallet If the particular address is already associated with the wallet, it will be added again
If not enough info is known to add the address, an error is returned
sourcepub fn next_address(&self) -> Result<BitcoinAddress, Error>
pub fn 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
sourcepub fn next_change_address(&self) -> Result<BitcoinAddress, Error>
pub fn 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.
sourcepub fn set_master_hd_key(&mut self, master_hd_key: HDKey)
pub fn set_master_hd_key(&mut self, master_hd_key: HDKey)
Sets the master hd key associated with the wallet.
sourcepub fn set_gap_limit(&mut self, gap_limit: usize)
pub fn 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.
sourcepub fn set_account_discovery(&mut self, account_discovery: bool)
pub fn 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.
sourcepub fn set_hd_path_builder(&mut self, hd_path_builder: HDPathBuilder)
pub fn 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.
sourcepub fn gap_limit(&self) -> usize
pub fn gap_limit(&self) -> usize
Returns the gap limit that is being used when searching for addresses with this wallet.
sourcepub fn account_discovery(&self) -> bool
pub fn account_discovery(&self) -> bool
Returns the account discovery flag that is being used when searching for addresses with this wallet.
sourcepub fn hd_path_builder(&self) -> Result<HDPathBuilder, Error>
pub fn 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
sourcepub fn signature_sighashall_for_transaction_hash(
transaction_hash: &str,
private_key: &BitcoinPrivateKey
) -> Result<String, Error>
pub fn signature_sighashall_for_transaction_hash( transaction_hash: &str, private_key: &BitcoinPrivateKey ) -> 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.
sourcepub fn sign_tx(
tx: &BTransaction,
keys_per_input: Vec<(BitcoinPrivateKey, BitcoinPublicKey)>
) -> Result<BTransaction, Error>
pub fn sign_tx( tx: &BTransaction, keys_per_input: Vec<(BitcoinPrivateKey, BitcoinPublicKey)> ) -> Result<BTransaction, Error>
Signs a transaction with the provided private keys and returns the signed transaction.
sourcepub fn choose_inputs_and_set_fee(
utxo_available: &Vec<Utxo>,
send_amount: &BitcoinAmount,
inputs_available_tx_info: &[BTransaction],
byte_fee: f64
) -> Result<(Vec<Input>, BitcoinAmount, Vec<usize>), Error>
pub fn choose_inputs_and_set_fee( utxo_available: &Vec<Utxo>, send_amount: &BitcoinAmount, inputs_available_tx_info: &[BTransaction], byte_fee: f64 ) -> 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.
sourcepub fn estimate_fee_with_default_sizes(
is_segwit: bool,
num_inputs: usize,
num_outputs: usize,
byte_fee: f64
) -> Result<u64, Error>
pub fn estimate_fee_with_default_sizes( is_segwit: bool, num_inputs: usize, num_outputs: usize, byte_fee: f64 ) -> 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.
sourcepub fn prepare_transaction(
fee_sat_per_byte: f64,
utxo_available: &Vec<Utxo>,
inputs_available_tx_info: &[BTransaction],
send_amount: &BitcoinAmount,
receiver_view_wallet: &BitcoinAddress,
change_addr: Address
) -> Result<(BTransaction, Vec<usize>), Error>
pub fn prepare_transaction( fee_sat_per_byte: f64, utxo_available: &Vec<Utxo>, inputs_available_tx_info: &[BTransaction], send_amount: &BitcoinAmount, receiver_view_wallet: &BitcoinAddress, change_addr: Address ) -> Result<(BTransaction, Vec<usize>), Error>
Prepares a transaction to be signed and broadcasted.
Trait Implementations§
source§impl Clone for BitcoinWallet
impl Clone for BitcoinWallet
source§fn clone(&self) -> BitcoinWallet
fn clone(&self) -> BitcoinWallet
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl CryptoWallet for BitcoinWallet
impl CryptoWallet for BitcoinWallet
§type BlockchainClient = Blockstream
type BlockchainClient = Blockstream
§type CryptoAmount = BitcoinAmount
type CryptoAmount = BitcoinAmount
§type NetworkType = Network
type NetworkType = Network
§type WalletBuilder = BitcoinWalletBuilder
type WalletBuilder = BitcoinWalletBuilder
§type AddressFormat = AddressType
type AddressFormat = AddressType
source§fn balance<'life0, 'async_trait>(
&'life0 self
) -> Pin<Box<dyn Future<Output = Result<BitcoinAmount, Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn balance<'life0, 'async_trait>( &'life0 self ) -> Pin<Box<dyn Future<Output = Result<BitcoinAmount, Error>> + Send + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait,
source§fn builder() -> Self::WalletBuilder
fn builder() -> Self::WalletBuilder
source§fn transfer<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
send_amount: &'life1 BitcoinAmount,
to_public_address: &'life2 str
) -> Pin<Box<dyn Future<Output = Result<String, Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn transfer<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, send_amount: &'life1 BitcoinAmount, to_public_address: &'life2 str ) -> Pin<Box<dyn Future<Output = Result<String, Error>> + Send + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,
source§fn set_blockchain_client(&mut self, client: Self::BlockchainClient)
fn set_blockchain_client(&mut self, client: Self::BlockchainClient)
source§fn sync<'life0, 'async_trait>(
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn sync<'life0, 'async_trait>( &'life0 mut self ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait,
source§fn receive_address(&self) -> Result<String, Error>
fn receive_address(&self) -> Result<String, Error>
source§fn blockchain_client(&self) -> Result<&Blockstream, Error>
fn blockchain_client(&self) -> Result<&Blockstream, Error>
source§impl CryptoWalletBuilder<BitcoinWallet> for BitcoinWalletBuilder
impl CryptoWalletBuilder<BitcoinWallet> for BitcoinWalletBuilder
source§fn master_hd_key(&mut self, master_hd_key: HDKey) -> &mut Self
fn master_hd_key(&mut self, master_hd_key: HDKey) -> &mut Self
Allows specification of the master HD key for the wallet
source§fn mnemonic_seed(&mut self, mnemonic_seed: Seed) -> &mut Self
fn mnemonic_seed(&mut self, mnemonic_seed: Seed) -> &mut Self
Allows specification of the mnemonic seed for the wallet
source§fn address_format(
&mut self,
address_format: <BitcoinWallet as CryptoWallet>::AddressFormat
) -> &mut Self
fn address_format( &mut self, address_format: <BitcoinWallet as CryptoWallet>::AddressFormat ) -> &mut Self
Allows specification of the address format to use for the wallet
source§fn network_type(&mut self, network_type: Network) -> &mut Self
fn network_type(&mut self, network_type: Network) -> &mut Self
Allows specification of the network type for the wallet, the default is Network::Bitcoin
source§fn hd_path_builder(&mut self, hd_path_builder: HDPathBuilder) -> &mut Self
fn hd_path_builder(&mut self, hd_path_builder: HDPathBuilder) -> &mut Self
Allows specifiction of the hd path builder, will override the default