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 alternative to [`CoinSelectionSource`] that can be implemented and used along [`Wallet`] to
12 * provide a default implementation to [`CoinSelectionSource`].
14 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
15 public class WalletSource extends CommonBase {
16 final bindings.LDKWalletSource bindings_instance;
17 WalletSource(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
18 private WalletSource(bindings.LDKWalletSource arg) {
19 super(bindings.LDKWalletSource_new(arg));
20 this.ptrs_to.add(arg);
21 this.bindings_instance = arg;
23 @Override @SuppressWarnings("deprecation")
24 protected void finalize() throws Throwable {
25 if (ptr != 0) { bindings.WalletSource_free(ptr); } super.finalize();
28 * Destroys the object, freeing associated resources. After this call, any access
29 * to this object may result in a SEGFAULT or worse.
31 * You should generally NEVER call this method. You should let the garbage collector
32 * do this for you when it finalizes objects. However, it may be useful for types
33 * which represent locks and should be closed immediately to avoid holding locks
36 public void destroy() {
37 if (ptr != 0) { bindings.WalletSource_free(ptr); }
40 public static interface WalletSourceInterface {
42 * Returns all UTXOs, with at least 1 confirmation each, that are available to spend.
44 Result_CVec_UtxoZNoneZ list_confirmed_utxos();
46 * Returns a script to use for change above dust resulting from a successful coin selection
49 Result_CVec_u8ZNoneZ get_change_script();
51 * Signs and provides the full [`TxIn::script_sig`] and [`TxIn::witness`] for all inputs within
52 * the transaction known to the wallet (i.e., any provided via
53 * [`WalletSource::list_confirmed_utxos`]).
55 Result_TransactionNoneZ sign_tx(byte[] tx);
57 private static class LDKWalletSourceHolder { WalletSource held; }
58 public static WalletSource new_impl(WalletSourceInterface arg) {
59 final LDKWalletSourceHolder impl_holder = new LDKWalletSourceHolder();
60 impl_holder.held = new WalletSource(new bindings.LDKWalletSource() {
61 @Override public long list_confirmed_utxos() {
62 Result_CVec_UtxoZNoneZ ret = arg.list_confirmed_utxos();
63 Reference.reachabilityFence(arg);
64 long result = ret == null ? 0 : ret.clone_ptr();
67 @Override public long get_change_script() {
68 Result_CVec_u8ZNoneZ ret = arg.get_change_script();
69 Reference.reachabilityFence(arg);
70 long result = ret == null ? 0 : ret.clone_ptr();
73 @Override public long sign_tx(byte[] tx) {
74 Result_TransactionNoneZ ret = arg.sign_tx(tx);
75 Reference.reachabilityFence(arg);
76 long result = ret == null ? 0 : ret.clone_ptr();
80 return impl_holder.held;
83 * Returns all UTXOs, with at least 1 confirmation each, that are available to spend.
85 public Result_CVec_UtxoZNoneZ list_confirmed_utxos() {
86 long ret = bindings.WalletSource_list_confirmed_utxos(this.ptr);
87 Reference.reachabilityFence(this);
88 if (ret >= 0 && ret <= 4096) { return null; }
89 Result_CVec_UtxoZNoneZ ret_hu_conv = Result_CVec_UtxoZNoneZ.constr_from_ptr(ret);
94 * Returns a script to use for change above dust resulting from a successful coin selection
97 public Result_CVec_u8ZNoneZ get_change_script() {
98 long ret = bindings.WalletSource_get_change_script(this.ptr);
99 Reference.reachabilityFence(this);
100 if (ret >= 0 && ret <= 4096) { return null; }
101 Result_CVec_u8ZNoneZ ret_hu_conv = Result_CVec_u8ZNoneZ.constr_from_ptr(ret);
106 * Signs and provides the full [`TxIn::script_sig`] and [`TxIn::witness`] for all inputs within
107 * the transaction known to the wallet (i.e., any provided via
108 * [`WalletSource::list_confirmed_utxos`]).
110 public Result_TransactionNoneZ sign_tx(byte[] tx) {
111 long ret = bindings.WalletSource_sign_tx(this.ptr, tx);
112 Reference.reachabilityFence(this);
113 Reference.reachabilityFence(tx);
114 if (ret >= 0 && ret <= 4096) { return null; }
115 Result_TransactionNoneZ ret_hu_conv = Result_TransactionNoneZ.constr_from_ptr(ret);