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 to handle Lightning channel key material without concretizing the channel type or
12 * the signature mechanism.
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.clone_ptr()));
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.ChannelSigner_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.ChannelSigner_free(ptr); }
40 public static interface ChannelSignerInterface {
42 * Gets the per-commitment point for a specific commitment number
44 * Note that the commitment number starts at `(1 << 48) - 1` and counts backwards.
46 byte[] get_per_commitment_point(long idx);
48 * Gets the commitment secret for a specific commitment number as part of the revocation process
50 * An external signer implementation should error here if the commitment was already signed
51 * and should refuse to sign it in the future.
53 * May be called more than once for the same index.
55 * Note that the commitment number starts at `(1 << 48) - 1` and counts backwards.
57 byte[] release_commitment_secret(long idx);
59 * Validate the counterparty's signatures on the holder commitment transaction and HTLCs.
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.
66 * The preimages of outbound 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.
70 * Note that all the relevant preimages will be provided, but there may also be additional
71 * irrelevant or duplicate preimages.
73 Result_NoneNoneZ validate_holder_commitment(HolderCommitmentTransaction holder_tx, byte[][] outbound_htlc_preimages);
75 * Validate the counterparty's revocation.
77 * This is required in order for the signer to make sure that the state has moved
78 * forward and it is safe to sign the next counterparty commitment.
80 Result_NoneNoneZ validate_counterparty_revocation(long idx, byte[] secret);
82 * Returns an arbitrary identifier describing the set of keys which are provided back to you in
83 * some [`SpendableOutputDescriptor`] types. This should be sufficient to identify this
84 * [`EcdsaChannelSigner`] object uniquely and lookup or re-derive its keys.
86 byte[] channel_keys_id();
88 * Set the counterparty static channel data, including basepoints,
89 * `counterparty_selected`/`holder_selected_contest_delay` and funding outpoint.
91 * This data is static, and will never change for a channel once set. For a given [`ChannelSigner`]
92 * instance, LDK will call this method exactly once - either immediately after construction
93 * (not including if done via [`SignerProvider::read_chan_signer`]) or when the funding
94 * information has been generated.
96 * channel_parameters.is_populated() MUST be true.
98 void provide_channel_parameters(ChannelTransactionParameters channel_parameters);
100 private static class LDKChannelSignerHolder { ChannelSigner held; }
101 public static ChannelSigner new_impl(ChannelSignerInterface arg, ChannelPublicKeys pubkeys) {
102 final LDKChannelSignerHolder impl_holder = new LDKChannelSignerHolder();
103 impl_holder.held = new ChannelSigner(new bindings.LDKChannelSigner() {
104 @Override public byte[] get_per_commitment_point(long idx) {
105 byte[] ret = arg.get_per_commitment_point(idx);
106 Reference.reachabilityFence(arg);
107 byte[] result = InternalUtils.check_arr_len(ret, 33);
110 @Override public byte[] release_commitment_secret(long idx) {
111 byte[] ret = arg.release_commitment_secret(idx);
112 Reference.reachabilityFence(arg);
113 byte[] result = InternalUtils.check_arr_len(ret, 32);
116 @Override public long validate_holder_commitment(long holder_tx, byte[][] outbound_htlc_preimages) {
117 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); }
118 Result_NoneNoneZ ret = arg.validate_holder_commitment(holder_tx_hu_conv, outbound_htlc_preimages);
119 Reference.reachabilityFence(arg);
120 long result = ret.clone_ptr();
123 @Override public long validate_counterparty_revocation(long idx, byte[] secret) {
124 Result_NoneNoneZ ret = arg.validate_counterparty_revocation(idx, secret);
125 Reference.reachabilityFence(arg);
126 long result = ret.clone_ptr();
129 @Override public byte[] channel_keys_id() {
130 byte[] ret = arg.channel_keys_id();
131 Reference.reachabilityFence(arg);
132 byte[] result = InternalUtils.check_arr_len(ret, 32);
135 @Override public void provide_channel_parameters(long channel_parameters) {
136 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); }
137 arg.provide_channel_parameters(channel_parameters_hu_conv);
138 Reference.reachabilityFence(arg);
141 return impl_holder.held;
144 * Gets the per-commitment point for a specific commitment number
146 * Note that the commitment number starts at `(1 << 48) - 1` and counts backwards.
148 public byte[] get_per_commitment_point(long idx) {
149 byte[] ret = bindings.ChannelSigner_get_per_commitment_point(this.ptr, idx);
150 Reference.reachabilityFence(this);
151 Reference.reachabilityFence(idx);
156 * Gets the commitment secret for a specific commitment number as part of the revocation process
158 * An external signer implementation should error here if the commitment was already signed
159 * and should refuse to sign it in the future.
161 * May be called more than once for the same index.
163 * Note that the commitment number starts at `(1 << 48) - 1` and counts backwards.
165 public byte[] release_commitment_secret(long idx) {
166 byte[] ret = bindings.ChannelSigner_release_commitment_secret(this.ptr, idx);
167 Reference.reachabilityFence(this);
168 Reference.reachabilityFence(idx);
173 * Validate the counterparty's signatures on the holder commitment transaction and HTLCs.
175 * This is required in order for the signer to make sure that releasing a commitment
176 * secret won't leave us without a broadcastable holder transaction.
177 * Policy checks should be implemented in this function, including checking the amount
178 * sent to us and checking the HTLCs.
180 * The preimages of outbound HTLCs that were fulfilled since the last commitment are provided.
181 * A validating signer should ensure that an HTLC output is removed only when the matching
182 * preimage is provided, or when the value to holder is restored.
184 * Note that all the relevant preimages will be provided, but there may also be additional
185 * irrelevant or duplicate preimages.
187 public Result_NoneNoneZ validate_holder_commitment(org.ldk.structs.HolderCommitmentTransaction holder_tx, byte[][] outbound_htlc_preimages) {
188 long ret = bindings.ChannelSigner_validate_holder_commitment(this.ptr, holder_tx.ptr, outbound_htlc_preimages != null ? Arrays.stream(outbound_htlc_preimages).map(outbound_htlc_preimages_conv_8 -> InternalUtils.check_arr_len(outbound_htlc_preimages_conv_8, 32)).toArray(byte[][]::new) : null);
189 Reference.reachabilityFence(this);
190 Reference.reachabilityFence(holder_tx);
191 Reference.reachabilityFence(outbound_htlc_preimages);
192 if (ret >= 0 && ret <= 4096) { return null; }
193 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
194 if (this != null) { this.ptrs_to.add(holder_tx); };
199 * Validate the counterparty's revocation.
201 * This is required in order for the signer to make sure that the state has moved
202 * forward and it is safe to sign the next counterparty commitment.
204 public Result_NoneNoneZ validate_counterparty_revocation(long idx, byte[] secret) {
205 long ret = bindings.ChannelSigner_validate_counterparty_revocation(this.ptr, idx, InternalUtils.check_arr_len(secret, 32));
206 Reference.reachabilityFence(this);
207 Reference.reachabilityFence(idx);
208 Reference.reachabilityFence(secret);
209 if (ret >= 0 && ret <= 4096) { return null; }
210 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
215 * Returns an arbitrary identifier describing the set of keys which are provided back to you in
216 * some [`SpendableOutputDescriptor`] types. This should be sufficient to identify this
217 * [`EcdsaChannelSigner`] object uniquely and lookup or re-derive its keys.
219 public byte[] channel_keys_id() {
220 byte[] ret = bindings.ChannelSigner_channel_keys_id(this.ptr);
221 Reference.reachabilityFence(this);
226 * Set the counterparty static channel data, including basepoints,
227 * `counterparty_selected`/`holder_selected_contest_delay` and funding outpoint.
229 * This data is static, and will never change for a channel once set. For a given [`ChannelSigner`]
230 * instance, LDK will call this method exactly once - either immediately after construction
231 * (not including if done via [`SignerProvider::read_chan_signer`]) or when the funding
232 * information has been generated.
234 * channel_parameters.is_populated() MUST be true.
236 public void provide_channel_parameters(org.ldk.structs.ChannelTransactionParameters channel_parameters) {
237 bindings.ChannelSigner_provide_channel_parameters(this.ptr, channel_parameters.ptr);
238 Reference.reachabilityFence(this);
239 Reference.reachabilityFence(channel_parameters);
240 if (this != null) { this.ptrs_to.add(channel_parameters); };
244 * Frees any resources associated with this object given its this_arg pointer.
245 * Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
247 public ChannelPublicKeys get_pubkeys() {
248 long ret = bindings.ChannelSigner_get_pubkeys(this.ptr);
249 Reference.reachabilityFence(this);
250 if (ret >= 0 && ret <= 4096) { return null; }
251 org.ldk.structs.ChannelPublicKeys ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChannelPublicKeys(null, ret); }
252 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };