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 * If your wallet does not support signing PSBTs you can call `psbt.extract_tx()` to get the
56 * unsigned transaction and then sign it with your wallet.
58 Result_TransactionNoneZ sign_psbt(byte[] psbt);
60 private static class LDKWalletSourceHolder { WalletSource held; }
61 public static WalletSource new_impl(WalletSourceInterface arg) {
62 final LDKWalletSourceHolder impl_holder = new LDKWalletSourceHolder();
63 impl_holder.held = new WalletSource(new bindings.LDKWalletSource() {
64 @Override public long list_confirmed_utxos() {
65 Result_CVec_UtxoZNoneZ ret = arg.list_confirmed_utxos();
66 Reference.reachabilityFence(arg);
67 long result = ret == null ? 0 : ret.clone_ptr();
70 @Override public long get_change_script() {
71 Result_CVec_u8ZNoneZ ret = arg.get_change_script();
72 Reference.reachabilityFence(arg);
73 long result = ret == null ? 0 : ret.clone_ptr();
76 @Override public long sign_psbt(byte[] psbt) {
77 Result_TransactionNoneZ ret = arg.sign_psbt(psbt);
78 Reference.reachabilityFence(arg);
79 long result = ret == null ? 0 : ret.clone_ptr();
83 return impl_holder.held;
86 * Returns all UTXOs, with at least 1 confirmation each, that are available to spend.
88 public Result_CVec_UtxoZNoneZ list_confirmed_utxos() {
89 long ret = bindings.WalletSource_list_confirmed_utxos(this.ptr);
90 Reference.reachabilityFence(this);
91 if (ret >= 0 && ret <= 4096) { return null; }
92 Result_CVec_UtxoZNoneZ ret_hu_conv = Result_CVec_UtxoZNoneZ.constr_from_ptr(ret);
97 * Returns a script to use for change above dust resulting from a successful coin selection
100 public Result_CVec_u8ZNoneZ get_change_script() {
101 long ret = bindings.WalletSource_get_change_script(this.ptr);
102 Reference.reachabilityFence(this);
103 if (ret >= 0 && ret <= 4096) { return null; }
104 Result_CVec_u8ZNoneZ ret_hu_conv = Result_CVec_u8ZNoneZ.constr_from_ptr(ret);
109 * Signs and provides the full [`TxIn::script_sig`] and [`TxIn::witness`] for all inputs within
110 * the transaction known to the wallet (i.e., any provided via
111 * [`WalletSource::list_confirmed_utxos`]).
113 * If your wallet does not support signing PSBTs you can call `psbt.extract_tx()` to get the
114 * unsigned transaction and then sign it with your wallet.
116 public Result_TransactionNoneZ sign_psbt(byte[] psbt) {
117 long ret = bindings.WalletSource_sign_psbt(this.ptr, psbt);
118 Reference.reachabilityFence(this);
119 Reference.reachabilityFence(psbt);
120 if (ret >= 0 && ret <= 4096) { return null; }
121 Result_TransactionNoneZ ret_hu_conv = Result_TransactionNoneZ.constr_from_ptr(ret);