feat(wallet)!: remove TransactionDetails from bdk::Wallet API
Added - Wallet::sent_and_received function - Wallet::calculate_fee and Wallet::calculate_fee_rate functions - Wallet::error::CalculateFeeError BREAKING CHANGES: Removed - TransactionDetails struct Changed - Wallet::get_tx now returns CanonicalTx instead of TransactionDetails - TxBuilder::finish now returns only a PartiallySignedTransaction
This commit is contained in:
parent
e5fb1ec7ff
commit
b4c31cd5ba
@ -9,6 +9,10 @@
|
|||||||
// You may not use this file except in accordance with one or both of these
|
// You may not use this file except in accordance with one or both of these
|
||||||
// licenses.
|
// licenses.
|
||||||
|
|
||||||
|
//! Errors
|
||||||
|
//!
|
||||||
|
//! This module defines the errors that can be thrown by [`crate`] functions.
|
||||||
|
|
||||||
use crate::bitcoin::Network;
|
use crate::bitcoin::Network;
|
||||||
use crate::{descriptor, wallet};
|
use crate::{descriptor, wallet};
|
||||||
use alloc::{string::String, vec::Vec};
|
use alloc::{string::String, vec::Vec};
|
||||||
@ -89,7 +93,17 @@ pub enum Error {
|
|||||||
Psbt(bitcoin::psbt::Error),
|
Psbt(bitcoin::psbt::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Errors returned by `Wallet::calculate_fee`.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum CalculateFeeError {
|
||||||
|
/// Missing `TxOut` for one of the inputs of the tx
|
||||||
|
MissingTxOut,
|
||||||
|
/// When the transaction is invalid according to the graph it has a negative fee
|
||||||
|
NegativeFee(i64),
|
||||||
|
}
|
||||||
|
|
||||||
/// Errors returned by miniscript when updating inconsistent PSBTs
|
/// Errors returned by miniscript when updating inconsistent PSBTs
|
||||||
|
#[allow(missing_docs)] // TODO add docs
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum MiniscriptPsbtError {
|
pub enum MiniscriptPsbtError {
|
||||||
Conversion(miniscript::descriptor::ConversionError),
|
Conversion(miniscript::descriptor::ConversionError),
|
||||||
|
@ -754,7 +754,7 @@ fn expand_multi_keys<Pk: IntoDescriptorKey<Ctx>, Ctx: ScriptContext>(
|
|||||||
let (key_map, valid_networks) = key_maps_networks.into_iter().fold(
|
let (key_map, valid_networks) = key_maps_networks.into_iter().fold(
|
||||||
(KeyMap::default(), any_network()),
|
(KeyMap::default(), any_network()),
|
||||||
|(mut keys_acc, net_acc), (key, net)| {
|
|(mut keys_acc, net_acc), (key, net)| {
|
||||||
keys_acc.extend(key.into_iter());
|
keys_acc.extend(key);
|
||||||
let net_acc = merge_networks(&net_acc, &net);
|
let net_acc = merge_networks(&net_acc, &net);
|
||||||
|
|
||||||
(keys_acc, net_acc)
|
(keys_acc, net_acc)
|
||||||
|
@ -29,7 +29,7 @@ extern crate bip39;
|
|||||||
|
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub(crate) mod error;
|
pub mod error;
|
||||||
pub mod descriptor;
|
pub mod descriptor;
|
||||||
pub mod keys;
|
pub mod keys;
|
||||||
pub mod psbt;
|
pub mod psbt;
|
||||||
|
@ -14,8 +14,8 @@ use core::convert::AsRef;
|
|||||||
use core::ops::Sub;
|
use core::ops::Sub;
|
||||||
|
|
||||||
use bdk_chain::ConfirmationTime;
|
use bdk_chain::ConfirmationTime;
|
||||||
use bitcoin::blockdata::transaction::{OutPoint, Transaction, TxOut};
|
use bitcoin::blockdata::transaction::{OutPoint, TxOut};
|
||||||
use bitcoin::{hash_types::Txid, psbt, Weight};
|
use bitcoin::{psbt, Weight};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@ -234,40 +234,6 @@ impl Utxo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A wallet transaction
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
|
|
||||||
pub struct TransactionDetails {
|
|
||||||
/// Optional transaction
|
|
||||||
pub transaction: Option<Transaction>,
|
|
||||||
/// Transaction id
|
|
||||||
pub txid: Txid,
|
|
||||||
/// Received value (sats)
|
|
||||||
/// Sum of owned outputs of this transaction.
|
|
||||||
pub received: u64,
|
|
||||||
/// Sent value (sats)
|
|
||||||
/// Sum of owned inputs of this transaction.
|
|
||||||
pub sent: u64,
|
|
||||||
/// Fee value in sats if it was available.
|
|
||||||
pub fee: Option<u64>,
|
|
||||||
/// If the transaction is confirmed, contains height and Unix timestamp of the block containing the
|
|
||||||
/// transaction, unconfirmed transaction contains `None`.
|
|
||||||
pub confirmation_time: ConfirmationTime,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOrd for TransactionDetails {
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
|
|
||||||
Some(self.cmp(other))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ord for TransactionDetails {
|
|
||||||
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
|
|
||||||
self.confirmation_time
|
|
||||||
.cmp(&other.confirmation_time)
|
|
||||||
.then_with(|| self.txid.cmp(&other.txid))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -86,7 +86,7 @@
|
|||||||
//! .unwrap()
|
//! .unwrap()
|
||||||
//! .require_network(Network::Testnet)
|
//! .require_network(Network::Testnet)
|
||||||
//! .unwrap();
|
//! .unwrap();
|
||||||
//! let (psbt, details) = {
|
//! let psbt = {
|
||||||
//! let mut builder = wallet.build_tx().coin_selection(AlwaysSpendEverything);
|
//! let mut builder = wallet.build_tx().coin_selection(AlwaysSpendEverything);
|
||||||
//! builder.add_recipient(to_address.script_pubkey(), 50_000);
|
//! builder.add_recipient(to_address.script_pubkey(), 50_000);
|
||||||
//! builder.finish()?
|
//! builder.finish()?
|
||||||
|
@ -66,7 +66,7 @@ use crate::descriptor::{
|
|||||||
calc_checksum, into_wallet_descriptor_checked, DerivedDescriptor, DescriptorMeta,
|
calc_checksum, into_wallet_descriptor_checked, DerivedDescriptor, DescriptorMeta,
|
||||||
ExtendedDescriptor, ExtractPolicy, IntoWalletDescriptor, Policy, XKeyUtils,
|
ExtendedDescriptor, ExtractPolicy, IntoWalletDescriptor, Policy, XKeyUtils,
|
||||||
};
|
};
|
||||||
use crate::error::{Error, MiniscriptPsbtError};
|
use crate::error::{CalculateFeeError, Error, MiniscriptPsbtError};
|
||||||
use crate::psbt::PsbtUtils;
|
use crate::psbt::PsbtUtils;
|
||||||
use crate::signer::SignerError;
|
use crate::signer::SignerError;
|
||||||
use crate::types::*;
|
use crate::types::*;
|
||||||
@ -430,27 +430,52 @@ impl<D> Wallet<D> {
|
|||||||
.next()
|
.next()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return a single transactions made and received by the wallet
|
/// Calculates the fee of a given transaction. Returns 0 if `tx` is a coinbase transaction.
|
||||||
///
|
///
|
||||||
/// Optionally fill the [`TransactionDetails::transaction`] field with the raw transaction if
|
/// Note `tx` does not have to be in the graph for this to work.
|
||||||
/// `include_raw` is `true`.
|
pub fn calculate_fee(&self, tx: &Transaction) -> Result<u64, CalculateFeeError> {
|
||||||
pub fn get_tx(&self, txid: Txid, include_raw: bool) -> Option<TransactionDetails> {
|
match self.indexed_graph.graph().calculate_fee(tx) {
|
||||||
|
None => Err(CalculateFeeError::MissingTxOut),
|
||||||
|
Some(fee) if fee < 0 => Err(CalculateFeeError::NegativeFee(fee)),
|
||||||
|
Some(fee) => Ok(u64::try_from(fee).unwrap()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Calculate the `FeeRate` for a given transaction.
|
||||||
|
///
|
||||||
|
/// Note `tx` does not have to be in the graph for this to work.
|
||||||
|
pub fn calculate_fee_rate(&self, tx: &Transaction) -> Result<FeeRate, CalculateFeeError> {
|
||||||
|
self.calculate_fee(tx).map(|fee| {
|
||||||
|
let weight = tx.weight();
|
||||||
|
FeeRate::from_wu(fee, weight)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Computes total input value going from script pubkeys in the index (sent) and the total output
|
||||||
|
/// value going to script pubkeys in the index (received) in `tx`. For the `sent` to be computed
|
||||||
|
/// correctly, the output being spent must have already been scanned by the index. Calculating
|
||||||
|
/// received just uses the transaction outputs directly, so it will be correct even if it has not
|
||||||
|
/// been scanned.
|
||||||
|
pub fn sent_and_received(&self, tx: &Transaction) -> (u64, u64) {
|
||||||
|
self.indexed_graph.index.sent_and_received(tx)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return a single `CanonicalTx` made and received by the wallet or `None` if it doesn't
|
||||||
|
/// exist in the wallet
|
||||||
|
pub fn get_tx(
|
||||||
|
&self,
|
||||||
|
txid: Txid,
|
||||||
|
) -> Option<CanonicalTx<'_, Transaction, ConfirmationTimeAnchor>> {
|
||||||
let graph = self.indexed_graph.graph();
|
let graph = self.indexed_graph.graph();
|
||||||
|
|
||||||
let canonical_tx = CanonicalTx {
|
Some(CanonicalTx {
|
||||||
chain_position: graph.get_chain_position(
|
chain_position: graph.get_chain_position(
|
||||||
&self.chain,
|
&self.chain,
|
||||||
self.chain.tip().map(|cp| cp.block_id()).unwrap_or_default(),
|
self.chain.tip().map(|cp| cp.block_id()).unwrap_or_default(),
|
||||||
txid,
|
txid,
|
||||||
)?,
|
)?,
|
||||||
tx_node: graph.get_tx_node(txid)?,
|
tx_node: graph.get_tx_node(txid)?,
|
||||||
};
|
})
|
||||||
|
|
||||||
Some(new_tx_details(
|
|
||||||
&self.indexed_graph,
|
|
||||||
canonical_tx,
|
|
||||||
include_raw,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a new checkpoint to the wallet's internal view of the chain.
|
/// Add a new checkpoint to the wallet's internal view of the chain.
|
||||||
@ -603,7 +628,7 @@ impl<D> Wallet<D> {
|
|||||||
/// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
|
/// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
|
||||||
/// # let mut wallet = doctest_wallet!();
|
/// # let mut wallet = doctest_wallet!();
|
||||||
/// # let to_address = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
|
/// # let to_address = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
|
||||||
/// let (psbt, details) = {
|
/// let psbt = {
|
||||||
/// let mut builder = wallet.build_tx();
|
/// let mut builder = wallet.build_tx();
|
||||||
/// builder
|
/// builder
|
||||||
/// .add_recipient(to_address.script_pubkey(), 50_000);
|
/// .add_recipient(to_address.script_pubkey(), 50_000);
|
||||||
@ -628,7 +653,7 @@ impl<D> Wallet<D> {
|
|||||||
&mut self,
|
&mut self,
|
||||||
coin_selection: Cs,
|
coin_selection: Cs,
|
||||||
params: TxParams,
|
params: TxParams,
|
||||||
) -> Result<(psbt::PartiallySignedTransaction, TransactionDetails), Error>
|
) -> Result<psbt::PartiallySignedTransaction, Error>
|
||||||
where
|
where
|
||||||
D: PersistBackend<ChangeSet>,
|
D: PersistBackend<ChangeSet>,
|
||||||
{
|
{
|
||||||
@ -976,20 +1001,8 @@ impl<D> Wallet<D> {
|
|||||||
// sort input/outputs according to the chosen algorithm
|
// sort input/outputs according to the chosen algorithm
|
||||||
params.ordering.sort_tx(&mut tx);
|
params.ordering.sort_tx(&mut tx);
|
||||||
|
|
||||||
let txid = tx.txid();
|
|
||||||
let sent = coin_selection.local_selected_amount();
|
|
||||||
let psbt = self.complete_transaction(tx, coin_selection.selected, params)?;
|
let psbt = self.complete_transaction(tx, coin_selection.selected, params)?;
|
||||||
|
Ok(psbt)
|
||||||
let transaction_details = TransactionDetails {
|
|
||||||
transaction: None,
|
|
||||||
txid,
|
|
||||||
confirmation_time: ConfirmationTime::Unconfirmed { last_seen: 0 },
|
|
||||||
received,
|
|
||||||
sent,
|
|
||||||
fee: Some(fee_amount),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok((psbt, transaction_details))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bump the fee of a transaction previously created with this wallet.
|
/// Bump the fee of a transaction previously created with this wallet.
|
||||||
@ -1008,7 +1021,7 @@ impl<D> Wallet<D> {
|
|||||||
/// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
|
/// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
|
||||||
/// # let mut wallet = doctest_wallet!();
|
/// # let mut wallet = doctest_wallet!();
|
||||||
/// # let to_address = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
|
/// # let to_address = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
|
||||||
/// let (mut psbt, _) = {
|
/// let mut psbt = {
|
||||||
/// let mut builder = wallet.build_tx();
|
/// let mut builder = wallet.build_tx();
|
||||||
/// builder
|
/// builder
|
||||||
/// .add_recipient(to_address.script_pubkey(), 50_000)
|
/// .add_recipient(to_address.script_pubkey(), 50_000)
|
||||||
@ -1018,7 +1031,7 @@ impl<D> Wallet<D> {
|
|||||||
/// let _ = wallet.sign(&mut psbt, SignOptions::default())?;
|
/// let _ = wallet.sign(&mut psbt, SignOptions::default())?;
|
||||||
/// let tx = psbt.extract_tx();
|
/// let tx = psbt.extract_tx();
|
||||||
/// // broadcast tx but it's taking too long to confirm so we want to bump the fee
|
/// // broadcast tx but it's taking too long to confirm so we want to bump the fee
|
||||||
/// let (mut psbt, _) = {
|
/// let mut psbt = {
|
||||||
/// let mut builder = wallet.build_fee_bump(tx.txid())?;
|
/// let mut builder = wallet.build_fee_bump(tx.txid())?;
|
||||||
/// builder
|
/// builder
|
||||||
/// .fee_rate(bdk::FeeRate::from_sat_per_vb(5.0));
|
/// .fee_rate(bdk::FeeRate::from_sat_per_vb(5.0));
|
||||||
@ -1179,7 +1192,7 @@ impl<D> Wallet<D> {
|
|||||||
/// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
|
/// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
|
||||||
/// # let mut wallet = doctest_wallet!();
|
/// # let mut wallet = doctest_wallet!();
|
||||||
/// # let to_address = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
|
/// # let to_address = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
|
||||||
/// let (mut psbt, _) = {
|
/// let mut psbt = {
|
||||||
/// let mut builder = wallet.build_tx();
|
/// let mut builder = wallet.build_tx();
|
||||||
/// builder.add_recipient(to_address.script_pubkey(), 50_000);
|
/// builder.add_recipient(to_address.script_pubkey(), 50_000);
|
||||||
/// builder.finish()?
|
/// builder.finish()?
|
||||||
@ -1735,7 +1748,7 @@ impl<D> Wallet<D> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Commits all curently [`staged`] changed to the persistence backend returning and error when
|
/// Commits all currently [`staged`] changed to the persistence backend returning and error when
|
||||||
/// this fails.
|
/// this fails.
|
||||||
///
|
///
|
||||||
/// This returns whether the `update` resulted in any changes.
|
/// This returns whether the `update` resulted in any changes.
|
||||||
@ -1826,61 +1839,6 @@ fn new_local_utxo(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_tx_details(
|
|
||||||
indexed_graph: &IndexedTxGraph<ConfirmationTimeAnchor, KeychainTxOutIndex<KeychainKind>>,
|
|
||||||
canonical_tx: CanonicalTx<'_, Transaction, ConfirmationTimeAnchor>,
|
|
||||||
include_raw: bool,
|
|
||||||
) -> TransactionDetails {
|
|
||||||
let graph = indexed_graph.graph();
|
|
||||||
let index = &indexed_graph.index;
|
|
||||||
let tx = canonical_tx.tx_node.tx;
|
|
||||||
|
|
||||||
let received = tx
|
|
||||||
.output
|
|
||||||
.iter()
|
|
||||||
.map(|txout| {
|
|
||||||
if index.index_of_spk(&txout.script_pubkey).is_some() {
|
|
||||||
txout.value
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.sum();
|
|
||||||
|
|
||||||
let sent = tx
|
|
||||||
.input
|
|
||||||
.iter()
|
|
||||||
.map(|txin| {
|
|
||||||
if let Some((_, txout)) = index.txout(txin.previous_output) {
|
|
||||||
txout.value
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.sum();
|
|
||||||
|
|
||||||
let inputs = tx
|
|
||||||
.input
|
|
||||||
.iter()
|
|
||||||
.map(|txin| {
|
|
||||||
graph
|
|
||||||
.get_txout(txin.previous_output)
|
|
||||||
.map(|txout| txout.value)
|
|
||||||
})
|
|
||||||
.sum::<Option<u64>>();
|
|
||||||
let outputs = tx.output.iter().map(|txout| txout.value).sum();
|
|
||||||
let fee = inputs.map(|inputs| inputs.saturating_sub(outputs));
|
|
||||||
|
|
||||||
TransactionDetails {
|
|
||||||
transaction: if include_raw { Some(tx.clone()) } else { None },
|
|
||||||
txid: canonical_tx.tx_node.txid,
|
|
||||||
received,
|
|
||||||
sent,
|
|
||||||
fee,
|
|
||||||
confirmation_time: canonical_tx.chain_position.cloned().into(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
/// Macro for getting a wallet for use in a doctest
|
/// Macro for getting a wallet for use in a doctest
|
||||||
|
@ -32,7 +32,7 @@
|
|||||||
//! .do_not_spend_change()
|
//! .do_not_spend_change()
|
||||||
//! // Turn on RBF signaling
|
//! // Turn on RBF signaling
|
||||||
//! .enable_rbf();
|
//! .enable_rbf();
|
||||||
//! let (psbt, tx_details) = tx_builder.finish()?;
|
//! let psbt = tx_builder.finish()?;
|
||||||
//! # Ok::<(), bdk::Error>(())
|
//! # Ok::<(), bdk::Error>(())
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
@ -48,10 +48,7 @@ use bitcoin::{absolute, script::PushBytes, OutPoint, ScriptBuf, Sequence, Transa
|
|||||||
|
|
||||||
use super::coin_selection::{CoinSelectionAlgorithm, DefaultCoinSelectionAlgorithm};
|
use super::coin_selection::{CoinSelectionAlgorithm, DefaultCoinSelectionAlgorithm};
|
||||||
use super::ChangeSet;
|
use super::ChangeSet;
|
||||||
use crate::{
|
use crate::types::{FeeRate, KeychainKind, LocalUtxo, WeightedUtxo};
|
||||||
types::{FeeRate, KeychainKind, LocalUtxo, WeightedUtxo},
|
|
||||||
TransactionDetails,
|
|
||||||
};
|
|
||||||
use crate::{Error, Utxo, Wallet};
|
use crate::{Error, Utxo, Wallet};
|
||||||
/// Context in which the [`TxBuilder`] is valid
|
/// Context in which the [`TxBuilder`] is valid
|
||||||
pub trait TxBuilderContext: core::fmt::Debug + Default + Clone {}
|
pub trait TxBuilderContext: core::fmt::Debug + Default + Clone {}
|
||||||
@ -85,7 +82,7 @@ impl TxBuilderContext for BumpFee {}
|
|||||||
/// # let addr1 = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
|
/// # let addr1 = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
|
||||||
/// # let addr2 = addr1.clone();
|
/// # let addr2 = addr1.clone();
|
||||||
/// // chaining
|
/// // chaining
|
||||||
/// let (psbt1, details) = {
|
/// let psbt1 = {
|
||||||
/// let mut builder = wallet.build_tx();
|
/// let mut builder = wallet.build_tx();
|
||||||
/// builder
|
/// builder
|
||||||
/// .ordering(TxOrdering::Untouched)
|
/// .ordering(TxOrdering::Untouched)
|
||||||
@ -95,7 +92,7 @@ impl TxBuilderContext for BumpFee {}
|
|||||||
/// };
|
/// };
|
||||||
///
|
///
|
||||||
/// // non-chaining
|
/// // non-chaining
|
||||||
/// let (psbt2, details) = {
|
/// let psbt2 = {
|
||||||
/// let mut builder = wallet.build_tx();
|
/// let mut builder = wallet.build_tx();
|
||||||
/// builder.ordering(TxOrdering::Untouched);
|
/// builder.ordering(TxOrdering::Untouched);
|
||||||
/// for addr in &[addr1, addr2] {
|
/// for addr in &[addr1, addr2] {
|
||||||
@ -531,7 +528,7 @@ impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx: TxBuilderContext> TxBuilder<'a, D,
|
|||||||
/// Returns the [`BIP174`] "PSBT" and summary details about the transaction.
|
/// Returns the [`BIP174`] "PSBT" and summary details about the transaction.
|
||||||
///
|
///
|
||||||
/// [`BIP174`]: https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki
|
/// [`BIP174`]: https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki
|
||||||
pub fn finish(self) -> Result<(Psbt, TransactionDetails), Error>
|
pub fn finish(self) -> Result<Psbt, Error>
|
||||||
where
|
where
|
||||||
D: PersistBackend<ChangeSet>,
|
D: PersistBackend<ChangeSet>,
|
||||||
{
|
{
|
||||||
@ -645,7 +642,7 @@ impl<'a, D, Cs: CoinSelectionAlgorithm> TxBuilder<'a, D, Cs, CreateTx> {
|
|||||||
/// .drain_to(to_address.script_pubkey())
|
/// .drain_to(to_address.script_pubkey())
|
||||||
/// .fee_rate(bdk::FeeRate::from_sat_per_vb(5.0))
|
/// .fee_rate(bdk::FeeRate::from_sat_per_vb(5.0))
|
||||||
/// .enable_rbf();
|
/// .enable_rbf();
|
||||||
/// let (psbt, tx_details) = tx_builder.finish()?;
|
/// let psbt = tx_builder.finish()?;
|
||||||
/// # Ok::<(), bdk::Error>(())
|
/// # Ok::<(), bdk::Error>(())
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
#![allow(unused)]
|
#![allow(unused)]
|
||||||
use bdk::{wallet::AddressIndex, Wallet};
|
|
||||||
|
use bdk::{wallet::AddressIndex, KeychainKind, LocalUtxo, Wallet};
|
||||||
|
use bdk_chain::indexed_tx_graph::Indexer;
|
||||||
use bdk_chain::{BlockId, ConfirmationTime};
|
use bdk_chain::{BlockId, ConfirmationTime};
|
||||||
use bitcoin::hashes::Hash;
|
use bitcoin::hashes::Hash;
|
||||||
use bitcoin::{BlockHash, Network, Transaction, TxOut};
|
use bitcoin::{Address, BlockHash, Network, OutPoint, Transaction, TxIn, TxOut, Txid};
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
/// Return a fake wallet that appears to be funded for testing.
|
/// Return a fake wallet that appears to be funded for testing.
|
||||||
pub fn get_funded_wallet_with_change(
|
pub fn get_funded_wallet_with_change(
|
||||||
@ -10,16 +13,52 @@ pub fn get_funded_wallet_with_change(
|
|||||||
change: Option<&str>,
|
change: Option<&str>,
|
||||||
) -> (Wallet, bitcoin::Txid) {
|
) -> (Wallet, bitcoin::Txid) {
|
||||||
let mut wallet = Wallet::new_no_persist(descriptor, change, Network::Regtest).unwrap();
|
let mut wallet = Wallet::new_no_persist(descriptor, change, Network::Regtest).unwrap();
|
||||||
let address = wallet.get_address(AddressIndex::New).address;
|
let change_address = wallet.get_address(AddressIndex::New).address;
|
||||||
|
let sendto_address = Address::from_str("bcrt1q3qtze4ys45tgdvguj66zrk4fu6hq3a3v9pfly5")
|
||||||
|
.expect("address")
|
||||||
|
.require_network(Network::Regtest)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let tx = Transaction {
|
let tx0 = Transaction {
|
||||||
version: 1,
|
version: 1,
|
||||||
lock_time: bitcoin::absolute::LockTime::ZERO,
|
lock_time: bitcoin::absolute::LockTime::ZERO,
|
||||||
input: vec![],
|
input: vec![TxIn {
|
||||||
output: vec![TxOut {
|
previous_output: OutPoint {
|
||||||
value: 50_000,
|
txid: Txid::all_zeros(),
|
||||||
script_pubkey: address.script_pubkey(),
|
vout: 0,
|
||||||
|
},
|
||||||
|
script_sig: Default::default(),
|
||||||
|
sequence: Default::default(),
|
||||||
|
witness: Default::default(),
|
||||||
}],
|
}],
|
||||||
|
output: vec![TxOut {
|
||||||
|
value: 76_000,
|
||||||
|
script_pubkey: change_address.script_pubkey(),
|
||||||
|
}],
|
||||||
|
};
|
||||||
|
|
||||||
|
let tx1 = Transaction {
|
||||||
|
version: 1,
|
||||||
|
lock_time: bitcoin::absolute::LockTime::ZERO,
|
||||||
|
input: vec![TxIn {
|
||||||
|
previous_output: OutPoint {
|
||||||
|
txid: tx0.txid(),
|
||||||
|
vout: 0,
|
||||||
|
},
|
||||||
|
script_sig: Default::default(),
|
||||||
|
sequence: Default::default(),
|
||||||
|
witness: Default::default(),
|
||||||
|
}],
|
||||||
|
output: vec![
|
||||||
|
TxOut {
|
||||||
|
value: 50_000,
|
||||||
|
script_pubkey: change_address.script_pubkey(),
|
||||||
|
},
|
||||||
|
TxOut {
|
||||||
|
value: 25_000,
|
||||||
|
script_pubkey: sendto_address.script_pubkey(),
|
||||||
|
},
|
||||||
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
wallet
|
wallet
|
||||||
@ -28,17 +67,32 @@ pub fn get_funded_wallet_with_change(
|
|||||||
hash: BlockHash::all_zeros(),
|
hash: BlockHash::all_zeros(),
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
wallet
|
||||||
|
.insert_checkpoint(BlockId {
|
||||||
|
height: 2_000,
|
||||||
|
hash: BlockHash::all_zeros(),
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
wallet
|
wallet
|
||||||
.insert_tx(
|
.insert_tx(
|
||||||
tx.clone(),
|
tx0,
|
||||||
ConfirmationTime::Confirmed {
|
ConfirmationTime::Confirmed {
|
||||||
height: 1_000,
|
height: 1_000,
|
||||||
time: 100,
|
time: 100,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
wallet
|
||||||
|
.insert_tx(
|
||||||
|
tx1.clone(),
|
||||||
|
ConfirmationTime::Confirmed {
|
||||||
|
height: 2_000,
|
||||||
|
time: 200,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
(wallet, tx.txid())
|
(wallet, tx1.txid())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_funded_wallet(descriptor: &str) -> (Wallet, bitcoin::Txid) {
|
pub fn get_funded_wallet(descriptor: &str) -> (Wallet, bitcoin::Txid) {
|
||||||
|
@ -18,7 +18,7 @@ fn test_psbt_malformed_psbt_input_legacy() {
|
|||||||
let send_to = wallet.get_address(AddressIndex::New);
|
let send_to = wallet.get_address(AddressIndex::New);
|
||||||
let mut builder = wallet.build_tx();
|
let mut builder = wallet.build_tx();
|
||||||
builder.add_recipient(send_to.script_pubkey(), 10_000);
|
builder.add_recipient(send_to.script_pubkey(), 10_000);
|
||||||
let (mut psbt, _) = builder.finish().unwrap();
|
let mut psbt = builder.finish().unwrap();
|
||||||
psbt.inputs.push(psbt_bip.inputs[0].clone());
|
psbt.inputs.push(psbt_bip.inputs[0].clone());
|
||||||
let options = SignOptions {
|
let options = SignOptions {
|
||||||
trust_witness_utxo: true,
|
trust_witness_utxo: true,
|
||||||
@ -35,7 +35,7 @@ fn test_psbt_malformed_psbt_input_segwit() {
|
|||||||
let send_to = wallet.get_address(AddressIndex::New);
|
let send_to = wallet.get_address(AddressIndex::New);
|
||||||
let mut builder = wallet.build_tx();
|
let mut builder = wallet.build_tx();
|
||||||
builder.add_recipient(send_to.script_pubkey(), 10_000);
|
builder.add_recipient(send_to.script_pubkey(), 10_000);
|
||||||
let (mut psbt, _) = builder.finish().unwrap();
|
let mut psbt = builder.finish().unwrap();
|
||||||
psbt.inputs.push(psbt_bip.inputs[1].clone());
|
psbt.inputs.push(psbt_bip.inputs[1].clone());
|
||||||
let options = SignOptions {
|
let options = SignOptions {
|
||||||
trust_witness_utxo: true,
|
trust_witness_utxo: true,
|
||||||
@ -51,7 +51,7 @@ fn test_psbt_malformed_tx_input() {
|
|||||||
let send_to = wallet.get_address(AddressIndex::New);
|
let send_to = wallet.get_address(AddressIndex::New);
|
||||||
let mut builder = wallet.build_tx();
|
let mut builder = wallet.build_tx();
|
||||||
builder.add_recipient(send_to.script_pubkey(), 10_000);
|
builder.add_recipient(send_to.script_pubkey(), 10_000);
|
||||||
let (mut psbt, _) = builder.finish().unwrap();
|
let mut psbt = builder.finish().unwrap();
|
||||||
psbt.unsigned_tx.input.push(TxIn::default());
|
psbt.unsigned_tx.input.push(TxIn::default());
|
||||||
let options = SignOptions {
|
let options = SignOptions {
|
||||||
trust_witness_utxo: true,
|
trust_witness_utxo: true,
|
||||||
@ -67,7 +67,7 @@ fn test_psbt_sign_with_finalized() {
|
|||||||
let send_to = wallet.get_address(AddressIndex::New);
|
let send_to = wallet.get_address(AddressIndex::New);
|
||||||
let mut builder = wallet.build_tx();
|
let mut builder = wallet.build_tx();
|
||||||
builder.add_recipient(send_to.script_pubkey(), 10_000);
|
builder.add_recipient(send_to.script_pubkey(), 10_000);
|
||||||
let (mut psbt, _) = builder.finish().unwrap();
|
let mut psbt = builder.finish().unwrap();
|
||||||
|
|
||||||
// add a finalized input
|
// add a finalized input
|
||||||
psbt.inputs.push(psbt_bip.inputs[0].clone());
|
psbt.inputs.push(psbt_bip.inputs[0].clone());
|
||||||
@ -89,7 +89,7 @@ fn test_psbt_fee_rate_with_witness_utxo() {
|
|||||||
let mut builder = wallet.build_tx();
|
let mut builder = wallet.build_tx();
|
||||||
builder.drain_to(addr.script_pubkey()).drain_wallet();
|
builder.drain_to(addr.script_pubkey()).drain_wallet();
|
||||||
builder.fee_rate(FeeRate::from_sat_per_vb(expected_fee_rate));
|
builder.fee_rate(FeeRate::from_sat_per_vb(expected_fee_rate));
|
||||||
let (mut psbt, _) = builder.finish().unwrap();
|
let mut psbt = builder.finish().unwrap();
|
||||||
let fee_amount = psbt.fee_amount();
|
let fee_amount = psbt.fee_amount();
|
||||||
assert!(fee_amount.is_some());
|
assert!(fee_amount.is_some());
|
||||||
|
|
||||||
@ -114,7 +114,7 @@ fn test_psbt_fee_rate_with_nonwitness_utxo() {
|
|||||||
let mut builder = wallet.build_tx();
|
let mut builder = wallet.build_tx();
|
||||||
builder.drain_to(addr.script_pubkey()).drain_wallet();
|
builder.drain_to(addr.script_pubkey()).drain_wallet();
|
||||||
builder.fee_rate(FeeRate::from_sat_per_vb(expected_fee_rate));
|
builder.fee_rate(FeeRate::from_sat_per_vb(expected_fee_rate));
|
||||||
let (mut psbt, _) = builder.finish().unwrap();
|
let mut psbt = builder.finish().unwrap();
|
||||||
let fee_amount = psbt.fee_amount();
|
let fee_amount = psbt.fee_amount();
|
||||||
assert!(fee_amount.is_some());
|
assert!(fee_amount.is_some());
|
||||||
let unfinalized_fee_rate = psbt.fee_rate().unwrap();
|
let unfinalized_fee_rate = psbt.fee_rate().unwrap();
|
||||||
@ -138,7 +138,7 @@ fn test_psbt_fee_rate_with_missing_txout() {
|
|||||||
let mut builder = wpkh_wallet.build_tx();
|
let mut builder = wpkh_wallet.build_tx();
|
||||||
builder.drain_to(addr.script_pubkey()).drain_wallet();
|
builder.drain_to(addr.script_pubkey()).drain_wallet();
|
||||||
builder.fee_rate(FeeRate::from_sat_per_vb(expected_fee_rate));
|
builder.fee_rate(FeeRate::from_sat_per_vb(expected_fee_rate));
|
||||||
let (mut wpkh_psbt, _) = builder.finish().unwrap();
|
let mut wpkh_psbt = builder.finish().unwrap();
|
||||||
|
|
||||||
wpkh_psbt.inputs[0].witness_utxo = None;
|
wpkh_psbt.inputs[0].witness_utxo = None;
|
||||||
wpkh_psbt.inputs[0].non_witness_utxo = None;
|
wpkh_psbt.inputs[0].non_witness_utxo = None;
|
||||||
@ -150,7 +150,7 @@ fn test_psbt_fee_rate_with_missing_txout() {
|
|||||||
let mut builder = pkh_wallet.build_tx();
|
let mut builder = pkh_wallet.build_tx();
|
||||||
builder.drain_to(addr.script_pubkey()).drain_wallet();
|
builder.drain_to(addr.script_pubkey()).drain_wallet();
|
||||||
builder.fee_rate(FeeRate::from_sat_per_vb(expected_fee_rate));
|
builder.fee_rate(FeeRate::from_sat_per_vb(expected_fee_rate));
|
||||||
let (mut pkh_psbt, _) = builder.finish().unwrap();
|
let mut pkh_psbt = builder.finish().unwrap();
|
||||||
|
|
||||||
pkh_psbt.inputs[0].non_witness_utxo = None;
|
pkh_psbt.inputs[0].non_witness_utxo = None;
|
||||||
assert!(pkh_psbt.fee_amount().is_none());
|
assert!(pkh_psbt.fee_amount().is_none());
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -81,7 +81,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT)
|
.add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT)
|
||||||
.enable_rbf();
|
.enable_rbf();
|
||||||
|
|
||||||
let (mut psbt, _) = tx_builder.finish()?;
|
let mut psbt = tx_builder.finish()?;
|
||||||
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
|
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
|
||||||
assert!(finalized);
|
assert!(finalized);
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT)
|
.add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT)
|
||||||
.enable_rbf();
|
.enable_rbf();
|
||||||
|
|
||||||
let (mut psbt, _) = tx_builder.finish()?;
|
let mut psbt = tx_builder.finish()?;
|
||||||
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
|
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
|
||||||
assert!(finalized);
|
assert!(finalized);
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT)
|
.add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT)
|
||||||
.enable_rbf();
|
.enable_rbf();
|
||||||
|
|
||||||
let (mut psbt, _) = tx_builder.finish()?;
|
let mut psbt = tx_builder.finish()?;
|
||||||
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
|
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
|
||||||
assert!(finalized);
|
assert!(finalized);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user