Merge pull request #56 from TheBlueMatt/main
[ldk-java] / src / main / java / org / ldk / structs / SpendableOutputDescriptor.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 javax.annotation.Nullable;
8
9
10 /**
11  * When on-chain outputs are created by rust-lightning (which our counterparty is not able to
12  * claim at any point in the future) an event is generated which you must track and be able to
13  * spend on-chain. The information needed to do this is provided in this enum, including the
14  * outpoint describing which txid and output index is available, the full output which exists at
15  * that txid/index, and any keys or other information required to sign.
16  */
17 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
18 public class SpendableOutputDescriptor extends CommonBase {
19         private SpendableOutputDescriptor(Object _dummy, long ptr) { super(ptr); }
20         @Override @SuppressWarnings("deprecation")
21         protected void finalize() throws Throwable {
22                 super.finalize();
23                 if (ptr != 0) { bindings.SpendableOutputDescriptor_free(ptr); }
24         }
25         static SpendableOutputDescriptor constr_from_ptr(long ptr) {
26                 bindings.LDKSpendableOutputDescriptor raw_val = bindings.LDKSpendableOutputDescriptor_ref_from_ptr(ptr);
27                 if (raw_val.getClass() == bindings.LDKSpendableOutputDescriptor.StaticOutput.class) {
28                         return new StaticOutput(ptr, (bindings.LDKSpendableOutputDescriptor.StaticOutput)raw_val);
29                 }
30                 if (raw_val.getClass() == bindings.LDKSpendableOutputDescriptor.DelayedPaymentOutput.class) {
31                         return new DelayedPaymentOutput(ptr, (bindings.LDKSpendableOutputDescriptor.DelayedPaymentOutput)raw_val);
32                 }
33                 if (raw_val.getClass() == bindings.LDKSpendableOutputDescriptor.StaticPaymentOutput.class) {
34                         return new StaticPaymentOutput(ptr, (bindings.LDKSpendableOutputDescriptor.StaticPaymentOutput)raw_val);
35                 }
36                 assert false; return null; // Unreachable without extending the (internal) bindings interface
37         }
38
39         /**
40          * An output to a script which was provided via KeysInterface directly, either from
41          * `get_destination_script()` or `get_shutdown_scriptpubkey()`, thus you should already know
42          * how to spend it. No secret keys are provided as rust-lightning was never given any key.
43          * These may include outputs from a transaction punishing our counterparty or claiming an HTLC
44          * on-chain using the payment preimage or after it has timed out.
45          */
46         public final static class StaticOutput extends SpendableOutputDescriptor {
47                 /**
48                  * The outpoint which is spendable
49                 */
50                 public final OutPoint outpoint;
51                 /**
52                  * The output which is referenced by the given outpoint.
53                 */
54                 public final TxOut output;
55                 private StaticOutput(long ptr, bindings.LDKSpendableOutputDescriptor.StaticOutput obj) {
56                         super(null, ptr);
57                         long outpoint = obj.outpoint;
58                         OutPoint outpoint_hu_conv = null; if (outpoint < 0 || outpoint > 4096) { outpoint_hu_conv = new OutPoint(null, outpoint); }
59                         outpoint_hu_conv.ptrs_to.add(this);
60                         this.outpoint = outpoint_hu_conv;
61                         long output = obj.output;
62                         TxOut output_conv = new TxOut(null, output);
63                         this.output = output_conv;
64                 }
65         }
66         /**
67          * An output to a P2WSH script which can be spent with a single signature after a CSV delay.
68          * 
69          * The witness in the spending input should be:
70          * <BIP 143 signature> <empty vector> (MINIMALIF standard rule) <provided witnessScript>
71          * 
72          * Note that the nSequence field in the spending input must be set to to_self_delay
73          * (which means the transaction is not broadcastable until at least to_self_delay
74          * blocks after the outpoint confirms).
75          * 
76          * These are generally the result of a \"revocable\" output to us, spendable only by us unless
77          * it is an output from an old state which we broadcast (which should never happen).
78          * 
79          * To derive the delayed_payment key which is used to sign for this input, you must pass the
80          * holder delayed_payment_base_key (ie the private key which corresponds to the pubkey in
81          * Sign::pubkeys().delayed_payment_basepoint) and the provided per_commitment_point to
82          * chan_utils::derive_private_key. The public key can be generated without the secret key
83          * using chan_utils::derive_public_key and only the delayed_payment_basepoint which appears in
84          * Sign::pubkeys().
85          * 
86          * To derive the revocation_pubkey provided here (which is used in the witness
87          * script generation), you must pass the counterparty revocation_basepoint (which appears in the
88          * call to Sign::ready_channel) and the provided per_commitment point
89          * to chan_utils::derive_public_revocation_key.
90          * 
91          * The witness script which is hashed and included in the output script_pubkey may be
92          * regenerated by passing the revocation_pubkey (derived as above), our delayed_payment pubkey
93          * (derived as above), and the to_self_delay contained here to
94          * chan_utils::get_revokeable_redeemscript.
95          */
96         public final static class DelayedPaymentOutput extends SpendableOutputDescriptor {
97                 public final DelayedPaymentOutputDescriptor delayed_payment_output;
98                 private DelayedPaymentOutput(long ptr, bindings.LDKSpendableOutputDescriptor.DelayedPaymentOutput obj) {
99                         super(null, ptr);
100                         long delayed_payment_output = obj.delayed_payment_output;
101                         DelayedPaymentOutputDescriptor delayed_payment_output_hu_conv = null; if (delayed_payment_output < 0 || delayed_payment_output > 4096) { delayed_payment_output_hu_conv = new DelayedPaymentOutputDescriptor(null, delayed_payment_output); }
102                         delayed_payment_output_hu_conv.ptrs_to.add(this);
103                         this.delayed_payment_output = delayed_payment_output_hu_conv;
104                 }
105         }
106         /**
107          * An output to a P2WPKH, spendable exclusively by our payment key (ie the private key which
108          * corresponds to the public key in Sign::pubkeys().payment_point).
109          * The witness in the spending input, is, thus, simply:
110          * <BIP 143 signature> <payment key>
111          * 
112          * These are generally the result of our counterparty having broadcast the current state,
113          * allowing us to claim the non-HTLC-encumbered outputs immediately.
114          */
115         public final static class StaticPaymentOutput extends SpendableOutputDescriptor {
116                 public final StaticPaymentOutputDescriptor static_payment_output;
117                 private StaticPaymentOutput(long ptr, bindings.LDKSpendableOutputDescriptor.StaticPaymentOutput obj) {
118                         super(null, ptr);
119                         long static_payment_output = obj.static_payment_output;
120                         StaticPaymentOutputDescriptor static_payment_output_hu_conv = null; if (static_payment_output < 0 || static_payment_output > 4096) { static_payment_output_hu_conv = new StaticPaymentOutputDescriptor(null, static_payment_output); }
121                         static_payment_output_hu_conv.ptrs_to.add(this);
122                         this.static_payment_output = static_payment_output_hu_conv;
123                 }
124         }
125         long clone_ptr() {
126                 long ret = bindings.SpendableOutputDescriptor_clone_ptr(this.ptr);
127                 return ret;
128         }
129
130         /**
131          * Creates a copy of the SpendableOutputDescriptor
132          */
133         public SpendableOutputDescriptor clone() {
134                 long ret = bindings.SpendableOutputDescriptor_clone(this.ptr);
135                 if (ret >= 0 && ret <= 4096) { return null; }
136                 SpendableOutputDescriptor ret_hu_conv = SpendableOutputDescriptor.constr_from_ptr(ret);
137                 ret_hu_conv.ptrs_to.add(this);
138                 return ret_hu_conv;
139         }
140
141         /**
142          * Utility method to constructs a new StaticOutput-variant SpendableOutputDescriptor
143          */
144         public static SpendableOutputDescriptor static_output(OutPoint outpoint, TxOut output) {
145                 long ret = bindings.SpendableOutputDescriptor_static_output(outpoint == null ? 0 : outpoint.ptr & ~1, output.ptr);
146                 if (ret >= 0 && ret <= 4096) { return null; }
147                 SpendableOutputDescriptor ret_hu_conv = SpendableOutputDescriptor.constr_from_ptr(ret);
148                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
149                 return ret_hu_conv;
150         }
151
152         /**
153          * Utility method to constructs a new DelayedPaymentOutput-variant SpendableOutputDescriptor
154          */
155         public static SpendableOutputDescriptor delayed_payment_output(DelayedPaymentOutputDescriptor a) {
156                 long ret = bindings.SpendableOutputDescriptor_delayed_payment_output(a == null ? 0 : a.ptr & ~1);
157                 if (ret >= 0 && ret <= 4096) { return null; }
158                 SpendableOutputDescriptor ret_hu_conv = SpendableOutputDescriptor.constr_from_ptr(ret);
159                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
160                 return ret_hu_conv;
161         }
162
163         /**
164          * Utility method to constructs a new StaticPaymentOutput-variant SpendableOutputDescriptor
165          */
166         public static SpendableOutputDescriptor static_payment_output(StaticPaymentOutputDescriptor a) {
167                 long ret = bindings.SpendableOutputDescriptor_static_payment_output(a == null ? 0 : a.ptr & ~1);
168                 if (ret >= 0 && ret <= 4096) { return null; }
169                 SpendableOutputDescriptor ret_hu_conv = SpendableOutputDescriptor.constr_from_ptr(ret);
170                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
171                 return ret_hu_conv;
172         }
173
174         /**
175          * Serialize the SpendableOutputDescriptor object into a byte array which can be read by SpendableOutputDescriptor_read
176          */
177         public byte[] write() {
178                 byte[] ret = bindings.SpendableOutputDescriptor_write(this.ptr);
179                 return ret;
180         }
181
182         /**
183          * Read a SpendableOutputDescriptor from a byte array, created by SpendableOutputDescriptor_write
184          */
185         public static Result_SpendableOutputDescriptorDecodeErrorZ read(byte[] ser) {
186                 long ret = bindings.SpendableOutputDescriptor_read(ser);
187                 if (ret >= 0 && ret <= 4096) { return null; }
188                 Result_SpendableOutputDescriptorDecodeErrorZ ret_hu_conv = Result_SpendableOutputDescriptorDecodeErrorZ.constr_from_ptr(ret);
189                 return ret_hu_conv;
190         }
191
192 }