Struct bdk::wallet::Wallet[][src]

pub struct Wallet<B, D> { /* fields omitted */ }
Expand description

A Bitcoin wallet

A wallet takes descriptors, a database and a blockchain and implements the basic functions that a Bitcoin wallets needs to operate, like generating addresses, returning the balance, creating transactions, etc.

A wallet can be either “online” if the blockchain type provided implements Blockchain, or “offline” if it is the unit type (). Offline wallets only expose methods that don’t need any interaction with the blockchain to work.

Implementations

impl<D> Wallet<(), D> where
    D: BatchDatabase
[src]

pub fn new_offline<E: IntoWalletDescriptor>(
    descriptor: E,
    change_descriptor: Option<E>,
    network: Network,
    database: D
) -> Result<Self, Error>
[src]

Create a new “offline” wallet

impl<B, D> Wallet<B, D> where
    D: BatchDatabase
[src]

pub fn get_address(&self, address_index: AddressIndex) -> Result<Address, Error>[src]

Return a derived address using the external descriptor, see AddressIndex for available address index selection strategies. If none of the keys in the descriptor are derivable (ie. does not end with /*) then the same address will always be returned for any AddressIndex.

pub fn is_mine(&self, script: &Script) -> Result<bool, Error>[src]

Return whether or not a script is part of this wallet (either internal or external)

pub fn list_unspent(&self) -> Result<Vec<LocalUtxo>, Error>[src]

Return the list of unspent outputs of this wallet

Note that this methods only operate on the internal database, which first needs to be Wallet::sync manually.

pub fn get_utxo(&self, outpoint: OutPoint) -> Result<Option<LocalUtxo>, Error>[src]

Returns the UTXO owned by this wallet corresponding to outpoint if it exists in the wallet’s database.

pub fn list_transactions(
    &self,
    include_raw: bool
) -> Result<Vec<TransactionDetails>, Error>
[src]

Return the list of transactions made and received by the wallet

Optionally fill the TransactionDetails::transaction field with the raw transaction if include_raw is true.

Note that this methods only operate on the internal database, which first needs to be Wallet::sync manually.

pub fn get_balance(&self) -> Result<u64, Error>[src]

Return the balance, meaning the sum of this wallet’s unspent outputs’ values

Note that this methods only operate on the internal database, which first needs to be Wallet::sync manually.

pub fn add_signer(
    &mut self,
    keychain: KeychainKind,
    ordering: SignerOrdering,
    signer: Arc<dyn Signer>
)
[src]

Add an external signer

See the signer module for an example.

pub fn add_address_validator(&mut self, validator: Arc<dyn AddressValidator>)[src]

Add an address validator

See the address_validator module for an example.

pub fn build_tx(
    &self
) -> TxBuilder<'_, B, D, DefaultCoinSelectionAlgorithm, CreateTx>
[src]

Start building a transaction.

This returns a blank TxBuilder from which you can specify the parameters for the transaction.

Example

let (psbt, details) = {
   let mut builder =  wallet.build_tx();
   builder
       .add_recipient(to_address.script_pubkey(), 50_000);
   builder.finish()?
};

// sign and broadcast ...

pub fn build_fee_bump(
    &self,
    txid: Txid
) -> Result<TxBuilder<'_, B, D, DefaultCoinSelectionAlgorithm, BumpFee>, Error>
[src]

Bump the fee of a transaction previously created with this wallet.

Returns an error if the transaction is already confirmed or doesn’t explicitly signal repalce by fee (RBF). If the transaction can be fee bumped then it returns a TxBuilder pre-populated with the inputs and outputs of the original transaction.

NOTE: if the original transaction was made with TxBuilder::set_single_recipient, the TxBuilder::maintain_single_recipient flag should be enabled to correctly reduce the only output’s value in order to increase the fees.

Example

let (mut psbt, _) = {
    let mut builder = wallet.build_tx();
    builder
        .add_recipient(to_address.script_pubkey(), 50_000)
        .enable_rbf();
    builder.finish()?
};
let _ = wallet.sign(&mut psbt, SignOptions::default())?;
let tx = psbt.extract_tx();
// broadcast tx but it's taking too long to confirm so we want to bump the fee
let (mut psbt, _) =  {
    let mut builder = wallet.build_fee_bump(tx.txid())?;
    builder
        .fee_rate(FeeRate::from_sat_per_vb(5.0));
    builder.finish()?
};

let _ = wallet.sign(&mut psbt, SignOptions::default())?;
let fee_bumped_tx = psbt.extract_tx();
// broadcast fee_bumped_tx to replace original

pub fn sign(
    &self,
    psbt: &mut PSBT,
    sign_options: SignOptions
) -> Result<bool, Error>
[src]

Sign a transaction with all the wallet’s signers, in the order specified by every signer’s SignerOrdering

The SignOptions can be used to tweak the behavior of the software signers, and the way the transaction is finalized at the end. Note that it can’t be guaranteed that every signers will follow the options, but the “software signers” (WIF keys and xprv) defined in this library will.

Example

let (mut psbt, _) = {
    let mut builder = wallet.build_tx();
    builder.add_recipient(to_address.script_pubkey(), 50_000);
    builder.finish()?
};
let  finalized = wallet.sign(&mut psbt, SignOptions::default())?;
assert!(finalized, "we should have signed all the inputs");

pub fn policies(&self, keychain: KeychainKind) -> Result<Option<Policy>, Error>[src]

Return the spending policies for the wallet’s descriptor

pub fn public_descriptor(
    &self,
    keychain: KeychainKind
) -> Result<Option<ExtendedDescriptor>, Error>
[src]

Return the “public” version of the wallet’s descriptor, meaning a new descriptor that has the same structure but with every secret key removed

This can be used to build a watch-only version of a wallet

pub fn finalize_psbt(
    &self,
    psbt: &mut PSBT,
    sign_options: SignOptions
) -> Result<bool, Error>
[src]

Try to finalize a PSBT

The SignOptions can be used to tweak the behavior of the finalizer.

pub fn secp_ctx(&self) -> &Secp256k1<All>[src]

Return the secp256k1 context used for all signing operations

pub fn get_descriptor_for_keychain(
    &self,
    keychain: KeychainKind
) -> &ExtendedDescriptor
[src]

Returns the descriptor used to create adddresses for a particular keychain.

pub fn get_psbt_input(
    &self,
    utxo: LocalUtxo,
    sighash_type: Option<SigHashType>,
    only_witness_utxo: bool
) -> Result<Input, Error>
[src]

get the corresponding PSBT Input for a LocalUtxo

impl<B, D> Wallet<B, D> where
    B: Blockchain,
    D: BatchDatabase
[src]

pub fn new<E: IntoWalletDescriptor>(
    descriptor: E,
    change_descriptor: Option<E>,
    network: Network,
    database: D,
    client: B
) -> Result<Self, Error>
[src]

Create a new “online” wallet

pub fn sync<P: 'static + Progress>(
    &self,
    progress_update: P,
    max_address_param: Option<u32>
) -> Result<(), Error>
[src]

Sync the internal database with the blockchain

pub fn client(&self) -> &B[src]

Return a reference to the internal blockchain client

pub fn network(&self) -> Network[src]

Get the Bitcoin network the wallet is using.

pub fn broadcast(&self, tx: Transaction) -> Result<Txid, Error>[src]

Broadcast a transaction to the network

Trait Implementations

impl<B: Debug, D: Debug> Debug for Wallet<B, D>[src]

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

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl<B, D> !RefUnwindSafe for Wallet<B, D>

impl<B, D> Send for Wallet<B, D> where
    B: Send,
    D: Send

impl<B, D> !Sync for Wallet<B, D>

impl<B, D> Unpin for Wallet<B, D> where
    B: Unpin,
    D: Unpin

impl<B, D> !UnwindSafe for Wallet<B, D>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

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

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

fn in_current_span(self) -> Instrumented<Self>[src]

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

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Pointable for T

pub const ALIGN: usize

The alignment of pointer.

type Init = T

The type for initializers.

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

Initializes a with the given initializer. Read more

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

Dereferences the given pointer. Read more

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

Mutably dereferences the given pointer. Read more

pub unsafe fn drop(ptr: usize)

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

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

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

pub fn vzip(self) -> V