Class TxBuilder
-
- All Implemented Interfaces:
-
java.lang.AutoCloseable
,org.bitcoindevkit.Disposable
,org.bitcoindevkit.TxBuilderInterface
public class TxBuilder implements Disposable, AutoCloseable, TxBuilderInterface
A
TxBuilder
is created by callingbuild_tx
on a wallet. After assigning it, you set options on it until finally callingfinish
to consume the builder and generate the transaction.
-
-
Method Summary
Modifier and Type Method Description Unit
destroy()
Unit
close()
final Pointer
uniffiClonePointer()
TxBuilder
addData(ByteArray data)
Add data as an output using OP_RETURN
.TxBuilder
addGlobalXpubs()
Fill-in the PSBT_GLOBAL_XPUB
field with the extended keys contained in both the external and internal descriptors.TxBuilder
addRecipient(Script script, Amount amount)
Add a recipient to the internal list of recipients. TxBuilder
addUnspendable(OutPoint unspendable)
Add a utxo to the internal list of unspendable utxos. TxBuilder
addUtxo(OutPoint outpoint)
Add a utxo to the internal list of utxos that must be spent. TxBuilder
addUtxos(List<OutPoint> outpoints)
Add the list of outpoints to the internal list of UTXOs that must be spent. TxBuilder
allowDust(Boolean allowDust)
Set whether or not the dust limit is checked. TxBuilder
changePolicy(ChangeSpendPolicy changePolicy)
Set a specific ChangeSpendPolicy
.TxBuilder
currentHeight(UInt height)
Set the current blockchain height. TxBuilder
doNotSpendChange()
Do not spend change outputs. TxBuilder
drainTo(Script script)
Sets the address to drain excess coins to. TxBuilder
drainWallet()
Spend all the available inputs. TxBuilder
feeAbsolute(Amount feeAmount)
Set an absolute fee The fee_absolute
method refers to the absolute transaction fee inAmount
.TxBuilder
feeRate(FeeRate feeRate)
Set a custom fee rate. Psbt
finish(Wallet wallet)
Finish building the transaction. TxBuilder
manuallySelectedOnly()
Only spend utxos added by TxBuilder::add_utxo
.TxBuilder
nlocktime(LockTime locktime)
Use a specific nLockTime while creating the transaction. TxBuilder
onlySpendChange()
Only spend change outputs. TxBuilder
policyPath(Map<String, List<ULong>> policyPath, KeychainKind keychain)
The TxBuilder::policy_path is a complex API. TxBuilder
setExactSequence(UInt nsequence)
Set an exact nSequence
value.TxBuilder
setRecipients(List<ScriptAmount> recipients)
Replace the recipients already added with a new list of recipients. TxBuilder
unspendable(List<OutPoint> unspendable)
Replace the internal list of unspendable utxos with a new list. TxBuilder
version(Integer version)
Build a transaction with a specific version.
-
-
-
Method Detail
-
uniffiClonePointer
final Pointer uniffiClonePointer()
-
addGlobalXpubs
TxBuilder addGlobalXpubs()
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.
-
addRecipient
TxBuilder addRecipient(Script script, Amount amount)
Add a recipient to the internal list of recipients.
-
addUnspendable
TxBuilder addUnspendable(OutPoint unspendable)
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.
-
addUtxo
TxBuilder addUtxo(OutPoint outpoint)
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.
-
addUtxos
TxBuilder addUtxos(List<OutPoint> outpoints)
Add the list of outpoints to the internal list of UTXOs that must be spent.
-
allowDust
TxBuilder allowDust(Boolean allowDust)
Set whether or not the dust limit is checked.
Note: by avoiding a dust limit check you may end up with a transaction that is non-standard.
-
changePolicy
TxBuilder changePolicy(ChangeSpendPolicy changePolicy)
Set a specific
ChangeSpendPolicy
. SeeTxBuilder::do_not_spend_change
andTxBuilder::only_spend_change
for some shortcuts. This method assumes the presence of an internal keychain, otherwise it has no effect.
-
currentHeight
TxBuilder currentHeight(UInt height)
Set the current blockchain height.
This will be used to:
Set the
nLockTime
for preventing fee sniping. Note: This will be ignored if you manually specify anlocktime
usingTxBuilder::nlocktime
.Decide whether coinbase outputs are mature or not. If the coinbase outputs are not mature at
current_height
, we ignore them in the coin selection. If you want to create a transaction that spends immature coinbase inputs, manually add them usingTxBuilder::add_utxos
. In both cases, if you don’t provide a current height, we use the last sync height.
-
doNotSpendChange
TxBuilder doNotSpendChange()
Do not spend change outputs.
This effectively adds all the change outputs to the "unspendable" list. See
TxBuilder::unspendable
. This method assumes the presence of an internal keychain, otherwise it has no effect.
-
drainTo
TxBuilder drainTo(Script script)
Sets the address to drain excess coins to.
Usually, when there are excess coins they are sent to a change address generated by the wallet. This option replaces the usual change address with an arbitrary script_pubkey of your choosing. Just as with a change output, if the drain output is not needed (the excess coins are too small) it will not be included in the resulting transaction. The only difference is that it is valid to use
drain_to
without setting any ordinary recipients withadd_recipient
(but it is perfectly fine to add recipients as well).If you choose not to set any recipients, you should provide the utxos that the transaction should spend via
add_utxos
.drain_to
is very useful for draining all the coins in a wallet withdrain_wallet
to a single address.
-
drainWallet
TxBuilder drainWallet()
Spend all the available inputs. This respects filters like
TxBuilder::unspendable
and the change policy.
-
feeAbsolute
TxBuilder feeAbsolute(Amount feeAmount)
Set an absolute fee The
fee_absolute
method refers to the absolute transaction fee inAmount
. If anyone sets both thefee_absolute
method and thefee_rate
method, theFeePolicy
enum will be set by whichever method was called last, as theFeeRate
andFeeAmount
are mutually exclusive.Note that this is really a minimum absolute fee – it’s possible to overshoot it slightly since adding a change output to drain the remaining excess might not be viable.
-
feeRate
TxBuilder feeRate(FeeRate feeRate)
Set a custom fee rate.
This method sets the mining fee paid by the transaction as a rate on its size. This means that the total fee paid is equal to fee_rate times the size of the transaction. Default is 1 sat/vB in accordance with Bitcoin Core’s default relay policy.
Note that this is really a minimum feerate – it’s possible to overshoot it slightly since adding a change output to drain the remaining excess might not be viable.
-
finish
Psbt finish(Wallet wallet)
Finish building the transaction.
Uses the thread-local random number generator (rng).
Returns a new
Psbt
per BIP174.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
.
-
manuallySelectedOnly
TxBuilder manuallySelectedOnly()
Only spend utxos added by
TxBuilder::add_utxo
.The wallet will not add additional utxos to the transaction even if they are needed to make the transaction valid.
-
nlocktime
TxBuilder nlocktime(LockTime locktime)
Use a specific nLockTime while creating the transaction.
This can cause conflicts if the wallet’s descriptors contain an "after" (
OP_CLTV
) operator.
-
onlySpendChange
TxBuilder onlySpendChange()
Only spend change outputs.
This effectively adds all the non-change outputs to the "unspendable" list. See
TxBuilder::unspendable
. This method assumes the presence of an internal keychain, otherwise it has no effect.
-
policyPath
TxBuilder policyPath(Map<String, List<ULong>> policyPath, KeychainKind keychain)
The TxBuilder::policy_path is a complex API. See the Rust docs for complete information: https://docs.rs/bdk_wallet/latest/bdk_wallet/struct.TxBuilder.html#method.policy_path
-
setExactSequence
TxBuilder setExactSequence(UInt nsequence)
Set an exact
nSequence
value.This can cause conflicts if the wallet’s descriptors contain an "older" (
OP_CSV
) operator and the givennsequence
is lower than the CSV value.
-
setRecipients
TxBuilder setRecipients(List<ScriptAmount> recipients)
Replace the recipients already added with a new list of recipients.
-
unspendable
TxBuilder unspendable(List<OutPoint> unspendable)
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.
-
-