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 * A trait that describes a wallet capable of creating a spending [`Transaction`] from a set of
12 * [`SpendableOutputDescriptor`]s.
14 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
15 public class OutputSpender extends CommonBase {
16 final bindings.LDKOutputSpender bindings_instance;
17 OutputSpender(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
18 private OutputSpender(bindings.LDKOutputSpender arg) {
19 super(bindings.LDKOutputSpender_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.OutputSpender_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.OutputSpender_free(ptr); }
40 public static interface OutputSpenderInterface {
42 * Creates a [`Transaction`] which spends the given descriptors to the given outputs, plus an
43 * output to the given change destination (if sufficient change value remains). The
44 * transaction will have a feerate, at least, of the given value.
46 * The `locktime` argument is used to set the transaction's locktime. If `None`, the
47 * transaction will have a locktime of 0. It it recommended to set this to the current block
48 * height to avoid fee sniping, unless you have some specific reason to use a different
51 * Returns `Err(())` if the output value is greater than the input value minus required fee,
52 * if a descriptor was duplicated, or if an output descriptor `script_pubkey`
53 * does not match the one we can spend.
55 Result_TransactionNoneZ spend_spendable_outputs(SpendableOutputDescriptor[] descriptors, TxOut[] outputs, byte[] change_destination_script, int feerate_sat_per_1000_weight, Option_u32Z locktime);
57 private static class LDKOutputSpenderHolder { OutputSpender held; }
58 public static OutputSpender new_impl(OutputSpenderInterface arg) {
59 final LDKOutputSpenderHolder impl_holder = new LDKOutputSpenderHolder();
60 impl_holder.held = new OutputSpender(new bindings.LDKOutputSpender() {
61 @Override public long spend_spendable_outputs(long[] descriptors, long[] outputs, byte[] change_destination_script, int feerate_sat_per_1000_weight, long locktime) {
62 int descriptors_conv_27_len = descriptors.length;
63 SpendableOutputDescriptor[] descriptors_conv_27_arr = new SpendableOutputDescriptor[descriptors_conv_27_len];
64 for (int b = 0; b < descriptors_conv_27_len; b++) {
65 long descriptors_conv_27 = descriptors[b];
66 org.ldk.structs.SpendableOutputDescriptor descriptors_conv_27_hu_conv = org.ldk.structs.SpendableOutputDescriptor.constr_from_ptr(descriptors_conv_27);
67 if (descriptors_conv_27_hu_conv != null) { descriptors_conv_27_hu_conv.ptrs_to.add(this); };
68 descriptors_conv_27_arr[b] = descriptors_conv_27_hu_conv;
70 int outputs_conv_7_len = outputs.length;
71 TxOut[] outputs_conv_7_arr = new TxOut[outputs_conv_7_len];
72 for (int h = 0; h < outputs_conv_7_len; h++) {
73 long outputs_conv_7 = outputs[h];
74 TxOut outputs_conv_7_conv = new TxOut(null, outputs_conv_7);
75 outputs_conv_7_arr[h] = outputs_conv_7_conv;
77 org.ldk.structs.Option_u32Z locktime_hu_conv = org.ldk.structs.Option_u32Z.constr_from_ptr(locktime);
78 if (locktime_hu_conv != null) { locktime_hu_conv.ptrs_to.add(this); };
79 Result_TransactionNoneZ ret = arg.spend_spendable_outputs(descriptors_conv_27_arr, outputs_conv_7_arr, change_destination_script, feerate_sat_per_1000_weight, locktime_hu_conv);
80 Reference.reachabilityFence(arg);
81 long result = ret.clone_ptr();
85 return impl_holder.held;
88 * Creates a [`Transaction`] which spends the given descriptors to the given outputs, plus an
89 * output to the given change destination (if sufficient change value remains). The
90 * transaction will have a feerate, at least, of the given value.
92 * The `locktime` argument is used to set the transaction's locktime. If `None`, the
93 * transaction will have a locktime of 0. It it recommended to set this to the current block
94 * height to avoid fee sniping, unless you have some specific reason to use a different
97 * Returns `Err(())` if the output value is greater than the input value minus required fee,
98 * if a descriptor was duplicated, or if an output descriptor `script_pubkey`
99 * does not match the one we can spend.
101 public Result_TransactionNoneZ spend_spendable_outputs(SpendableOutputDescriptor[] descriptors, TxOut[] outputs, byte[] change_destination_script, int feerate_sat_per_1000_weight, org.ldk.structs.Option_u32Z locktime) {
102 long ret = bindings.OutputSpender_spend_spendable_outputs(this.ptr, descriptors != null ? Arrays.stream(descriptors).mapToLong(descriptors_conv_27 -> descriptors_conv_27.ptr).toArray() : null, outputs != null ? Arrays.stream(outputs).mapToLong(outputs_conv_7 -> outputs_conv_7.ptr).toArray() : null, change_destination_script, feerate_sat_per_1000_weight, locktime.ptr);
103 Reference.reachabilityFence(this);
104 Reference.reachabilityFence(descriptors);
105 Reference.reachabilityFence(outputs);
106 Reference.reachabilityFence(change_destination_script);
107 Reference.reachabilityFence(feerate_sat_per_1000_weight);
108 Reference.reachabilityFence(locktime);
109 if (ret >= 0 && ret <= 4096) { return null; }
110 Result_TransactionNoneZ ret_hu_conv = Result_TransactionNoneZ.constr_from_ptr(ret);
111 for (SpendableOutputDescriptor descriptors_conv_27: descriptors) { if (this != null) { this.ptrs_to.add(descriptors_conv_27); }; };
112 if (this != null) { this.ptrs_to.add(locktime); };