ca3c5e31893977e50ee4a108db4f49689321663d
[ldk-java] / src / main / java / org / ldk / structs / ChannelSigner.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  * A trait to handle Lightning channel key material without concretizing the channel type or
12  * the signature mechanism.
13  */
14 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
15 public class ChannelSigner extends CommonBase {
16         final bindings.LDKChannelSigner bindings_instance;
17         ChannelSigner(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
18         private ChannelSigner(bindings.LDKChannelSigner arg, ChannelPublicKeys pubkeys) {
19                 super(bindings.LDKChannelSigner_new(arg, pubkeys == null ? 0 : pubkeys.clone_ptr()));
20                 this.ptrs_to.add(arg);
21                 this.bindings_instance = arg;
22         }
23         @Override @SuppressWarnings("deprecation")
24         protected void finalize() throws Throwable {
25                 if (ptr != 0) { bindings.ChannelSigner_free(ptr); } super.finalize();
26         }
27         /**
28          * Destroys the object, freeing associated resources. After this call, any access
29          * to this object may result in a SEGFAULT or worse.
30          *
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
34          * until the GC runs.
35          */
36         public void destroy() {
37                 if (ptr != 0) { bindings.ChannelSigner_free(ptr); }
38                 ptr = 0;
39         }
40         public static interface ChannelSignerInterface {
41                 /**
42                  * Gets the per-commitment point for a specific commitment number
43                  * 
44                  * Note that the commitment number starts at `(1 << 48) - 1` and counts backwards.
45                  */
46                 byte[] get_per_commitment_point(long idx);
47                 /**
48                  * Gets the commitment secret for a specific commitment number as part of the revocation process
49                  * 
50                  * An external signer implementation should error here if the commitment was already signed
51                  * and should refuse to sign it in the future.
52                  * 
53                  * May be called more than once for the same index.
54                  * 
55                  * Note that the commitment number starts at `(1 << 48) - 1` and counts backwards.
56                  */
57                 byte[] release_commitment_secret(long idx);
58                 /**
59                  * Validate the counterparty's signatures on the holder commitment transaction and HTLCs.
60                  * 
61                  * This is required in order for the signer to make sure that releasing a commitment
62                  * secret won't leave us without a broadcastable holder transaction.
63                  * Policy checks should be implemented in this function, including checking the amount
64                  * sent to us and checking the HTLCs.
65                  * 
66                  * The preimages of outgoing HTLCs that were fulfilled since the last commitment are provided.
67                  * A validating signer should ensure that an HTLC output is removed only when the matching
68                  * preimage is provided, or when the value to holder is restored.
69                  * 
70                  * Note that all the relevant preimages will be provided, but there may also be additional
71                  * irrelevant or duplicate preimages.
72                  */
73                 Result_NoneNoneZ validate_holder_commitment(HolderCommitmentTransaction holder_tx, byte[][] preimages);
74                 /**
75                  * Returns an arbitrary identifier describing the set of keys which are provided back to you in
76                  * some [`SpendableOutputDescriptor`] types. This should be sufficient to identify this
77                  * [`EcdsaChannelSigner`] object uniquely and lookup or re-derive its keys.
78                  */
79                 byte[] channel_keys_id();
80                 /**
81                  * Set the counterparty static channel data, including basepoints,
82                  * `counterparty_selected`/`holder_selected_contest_delay` and funding outpoint.
83                  * 
84                  * This data is static, and will never change for a channel once set. For a given [`ChannelSigner`]
85                  * instance, LDK will call this method exactly once - either immediately after construction
86                  * (not including if done via [`SignerProvider::read_chan_signer`]) or when the funding
87                  * information has been generated.
88                  * 
89                  * channel_parameters.is_populated() MUST be true.
90                  */
91                 void provide_channel_parameters(ChannelTransactionParameters channel_parameters);
92         }
93         private static class LDKChannelSignerHolder { ChannelSigner held; }
94         public static ChannelSigner new_impl(ChannelSignerInterface arg, ChannelPublicKeys pubkeys) {
95                 final LDKChannelSignerHolder impl_holder = new LDKChannelSignerHolder();
96                 impl_holder.held = new ChannelSigner(new bindings.LDKChannelSigner() {
97                         @Override public byte[] get_per_commitment_point(long idx) {
98                                 byte[] ret = arg.get_per_commitment_point(idx);
99                                 Reference.reachabilityFence(arg);
100                                 byte[] result = InternalUtils.check_arr_len(ret, 33);
101                                 return result;
102                         }
103                         @Override public byte[] release_commitment_secret(long idx) {
104                                 byte[] ret = arg.release_commitment_secret(idx);
105                                 Reference.reachabilityFence(arg);
106                                 byte[] result = InternalUtils.check_arr_len(ret, 32);
107                                 return result;
108                         }
109                         @Override public long validate_holder_commitment(long holder_tx, byte[][] preimages) {
110                                 org.ldk.structs.HolderCommitmentTransaction holder_tx_hu_conv = null; if (holder_tx < 0 || holder_tx > 4096) { holder_tx_hu_conv = new org.ldk.structs.HolderCommitmentTransaction(null, holder_tx); }
111                                 Result_NoneNoneZ ret = arg.validate_holder_commitment(holder_tx_hu_conv, preimages);
112                                 Reference.reachabilityFence(arg);
113                                 long result = ret == null ? 0 : ret.clone_ptr();
114                                 return result;
115                         }
116                         @Override public byte[] channel_keys_id() {
117                                 byte[] ret = arg.channel_keys_id();
118                                 Reference.reachabilityFence(arg);
119                                 byte[] result = InternalUtils.check_arr_len(ret, 32);
120                                 return result;
121                         }
122                         @Override public void provide_channel_parameters(long channel_parameters) {
123                                 org.ldk.structs.ChannelTransactionParameters channel_parameters_hu_conv = null; if (channel_parameters < 0 || channel_parameters > 4096) { channel_parameters_hu_conv = new org.ldk.structs.ChannelTransactionParameters(null, channel_parameters); }
124                                 arg.provide_channel_parameters(channel_parameters_hu_conv);
125                                 Reference.reachabilityFence(arg);
126                         }
127                 }, pubkeys);
128                 return impl_holder.held;
129         }
130         /**
131          * Gets the per-commitment point for a specific commitment number
132          * 
133          * Note that the commitment number starts at `(1 << 48) - 1` and counts backwards.
134          */
135         public byte[] get_per_commitment_point(long idx) {
136                 byte[] ret = bindings.ChannelSigner_get_per_commitment_point(this.ptr, idx);
137                 Reference.reachabilityFence(this);
138                 Reference.reachabilityFence(idx);
139                 return ret;
140         }
141
142         /**
143          * Gets the commitment secret for a specific commitment number as part of the revocation process
144          * 
145          * An external signer implementation should error here if the commitment was already signed
146          * and should refuse to sign it in the future.
147          * 
148          * May be called more than once for the same index.
149          * 
150          * Note that the commitment number starts at `(1 << 48) - 1` and counts backwards.
151          */
152         public byte[] release_commitment_secret(long idx) {
153                 byte[] ret = bindings.ChannelSigner_release_commitment_secret(this.ptr, idx);
154                 Reference.reachabilityFence(this);
155                 Reference.reachabilityFence(idx);
156                 return ret;
157         }
158
159         /**
160          * Validate the counterparty's signatures on the holder commitment transaction and HTLCs.
161          * 
162          * This is required in order for the signer to make sure that releasing a commitment
163          * secret won't leave us without a broadcastable holder transaction.
164          * Policy checks should be implemented in this function, including checking the amount
165          * sent to us and checking the HTLCs.
166          * 
167          * The preimages of outgoing HTLCs that were fulfilled since the last commitment are provided.
168          * A validating signer should ensure that an HTLC output is removed only when the matching
169          * preimage is provided, or when the value to holder is restored.
170          * 
171          * Note that all the relevant preimages will be provided, but there may also be additional
172          * irrelevant or duplicate preimages.
173          */
174         public Result_NoneNoneZ validate_holder_commitment(org.ldk.structs.HolderCommitmentTransaction holder_tx, byte[][] preimages) {
175                 long ret = bindings.ChannelSigner_validate_holder_commitment(this.ptr, holder_tx == null ? 0 : holder_tx.ptr, preimages != null ? Arrays.stream(preimages).map(preimages_conv_8 -> InternalUtils.check_arr_len(preimages_conv_8, 32)).toArray(byte[][]::new) : null);
176                 Reference.reachabilityFence(this);
177                 Reference.reachabilityFence(holder_tx);
178                 Reference.reachabilityFence(preimages);
179                 if (ret >= 0 && ret <= 4096) { return null; }
180                 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
181                 if (this != null) { this.ptrs_to.add(holder_tx); };
182                 return ret_hu_conv;
183         }
184
185         /**
186          * Returns an arbitrary identifier describing the set of keys which are provided back to you in
187          * some [`SpendableOutputDescriptor`] types. This should be sufficient to identify this
188          * [`EcdsaChannelSigner`] object uniquely and lookup or re-derive its keys.
189          */
190         public byte[] channel_keys_id() {
191                 byte[] ret = bindings.ChannelSigner_channel_keys_id(this.ptr);
192                 Reference.reachabilityFence(this);
193                 return ret;
194         }
195
196         /**
197          * Set the counterparty static channel data, including basepoints,
198          * `counterparty_selected`/`holder_selected_contest_delay` and funding outpoint.
199          * 
200          * This data is static, and will never change for a channel once set. For a given [`ChannelSigner`]
201          * instance, LDK will call this method exactly once - either immediately after construction
202          * (not including if done via [`SignerProvider::read_chan_signer`]) or when the funding
203          * information has been generated.
204          * 
205          * channel_parameters.is_populated() MUST be true.
206          */
207         public void provide_channel_parameters(org.ldk.structs.ChannelTransactionParameters channel_parameters) {
208                 bindings.ChannelSigner_provide_channel_parameters(this.ptr, channel_parameters == null ? 0 : channel_parameters.ptr);
209                 Reference.reachabilityFence(this);
210                 Reference.reachabilityFence(channel_parameters);
211                 if (this != null) { this.ptrs_to.add(channel_parameters); };
212         }
213
214         /**
215          * Frees any resources associated with this object given its this_arg pointer.
216          * Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
217          */
218         public ChannelPublicKeys get_pubkeys() {
219                 long ret = bindings.ChannelSigner_get_pubkeys(this.ptr);
220                 Reference.reachabilityFence(this);
221                 if (ret >= 0 && ret <= 4096) { return null; }
222                 org.ldk.structs.ChannelPublicKeys ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChannelPublicKeys(null, ret); }
223                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
224                 return ret_hu_conv;
225         }
226
227 }