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

source

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.

source

pub fn associated_info(&self) -> &[AssociatedAddress]

Returns the associated info: info on the associated Bitcoin addresses paired with their derived HD key.

source

pub fn addresses(&self) -> Vec<BitcoinAddress>

Returns a vector of the BitcoinAddress objects associated with the wallet.

source

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.

source

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

source

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

source

pub fn address_format(&self) -> AddressType

Returns the address format

source

pub fn master_hd_key(&self) -> Result<HDKey, Error>

Returns the master HDKey, if it exists otherwise returns an error

source

pub fn network(&self) -> Result<Network, Error>

Returns the network based on the master HDKey

source

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

source

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

source

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.

source

pub fn set_master_hd_key(&mut self, master_hd_key: HDKey)

Sets the master hd key associated with the wallet.

source

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.

source

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.

source

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.

source

pub fn gap_limit(&self) -> usize

Returns the gap limit that is being used when searching for addresses with this wallet.

source

pub fn account_discovery(&self) -> bool

Returns the account discovery flag that is being used when searching for addresses with this wallet.

source

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

source

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.

source

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.

source

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.

source

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.

source

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

source§

fn clone(&self) -> BitcoinWallet

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl CryptoWallet for BitcoinWallet

§

type ErrorType = Error

ErrorType is the type of error that is returned by the CryptoWallet
§

type BlockchainClient = Blockstream

BlockchainClient is the type of BlockchainConnector that is used by the CryptoWallet to connect to the blockchain
§

type CryptoAmount = BitcoinAmount

CryptoAmount is the type of amount that is used by the CryptoWallet to represent amounts of cryptocurrency
§

type NetworkType = Network

NetworkType is the type of network that the CryptoWallet is connected to
§

type WalletBuilder = BitcoinWalletBuilder

WalletBuilder is the type of builder that is used to build a CryptoWallet
§

type AddressFormat = AddressType

AddressFormat is the type of address format that is used by the CryptoWallet
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,

Returns the balance of the CryptoWallet as a CryptoAmount
source§

fn builder() -> Self::WalletBuilder

Returns a builder for the CryptoWallet that can be used to build a CryptoWallet with custom options
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,

Sends a transaction from the CryptoWallet to a given public address with a given amount
source§

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

Associates a particular blockchain client with the CryptoWallet
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,

Syncs the CryptoWallet with the blockchain
source§

fn receive_address(&self) -> Result<String, Error>

Returns the receive address of the CryptoWallet, this is the address that is used to receive transactions
source§

fn blockchain_client(&self) -> Result<&Blockstream, Error>

Returns the blockchain client that is associated with the CryptoWallet if it exists, otherwise returns an error
source§

impl CryptoWalletBuilder<BitcoinWallet> for BitcoinWalletBuilder

source§

fn new() -> Self

Generates a new BitcoinWalletBuilder with the default options

source§

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

Allows specification of the mnemonic seed for the wallet

source§

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

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

Allows specifiction of the hd path builder, will override the default

source§

fn build(&self) -> Result<BitcoinWallet, Error>

Used to import an existing wallet from a master HD key or a mnemonic seed and specified network type

source§

impl Debug for BitcoinWallet

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for BitcoinWallet

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more