bdk_wallet/psbt/mod.rs
1// Bitcoin Dev Kit
2// Written in 2020 by Alekos Filini <alekos.filini@gmail.com>
3//
4// Copyright (c) 2020-2021 Bitcoin Dev Kit Developers
5//
6// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
7// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
9// You may not use this file except in accordance with one or both of these
10// licenses.
11
12//! Additional functions on the `rust-bitcoin` `Psbt` structure.
13
14use alloc::vec::Vec;
15use bitcoin::Amount;
16use bitcoin::FeeRate;
17use bitcoin::Psbt;
18use bitcoin::TxOut;
19
20// TODO upstream the functions here to `rust-bitcoin`?
21
22/// Trait to add functions to extract utxos and calculate fees.
23pub trait PsbtUtils {
24 /// Get the `TxOut` for the specified input index, if it doesn't exist in the PSBT `None` is
25 /// returned.
26 fn get_utxo_for(&self, input_index: usize) -> Option<TxOut>;
27
28 /// The total transaction fee amount, sum of input amounts minus sum of output amounts, in sats.
29 /// If the PSBT is missing a TxOut for an input returns None.
30 fn fee_amount(&self) -> Option<Amount>;
31
32 /// The transaction's fee rate. This value will only be accurate if calculated AFTER the
33 /// `Psbt` is finalized and all witness/signature data is added to the
34 /// transaction.
35 /// If the PSBT is missing a TxOut for an input returns None.
36 fn fee_rate(&self) -> Option<FeeRate>;
37}
38
39impl PsbtUtils for Psbt {
40 fn get_utxo_for(&self, input_index: usize) -> Option<TxOut> {
41 let tx = &self.unsigned_tx;
42 let input = self.inputs.get(input_index)?;
43
44 match (&input.witness_utxo, &input.non_witness_utxo) {
45 (Some(_), _) => input.witness_utxo.clone(),
46 (_, Some(_)) => input.non_witness_utxo.as_ref().map(|in_tx| {
47 in_tx.output[tx.input[input_index].previous_output.vout as usize].clone()
48 }),
49 _ => None,
50 }
51 }
52
53 fn fee_amount(&self) -> Option<Amount> {
54 let tx = &self.unsigned_tx;
55 let utxos: Option<Vec<TxOut>> = (0..tx.input.len()).map(|i| self.get_utxo_for(i)).collect();
56
57 utxos.map(|inputs| {
58 let input_amount: Amount = inputs.iter().map(|i| i.value).sum();
59 let output_amount: Amount = self.unsigned_tx.output.iter().map(|o| o.value).sum();
60 input_amount
61 .checked_sub(output_amount)
62 .expect("input amount must be greater than output amount")
63 })
64 }
65
66 fn fee_rate(&self) -> Option<FeeRate> {
67 let fee_amount = self.fee_amount();
68 let weight = self.clone().extract_tx().ok()?.weight();
69 fee_amount.map(|fee| fee / weight)
70 }
71}