1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
11 * An abstraction over a bitcoin wallet that can perform coin selection over a set of UTXOs and can
12 * sign for them. The coin selection method aims to mimic Bitcoin Core's `fundrawtransaction` RPC,
13 * which most wallets should be able to satisfy. Otherwise, consider implementing [`WalletSource`],
14 * which can provide a default implementation of this trait when used with [`Wallet`].
16 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
17 public class CoinSelectionSource extends CommonBase {
18 final bindings.LDKCoinSelectionSource bindings_instance;
19 CoinSelectionSource(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
20 private CoinSelectionSource(bindings.LDKCoinSelectionSource arg) {
21 super(bindings.LDKCoinSelectionSource_new(arg));
22 this.ptrs_to.add(arg);
23 this.bindings_instance = arg;
25 @Override @SuppressWarnings("deprecation")
26 protected void finalize() throws Throwable {
27 if (ptr != 0) { bindings.CoinSelectionSource_free(ptr); } super.finalize();
30 * Destroys the object, freeing associated resources. After this call, any access
31 * to this object may result in a SEGFAULT or worse.
33 * You should generally NEVER call this method. You should let the garbage collector
34 * do this for you when it finalizes objects. However, it may be useful for types
35 * which represent locks and should be closed immediately to avoid holding locks
38 public void destroy() {
39 if (ptr != 0) { bindings.CoinSelectionSource_free(ptr); }
42 public static interface CoinSelectionSourceInterface {
44 * Performs coin selection of a set of UTXOs, with at least 1 confirmation each, that are
45 * available to spend. Implementations are free to pick their coin selection algorithm of
46 * choice, as long as the following requirements are met:
48 * 1. `must_spend` contains a set of [`Input`]s that must be included in the transaction
49 * throughout coin selection, but must not be returned as part of the result.
50 * 2. `must_pay_to` contains a set of [`TxOut`]s that must be included in the transaction
51 * throughout coin selection. In some cases, like when funding an anchor transaction, this
52 * set is empty. Implementations should ensure they handle this correctly on their end,
53 * e.g., Bitcoin Core's `fundrawtransaction` RPC requires at least one output to be
54 * provided, in which case a zero-value empty OP_RETURN output can be used instead.
55 * 3. Enough inputs must be selected/contributed for the resulting transaction (including the
56 * inputs and outputs noted above) to meet `target_feerate_sat_per_1000_weight`.
58 * Implementations must take note that [`Input::satisfaction_weight`] only tracks the weight of
59 * the input's `script_sig` and `witness`. Some wallets, like Bitcoin Core's, may require
60 * providing the full input weight. Failing to do so may lead to underestimating fee bumps and
61 * delaying block inclusion.
63 * The `claim_id` must map to the set of external UTXOs assigned to the claim, such that they
64 * can be re-used within new fee-bumped iterations of the original claiming transaction,
65 * ensuring that claims don't double spend each other. If a specific `claim_id` has never had a
66 * transaction associated with it, and all of the available UTXOs have already been assigned to
67 * other claims, implementations must be willing to double spend their UTXOs. The choice of
68 * which UTXOs to double spend is left to the implementation, but it must strive to keep the
69 * set of other claims being double spent to a minimum.
71 Result_CoinSelectionNoneZ select_confirmed_utxos(byte[] claim_id, Input[] must_spend, TxOut[] must_pay_to, int target_feerate_sat_per_1000_weight);
73 * Signs and provides the full witness for all inputs within the transaction known to the
74 * trait (i.e., any provided via [`CoinSelectionSource::select_confirmed_utxos`]).
76 * If your wallet does not support signing PSBTs you can call `psbt.extract_tx()` to get the
77 * unsigned transaction and then sign it with your wallet.
79 Result_TransactionNoneZ sign_psbt(byte[] psbt);
81 private static class LDKCoinSelectionSourceHolder { CoinSelectionSource held; }
82 public static CoinSelectionSource new_impl(CoinSelectionSourceInterface arg) {
83 final LDKCoinSelectionSourceHolder impl_holder = new LDKCoinSelectionSourceHolder();
84 impl_holder.held = new CoinSelectionSource(new bindings.LDKCoinSelectionSource() {
85 @Override public long select_confirmed_utxos(byte[] claim_id, long[] must_spend, long[] must_pay_to, int target_feerate_sat_per_1000_weight) {
86 int must_spend_conv_7_len = must_spend.length;
87 Input[] must_spend_conv_7_arr = new Input[must_spend_conv_7_len];
88 for (int h = 0; h < must_spend_conv_7_len; h++) {
89 long must_spend_conv_7 = must_spend[h];
90 org.ldk.structs.Input must_spend_conv_7_hu_conv = null; if (must_spend_conv_7 < 0 || must_spend_conv_7 > 4096) { must_spend_conv_7_hu_conv = new org.ldk.structs.Input(null, must_spend_conv_7); }
91 if (must_spend_conv_7_hu_conv != null) { must_spend_conv_7_hu_conv.ptrs_to.add(this); };
92 must_spend_conv_7_arr[h] = must_spend_conv_7_hu_conv;
94 int must_pay_to_conv_7_len = must_pay_to.length;
95 TxOut[] must_pay_to_conv_7_arr = new TxOut[must_pay_to_conv_7_len];
96 for (int h = 0; h < must_pay_to_conv_7_len; h++) {
97 long must_pay_to_conv_7 = must_pay_to[h];
98 TxOut must_pay_to_conv_7_conv = new TxOut(null, must_pay_to_conv_7);
99 must_pay_to_conv_7_arr[h] = must_pay_to_conv_7_conv;
101 Result_CoinSelectionNoneZ ret = arg.select_confirmed_utxos(claim_id, must_spend_conv_7_arr, must_pay_to_conv_7_arr, target_feerate_sat_per_1000_weight);
102 Reference.reachabilityFence(arg);
103 long result = ret == null ? 0 : ret.clone_ptr();
106 @Override public long sign_psbt(byte[] psbt) {
107 Result_TransactionNoneZ ret = arg.sign_psbt(psbt);
108 Reference.reachabilityFence(arg);
109 long result = ret == null ? 0 : ret.clone_ptr();
113 return impl_holder.held;
116 * Performs coin selection of a set of UTXOs, with at least 1 confirmation each, that are
117 * available to spend. Implementations are free to pick their coin selection algorithm of
118 * choice, as long as the following requirements are met:
120 * 1. `must_spend` contains a set of [`Input`]s that must be included in the transaction
121 * throughout coin selection, but must not be returned as part of the result.
122 * 2. `must_pay_to` contains a set of [`TxOut`]s that must be included in the transaction
123 * throughout coin selection. In some cases, like when funding an anchor transaction, this
124 * set is empty. Implementations should ensure they handle this correctly on their end,
125 * e.g., Bitcoin Core's `fundrawtransaction` RPC requires at least one output to be
126 * provided, in which case a zero-value empty OP_RETURN output can be used instead.
127 * 3. Enough inputs must be selected/contributed for the resulting transaction (including the
128 * inputs and outputs noted above) to meet `target_feerate_sat_per_1000_weight`.
130 * Implementations must take note that [`Input::satisfaction_weight`] only tracks the weight of
131 * the input's `script_sig` and `witness`. Some wallets, like Bitcoin Core's, may require
132 * providing the full input weight. Failing to do so may lead to underestimating fee bumps and
133 * delaying block inclusion.
135 * The `claim_id` must map to the set of external UTXOs assigned to the claim, such that they
136 * can be re-used within new fee-bumped iterations of the original claiming transaction,
137 * ensuring that claims don't double spend each other. If a specific `claim_id` has never had a
138 * transaction associated with it, and all of the available UTXOs have already been assigned to
139 * other claims, implementations must be willing to double spend their UTXOs. The choice of
140 * which UTXOs to double spend is left to the implementation, but it must strive to keep the
141 * set of other claims being double spent to a minimum.
143 public Result_CoinSelectionNoneZ select_confirmed_utxos(byte[] claim_id, Input[] must_spend, TxOut[] must_pay_to, int target_feerate_sat_per_1000_weight) {
144 long ret = bindings.CoinSelectionSource_select_confirmed_utxos(this.ptr, InternalUtils.check_arr_len(claim_id, 32), must_spend != null ? Arrays.stream(must_spend).mapToLong(must_spend_conv_7 -> must_spend_conv_7 == null ? 0 : must_spend_conv_7.ptr).toArray() : null, must_pay_to != null ? Arrays.stream(must_pay_to).mapToLong(must_pay_to_conv_7 -> must_pay_to_conv_7.ptr).toArray() : null, target_feerate_sat_per_1000_weight);
145 Reference.reachabilityFence(this);
146 Reference.reachabilityFence(claim_id);
147 Reference.reachabilityFence(must_spend);
148 Reference.reachabilityFence(must_pay_to);
149 Reference.reachabilityFence(target_feerate_sat_per_1000_weight);
150 if (ret >= 0 && ret <= 4096) { return null; }
151 Result_CoinSelectionNoneZ ret_hu_conv = Result_CoinSelectionNoneZ.constr_from_ptr(ret);
152 for (Input must_spend_conv_7: must_spend) { if (this != null) { this.ptrs_to.add(must_spend_conv_7); }; };
157 * Signs and provides the full witness for all inputs within the transaction known to the
158 * trait (i.e., any provided via [`CoinSelectionSource::select_confirmed_utxos`]).
160 * If your wallet does not support signing PSBTs you can call `psbt.extract_tx()` to get the
161 * unsigned transaction and then sign it with your wallet.
163 public Result_TransactionNoneZ sign_psbt(byte[] psbt) {
164 long ret = bindings.CoinSelectionSource_sign_psbt(this.ptr, psbt);
165 Reference.reachabilityFence(this);
166 Reference.reachabilityFence(psbt);
167 if (ret >= 0 && ret <= 4096) { return null; }
168 Result_TransactionNoneZ ret_hu_conv = Result_TransactionNoneZ.constr_from_ptr(ret);