1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
10 * A trait to sign lightning channel transactions as described in BOLT 3.
12 * Signing services could be implemented on a hardware wallet. In this case,
13 * the current Sign would be a front-end on top of a communication
14 * channel connected to your secure device and lightning key material wouldn't
15 * reside on a hot server. Nevertheless, a this deployment would still need
16 * to trust the ChannelManager to avoid loss of funds as this latest component
17 * could ask to sign commitment transaction with HTLCs paying to attacker pubkeys.
19 * A more secure iteration would be to use hashlock (or payment points) to pair
20 * invoice/incoming HTLCs with outgoing HTLCs to implement a no-trust-ChannelManager
21 * at the price of more state and computation on the hardware wallet side. In the future,
22 * we are looking forward to design such interface.
24 * In any case, ChannelMonitor or fallback watchtowers are always going to be trusted
25 * to act, as liveness and breach reply correctness are always going to be hard requirements
26 * of LN security model, orthogonal of key management issues.
28 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
29 public class BaseSign extends CommonBase {
30 final bindings.LDKBaseSign bindings_instance;
31 BaseSign(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
32 private BaseSign(bindings.LDKBaseSign arg, ChannelPublicKeys pubkeys) {
33 super(bindings.LDKBaseSign_new(arg, pubkeys == null ? 0 : pubkeys.ptr & ~1));
34 this.ptrs_to.add(arg);
35 this.ptrs_to.add(pubkeys);
36 this.bindings_instance = arg;
38 @Override @SuppressWarnings("deprecation")
39 protected void finalize() throws Throwable {
40 if (ptr != 0) { bindings.BaseSign_free(ptr); } super.finalize();
43 public static interface BaseSignInterface {
45 * Gets the per-commitment point for a specific commitment number
47 * Note that the commitment number starts at (1 << 48) - 1 and counts backwards.
49 byte[] get_per_commitment_point(long idx);
51 * Gets the commitment secret for a specific commitment number as part of the revocation process
53 * An external signer implementation should error here if the commitment was already signed
54 * and should refuse to sign it in the future.
56 * May be called more than once for the same index.
58 * Note that the commitment number starts at (1 << 48) - 1 and counts backwards.
60 byte[] release_commitment_secret(long idx);
62 * Validate the counterparty's signatures on the holder commitment transaction and HTLCs.
64 * This is required in order for the signer to make sure that releasing a commitment
65 * secret won't leave us without a broadcastable holder transaction.
66 * Policy checks should be implemented in this function, including checking the amount
67 * sent to us and checking the HTLCs.
69 Result_NoneNoneZ validate_holder_commitment(HolderCommitmentTransaction holder_tx);
71 * Gets an arbitrary identifier describing the set of keys which are provided back to you in
72 * some SpendableOutputDescriptor types. This should be sufficient to identify this
73 * Sign object uniquely and lookup or re-derive its keys.
75 byte[] channel_keys_id();
77 * Create a signature for a counterparty's commitment transaction and associated HTLC transactions.
79 * Note that if signing fails or is rejected, the channel will be force-closed.
81 * Policy checks should be implemented in this function, including checking the amount
82 * sent to us and checking the HTLCs.
84 Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_counterparty_commitment(CommitmentTransaction commitment_tx);
86 * Validate the counterparty's revocation.
88 * This is required in order for the signer to make sure that the state has moved
89 * forward and it is safe to sign the next counterparty commitment.
91 Result_NoneNoneZ validate_counterparty_revocation(long idx, byte[] secret);
93 * Create a signatures for a holder's commitment transaction and its claiming HTLC transactions.
94 * This will only ever be called with a non-revoked commitment_tx. This will be called with the
95 * latest commitment_tx when we initiate a force-close.
96 * This will be called with the previous latest, just to get claiming HTLC signatures, if we are
97 * reacting to a ChannelMonitor replica that decided to broadcast before it had been updated to
99 * This may be called multiple times for the same transaction.
101 * An external signer implementation should check that the commitment has not been revoked.
103 * May return Err if key derivation fails. Callers, such as ChannelMonitor, will panic in such a case.
105 Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_holder_commitment_and_htlcs(HolderCommitmentTransaction commitment_tx);
107 * Create a signature for the given input in a transaction spending an HTLC transaction output
108 * or a commitment transaction `to_local` output when our counterparty broadcasts an old state.
110 * A justice transaction may claim multiple outputs at the same time if timelocks are
111 * similar, but only a signature for the input at index `input` should be signed for here.
112 * It may be called multiple times for same output(s) if a fee-bump is needed with regards
113 * to an upcoming timelock expiration.
115 * Amount is value of the output spent by this input, committed to in the BIP 143 signature.
117 * per_commitment_key is revocation secret which was provided by our counterparty when they
118 * revoked the state which they eventually broadcast. It's not a _holder_ secret key and does
119 * not allow the spending of any funds by itself (you need our holder revocation_secret to do
122 Result_SignatureNoneZ sign_justice_revoked_output(byte[] justice_tx, long input, long amount, byte[] per_commitment_key);
124 * Create a signature for the given input in a transaction spending a commitment transaction
125 * HTLC output when our counterparty broadcasts an old state.
127 * A justice transaction may claim multiple outputs at the same time if timelocks are
128 * similar, but only a signature for the input at index `input` should be signed for here.
129 * It may be called multiple times for same output(s) if a fee-bump is needed with regards
130 * to an upcoming timelock expiration.
132 * Amount is value of the output spent by this input, committed to in the BIP 143 signature.
134 * per_commitment_key is revocation secret which was provided by our counterparty when they
135 * revoked the state which they eventually broadcast. It's not a _holder_ secret key and does
136 * not allow the spending of any funds by itself (you need our holder revocation_secret to do
139 * htlc holds HTLC elements (hash, timelock), thus changing the format of the witness script
140 * (which is committed to in the BIP 143 signatures).
142 Result_SignatureNoneZ sign_justice_revoked_htlc(byte[] justice_tx, long input, long amount, byte[] per_commitment_key, HTLCOutputInCommitment htlc);
144 * Create a signature for a claiming transaction for a HTLC output on a counterparty's commitment
145 * transaction, either offered or received.
147 * Such a transaction may claim multiples offered outputs at same time if we know the
148 * preimage for each when we create it, but only the input at index `input` should be
149 * signed for here. It may be called multiple times for same output(s) if a fee-bump is
150 * needed with regards to an upcoming timelock expiration.
152 * Witness_script is either a offered or received script as defined in BOLT3 for HTLC
155 * Amount is value of the output spent by this input, committed to in the BIP 143 signature.
157 * Per_commitment_point is the dynamic point corresponding to the channel state
158 * detected onchain. It has been generated by our counterparty and is used to derive
159 * channel state keys, which are then included in the witness script and committed to in the
162 Result_SignatureNoneZ sign_counterparty_htlc_transaction(byte[] htlc_tx, long input, long amount, byte[] per_commitment_point, HTLCOutputInCommitment htlc);
164 * Create a signature for a (proposed) closing transaction.
166 * Note that, due to rounding, there may be one \"missing\" satoshi, and either party may have
167 * chosen to forgo their output as dust.
169 Result_SignatureNoneZ sign_closing_transaction(ClosingTransaction closing_tx);
171 * Signs a channel announcement message with our funding key, proving it comes from one
172 * of the channel participants.
174 * Note that if this fails or is rejected, the channel will not be publicly announced and
175 * our counterparty may (though likely will not) close the channel on us for violating the
178 Result_SignatureNoneZ sign_channel_announcement(UnsignedChannelAnnouncement msg);
180 * Set the counterparty static channel data, including basepoints,
181 * counterparty_selected/holder_selected_contest_delay and funding outpoint.
182 * This is done as soon as the funding outpoint is known. Since these are static channel data,
183 * they MUST NOT be allowed to change to different values once set.
185 * channel_parameters.is_populated() MUST be true.
187 * We bind holder_selected_contest_delay late here for API convenience.
189 * Will be called before any signatures are applied.
191 void ready_channel(ChannelTransactionParameters channel_parameters);
193 private static class LDKBaseSignHolder { BaseSign held; }
194 public static BaseSign new_impl(BaseSignInterface arg, ChannelPublicKeys pubkeys) {
195 final LDKBaseSignHolder impl_holder = new LDKBaseSignHolder();
196 impl_holder.held = new BaseSign(new bindings.LDKBaseSign() {
197 @Override public byte[] get_per_commitment_point(long idx) {
198 byte[] ret = arg.get_per_commitment_point(idx);
201 @Override public byte[] release_commitment_secret(long idx) {
202 byte[] ret = arg.release_commitment_secret(idx);
205 @Override public long validate_holder_commitment(long holder_tx) {
206 HolderCommitmentTransaction holder_tx_hu_conv = new HolderCommitmentTransaction(null, holder_tx);
207 Result_NoneNoneZ ret = arg.validate_holder_commitment(holder_tx_hu_conv);
208 long result = ret != null ? ret.ptr : 0;
211 @Override public byte[] channel_keys_id() {
212 byte[] ret = arg.channel_keys_id();
215 @Override public long sign_counterparty_commitment(long commitment_tx) {
216 CommitmentTransaction commitment_tx_hu_conv = new CommitmentTransaction(null, commitment_tx);
217 Result_C2Tuple_SignatureCVec_SignatureZZNoneZ ret = arg.sign_counterparty_commitment(commitment_tx_hu_conv);
218 long result = ret != null ? ret.ptr : 0;
221 @Override public long validate_counterparty_revocation(long idx, byte[] secret) {
222 Result_NoneNoneZ ret = arg.validate_counterparty_revocation(idx, secret);
223 long result = ret != null ? ret.ptr : 0;
226 @Override public long sign_holder_commitment_and_htlcs(long commitment_tx) {
227 HolderCommitmentTransaction commitment_tx_hu_conv = new HolderCommitmentTransaction(null, commitment_tx);
228 Result_C2Tuple_SignatureCVec_SignatureZZNoneZ ret = arg.sign_holder_commitment_and_htlcs(commitment_tx_hu_conv);
229 long result = ret != null ? ret.ptr : 0;
232 @Override public long sign_justice_revoked_output(byte[] justice_tx, long input, long amount, byte[] per_commitment_key) {
233 Result_SignatureNoneZ ret = arg.sign_justice_revoked_output(justice_tx, input, amount, per_commitment_key);
234 long result = ret != null ? ret.ptr : 0;
237 @Override public long sign_justice_revoked_htlc(byte[] justice_tx, long input, long amount, byte[] per_commitment_key, long htlc) {
238 HTLCOutputInCommitment htlc_hu_conv = new HTLCOutputInCommitment(null, htlc);
239 Result_SignatureNoneZ ret = arg.sign_justice_revoked_htlc(justice_tx, input, amount, per_commitment_key, htlc_hu_conv);
240 long result = ret != null ? ret.ptr : 0;
243 @Override public long sign_counterparty_htlc_transaction(byte[] htlc_tx, long input, long amount, byte[] per_commitment_point, long htlc) {
244 HTLCOutputInCommitment htlc_hu_conv = new HTLCOutputInCommitment(null, htlc);
245 Result_SignatureNoneZ ret = arg.sign_counterparty_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc_hu_conv);
246 long result = ret != null ? ret.ptr : 0;
249 @Override public long sign_closing_transaction(long closing_tx) {
250 ClosingTransaction closing_tx_hu_conv = new ClosingTransaction(null, closing_tx);
251 Result_SignatureNoneZ ret = arg.sign_closing_transaction(closing_tx_hu_conv);
252 long result = ret != null ? ret.ptr : 0;
255 @Override public long sign_channel_announcement(long msg) {
256 UnsignedChannelAnnouncement msg_hu_conv = new UnsignedChannelAnnouncement(null, msg);
257 Result_SignatureNoneZ ret = arg.sign_channel_announcement(msg_hu_conv);
258 long result = ret != null ? ret.ptr : 0;
261 @Override public void ready_channel(long channel_parameters) {
262 ChannelTransactionParameters channel_parameters_hu_conv = new ChannelTransactionParameters(null, channel_parameters);
263 arg.ready_channel(channel_parameters_hu_conv);
266 return impl_holder.held;
269 * Gets the per-commitment point for a specific commitment number
271 * Note that the commitment number starts at (1 << 48) - 1 and counts backwards.
273 public byte[] get_per_commitment_point(long idx) {
274 byte[] ret = bindings.BaseSign_get_per_commitment_point(this.ptr, idx);
279 * Gets the commitment secret for a specific commitment number as part of the revocation process
281 * An external signer implementation should error here if the commitment was already signed
282 * and should refuse to sign it in the future.
284 * May be called more than once for the same index.
286 * Note that the commitment number starts at (1 << 48) - 1 and counts backwards.
288 public byte[] release_commitment_secret(long idx) {
289 byte[] ret = bindings.BaseSign_release_commitment_secret(this.ptr, idx);
294 * Validate the counterparty's signatures on the holder commitment transaction and HTLCs.
296 * This is required in order for the signer to make sure that releasing a commitment
297 * secret won't leave us without a broadcastable holder transaction.
298 * Policy checks should be implemented in this function, including checking the amount
299 * sent to us and checking the HTLCs.
301 public Result_NoneNoneZ validate_holder_commitment(HolderCommitmentTransaction holder_tx) {
302 long ret = bindings.BaseSign_validate_holder_commitment(this.ptr, holder_tx == null ? 0 : holder_tx.ptr & ~1);
303 if (ret < 1024) { return null; }
304 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
305 this.ptrs_to.add(holder_tx);
310 * Gets an arbitrary identifier describing the set of keys which are provided back to you in
311 * some SpendableOutputDescriptor types. This should be sufficient to identify this
312 * Sign object uniquely and lookup or re-derive its keys.
314 public byte[] channel_keys_id() {
315 byte[] ret = bindings.BaseSign_channel_keys_id(this.ptr);
320 * Create a signature for a counterparty's commitment transaction and associated HTLC transactions.
322 * Note that if signing fails or is rejected, the channel will be force-closed.
324 * Policy checks should be implemented in this function, including checking the amount
325 * sent to us and checking the HTLCs.
327 public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_counterparty_commitment(CommitmentTransaction commitment_tx) {
328 long ret = bindings.BaseSign_sign_counterparty_commitment(this.ptr, commitment_tx == null ? 0 : commitment_tx.ptr & ~1);
329 if (ret < 1024) { return null; }
330 Result_C2Tuple_SignatureCVec_SignatureZZNoneZ ret_hu_conv = Result_C2Tuple_SignatureCVec_SignatureZZNoneZ.constr_from_ptr(ret);
331 this.ptrs_to.add(commitment_tx);
336 * Validate the counterparty's revocation.
338 * This is required in order for the signer to make sure that the state has moved
339 * forward and it is safe to sign the next counterparty commitment.
341 public Result_NoneNoneZ validate_counterparty_revocation(long idx, byte[] secret) {
342 long ret = bindings.BaseSign_validate_counterparty_revocation(this.ptr, idx, secret);
343 if (ret < 1024) { return null; }
344 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
349 * Create a signatures for a holder's commitment transaction and its claiming HTLC transactions.
350 * This will only ever be called with a non-revoked commitment_tx. This will be called with the
351 * latest commitment_tx when we initiate a force-close.
352 * This will be called with the previous latest, just to get claiming HTLC signatures, if we are
353 * reacting to a ChannelMonitor replica that decided to broadcast before it had been updated to
355 * This may be called multiple times for the same transaction.
357 * An external signer implementation should check that the commitment has not been revoked.
359 * May return Err if key derivation fails. Callers, such as ChannelMonitor, will panic in such a case.
361 public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_holder_commitment_and_htlcs(HolderCommitmentTransaction commitment_tx) {
362 long ret = bindings.BaseSign_sign_holder_commitment_and_htlcs(this.ptr, commitment_tx == null ? 0 : commitment_tx.ptr & ~1);
363 if (ret < 1024) { return null; }
364 Result_C2Tuple_SignatureCVec_SignatureZZNoneZ ret_hu_conv = Result_C2Tuple_SignatureCVec_SignatureZZNoneZ.constr_from_ptr(ret);
365 this.ptrs_to.add(commitment_tx);
370 * Create a signature for the given input in a transaction spending an HTLC transaction output
371 * or a commitment transaction `to_local` output when our counterparty broadcasts an old state.
373 * A justice transaction may claim multiple outputs at the same time if timelocks are
374 * similar, but only a signature for the input at index `input` should be signed for here.
375 * It may be called multiple times for same output(s) if a fee-bump is needed with regards
376 * to an upcoming timelock expiration.
378 * Amount is value of the output spent by this input, committed to in the BIP 143 signature.
380 * per_commitment_key is revocation secret which was provided by our counterparty when they
381 * revoked the state which they eventually broadcast. It's not a _holder_ secret key and does
382 * not allow the spending of any funds by itself (you need our holder revocation_secret to do
385 public Result_SignatureNoneZ sign_justice_revoked_output(byte[] justice_tx, long input, long amount, byte[] per_commitment_key) {
386 long ret = bindings.BaseSign_sign_justice_revoked_output(this.ptr, justice_tx, input, amount, per_commitment_key);
387 if (ret < 1024) { return null; }
388 Result_SignatureNoneZ ret_hu_conv = Result_SignatureNoneZ.constr_from_ptr(ret);
393 * Create a signature for the given input in a transaction spending a commitment transaction
394 * HTLC output when our counterparty broadcasts an old state.
396 * A justice transaction may claim multiple outputs at the same time if timelocks are
397 * similar, but only a signature for the input at index `input` should be signed for here.
398 * It may be called multiple times for same output(s) if a fee-bump is needed with regards
399 * to an upcoming timelock expiration.
401 * Amount is value of the output spent by this input, committed to in the BIP 143 signature.
403 * per_commitment_key is revocation secret which was provided by our counterparty when they
404 * revoked the state which they eventually broadcast. It's not a _holder_ secret key and does
405 * not allow the spending of any funds by itself (you need our holder revocation_secret to do
408 * htlc holds HTLC elements (hash, timelock), thus changing the format of the witness script
409 * (which is committed to in the BIP 143 signatures).
411 public Result_SignatureNoneZ sign_justice_revoked_htlc(byte[] justice_tx, long input, long amount, byte[] per_commitment_key, HTLCOutputInCommitment htlc) {
412 long ret = bindings.BaseSign_sign_justice_revoked_htlc(this.ptr, justice_tx, input, amount, per_commitment_key, htlc == null ? 0 : htlc.ptr & ~1);
413 if (ret < 1024) { return null; }
414 Result_SignatureNoneZ ret_hu_conv = Result_SignatureNoneZ.constr_from_ptr(ret);
415 this.ptrs_to.add(htlc);
420 * Create a signature for a claiming transaction for a HTLC output on a counterparty's commitment
421 * transaction, either offered or received.
423 * Such a transaction may claim multiples offered outputs at same time if we know the
424 * preimage for each when we create it, but only the input at index `input` should be
425 * signed for here. It may be called multiple times for same output(s) if a fee-bump is
426 * needed with regards to an upcoming timelock expiration.
428 * Witness_script is either a offered or received script as defined in BOLT3 for HTLC
431 * Amount is value of the output spent by this input, committed to in the BIP 143 signature.
433 * Per_commitment_point is the dynamic point corresponding to the channel state
434 * detected onchain. It has been generated by our counterparty and is used to derive
435 * channel state keys, which are then included in the witness script and committed to in the
438 public Result_SignatureNoneZ sign_counterparty_htlc_transaction(byte[] htlc_tx, long input, long amount, byte[] per_commitment_point, HTLCOutputInCommitment htlc) {
439 long ret = bindings.BaseSign_sign_counterparty_htlc_transaction(this.ptr, htlc_tx, input, amount, per_commitment_point, htlc == null ? 0 : htlc.ptr & ~1);
440 if (ret < 1024) { return null; }
441 Result_SignatureNoneZ ret_hu_conv = Result_SignatureNoneZ.constr_from_ptr(ret);
442 this.ptrs_to.add(htlc);
447 * Create a signature for a (proposed) closing transaction.
449 * Note that, due to rounding, there may be one \"missing\" satoshi, and either party may have
450 * chosen to forgo their output as dust.
452 public Result_SignatureNoneZ sign_closing_transaction(ClosingTransaction closing_tx) {
453 long ret = bindings.BaseSign_sign_closing_transaction(this.ptr, closing_tx == null ? 0 : closing_tx.ptr & ~1);
454 if (ret < 1024) { return null; }
455 Result_SignatureNoneZ ret_hu_conv = Result_SignatureNoneZ.constr_from_ptr(ret);
456 this.ptrs_to.add(closing_tx);
461 * Signs a channel announcement message with our funding key, proving it comes from one
462 * of the channel participants.
464 * Note that if this fails or is rejected, the channel will not be publicly announced and
465 * our counterparty may (though likely will not) close the channel on us for violating the
468 public Result_SignatureNoneZ sign_channel_announcement(UnsignedChannelAnnouncement msg) {
469 long ret = bindings.BaseSign_sign_channel_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
470 if (ret < 1024) { return null; }
471 Result_SignatureNoneZ ret_hu_conv = Result_SignatureNoneZ.constr_from_ptr(ret);
472 this.ptrs_to.add(msg);
477 * Set the counterparty static channel data, including basepoints,
478 * counterparty_selected/holder_selected_contest_delay and funding outpoint.
479 * This is done as soon as the funding outpoint is known. Since these are static channel data,
480 * they MUST NOT be allowed to change to different values once set.
482 * channel_parameters.is_populated() MUST be true.
484 * We bind holder_selected_contest_delay late here for API convenience.
486 * Will be called before any signatures are applied.
488 public void ready_channel(ChannelTransactionParameters channel_parameters) {
489 bindings.BaseSign_ready_channel(this.ptr, channel_parameters == null ? 0 : channel_parameters.ptr & ~1);
490 this.ptrs_to.add(channel_parameters);
494 * Frees any resources associated with this object given its this_arg pointer.
495 * Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
497 public ChannelPublicKeys get_pubkeys() {
498 long ret = bindings.BaseSign_get_pubkeys(this.ptr);
499 if (ret < 1024) { return null; }
500 ChannelPublicKeys ret_hu_conv = new ChannelPublicKeys(null, ret);
501 ret_hu_conv.ptrs_to.add(this);