Struct bdk::wallet::tx_builder::TxBuilder [−][src]
pub struct TxBuilder<'a, B, D, Cs, Ctx> { /* fields omitted */ }
Expand description
A transaction builder
A TxBuilder
is created by calling build_tx
or build_fee_bump
on a wallet. After
assigning it, you set options on it until finally calling finish
to consume the builder and
generate the transaction.
Each option setting method on TxBuilder
takes and returns &mut self
so you can chain calls
as in the following example:
// chaining let (psbt1, details) = { let mut builder = wallet.build_tx(); builder .ordering(TxOrdering::Untouched) .add_recipient(addr1.script_pubkey(), 50_000) .add_recipient(addr2.script_pubkey(), 50_000); builder.finish()? }; // non-chaining let (psbt2, details) = { let mut builder = wallet.build_tx(); builder.ordering(TxOrdering::Untouched); for addr in &[addr1, addr2] { builder.add_recipient(addr.script_pubkey(), 50_000); } builder.finish()? }; assert_eq!( psbt1.global.unsigned_tx.output[..2], psbt2.global.unsigned_tx.output[..2] );
At the moment coin_selection
is an exception to the rule as it consumes self
.
This means it is usually best to call coin_selection
on the return value of build_tx
before assigning it.
For further examples see this module’s documentation;
Implementations
impl<'a, B, D: BatchDatabase, Cs: CoinSelectionAlgorithm<D>, Ctx: TxBuilderContext> TxBuilder<'a, B, D, Cs, Ctx>
impl<'a, B, D: BatchDatabase, Cs: CoinSelectionAlgorithm<D>, Ctx: TxBuilderContext> TxBuilder<'a, B, D, Cs, Ctx>
Set an absolute fee
pub fn policy_path(
&mut self,
policy_path: BTreeMap<String, Vec<usize>>,
keychain: KeychainKind
) -> &mut Self
pub fn policy_path(
&mut self,
policy_path: BTreeMap<String, Vec<usize>>,
keychain: KeychainKind
) -> &mut Self
Set the policy path to use while creating the transaction for a given keychain.
This method accepts a map where the key is the policy node id (see
Policy::id
) and the value is the list of the indexes of
the items that are intended to be satisfied from the policy node (see
SatisfiableItem::Thresh::items
).
Example
An example of when the policy path is needed is the following descriptor:
wsh(thresh(2,pk(A),sj:and_v(v:pk(B),n:older(6)),snj:and_v(v:pk(C),after(630000))))
,
derived from the miniscript policy thresh(2,pk(A),and(pk(B),older(6)),and(pk(C),after(630000)))
.
It declares three descriptor fragments, and at the top level it uses thresh()
to
ensure that at least two of them are satisfied. The individual fragments are:
pk(A)
and(pk(B),older(6))
and(pk(C),after(630000))
When those conditions are combined in pairs, it’s clear that the transaction needs to be created differently depending on how the user intends to satisfy the policy afterwards:
- If fragments
1
and2
are used, the transaction will need to use a specificn_sequence
in order to spend anOP_CSV
branch. - If fragments
1
and3
are used, the transaction will need to use a specificlocktime
in order to spend anOP_CLTV
branch. - If fragments
2
and3
are used, the transaction will need both.
When the spending policy is represented as a tree (see
Wallet::policies
), every node
is assigned a unique identifier that can be used in the policy path to specify which of
the node’s children the user intends to satisfy: for instance, assuming the thresh()
root node of this example has an id of aabbccdd
, the policy path map would look like:
{ "aabbccdd" => [0, 1] }
where the key is the node’s id, and the value is a list of the children that should be used, in no particular order.
If a particularly complex descriptor has multiple ambiguous thresholds in its structure, multiple entries can be added to the map, one for each node that requires an explicit path.
let mut path = BTreeMap::new(); path.insert("aabbccdd".to_string(), vec![0, 1]); let builder = wallet .build_tx() .add_recipient(to_address.script_pubkey(), 50_000) .policy_path(path, KeychainKind::External);
Add the list of outpoints to the internal list of UTXOs that must be spent.
If an error occurs while adding any of the UTXOs then none of them are added and the error is returned.
These have priority over the “unspendable” utxos, meaning that if a utxo is present both in the “utxos” and the “unspendable” list, it will be spent.
Add a utxo to the internal list of utxos that must be spent
These have priority over the “unspendable” utxos, meaning that if a utxo is present both in the “utxos” and the “unspendable” list, it will be spent.
Add a foreign UTXO i.e. a UTXO not owned by this wallet.
At a minimum to add a foreign UTXO we need:
outpoint
: To add it to the raw transaction.psbt_input
: To know the value.satisfaction_weight
: To know how much weight/vbytes the input will add to the transaction for fee calculation.
There are several security concerns about adding foregin UTXOs that application
developers should consider. First, how do you know the value of the input is correct? If a
non_witness_utxo
is provided in the psbt_input
then this method implicitly verifies the
value by checking it against the transaction. If only a witness_utxo
is provided then this
method doesn’t verify the value but just takes it as a given – it is up to you to check
that whoever sent you the input_psbt
was not lying!
Secondly, you must somehow provide satisfaction_weight
of the input. Depending on your
application it may be important that this be known precisely. If not, a malicious
counterparty may fool you into putting in a value that is too low, giving the transaction a
lower than expected feerate. They could also fool you into putting a value that is too high
causing you to pay a fee that is too high. The party who is broadcasting the transaction can
of course check the real input weight matches the expected weight prior to broadcasting.
To guarantee the satisfaction_weight
is correct, you can require the party providing the
psbt_input
provide a miniscript descriptor for the input so you can check it against the
script_pubkey
and then ask it for the max_satisfaction_weight
.
This is an EXPERIMENTAL feature, API and other major changes are expected.
Errors
This method returns errors in the following circumstances:
- The
psbt_input
does not contain awitness_utxo
ornon_witness_utxo
. - The data in
non_witness_utxo
does not match what is inoutpoint
.
Note unless you set only_witness_utxo
any psbt_input
you pass to this method must
have non_witness_utxo
set otherwise you will get an error when finish
is called.
Only spend utxos added by add_utxo
.
The wallet will not add additional utxos to the transaction even if they are needed to make the transaction valid.
Replace the internal list of unspendable utxos with a new list
It’s important to note that the “must-be-spent” utxos added with TxBuilder::add_utxo
have priority over these. See the docs of the two linked methods for more details.
Add a utxo to the internal list of unspendable utxos
It’s important to note that the “must-be-spent” utxos added with TxBuilder::add_utxo
have priority over this. See the docs of the two linked methods for more details.
Sign with a specific sig hash
Use this option very carefully
Choose the ordering for inputs and outputs of the transaction
Use a specific nLockTime while creating the transaction
This can cause conflicts if the wallet’s descriptors contain an “after” (OP_CLTV) operator.
Build a transaction with a specific version
The version
should always be greater than 0
and greater than 1
if the wallet’s
descriptors contain an “older” (OP_CSV) operator.
Do not spend change outputs
This effectively adds all the change outputs to the “unspendable” list. See
TxBuilder::unspendable
.
Only spend change outputs
This effectively adds all the non-change outputs to the “unspendable” list. See
TxBuilder::unspendable
.
Set a specific ChangeSpendPolicy
. See TxBuilder::do_not_spend_change
and
TxBuilder::only_spend_change
for some shortcuts.
Only Fill-in the psbt::Input::witness_utxo
field when spending from
SegWit descriptors.
This reduces the size of the PSBT, but some signers might reject them due to the lack of
the non_witness_utxo
.
Fill-in the psbt::Output::redeem_script
and
psbt::Output::witness_script
fields.
This is useful for signers which always require it, like ColdCard hardware wallets.
Fill-in the PSBT_GLOBAL_XPUB
field with the extended keys contained in both the external
and internal descriptors
This is useful for offline signers that take part to a multisig. Some hardware wallets like BitBox and ColdCard are known to require this.
Spend all the available inputs. This respects filters like TxBuilder::unspendable
and the change policy.
pub fn coin_selection<P: CoinSelectionAlgorithm<D>>(
self,
coin_selection: P
) -> TxBuilder<'a, B, D, P, Ctx>
pub fn coin_selection<P: CoinSelectionAlgorithm<D>>(
self,
coin_selection: P
) -> TxBuilder<'a, B, D, P, Ctx>
Choose the coin selection algorithm
Overrides the DefaultCoinSelectionAlgorithm
.
Note that this function consumes the builder and returns it so it is usually best to put this as the first call on the builder.
Finish the building the transaction.
Returns the BIP174
“PSBT” and summary details about the transaction.
Enable signaling RBF
This will use the default nSequence value of 0xFFFFFFFD
.
Enable signaling RBF with a specific nSequence value
This can cause conflicts if the wallet’s descriptors contain an “older” (OP_CSV) operator
and the given nsequence
is lower than the CSV value.
If the nsequence
is higher than 0xFFFFFFFD
an error will be thrown, since it would not
be a valid nSequence to signal RBF.
Replace the recipients already added with a new list
Add a recipient to the internal list
Set a single recipient that will get all the selected funds minus the fee. No change will be created
This method overrides any recipient set with set_recipients
or
add_recipient
.
It can only be used in conjunction with drain_wallet
to send the
entire content of the wallet (minus filters) to a single recipient or with a
list of manually selected UTXOs by enabling manually_selected_only
and selecting them with or add_utxo
.
When bumping the fees of a transaction made with this option, the user should remeber to
add maintain_single_recipient
to correctly update the
single output instead of adding one more for the change.
Bump the fees of a transaction made with set_single_recipient
Unless extra inputs are specified with add_utxo
, this flag will make
bump_fee
reduce the value of the existing output, or fail if it would be consumed
entirely given the higher new fee rate.
If extra inputs are added and they are not entirely consumed in fees, a change output will not be added; the existing output will simply grow in value.
Fails if the transaction has more than one outputs.
Trait Implementations
Auto Trait Implementations
impl<'a, B, D, Cs, Ctx> !RefUnwindSafe for TxBuilder<'a, B, D, Cs, Ctx>
impl<'a, B, D, Cs, Ctx> !UnwindSafe for TxBuilder<'a, B, D, Cs, Ctx>
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
type Output = T
type Output = T
Should always be Self
pub fn vzip(self) -> V