Class TxBuilder
-
- All Implemented Interfaces:
-
java.lang.AutoCloseable,org.bitcoindevkit.Disposable,org.bitcoindevkit.TxBuilderInterface
public class TxBuilder implements Disposable, AutoCloseable, TxBuilderInterface
A
TxBuilderis created by callingbuild_txon a wallet. After assigning it, you set options on it until finally callingfinishto consume the builder and generate the transaction.
-
-
Method Summary
Modifier and Type Method Description Unitdestroy()Unitclose()final PointeruniffiClonePointer()TxBuilderaddData(ByteArray data)Add data as an output using OP_RETURN.TxBuilderaddGlobalXpubs()Fill-in the PSBT_GLOBAL_XPUBfield with the extended keys contained in both the external and internal descriptors.TxBuilderaddRecipient(Script script, Amount amount)Add a recipient to the internal list of recipients. TxBuilderaddUnspendable(OutPoint unspendable)Add a utxo to the internal list of unspendable utxos. TxBuilderaddUtxo(OutPoint outpoint)Add a utxo to the internal list of utxos that must be spent. TxBuilderaddUtxos(List<OutPoint> outpoints)Add the list of outpoints to the internal list of UTXOs that must be spent. TxBuilderallowDust(Boolean allowDust)Set whether or not the dust limit is checked. TxBuilderchangePolicy(ChangeSpendPolicy changePolicy)Set a specific ChangeSpendPolicy.TxBuildercurrentHeight(UInt height)Set the current blockchain height. TxBuilderdoNotSpendChange()Do not spend change outputs. TxBuilderdrainTo(Script script)Sets the address to drain excess coins to. TxBuilderdrainWallet()Spend all the available inputs. TxBuilderfeeAbsolute(Amount feeAmount)Set an absolute fee The fee_absolutemethod refers to the absolute transaction fee inAmount.TxBuilderfeeRate(FeeRate feeRate)Set a custom fee rate. Psbtfinish(Wallet wallet)Finish building the transaction. TxBuildermanuallySelectedOnly()Only spend utxos added by TxBuilder::add_utxo.TxBuildernlocktime(LockTime locktime)Use a specific nLockTime while creating the transaction. TxBuilderonlySpendChange()Only spend change outputs. TxBuilderpolicyPath(Map<String, List<ULong>> policyPath, KeychainKind keychain)The TxBuilder::policy_path is a complex API. TxBuildersetExactSequence(UInt nsequence)Set an exact nSequencevalue.TxBuildersetRecipients(List<ScriptAmount> recipients)Replace the recipients already added with a new list of recipients. TxBuilderunspendable(List<OutPoint> unspendable)Replace the internal list of unspendable utxos with a new list. TxBuilderversion(Integer version)Build a transaction with a specific version. -
-
Method Detail
-
uniffiClonePointer
final Pointer uniffiClonePointer()
-
addGlobalXpubs
TxBuilder addGlobalXpubs()
Fill-in the
PSBT_GLOBAL_XPUBfield 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_utxohave 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_changeandTxBuilder::only_spend_changefor 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
nLockTimefor preventing fee sniping. Note: This will be ignored if you manually specify anlocktimeusingTxBuilder::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_towithout 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_tois very useful for draining all the coins in a wallet withdrain_walletto a single address.
-
drainWallet
TxBuilder drainWallet()
Spend all the available inputs. This respects filters like
TxBuilder::unspendableand the change policy.
-
feeAbsolute
TxBuilder feeAbsolute(Amount feeAmount)
Set an absolute fee The
fee_absolutemethod refers to the absolute transaction fee inAmount. If anyone sets both thefee_absolutemethod and thefee_ratemethod, theFeePolicyenum will be set by whichever method was called last, as theFeeRateandFeeAmountare 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
Psbtper 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
nSequencevalue.This can cause conflicts if the wallet’s descriptors contain an "older" (
OP_CSV) operator and the givennsequenceis 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_utxohave priority over these.
-
-
-
-