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 Result_TransactionNoneZ sign_tx(byte[] tx);
48 * An abstraction over a bitcoin wallet that can perform coin selection over a set of UTXOs and can
49 * sign for them. The coin selection method aims to mimic Bitcoin Core's `fundrawtransaction` RPC,
50 * which most wallets should be able to satisfy. Otherwise, consider implementing [`WalletSource`],
51 * which can provide a default implementation of this trait when used with [`Wallet`].
53 public class CoinSelectionSource : CommonBase {
54 internal bindings.LDKCoinSelectionSource bindings_instance;
55 internal long instance_idx;
57 internal CoinSelectionSource(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
58 ~CoinSelectionSource() {
59 if (ptr != 0) { bindings.CoinSelectionSource_free(ptr); }
62 private class LDKCoinSelectionSourceHolder { internal CoinSelectionSource held; }
63 private class LDKCoinSelectionSourceImpl : bindings.LDKCoinSelectionSource {
64 internal LDKCoinSelectionSourceImpl(CoinSelectionSourceInterface arg, LDKCoinSelectionSourceHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
65 private CoinSelectionSourceInterface arg;
66 private LDKCoinSelectionSourceHolder impl_holder;
67 public long select_confirmed_utxos(long _claim_id, long _must_spend, long _must_pay_to, int _target_feerate_sat_per_1000_weight) {
68 byte[] _claim_id_conv = InternalUtils.decodeUint8Array(_claim_id);
69 int _must_spend_conv_7_len = InternalUtils.getArrayLength(_must_spend);
70 Input[] _must_spend_conv_7_arr = new Input[_must_spend_conv_7_len];
71 for (int h = 0; h < _must_spend_conv_7_len; h++) {
72 long _must_spend_conv_7 = InternalUtils.getU64ArrayElem(_must_spend, h);
73 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); }
74 if (_must_spend_conv_7_hu_conv != null) { _must_spend_conv_7_hu_conv.ptrs_to.AddLast(this); };
75 _must_spend_conv_7_arr[h] = _must_spend_conv_7_hu_conv;
77 bindings.free_buffer(_must_spend);
78 int _must_pay_to_conv_7_len = InternalUtils.getArrayLength(_must_pay_to);
79 TxOut[] _must_pay_to_conv_7_arr = new TxOut[_must_pay_to_conv_7_len];
80 for (int h = 0; h < _must_pay_to_conv_7_len; h++) {
81 long _must_pay_to_conv_7 = InternalUtils.getU64ArrayElem(_must_pay_to, h);
82 TxOut _must_pay_to_conv_7_conv = new TxOut(null, _must_pay_to_conv_7);
83 _must_pay_to_conv_7_arr[h] = _must_pay_to_conv_7_conv;
85 bindings.free_buffer(_must_pay_to);
86 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);
88 long result = ret == null ? 0 : ret.clone_ptr();
91 public long sign_tx(long _tx) {
92 byte[] _tx_conv = InternalUtils.decodeUint8Array(_tx);
93 Result_TransactionNoneZ ret = arg.sign_tx(_tx_conv);
95 long result = ret == null ? 0 : ret.clone_ptr();
100 /** Creates a new instance of CoinSelectionSource from a given implementation */
101 public static CoinSelectionSource new_impl(CoinSelectionSourceInterface arg) {
102 LDKCoinSelectionSourceHolder impl_holder = new LDKCoinSelectionSourceHolder();
103 LDKCoinSelectionSourceImpl impl = new LDKCoinSelectionSourceImpl(arg, impl_holder);
104 long[] ptr_idx = bindings.LDKCoinSelectionSource_new(impl);
106 impl_holder.held = new CoinSelectionSource(null, ptr_idx[0]);
107 impl_holder.held.instance_idx = ptr_idx[1];
108 impl_holder.held.bindings_instance = impl;
109 return impl_holder.held;
113 * Performs coin selection of a set of UTXOs, with at least 1 confirmation each, that are
114 * available to spend. Implementations are free to pick their coin selection algorithm of
115 * choice, as long as the following requirements are met:
117 * 1. `must_spend` contains a set of [`Input`]s that must be included in the transaction
118 * throughout coin selection, but must not be returned as part of the result.
119 * 2. `must_pay_to` contains a set of [`TxOut`]s that must be included in the transaction
120 * throughout coin selection. In some cases, like when funding an anchor transaction, this
121 * set is empty. Implementations should ensure they handle this correctly on their end,
122 * e.g., Bitcoin Core's `fundrawtransaction` RPC requires at least one output to be
123 * provided, in which case a zero-value empty OP_RETURN output can be used instead.
124 * 3. Enough inputs must be selected/contributed for the resulting transaction (including the
125 * inputs and outputs noted above) to meet `target_feerate_sat_per_1000_weight`.
127 * Implementations must take note that [`Input::satisfaction_weight`] only tracks the weight of
128 * the input's `script_sig` and `witness`. Some wallets, like Bitcoin Core's, may require
129 * providing the full input weight. Failing to do so may lead to underestimating fee bumps and
130 * delaying block inclusion.
132 * The `claim_id` must map to the set of external UTXOs assigned to the claim, such that they
133 * can be re-used within new fee-bumped iterations of the original claiming transaction,
134 * ensuring that claims don't double spend each other. If a specific `claim_id` has never had a
135 * transaction associated with it, and all of the available UTXOs have already been assigned to
136 * other claims, implementations must be willing to double spend their UTXOs. The choice of
137 * which UTXOs to double spend is left to the implementation, but it must strive to keep the
138 * set of other claims being double spent to a minimum.
140 public Result_CoinSelectionNoneZ select_confirmed_utxos(byte[] claim_id, Input[] must_spend, TxOut[] must_pay_to, int target_feerate_sat_per_1000_weight) {
141 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);
143 GC.KeepAlive(claim_id);
144 GC.KeepAlive(must_spend);
145 GC.KeepAlive(must_pay_to);
146 GC.KeepAlive(target_feerate_sat_per_1000_weight);
147 if (ret >= 0 && ret <= 4096) { return null; }
148 Result_CoinSelectionNoneZ ret_hu_conv = Result_CoinSelectionNoneZ.constr_from_ptr(ret);
149 foreach (Input must_spend_conv_7 in must_spend) { if (this != null) { this.ptrs_to.AddLast(must_spend_conv_7); }; };
154 * Signs and provides the full witness for all inputs within the transaction known to the
155 * trait (i.e., any provided via [`CoinSelectionSource::select_confirmed_utxos`]).
157 public Result_TransactionNoneZ sign_tx(byte[] tx) {
158 long ret = bindings.CoinSelectionSource_sign_tx(this.ptr, InternalUtils.encodeUint8Array(tx));
161 if (ret >= 0 && ret <= 4096) { return null; }
162 Result_TransactionNoneZ ret_hu_conv = Result_TransactionNoneZ.constr_from_ptr(ret);