be172c1b2eb310ace7ebff6d55ff056e856f278d
[ldk-java] / src / main / java / org / ldk / structs / CoinSelectionSource.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10 /**
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`].
15  */
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;
24         }
25         @Override @SuppressWarnings("deprecation")
26         protected void finalize() throws Throwable {
27                 if (ptr != 0) { bindings.CoinSelectionSource_free(ptr); } super.finalize();
28         }
29         /**
30          * Destroys the object, freeing associated resources. After this call, any access
31          * to this object may result in a SEGFAULT or worse.
32          *
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
36          * until the GC runs.
37          */
38         public void destroy() {
39                 if (ptr != 0) { bindings.CoinSelectionSource_free(ptr); }
40                 ptr = 0;
41         }
42         public static interface CoinSelectionSourceInterface {
43                 /**
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:
47                  * 
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`.
57                  * 
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.
62                  * 
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.
70                  */
71                 Result_CoinSelectionNoneZ select_confirmed_utxos(byte[] claim_id, Input[] must_spend, TxOut[] must_pay_to, int target_feerate_sat_per_1000_weight);
72                 /**
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`]).
75                  */
76                 Result_TransactionNoneZ sign_tx(byte[] tx);
77         }
78         private static class LDKCoinSelectionSourceHolder { CoinSelectionSource held; }
79         public static CoinSelectionSource new_impl(CoinSelectionSourceInterface arg) {
80                 final LDKCoinSelectionSourceHolder impl_holder = new LDKCoinSelectionSourceHolder();
81                 impl_holder.held = new CoinSelectionSource(new bindings.LDKCoinSelectionSource() {
82                         @Override public long select_confirmed_utxos(byte[] claim_id, long[] must_spend, long[] must_pay_to, int target_feerate_sat_per_1000_weight) {
83                                 int must_spend_conv_7_len = must_spend.length;
84                                 Input[] must_spend_conv_7_arr = new Input[must_spend_conv_7_len];
85                                 for (int h = 0; h < must_spend_conv_7_len; h++) {
86                                         long must_spend_conv_7 = must_spend[h];
87                                         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); }
88                                         if (must_spend_conv_7_hu_conv != null) { must_spend_conv_7_hu_conv.ptrs_to.add(this); };
89                                         must_spend_conv_7_arr[h] = must_spend_conv_7_hu_conv;
90                                 }
91                                 int must_pay_to_conv_7_len = must_pay_to.length;
92                                 TxOut[] must_pay_to_conv_7_arr = new TxOut[must_pay_to_conv_7_len];
93                                 for (int h = 0; h < must_pay_to_conv_7_len; h++) {
94                                         long must_pay_to_conv_7 = must_pay_to[h];
95                                         TxOut must_pay_to_conv_7_conv = new TxOut(null, must_pay_to_conv_7);
96                                         must_pay_to_conv_7_arr[h] = must_pay_to_conv_7_conv;
97                                 }
98                                 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);
99                                 Reference.reachabilityFence(arg);
100                                 long result = ret == null ? 0 : ret.clone_ptr();
101                                 return result;
102                         }
103                         @Override public long sign_tx(byte[] tx) {
104                                 Result_TransactionNoneZ ret = arg.sign_tx(tx);
105                                 Reference.reachabilityFence(arg);
106                                 long result = ret == null ? 0 : ret.clone_ptr();
107                                 return result;
108                         }
109                 });
110                 return impl_holder.held;
111         }
112         /**
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:
116          * 
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`.
126          * 
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.
131          * 
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.
139          */
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.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);
142                 Reference.reachabilityFence(this);
143                 Reference.reachabilityFence(claim_id);
144                 Reference.reachabilityFence(must_spend);
145                 Reference.reachabilityFence(must_pay_to);
146                 Reference.reachabilityFence(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                 for (Input must_spend_conv_7: must_spend) { if (this != null) { this.ptrs_to.add(must_spend_conv_7); }; };
150                 return ret_hu_conv;
151         }
152
153         /**
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`]).
156          */
157         public Result_TransactionNoneZ sign_tx(byte[] tx) {
158                 long ret = bindings.CoinSelectionSource_sign_tx(this.ptr, tx);
159                 Reference.reachabilityFence(this);
160                 Reference.reachabilityFence(tx);
161                 if (ret >= 0 && ret <= 4096) { return null; }
162                 Result_TransactionNoneZ ret_hu_conv = Result_TransactionNoneZ.constr_from_ptr(ret);
163                 return ret_hu_conv;
164         }
165
166 }