Interface TxBuilderInterface
-
- All Implemented Interfaces:
public interface TxBuilderInterfaceA
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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classTxBuilderInterface.Companion
-
Method Summary
Modifier and Type Method Description abstract TxBuilderaddData(ByteArray data)Add data as an output using OP_RETURN.abstract TxBuilderaddGlobalXpubs()Fill-in the PSBT_GLOBAL_XPUBfield with the extended keys contained in both the external and internal descriptors.abstract TxBuilderaddRecipient(Script script, Amount amount)Add a recipient to the internal list of recipients. abstract TxBuilderaddUnspendable(OutPoint unspendable)Add a utxo to the internal list of unspendable utxos. abstract TxBuilderaddUtxo(OutPoint outpoint)Add a utxo to the internal list of utxos that must be spent. abstract TxBuilderaddUtxos(List<OutPoint> outpoints)Add the list of outpoints to the internal list of UTXOs that must be spent. abstract TxBuilderallowDust(Boolean allowDust)Set whether or not the dust limit is checked. abstract TxBuilderchangePolicy(ChangeSpendPolicy changePolicy)Set a specific ChangeSpendPolicy.abstract TxBuildercurrentHeight(UInt height)Set the current blockchain height. abstract TxBuilderdoNotSpendChange()Do not spend change outputs. abstract TxBuilderdrainTo(Script script)Sets the address to drain excess coins to. abstract TxBuilderdrainWallet()Spend all the available inputs. abstract TxBuilderfeeAbsolute(Amount feeAmount)Set an absolute fee The fee_absolutemethod refers to the absolute transaction fee inAmount.abstract TxBuilderfeeRate(FeeRate feeRate)Set a custom fee rate. abstract Psbtfinish(Wallet wallet)Finish building the transaction. abstract TxBuildermanuallySelectedOnly()Only spend utxos added by TxBuilder::add_utxo.abstract TxBuildernlocktime(LockTime locktime)Use a specific nLockTime while creating the transaction. abstract TxBuilderonlySpendChange()Only spend change outputs. abstract TxBuilderpolicyPath(Map<String, List<ULong>> policyPath, KeychainKind keychain)The TxBuilder::policy_path is a complex API. abstract TxBuildersetExactSequence(UInt nsequence)Set an exact nSequencevalue.abstract TxBuildersetRecipients(List<ScriptAmount> recipients)Replace the recipients already added with a new list of recipients. abstract TxBuilderunspendable(List<OutPoint> unspendable)Replace the internal list of unspendable utxos with a new list. abstract TxBuilderversion(Integer version)Build a transaction with a specific version. -
-
Method Detail
-
addGlobalXpubs
abstract 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
abstract TxBuilder addRecipient(Script script, Amount amount)
Add a recipient to the internal list of recipients.
-
addUnspendable
abstract 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
abstract 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
abstract TxBuilder addUtxos(List<OutPoint> outpoints)
Add the list of outpoints to the internal list of UTXOs that must be spent.
-
allowDust
abstract 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
abstract 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
abstract 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
abstract 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
abstract 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
abstract TxBuilder drainWallet()
Spend all the available inputs. This respects filters like
TxBuilder::unspendableand the change policy.
-
feeAbsolute
abstract 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
abstract 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
abstract 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
abstract 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
abstract 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
abstract 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
abstract 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
abstract 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
abstract TxBuilder setRecipients(List<ScriptAmount> recipients)
Replace the recipients already added with a new list of recipients.
-
unspendable
abstract 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.
-
-
-
-