Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/electrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn main() -> Result<(), anyhow::Error> {
tx_builder.add_recipient(address.script_pubkey(), SEND_AMOUNT);
tx_builder.fee_rate(target_fee_rate);

let mut psbt = tx_builder.finish()?;
let mut psbt = tx_builder.finish()?.psbt;
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
assert!(finalized);
let original_fee = psbt.fee_amount().unwrap();
Expand Down Expand Up @@ -123,7 +123,7 @@ fn main() -> Result<(), anyhow::Error> {
let feerate = FeeRate::from_sat_per_vb(tx_feerate.to_sat_per_vb_ceil() + 1).unwrap();
let mut builder = wallet.build_fee_bump(txid).expect("failed to bump tx");
builder.fee_rate(feerate);
let mut bumped_psbt = builder.finish().unwrap();
let mut bumped_psbt = builder.finish().unwrap().psbt;
let finalize_btx = wallet.sign(&mut bumped_psbt, SignOptions::default())?;
assert!(finalize_btx);
let new_fee = bumped_psbt.fee_amount().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/esplora_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async fn main() -> Result<(), anyhow::Error> {
tx_builder.add_recipient(address.script_pubkey(), SEND_AMOUNT);
tx_builder.fee_rate(target_fee_rate);

let mut psbt = tx_builder.finish()?;
let mut psbt = tx_builder.finish()?.psbt;
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
assert!(finalized);
let original_fee = psbt.fee_amount().unwrap();
Expand Down Expand Up @@ -116,7 +116,7 @@ async fn main() -> Result<(), anyhow::Error> {
let feerate = FeeRate::from_sat_per_vb(tx_feerate.to_sat_per_vb_ceil() + 1).unwrap();
let mut builder = wallet.build_fee_bump(txid).expect("failed to bump tx");
builder.fee_rate(feerate);
let mut bumped_psbt = builder.finish().unwrap();
let mut bumped_psbt = builder.finish().unwrap().psbt;
let finalize_btx = wallet.sign(&mut bumped_psbt, SignOptions::default())?;
assert!(finalize_btx);
let new_fee = bumped_psbt.fee_amount().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/esplora_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn main() -> Result<(), anyhow::Error> {
tx_builder.add_recipient(address.script_pubkey(), SEND_AMOUNT);
tx_builder.fee_rate(target_fee_rate);

let mut psbt = tx_builder.finish()?;
let mut psbt = tx_builder.finish()?.psbt;
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
assert!(finalized);
let original_fee = psbt.fee_amount().unwrap();
Expand Down Expand Up @@ -112,7 +112,7 @@ fn main() -> Result<(), anyhow::Error> {
let feerate = FeeRate::from_sat_per_vb(tx_feerate.to_sat_per_vb_ceil() + 1).unwrap();
let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_rate(feerate);
let mut new_psbt = builder.finish().unwrap();
let mut new_psbt = builder.finish().unwrap().psbt;
let finalize_tx = wallet.sign(&mut new_psbt, SignOptions::default())?;
assert!(finalize_tx);
let new_fee = new_psbt.fee_amount().unwrap();
Expand Down
14 changes: 7 additions & 7 deletions src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ impl Wallet {
coin_selection: Cs,
params: TxParams,
rng: &mut impl RngCore,
) -> Result<Psbt, CreateTxError> {
) -> Result<tx_builder::BuilderResult, CreateTxError> {
let keychains: BTreeMap<_, _> = self.indexed_graph.index.keychains().collect();
let external_descriptor = keychains.get(&KeychainKind::External).expect("must exist");
let internal_descriptor = keychains.get(&KeychainKind::Internal);
Expand Down Expand Up @@ -1679,7 +1679,7 @@ impl Wallet {
}
}

Ok(psbt)
Ok(tx_builder::BuilderResult { psbt, fee_rate })
}

/// Bump the fee of a transaction previously created with this wallet.
Expand Down Expand Up @@ -1707,8 +1707,8 @@ impl Wallet {
/// .add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000));
/// builder.finish()?
/// };
/// let _ = wallet.sign(&mut psbt, SignOptions::default())?;
/// let tx = psbt.clone().extract_tx().expect("tx");
/// let _ = wallet.sign(&mut psbt.psbt, SignOptions::default())?;
/// let tx = psbt.psbt.clone().extract_tx().expect("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.compute_txid())?;
Expand All @@ -1717,8 +1717,8 @@ impl Wallet {
/// builder.finish()?
/// };
///
/// let _ = wallet.sign(&mut psbt, SignOptions::default())?;
/// let fee_bumped_tx = psbt.extract_tx();
/// let _ = wallet.sign(&mut psbt.psbt, SignOptions::default())?;
/// let fee_bumped_tx = psbt.psbt.extract_tx();
/// // broadcast fee_bumped_tx to replace original
/// # Ok::<(), anyhow::Error>(())
/// ```
Expand Down Expand Up @@ -1883,7 +1883,7 @@ impl Wallet {
/// builder.add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000));
/// builder.finish()?
/// };
/// let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
/// let finalized = wallet.sign(&mut psbt.psbt, SignOptions::default())?;
/// assert!(finalized, "we should have signed all the inputs");
/// # Ok::<(),anyhow::Error>(())
pub fn sign(&self, psbt: &mut Psbt, sign_options: SignOptions) -> Result<bool, SignerError> {
Expand Down
43 changes: 36 additions & 7 deletions src/wallet/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ use crate::{KeychainKind, LocalOutput, Utxo, WeightedUtxo};
/// builder.finish()?
/// };
///
/// assert_eq!(psbt1.unsigned_tx.output[..2], psbt2.unsigned_tx.output[..2]);
/// assert_eq!(psbt1.psbt.unsigned_tx.output[..2], psbt2.psbt.unsigned_tx.output[..2]);
/// # Ok::<(), anyhow::Error>(())
/// ```
///
Expand Down Expand Up @@ -759,7 +759,7 @@ impl<Cs: CoinSelectionAlgorithm> TxBuilder<'_, Cs> {
/// **WARNING**: To avoid change address reuse you must persist the changes resulting from one
/// or more calls to this method before closing the wallet. See [`Wallet::reveal_next_address`].
#[cfg(feature = "std")]
pub fn finish(self) -> Result<Psbt, CreateTxError> {
pub fn finish(self) -> Result<BuilderResult, CreateTxError> {
self.finish_with_aux_rand(&mut bitcoin::key::rand::thread_rng())
}

Expand All @@ -773,11 +773,25 @@ impl<Cs: CoinSelectionAlgorithm> TxBuilder<'_, Cs> {
///
/// **WARNING**: To avoid change address reuse you must persist the changes resulting from one
/// or more calls to this method before closing the wallet. See [`Wallet::reveal_next_address`].
pub fn finish_with_aux_rand(self, rng: &mut impl RngCore) -> Result<Psbt, CreateTxError> {
pub fn finish_with_aux_rand(
self,
rng: &mut impl RngCore,
) -> Result<BuilderResult, CreateTxError> {
self.wallet.create_tx(self.coin_selection, self.params, rng)
}
}

/// Result of building a transaction.
///
/// Contains the unsigned PSBT and the feerate that was used during construction.
#[derive(Debug, Clone)]
pub struct BuilderResult {
/// The constructed PSBT (unsigned transaction)
pub psbt: Psbt,
/// The feerate used for this transaction (sat/vB)
pub fee_rate: FeeRate,
}

#[derive(Debug)]
/// Error returned from [`TxBuilder::add_utxo`] and [`TxBuilder::add_utxos`]
pub enum AddUtxoError {
Expand Down Expand Up @@ -1224,7 +1238,12 @@ mod test {
.drain_wallet()
.drain_to(recipient.script_pubkey());
let tx = builder.finish().unwrap();
let output = tx.unsigned_tx.output.first().expect("must have one output");
let output = tx
.psbt
.unsigned_tx
.output
.first()
.expect("must have one output");
assert_eq!(output.value, Amount::ONE_BTC * 6);
}

Expand All @@ -1237,7 +1256,12 @@ mod test {
.drain_wallet()
.drain_to(recipient.script_pubkey());
let tx = builder.finish().unwrap();
let output = tx.unsigned_tx.output.first().expect("must have one output");
let output = tx
.psbt
.unsigned_tx
.output
.first()
.expect("must have one output");
assert_eq!(output.value, Amount::ONE_BTC * 3);
}

Expand All @@ -1250,7 +1274,12 @@ mod test {
.drain_wallet()
.drain_to(recipient.script_pubkey());
let tx = builder.finish().unwrap();
let output = tx.unsigned_tx.output.first().expect("must have one output");
let output = tx
.psbt
.unsigned_tx
.output
.first()
.expect("must have one output");
assert_eq!(output.value, Amount::ONE_BTC);
}
}
Expand Down Expand Up @@ -1286,7 +1315,7 @@ mod test {
builder.fee_absolute(Amount::from_sat(1_000));
let psbt = builder.finish().unwrap();

let tx = psbt.extract_tx().unwrap();
let tx = psbt.psbt.extract_tx().unwrap();
let txid = tx.compute_txid();
let feerate = wallet.calculate_fee_rate(&tx).unwrap().to_sat_per_kwu();
insert_tx(&mut wallet, tx);
Expand Down
6 changes: 3 additions & 3 deletions tests/add_foreign_utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn test_add_foreign_utxo() {
.only_witness_utxo()
.add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction)
.unwrap();
let mut psbt = builder.finish().unwrap();
let mut psbt = builder.finish().unwrap().psbt;
wallet1.insert_txout(utxo.outpoint, utxo.txout);
let fee = check_fee!(wallet1, psbt);
let (sent, received) =
Expand Down Expand Up @@ -109,7 +109,7 @@ fn test_calculate_fee_with_missing_foreign_utxo() {
.only_witness_utxo()
.add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction)
.unwrap();
let psbt = builder.finish().unwrap();
let psbt = builder.finish().unwrap().psbt;
let tx = psbt.extract_tx().expect("failed to extract tx");
let res = wallet1.calculate_fee(&tx);
assert!(
Expand Down Expand Up @@ -270,7 +270,7 @@ fn test_taproot_foreign_utxo() {
.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000))
.add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction)
.unwrap();
let psbt = builder.finish().unwrap();
let psbt = builder.finish().unwrap().psbt;
let (sent, received) =
wallet1.sent_and_received(&psbt.clone().extract_tx().expect("failed to extract tx"));
wallet1.insert_txout(utxo.outpoint, utxo.txout);
Expand Down
Loading