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 that can handle cryptographic operations at the scope level of a node.
13 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
14 public class NodeSigner extends CommonBase {
15 final bindings.LDKNodeSigner bindings_instance;
16 NodeSigner(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
17 private NodeSigner(bindings.LDKNodeSigner arg) {
18 super(bindings.LDKNodeSigner_new(arg));
19 this.ptrs_to.add(arg);
20 this.bindings_instance = arg;
22 @Override @SuppressWarnings("deprecation")
23 protected void finalize() throws Throwable {
24 if (ptr != 0) { bindings.NodeSigner_free(ptr); } super.finalize();
27 * Destroys the object, freeing associated resources. After this call, any access
28 * to this object may result in a SEGFAULT or worse.
30 * You should generally NEVER call this method. You should let the garbage collector
31 * do this for you when it finalizes objects. However, it may be useful for types
32 * which represent locks and should be closed immediately to avoid holding locks
35 public void destroy() {
36 if (ptr != 0) { bindings.NodeSigner_free(ptr); }
39 public static interface NodeSignerInterface {
41 * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
43 * If the implementor of this trait supports [phantom node payments], then every node that is
44 * intended to be included in the phantom invoice route hints must return the same value from
47 * This method must return the same value each time it is called.
49 * [phantom node payments]: PhantomKeysManager
51 byte[] get_inbound_payment_key_material();
53 * Get node id based on the provided [`Recipient`].
55 * This method must return the same value each time it is called with a given [`Recipient`]
58 * Errors if the [`Recipient`] variant is not supported by the implementation.
60 Result_PublicKeyNoneZ get_node_id(Recipient recipient);
62 * Gets the ECDH shared secret of our node secret and `other_key`, multiplying by `tweak` if
63 * one is provided. Note that this tweak can be applied to `other_key` instead of our node
64 * secret, though this is less efficient.
66 * Note that if this fails while attempting to forward an HTLC, LDK will panic. The error
67 * should be resolved to allow LDK to resume forwarding HTLCs.
69 * Errors if the [`Recipient`] variant is not supported by the implementation.
71 Result_SharedSecretNoneZ ecdh(Recipient recipient, byte[] other_key, Option_ScalarZ tweak);
75 * By parameterizing by the raw invoice bytes instead of the hash, we allow implementors of
76 * this trait to parse the invoice and make sure they're signing what they expect, rather than
77 * blindly signing the hash.
79 * The `hrp_bytes` are ASCII bytes, while the `invoice_data` is base32.
81 * The secret key used to sign the invoice is dependent on the [`Recipient`].
83 * Errors if the [`Recipient`] variant is not supported by the implementation.
85 Result_RecoverableSignatureNoneZ sign_invoice(byte[] hrp_bytes, UInt5[] invoice_data, Recipient recipient);
87 * Sign a gossip message.
89 * Note that if this fails, LDK may panic and the message will not be broadcast to the network
90 * or a possible channel counterparty. If LDK panics, the error should be resolved to allow the
91 * message to be broadcast, as otherwise it may prevent one from receiving funds over the
92 * corresponding channel.
94 Result_SignatureNoneZ sign_gossip_message(UnsignedGossipMessage msg);
96 private static class LDKNodeSignerHolder { NodeSigner held; }
97 public static NodeSigner new_impl(NodeSignerInterface arg) {
98 final LDKNodeSignerHolder impl_holder = new LDKNodeSignerHolder();
99 impl_holder.held = new NodeSigner(new bindings.LDKNodeSigner() {
100 @Override public byte[] get_inbound_payment_key_material() {
101 byte[] ret = arg.get_inbound_payment_key_material();
102 Reference.reachabilityFence(arg);
103 byte[] result = InternalUtils.check_arr_len(ret, 32);
106 @Override public long get_node_id(Recipient recipient) {
107 Result_PublicKeyNoneZ ret = arg.get_node_id(recipient);
108 Reference.reachabilityFence(arg);
109 long result = ret == null ? 0 : ret.clone_ptr();
112 @Override public long ecdh(Recipient recipient, byte[] other_key, long tweak) {
113 org.ldk.structs.Option_ScalarZ tweak_hu_conv = org.ldk.structs.Option_ScalarZ.constr_from_ptr(tweak);
114 if (tweak_hu_conv != null) { tweak_hu_conv.ptrs_to.add(this); };
115 Result_SharedSecretNoneZ ret = arg.ecdh(recipient, other_key, tweak_hu_conv);
116 Reference.reachabilityFence(arg);
117 long result = ret == null ? 0 : ret.clone_ptr();
120 @Override public long sign_invoice(byte[] hrp_bytes, byte[] invoice_data, Recipient recipient) {
121 int invoice_data_conv_7_len = invoice_data.length;
122 UInt5[] invoice_data_conv_7_arr = new UInt5[invoice_data_conv_7_len];
123 for (int h = 0; h < invoice_data_conv_7_len; h++) {
124 byte invoice_data_conv_7 = invoice_data[h];
125 UInt5 invoice_data_conv_7_conv = new UInt5(invoice_data_conv_7);
126 invoice_data_conv_7_arr[h] = invoice_data_conv_7_conv;
128 Result_RecoverableSignatureNoneZ ret = arg.sign_invoice(hrp_bytes, invoice_data_conv_7_arr, recipient);
129 Reference.reachabilityFence(arg);
130 long result = ret == null ? 0 : ret.clone_ptr();
133 @Override public long sign_gossip_message(long msg) {
134 org.ldk.structs.UnsignedGossipMessage msg_hu_conv = org.ldk.structs.UnsignedGossipMessage.constr_from_ptr(msg);
135 if (msg_hu_conv != null) { msg_hu_conv.ptrs_to.add(this); };
136 Result_SignatureNoneZ ret = arg.sign_gossip_message(msg_hu_conv);
137 Reference.reachabilityFence(arg);
138 long result = ret == null ? 0 : ret.clone_ptr();
142 return impl_holder.held;
145 * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
147 * If the implementor of this trait supports [phantom node payments], then every node that is
148 * intended to be included in the phantom invoice route hints must return the same value from
151 * This method must return the same value each time it is called.
153 * [phantom node payments]: PhantomKeysManager
155 public byte[] get_inbound_payment_key_material() {
156 byte[] ret = bindings.NodeSigner_get_inbound_payment_key_material(this.ptr);
157 Reference.reachabilityFence(this);
162 * Get node id based on the provided [`Recipient`].
164 * This method must return the same value each time it is called with a given [`Recipient`]
167 * Errors if the [`Recipient`] variant is not supported by the implementation.
169 public Result_PublicKeyNoneZ get_node_id(org.ldk.enums.Recipient recipient) {
170 long ret = bindings.NodeSigner_get_node_id(this.ptr, recipient);
171 Reference.reachabilityFence(this);
172 Reference.reachabilityFence(recipient);
173 if (ret >= 0 && ret <= 4096) { return null; }
174 Result_PublicKeyNoneZ ret_hu_conv = Result_PublicKeyNoneZ.constr_from_ptr(ret);
179 * Gets the ECDH shared secret of our node secret and `other_key`, multiplying by `tweak` if
180 * one is provided. Note that this tweak can be applied to `other_key` instead of our node
181 * secret, though this is less efficient.
183 * Note that if this fails while attempting to forward an HTLC, LDK will panic. The error
184 * should be resolved to allow LDK to resume forwarding HTLCs.
186 * Errors if the [`Recipient`] variant is not supported by the implementation.
188 public Result_SharedSecretNoneZ ecdh(org.ldk.enums.Recipient recipient, byte[] other_key, org.ldk.structs.Option_ScalarZ tweak) {
189 long ret = bindings.NodeSigner_ecdh(this.ptr, recipient, InternalUtils.check_arr_len(other_key, 33), tweak.ptr);
190 Reference.reachabilityFence(this);
191 Reference.reachabilityFence(recipient);
192 Reference.reachabilityFence(other_key);
193 Reference.reachabilityFence(tweak);
194 if (ret >= 0 && ret <= 4096) { return null; }
195 Result_SharedSecretNoneZ ret_hu_conv = Result_SharedSecretNoneZ.constr_from_ptr(ret);
196 if (this != null) { this.ptrs_to.add(tweak); };
203 * By parameterizing by the raw invoice bytes instead of the hash, we allow implementors of
204 * this trait to parse the invoice and make sure they're signing what they expect, rather than
205 * blindly signing the hash.
207 * The `hrp_bytes` are ASCII bytes, while the `invoice_data` is base32.
209 * The secret key used to sign the invoice is dependent on the [`Recipient`].
211 * Errors if the [`Recipient`] variant is not supported by the implementation.
213 public Result_RecoverableSignatureNoneZ sign_invoice(byte[] hrp_bytes, UInt5[] invoice_data, org.ldk.enums.Recipient recipient) {
214 long ret = bindings.NodeSigner_sign_invoice(this.ptr, hrp_bytes, invoice_data != null ? InternalUtils.convUInt5Array(invoice_data) : null, recipient);
215 Reference.reachabilityFence(this);
216 Reference.reachabilityFence(hrp_bytes);
217 Reference.reachabilityFence(invoice_data);
218 Reference.reachabilityFence(recipient);
219 if (ret >= 0 && ret <= 4096) { return null; }
220 Result_RecoverableSignatureNoneZ ret_hu_conv = Result_RecoverableSignatureNoneZ.constr_from_ptr(ret);
225 * Sign a gossip message.
227 * Note that if this fails, LDK may panic and the message will not be broadcast to the network
228 * or a possible channel counterparty. If LDK panics, the error should be resolved to allow the
229 * message to be broadcast, as otherwise it may prevent one from receiving funds over the
230 * corresponding channel.
232 public Result_SignatureNoneZ sign_gossip_message(org.ldk.structs.UnsignedGossipMessage msg) {
233 long ret = bindings.NodeSigner_sign_gossip_message(this.ptr, msg.ptr);
234 Reference.reachabilityFence(this);
235 Reference.reachabilityFence(msg);
236 if (ret >= 0 && ret <= 4096) { return null; }
237 Result_SignatureNoneZ ret_hu_conv = Result_SignatureNoneZ.constr_from_ptr(ret);
238 if (this != null) { this.ptrs_to.add(msg); };