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;
12 * A derived key built from a [`HtlcBasepoint`] and `per_commitment_point`.
14 * HTLC keys are used to ensure only the recipient of an HTLC can claim it on-chain with the HTLC
15 * preimage and that only the sender of an HTLC can claim it on-chain after it has timed out.
16 * Thus, both channel counterparties' HTLC keys will appears in each HTLC output's script.
18 * [See the BOLT specs]
19 * (https://github.com/lightning/bolts/blob/master/03-transactions.md#localpubkey-local_htlcpubkey-remote_htlcpubkey-local_delayedpubkey-and-remote_delayedpubkey-derivation)
20 * for more information on key derivation details.
22 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
23 public class HtlcKey extends CommonBase {
24 HtlcKey(Object _dummy, long ptr) { super(ptr); }
25 @Override @SuppressWarnings("deprecation")
26 protected void finalize() throws Throwable {
28 if (ptr != 0) { bindings.HtlcKey_free(ptr); }
31 public byte[] get_a() {
32 byte[] ret = bindings.HtlcKey_get_a(this.ptr);
33 Reference.reachabilityFence(this);
37 public void set_a(byte[] val) {
38 bindings.HtlcKey_set_a(this.ptr, InternalUtils.check_arr_len(val, 33));
39 Reference.reachabilityFence(this);
40 Reference.reachabilityFence(val);
44 * Constructs a new HtlcKey given each field
46 public static HtlcKey of(byte[] a_arg) {
47 long ret = bindings.HtlcKey_new(InternalUtils.check_arr_len(a_arg, 33));
48 Reference.reachabilityFence(a_arg);
49 if (ret >= 0 && ret <= 4096) { return null; }
50 org.ldk.structs.HtlcKey ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.HtlcKey(null, ret); }
51 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
56 * Checks if two HtlcKeys contain equal inner contents.
57 * This ignores pointers and is_owned flags and looks at the values in fields.
58 * Two objects with NULL inner values will be considered "equal" here.
60 public boolean eq(org.ldk.structs.HtlcKey b) {
61 boolean ret = bindings.HtlcKey_eq(this.ptr, b == null ? 0 : b.ptr);
62 Reference.reachabilityFence(this);
63 Reference.reachabilityFence(b);
64 if (this != null) { this.ptrs_to.add(b); };
68 @Override public boolean equals(Object o) {
69 if (!(o instanceof HtlcKey)) return false;
70 return this.eq((HtlcKey)o);
73 long ret = bindings.HtlcKey_clone_ptr(this.ptr);
74 Reference.reachabilityFence(this);
79 * Creates a copy of the HtlcKey
81 public HtlcKey clone() {
82 long ret = bindings.HtlcKey_clone(this.ptr);
83 Reference.reachabilityFence(this);
84 if (ret >= 0 && ret <= 4096) { return null; }
85 org.ldk.structs.HtlcKey ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.HtlcKey(null, ret); }
86 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
91 * Derive a public htlcpubkey using one node\'s `per_commitment_point` and its countersignatory\'s `basepoint`
93 public static HtlcKey from_basepoint(org.ldk.structs.HtlcBasepoint countersignatory_basepoint, byte[] per_commitment_point) {
94 long ret = bindings.HtlcKey_from_basepoint(countersignatory_basepoint == null ? 0 : countersignatory_basepoint.ptr, InternalUtils.check_arr_len(per_commitment_point, 33));
95 Reference.reachabilityFence(countersignatory_basepoint);
96 Reference.reachabilityFence(per_commitment_point);
97 if (ret >= 0 && ret <= 4096) { return null; }
98 org.ldk.structs.HtlcKey ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.HtlcKey(null, ret); }
99 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
100 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(countersignatory_basepoint); };
105 * Build a htlcpubkey directly from an already-derived private key
107 public static HtlcKey from_secret_key(byte[] sk) {
108 long ret = bindings.HtlcKey_from_secret_key(InternalUtils.check_arr_len(sk, 32));
109 Reference.reachabilityFence(sk);
110 if (ret >= 0 && ret <= 4096) { return null; }
111 org.ldk.structs.HtlcKey ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.HtlcKey(null, ret); }
112 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
117 * Get inner Public Key
119 public byte[] to_public_key() {
120 byte[] ret = bindings.HtlcKey_to_public_key(this.ptr);
121 Reference.reachabilityFence(this);
126 * Serialize the HtlcKey object into a byte array which can be read by HtlcKey_read
128 public byte[] write() {
129 byte[] ret = bindings.HtlcKey_write(this.ptr);
130 Reference.reachabilityFence(this);
135 * Read a HtlcKey from a byte array, created by HtlcKey_write
137 public static Result_HtlcKeyDecodeErrorZ read(byte[] ser) {
138 long ret = bindings.HtlcKey_read(ser);
139 Reference.reachabilityFence(ser);
140 if (ret >= 0 && ret <= 4096) { return null; }
141 Result_HtlcKeyDecodeErrorZ ret_hu_conv = Result_HtlcKeyDecodeErrorZ.constr_from_ptr(ret);