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_ThirtyTwoBytesNoneZ ecdh(Recipient recipient, byte[] other_key, Option_BigEndianScalarZ 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 * Signs the [`TaggedHash`] of a BOLT 12 invoice request.
89 * May be called by a function passed to [`UnsignedInvoiceRequest::sign`] where
90 * `invoice_request` is the callee.
92 * Implementors may check that the `invoice_request` is expected rather than blindly signing
93 * the tagged hash. An `Ok` result should sign `invoice_request.tagged_hash().as_digest()` with
94 * the node's signing key or an ephemeral key to preserve privacy, whichever is associated with
95 * [`UnsignedInvoiceRequest::payer_id`].
97 * [`TaggedHash`]: crate::offers::merkle::TaggedHash
99 Result_SchnorrSignatureNoneZ sign_bolt12_invoice_request(UnsignedInvoiceRequest invoice_request);
101 * Signs the [`TaggedHash`] of a BOLT 12 invoice.
103 * May be called by a function passed to [`UnsignedBolt12Invoice::sign`] where `invoice` is the
106 * Implementors may check that the `invoice` is expected rather than blindly signing the tagged
107 * hash. An `Ok` result should sign `invoice.tagged_hash().as_digest()` with the node's signing
108 * key or an ephemeral key to preserve privacy, whichever is associated with
109 * [`UnsignedBolt12Invoice::signing_pubkey`].
111 * [`TaggedHash`]: crate::offers::merkle::TaggedHash
113 Result_SchnorrSignatureNoneZ sign_bolt12_invoice(UnsignedBolt12Invoice invoice);
115 * Sign a gossip message.
117 * Note that if this fails, LDK may panic and the message will not be broadcast to the network
118 * or a possible channel counterparty. If LDK panics, the error should be resolved to allow the
119 * message to be broadcast, as otherwise it may prevent one from receiving funds over the
120 * corresponding channel.
122 Result_ECDSASignatureNoneZ sign_gossip_message(UnsignedGossipMessage msg);
124 private static class LDKNodeSignerHolder { NodeSigner held; }
125 public static NodeSigner new_impl(NodeSignerInterface arg) {
126 final LDKNodeSignerHolder impl_holder = new LDKNodeSignerHolder();
127 impl_holder.held = new NodeSigner(new bindings.LDKNodeSigner() {
128 @Override public byte[] get_inbound_payment_key_material() {
129 byte[] ret = arg.get_inbound_payment_key_material();
130 Reference.reachabilityFence(arg);
131 byte[] result = InternalUtils.check_arr_len(ret, 32);
134 @Override public long get_node_id(Recipient recipient) {
135 Result_PublicKeyNoneZ ret = arg.get_node_id(recipient);
136 Reference.reachabilityFence(arg);
137 long result = ret.clone_ptr();
140 @Override public long ecdh(Recipient recipient, byte[] other_key, long tweak) {
141 org.ldk.structs.Option_BigEndianScalarZ tweak_hu_conv = org.ldk.structs.Option_BigEndianScalarZ.constr_from_ptr(tweak);
142 if (tweak_hu_conv != null) { tweak_hu_conv.ptrs_to.add(this); };
143 Result_ThirtyTwoBytesNoneZ ret = arg.ecdh(recipient, other_key, tweak_hu_conv);
144 Reference.reachabilityFence(arg);
145 long result = ret.clone_ptr();
148 @Override public long sign_invoice(byte[] hrp_bytes, byte[] invoice_data, Recipient recipient) {
149 int invoice_data_conv_7_len = invoice_data.length;
150 UInt5[] invoice_data_conv_7_arr = new UInt5[invoice_data_conv_7_len];
151 for (int h = 0; h < invoice_data_conv_7_len; h++) {
152 byte invoice_data_conv_7 = invoice_data[h];
153 UInt5 invoice_data_conv_7_conv = new UInt5(invoice_data_conv_7);
154 invoice_data_conv_7_arr[h] = invoice_data_conv_7_conv;
156 Result_RecoverableSignatureNoneZ ret = arg.sign_invoice(hrp_bytes, invoice_data_conv_7_arr, recipient);
157 Reference.reachabilityFence(arg);
158 long result = ret.clone_ptr();
161 @Override public long sign_bolt12_invoice_request(long invoice_request) {
162 org.ldk.structs.UnsignedInvoiceRequest invoice_request_hu_conv = null; if (invoice_request < 0 || invoice_request > 4096) { invoice_request_hu_conv = new org.ldk.structs.UnsignedInvoiceRequest(null, invoice_request); }
163 Result_SchnorrSignatureNoneZ ret = arg.sign_bolt12_invoice_request(invoice_request_hu_conv);
164 Reference.reachabilityFence(arg);
165 long result = ret.clone_ptr();
168 @Override public long sign_bolt12_invoice(long invoice) {
169 org.ldk.structs.UnsignedBolt12Invoice invoice_hu_conv = null; if (invoice < 0 || invoice > 4096) { invoice_hu_conv = new org.ldk.structs.UnsignedBolt12Invoice(null, invoice); }
170 Result_SchnorrSignatureNoneZ ret = arg.sign_bolt12_invoice(invoice_hu_conv);
171 Reference.reachabilityFence(arg);
172 long result = ret.clone_ptr();
175 @Override public long sign_gossip_message(long msg) {
176 org.ldk.structs.UnsignedGossipMessage msg_hu_conv = org.ldk.structs.UnsignedGossipMessage.constr_from_ptr(msg);
177 if (msg_hu_conv != null) { msg_hu_conv.ptrs_to.add(this); };
178 Result_ECDSASignatureNoneZ ret = arg.sign_gossip_message(msg_hu_conv);
179 Reference.reachabilityFence(arg);
180 long result = ret.clone_ptr();
184 return impl_holder.held;
187 * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
189 * If the implementor of this trait supports [phantom node payments], then every node that is
190 * intended to be included in the phantom invoice route hints must return the same value from
193 * This method must return the same value each time it is called.
195 * [phantom node payments]: PhantomKeysManager
197 public byte[] get_inbound_payment_key_material() {
198 byte[] ret = bindings.NodeSigner_get_inbound_payment_key_material(this.ptr);
199 Reference.reachabilityFence(this);
204 * Get node id based on the provided [`Recipient`].
206 * This method must return the same value each time it is called with a given [`Recipient`]
209 * Errors if the [`Recipient`] variant is not supported by the implementation.
211 public Result_PublicKeyNoneZ get_node_id(org.ldk.enums.Recipient recipient) {
212 long ret = bindings.NodeSigner_get_node_id(this.ptr, recipient);
213 Reference.reachabilityFence(this);
214 Reference.reachabilityFence(recipient);
215 if (ret >= 0 && ret <= 4096) { return null; }
216 Result_PublicKeyNoneZ ret_hu_conv = Result_PublicKeyNoneZ.constr_from_ptr(ret);
221 * Gets the ECDH shared secret of our node secret and `other_key`, multiplying by `tweak` if
222 * one is provided. Note that this tweak can be applied to `other_key` instead of our node
223 * secret, though this is less efficient.
225 * Note that if this fails while attempting to forward an HTLC, LDK will panic. The error
226 * should be resolved to allow LDK to resume forwarding HTLCs.
228 * Errors if the [`Recipient`] variant is not supported by the implementation.
230 public Result_ThirtyTwoBytesNoneZ ecdh(org.ldk.enums.Recipient recipient, byte[] other_key, org.ldk.structs.Option_BigEndianScalarZ tweak) {
231 long ret = bindings.NodeSigner_ecdh(this.ptr, recipient, InternalUtils.check_arr_len(other_key, 33), tweak.ptr);
232 Reference.reachabilityFence(this);
233 Reference.reachabilityFence(recipient);
234 Reference.reachabilityFence(other_key);
235 Reference.reachabilityFence(tweak);
236 if (ret >= 0 && ret <= 4096) { return null; }
237 Result_ThirtyTwoBytesNoneZ ret_hu_conv = Result_ThirtyTwoBytesNoneZ.constr_from_ptr(ret);
238 if (this != null) { this.ptrs_to.add(tweak); };
245 * By parameterizing by the raw invoice bytes instead of the hash, we allow implementors of
246 * this trait to parse the invoice and make sure they're signing what they expect, rather than
247 * blindly signing the hash.
249 * The `hrp_bytes` are ASCII bytes, while the `invoice_data` is base32.
251 * The secret key used to sign the invoice is dependent on the [`Recipient`].
253 * Errors if the [`Recipient`] variant is not supported by the implementation.
255 public Result_RecoverableSignatureNoneZ sign_invoice(byte[] hrp_bytes, UInt5[] invoice_data, org.ldk.enums.Recipient recipient) {
256 long ret = bindings.NodeSigner_sign_invoice(this.ptr, hrp_bytes, invoice_data != null ? InternalUtils.convUInt5Array(invoice_data) : null, recipient);
257 Reference.reachabilityFence(this);
258 Reference.reachabilityFence(hrp_bytes);
259 Reference.reachabilityFence(invoice_data);
260 Reference.reachabilityFence(recipient);
261 if (ret >= 0 && ret <= 4096) { return null; }
262 Result_RecoverableSignatureNoneZ ret_hu_conv = Result_RecoverableSignatureNoneZ.constr_from_ptr(ret);
267 * Signs the [`TaggedHash`] of a BOLT 12 invoice request.
269 * May be called by a function passed to [`UnsignedInvoiceRequest::sign`] where
270 * `invoice_request` is the callee.
272 * Implementors may check that the `invoice_request` is expected rather than blindly signing
273 * the tagged hash. An `Ok` result should sign `invoice_request.tagged_hash().as_digest()` with
274 * the node's signing key or an ephemeral key to preserve privacy, whichever is associated with
275 * [`UnsignedInvoiceRequest::payer_id`].
277 * [`TaggedHash`]: crate::offers::merkle::TaggedHash
279 public Result_SchnorrSignatureNoneZ sign_bolt12_invoice_request(org.ldk.structs.UnsignedInvoiceRequest invoice_request) {
280 long ret = bindings.NodeSigner_sign_bolt12_invoice_request(this.ptr, invoice_request.ptr);
281 Reference.reachabilityFence(this);
282 Reference.reachabilityFence(invoice_request);
283 if (ret >= 0 && ret <= 4096) { return null; }
284 Result_SchnorrSignatureNoneZ ret_hu_conv = Result_SchnorrSignatureNoneZ.constr_from_ptr(ret);
285 if (this != null) { this.ptrs_to.add(invoice_request); };
290 * Signs the [`TaggedHash`] of a BOLT 12 invoice.
292 * May be called by a function passed to [`UnsignedBolt12Invoice::sign`] where `invoice` is the
295 * Implementors may check that the `invoice` is expected rather than blindly signing the tagged
296 * hash. An `Ok` result should sign `invoice.tagged_hash().as_digest()` with the node's signing
297 * key or an ephemeral key to preserve privacy, whichever is associated with
298 * [`UnsignedBolt12Invoice::signing_pubkey`].
300 * [`TaggedHash`]: crate::offers::merkle::TaggedHash
302 public Result_SchnorrSignatureNoneZ sign_bolt12_invoice(org.ldk.structs.UnsignedBolt12Invoice invoice) {
303 long ret = bindings.NodeSigner_sign_bolt12_invoice(this.ptr, invoice.ptr);
304 Reference.reachabilityFence(this);
305 Reference.reachabilityFence(invoice);
306 if (ret >= 0 && ret <= 4096) { return null; }
307 Result_SchnorrSignatureNoneZ ret_hu_conv = Result_SchnorrSignatureNoneZ.constr_from_ptr(ret);
308 if (this != null) { this.ptrs_to.add(invoice); };
313 * Sign a gossip message.
315 * Note that if this fails, LDK may panic and the message will not be broadcast to the network
316 * or a possible channel counterparty. If LDK panics, the error should be resolved to allow the
317 * message to be broadcast, as otherwise it may prevent one from receiving funds over the
318 * corresponding channel.
320 public Result_ECDSASignatureNoneZ sign_gossip_message(org.ldk.structs.UnsignedGossipMessage msg) {
321 long ret = bindings.NodeSigner_sign_gossip_message(this.ptr, msg.ptr);
322 Reference.reachabilityFence(this);
323 Reference.reachabilityFence(msg);
324 if (ret >= 0 && ret <= 4096) { return null; }
325 Result_ECDSASignatureNoneZ ret_hu_conv = Result_ECDSASignatureNoneZ.constr_from_ptr(ret);
326 if (this != null) { this.ptrs_to.add(msg); };