7 namespace org { namespace ldk { namespace structs {
11 /** An implementation of CoinSelectionSource */
12 public interface CoinSelectionSourceInterface {
13 /**Performs coin selection of a set of UTXOs, with at least 1 confirmation each, that are
14 * available to spend. Implementations are free to pick their coin selection algorithm of
15 * choice, as long as the following requirements are met:
17 * 1. `must_spend` contains a set of [`Input`]s that must be included in the transaction
18 * throughout coin selection, but must not be returned as part of the result.
19 * 2. `must_pay_to` contains a set of [`TxOut`]s that must be included in the transaction
20 * throughout coin selection. In some cases, like when funding an anchor transaction, this
21 * set is empty. Implementations should ensure they handle this correctly on their end,
22 * e.g., Bitcoin Core's `fundrawtransaction` RPC requires at least one output to be
23 * provided, in which case a zero-value empty OP_RETURN output can be used instead.
24 * 3. Enough inputs must be selected/contributed for the resulting transaction (including the
25 * inputs and outputs noted above) to meet `target_feerate_sat_per_1000_weight`.
27 * Implementations must take note that [`Input::satisfaction_weight`] only tracks the weight of
28 * the input's `script_sig` and `witness`. Some wallets, like Bitcoin Core's, may require
29 * providing the full input weight. Failing to do so may lead to underestimating fee bumps and
30 * delaying block inclusion.
32 * The `claim_id` must map to the set of external UTXOs assigned to the claim, such that they
33 * can be re-used within new fee-bumped iterations of the original claiming transaction,
34 * ensuring that claims don't double spend each other. If a specific `claim_id` has never had a
35 * transaction associated with it, and all of the available UTXOs have already been assigned to
36 * other claims, implementations must be willing to double spend their UTXOs. The choice of
37 * which UTXOs to double spend is left to the implementation, but it must strive to keep the
38 * set of other claims being double spent to a minimum.
40 Result_CoinSelectionNoneZ select_confirmed_utxos(byte[] claim_id, Input[] must_spend, TxOut[] must_pay_to, int target_feerate_sat_per_1000_weight);
41 /**Signs and provides the full witness for all inputs within the transaction known to the
42 * trait (i.e., any provided via [`CoinSelectionSource::select_confirmed_utxos`]).
44 * If your wallet does not support signing PSBTs you can call `psbt.extract_tx()` to get the
45 * unsigned transaction and then sign it with your wallet.
47 Result_TransactionNoneZ sign_psbt(byte[] psbt);
51 * An abstraction over a bitcoin wallet that can perform coin selection over a set of UTXOs and can
52 * sign for them. The coin selection method aims to mimic Bitcoin Core's `fundrawtransaction` RPC,
53 * which most wallets should be able to satisfy. Otherwise, consider implementing [`WalletSource`],
54 * which can provide a default implementation of this trait when used with [`Wallet`].
56 public class CoinSelectionSource : CommonBase {
57 internal bindings.LDKCoinSelectionSource bindings_instance;
58 internal long instance_idx;
60 internal CoinSelectionSource(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
61 ~CoinSelectionSource() {
62 if (ptr != 0) { bindings.CoinSelectionSource_free(ptr); }
65 private class LDKCoinSelectionSourceHolder { internal CoinSelectionSource held; }
66 private class LDKCoinSelectionSourceImpl : bindings.LDKCoinSelectionSource {
67 internal LDKCoinSelectionSourceImpl(CoinSelectionSourceInterface arg, LDKCoinSelectionSourceHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
68 private CoinSelectionSourceInterface arg;
69 private LDKCoinSelectionSourceHolder impl_holder;
70 public long select_confirmed_utxos(long _claim_id, long _must_spend, long _must_pay_to, int _target_feerate_sat_per_1000_weight) {
71 byte[] _claim_id_conv = InternalUtils.decodeUint8Array(_claim_id);
72 int _must_spend_conv_7_len = InternalUtils.getArrayLength(_must_spend);
73 Input[] _must_spend_conv_7_arr = new Input[_must_spend_conv_7_len];
74 for (int h = 0; h < _must_spend_conv_7_len; h++) {
75 long _must_spend_conv_7 = InternalUtils.getU64ArrayElem(_must_spend, h);
76 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); }
77 if (_must_spend_conv_7_hu_conv != null) { _must_spend_conv_7_hu_conv.ptrs_to.AddLast(this); };
78 _must_spend_conv_7_arr[h] = _must_spend_conv_7_hu_conv;
80 bindings.free_buffer(_must_spend);
81 int _must_pay_to_conv_7_len = InternalUtils.getArrayLength(_must_pay_to);
82 TxOut[] _must_pay_to_conv_7_arr = new TxOut[_must_pay_to_conv_7_len];
83 for (int h = 0; h < _must_pay_to_conv_7_len; h++) {
84 long _must_pay_to_conv_7 = InternalUtils.getU64ArrayElem(_must_pay_to, h);
85 TxOut _must_pay_to_conv_7_conv = new TxOut(null, _must_pay_to_conv_7);
86 _must_pay_to_conv_7_arr[h] = _must_pay_to_conv_7_conv;
88 bindings.free_buffer(_must_pay_to);
89 Result_CoinSelectionNoneZ ret = arg.select_confirmed_utxos(_claim_id_conv, _must_spend_conv_7_arr, _must_pay_to_conv_7_arr, _target_feerate_sat_per_1000_weight);
91 long result = ret == null ? 0 : ret.clone_ptr();
94 public long sign_psbt(long _psbt) {
95 byte[] _psbt_conv = InternalUtils.decodeUint8Array(_psbt);
96 Result_TransactionNoneZ ret = arg.sign_psbt(_psbt_conv);
98 long result = ret == null ? 0 : ret.clone_ptr();
103 /** Creates a new instance of CoinSelectionSource from a given implementation */
104 public static CoinSelectionSource new_impl(CoinSelectionSourceInterface arg) {
105 LDKCoinSelectionSourceHolder impl_holder = new LDKCoinSelectionSourceHolder();
106 LDKCoinSelectionSourceImpl impl = new LDKCoinSelectionSourceImpl(arg, impl_holder);
107 long[] ptr_idx = bindings.LDKCoinSelectionSource_new(impl);
109 impl_holder.held = new CoinSelectionSource(null, ptr_idx[0]);
110 impl_holder.held.instance_idx = ptr_idx[1];
111 impl_holder.held.bindings_instance = impl;
112 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.encodeUint8Array(InternalUtils.check_arr_len(claim_id, 32)), InternalUtils.encodeUint64Array(InternalUtils.mapArray(must_spend, must_spend_conv_7 => must_spend_conv_7 == null ? 0 : must_spend_conv_7.ptr)), InternalUtils.encodeUint64Array(InternalUtils.mapArray(must_pay_to, must_pay_to_conv_7 => must_pay_to_conv_7.ptr)), target_feerate_sat_per_1000_weight);
146 GC.KeepAlive(claim_id);
147 GC.KeepAlive(must_spend);
148 GC.KeepAlive(must_pay_to);
149 GC.KeepAlive(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 foreach (Input must_spend_conv_7 in must_spend) { if (this != null) { this.ptrs_to.AddLast(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, InternalUtils.encodeUint8Array(psbt));
167 if (ret >= 0 && ret <= 4096) { return null; }
168 Result_TransactionNoneZ ret_hu_conv = Result_TransactionNoneZ.constr_from_ptr(ret);