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 describe an object which can get user secrets and key material.
13 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
14 public class KeysInterface extends CommonBase {
15 final bindings.LDKKeysInterface bindings_instance;
16 KeysInterface(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
17 private KeysInterface(bindings.LDKKeysInterface arg) {
18 super(bindings.LDKKeysInterface_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.KeysInterface_free(ptr); } super.finalize();
27 public static interface KeysInterfaceInterface {
29 * Get node secret key based on the provided [`Recipient`].
31 * The `node_id`/`network_key` is the public key that corresponds to this secret key.
33 * This method must return the same value each time it is called with a given [`Recipient`]
36 * Errors if the [`Recipient`] variant is not supported by the implementation.
38 Result_SecretKeyNoneZ get_node_secret(Recipient recipient);
40 * Get node id based on the provided [`Recipient`]. This public key corresponds to the secret in
41 * [`get_node_secret`].
43 * This method must return the same value each time it is called with a given [`Recipient`]
46 * Errors if the [`Recipient`] variant is not supported by the implementation.
48 * [`get_node_secret`]: Self::get_node_secret
50 Result_PublicKeyNoneZ get_node_id(Recipient recipient);
52 * Gets the ECDH shared secret of our [`node secret`] and `other_key`, multiplying by `tweak` if
53 * one is provided. Note that this tweak can be applied to `other_key` instead of our node
54 * secret, though this is less efficient.
56 * Errors if the [`Recipient`] variant is not supported by the implementation.
58 * [`node secret`]: Self::get_node_secret
60 Result_SharedSecretNoneZ ecdh(Recipient recipient, byte[] other_key, Option_ScalarZ tweak);
62 * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
64 * This method should return a different value each time it is called, to avoid linking
65 * on-chain funds across channels as controlled to the same user.
67 byte[] get_destination_script();
69 * Get a script pubkey which we will send funds to when closing a channel.
71 * This method should return a different value each time it is called, to avoid linking
72 * on-chain funds across channels as controlled to the same user.
74 ShutdownScript get_shutdown_scriptpubkey();
76 * Get a new set of [`Sign`] for per-channel secrets. These MUST be unique even if you
77 * restarted with some stale data!
79 * This method must return a different value each time it is called.
81 byte[] generate_channel_keys_id(boolean inbound, long channel_value_satoshis, UInt128 user_channel_id);
83 * Derives the private key material backing a `Signer`.
85 * To derive a new `Signer`, a fresh `channel_keys_id` should be obtained through
86 * [`KeysInterface::generate_channel_keys_id`]. Otherwise, an existing `Signer` can be
87 * re-derived from its `channel_keys_id`, which can be obtained through its trait method
88 * [`BaseSign::channel_keys_id`].
90 Sign derive_channel_signer(long channel_value_satoshis, byte[] channel_keys_id);
92 * Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
93 * onion packets and for temporary channel IDs. There is no requirement that these be
94 * persisted anywhere, though they must be unique across restarts.
96 * This method must return a different value each time it is called.
98 byte[] get_secure_random_bytes();
100 * Reads a [`Signer`] for this [`KeysInterface`] from the given input stream.
101 * This is only called during deserialization of other objects which contain
102 * [`Sign`]-implementing objects (i.e., [`ChannelMonitor`]s and [`ChannelManager`]s).
103 * The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
104 * contain no versioning scheme. You may wish to include your own version prefix and ensure
105 * you've read all of the provided bytes to ensure no corruption occurred.
107 * This method is slowly being phased out -- it will only be called when reading objects
108 * written by LDK versions prior to 0.0.113.
110 * [`Signer`]: Self::Signer
111 * [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
112 * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
114 Result_SignDecodeErrorZ read_chan_signer(byte[] reader);
117 * By parameterizing by the raw invoice bytes instead of the hash, we allow implementors of
118 * this trait to parse the invoice and make sure they're signing what they expect, rather than
119 * blindly signing the hash.
120 * The `hrp` is ASCII bytes, while the invoice data is base32-encoded.
122 * The secret key used to sign the invoice is dependent on the [`Recipient`].
124 * Errors if the [`Recipient`] variant is not supported by the implementation.
126 Result_RecoverableSignatureNoneZ sign_invoice(byte[] hrp_bytes, UInt5[] invoice_data, Recipient receipient);
128 * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
130 * If the implementor of this trait supports [phantom node payments], then every node that is
131 * intended to be included in the phantom invoice route hints must return the same value from
134 * This method must return the same value each time it is called.
136 * [phantom node payments]: PhantomKeysManager
138 byte[] get_inbound_payment_key_material();
140 private static class LDKKeysInterfaceHolder { KeysInterface held; }
141 public static KeysInterface new_impl(KeysInterfaceInterface arg) {
142 final LDKKeysInterfaceHolder impl_holder = new LDKKeysInterfaceHolder();
143 impl_holder.held = new KeysInterface(new bindings.LDKKeysInterface() {
144 @Override public long get_node_secret(Recipient recipient) {
145 Result_SecretKeyNoneZ ret = arg.get_node_secret(recipient);
146 Reference.reachabilityFence(arg);
147 long result = ret == null ? 0 : ret.clone_ptr();
150 @Override public long get_node_id(Recipient recipient) {
151 Result_PublicKeyNoneZ ret = arg.get_node_id(recipient);
152 Reference.reachabilityFence(arg);
153 long result = ret == null ? 0 : ret.clone_ptr();
156 @Override public long ecdh(Recipient recipient, byte[] other_key, long tweak) {
157 org.ldk.structs.Option_ScalarZ tweak_hu_conv = org.ldk.structs.Option_ScalarZ.constr_from_ptr(tweak);
158 if (tweak_hu_conv != null) { tweak_hu_conv.ptrs_to.add(this); };
159 Result_SharedSecretNoneZ ret = arg.ecdh(recipient, other_key, tweak_hu_conv);
160 Reference.reachabilityFence(arg);
161 long result = ret == null ? 0 : ret.clone_ptr();
164 @Override public byte[] get_destination_script() {
165 byte[] ret = arg.get_destination_script();
166 Reference.reachabilityFence(arg);
169 @Override public long get_shutdown_scriptpubkey() {
170 ShutdownScript ret = arg.get_shutdown_scriptpubkey();
171 Reference.reachabilityFence(arg);
172 long result = ret == null ? 0 : ret.clone_ptr();
175 @Override public byte[] generate_channel_keys_id(boolean inbound, long channel_value_satoshis, byte[] user_channel_id) {
176 org.ldk.util.UInt128 user_channel_id_conv = new org.ldk.util.UInt128(user_channel_id);
177 byte[] ret = arg.generate_channel_keys_id(inbound, channel_value_satoshis, user_channel_id_conv);
178 Reference.reachabilityFence(arg);
179 byte[] result = InternalUtils.check_arr_len(ret, 32);
182 @Override public long derive_channel_signer(long channel_value_satoshis, byte[] channel_keys_id) {
183 Sign ret = arg.derive_channel_signer(channel_value_satoshis, channel_keys_id);
184 Reference.reachabilityFence(arg);
185 long result = ret == null ? 0 : ret.clone_ptr();
186 if (impl_holder.held != null) { impl_holder.held.ptrs_to.add(ret); };
189 @Override public byte[] get_secure_random_bytes() {
190 byte[] ret = arg.get_secure_random_bytes();
191 Reference.reachabilityFence(arg);
192 byte[] result = InternalUtils.check_arr_len(ret, 32);
195 @Override public long read_chan_signer(byte[] reader) {
196 Result_SignDecodeErrorZ ret = arg.read_chan_signer(reader);
197 Reference.reachabilityFence(arg);
198 long result = ret == null ? 0 : ret.clone_ptr();
201 @Override public long sign_invoice(byte[] hrp_bytes, byte[] invoice_data, Recipient receipient) {
202 int invoice_data_conv_7_len = invoice_data.length;
203 UInt5[] invoice_data_conv_7_arr = new UInt5[invoice_data_conv_7_len];
204 for (int h = 0; h < invoice_data_conv_7_len; h++) {
205 byte invoice_data_conv_7 = invoice_data[h];
206 UInt5 invoice_data_conv_7_conv = new UInt5(invoice_data_conv_7);
207 invoice_data_conv_7_arr[h] = invoice_data_conv_7_conv;
209 Result_RecoverableSignatureNoneZ ret = arg.sign_invoice(hrp_bytes, invoice_data_conv_7_arr, receipient);
210 Reference.reachabilityFence(arg);
211 long result = ret == null ? 0 : ret.clone_ptr();
214 @Override public byte[] get_inbound_payment_key_material() {
215 byte[] ret = arg.get_inbound_payment_key_material();
216 Reference.reachabilityFence(arg);
217 byte[] result = InternalUtils.check_arr_len(ret, 32);
221 return impl_holder.held;
224 * Get node secret key based on the provided [`Recipient`].
226 * The `node_id`/`network_key` is the public key that corresponds to this secret key.
228 * This method must return the same value each time it is called with a given [`Recipient`]
231 * Errors if the [`Recipient`] variant is not supported by the implementation.
233 public Result_SecretKeyNoneZ get_node_secret(org.ldk.enums.Recipient recipient) {
234 long ret = bindings.KeysInterface_get_node_secret(this.ptr, recipient);
235 Reference.reachabilityFence(this);
236 Reference.reachabilityFence(recipient);
237 if (ret >= 0 && ret <= 4096) { return null; }
238 Result_SecretKeyNoneZ ret_hu_conv = Result_SecretKeyNoneZ.constr_from_ptr(ret);
243 * Get node id based on the provided [`Recipient`]. This public key corresponds to the secret in
244 * [`get_node_secret`].
246 * This method must return the same value each time it is called with a given [`Recipient`]
249 * Errors if the [`Recipient`] variant is not supported by the implementation.
251 * [`get_node_secret`]: Self::get_node_secret
253 public Result_PublicKeyNoneZ get_node_id(org.ldk.enums.Recipient recipient) {
254 long ret = bindings.KeysInterface_get_node_id(this.ptr, recipient);
255 Reference.reachabilityFence(this);
256 Reference.reachabilityFence(recipient);
257 if (ret >= 0 && ret <= 4096) { return null; }
258 Result_PublicKeyNoneZ ret_hu_conv = Result_PublicKeyNoneZ.constr_from_ptr(ret);
263 * Gets the ECDH shared secret of our [`node secret`] and `other_key`, multiplying by `tweak` if
264 * one is provided. Note that this tweak can be applied to `other_key` instead of our node
265 * secret, though this is less efficient.
267 * Errors if the [`Recipient`] variant is not supported by the implementation.
269 * [`node secret`]: Self::get_node_secret
271 public Result_SharedSecretNoneZ ecdh(org.ldk.enums.Recipient recipient, byte[] other_key, org.ldk.structs.Option_ScalarZ tweak) {
272 long ret = bindings.KeysInterface_ecdh(this.ptr, recipient, InternalUtils.check_arr_len(other_key, 33), tweak.ptr);
273 Reference.reachabilityFence(this);
274 Reference.reachabilityFence(recipient);
275 Reference.reachabilityFence(other_key);
276 Reference.reachabilityFence(tweak);
277 if (ret >= 0 && ret <= 4096) { return null; }
278 Result_SharedSecretNoneZ ret_hu_conv = Result_SharedSecretNoneZ.constr_from_ptr(ret);
283 * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
285 * This method should return a different value each time it is called, to avoid linking
286 * on-chain funds across channels as controlled to the same user.
288 public byte[] get_destination_script() {
289 byte[] ret = bindings.KeysInterface_get_destination_script(this.ptr);
290 Reference.reachabilityFence(this);
295 * Get a script pubkey which we will send funds to when closing a channel.
297 * This method should return a different value each time it is called, to avoid linking
298 * on-chain funds across channels as controlled to the same user.
300 public ShutdownScript get_shutdown_scriptpubkey() {
301 long ret = bindings.KeysInterface_get_shutdown_scriptpubkey(this.ptr);
302 Reference.reachabilityFence(this);
303 if (ret >= 0 && ret <= 4096) { return null; }
304 org.ldk.structs.ShutdownScript ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ShutdownScript(null, ret); }
305 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
310 * Get a new set of [`Sign`] for per-channel secrets. These MUST be unique even if you
311 * restarted with some stale data!
313 * This method must return a different value each time it is called.
315 public byte[] generate_channel_keys_id(boolean inbound, long channel_value_satoshis, org.ldk.util.UInt128 user_channel_id) {
316 byte[] ret = bindings.KeysInterface_generate_channel_keys_id(this.ptr, inbound, channel_value_satoshis, user_channel_id.getLEBytes());
317 Reference.reachabilityFence(this);
318 Reference.reachabilityFence(inbound);
319 Reference.reachabilityFence(channel_value_satoshis);
320 Reference.reachabilityFence(user_channel_id);
325 * Derives the private key material backing a `Signer`.
327 * To derive a new `Signer`, a fresh `channel_keys_id` should be obtained through
328 * [`KeysInterface::generate_channel_keys_id`]. Otherwise, an existing `Signer` can be
329 * re-derived from its `channel_keys_id`, which can be obtained through its trait method
330 * [`BaseSign::channel_keys_id`].
332 public Sign derive_channel_signer(long channel_value_satoshis, byte[] channel_keys_id) {
333 long ret = bindings.KeysInterface_derive_channel_signer(this.ptr, channel_value_satoshis, InternalUtils.check_arr_len(channel_keys_id, 32));
334 Reference.reachabilityFence(this);
335 Reference.reachabilityFence(channel_value_satoshis);
336 Reference.reachabilityFence(channel_keys_id);
337 if (ret >= 0 && ret <= 4096) { return null; }
338 Sign ret_hu_conv = new Sign(null, ret);
339 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
344 * Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
345 * onion packets and for temporary channel IDs. There is no requirement that these be
346 * persisted anywhere, though they must be unique across restarts.
348 * This method must return a different value each time it is called.
350 public byte[] get_secure_random_bytes() {
351 byte[] ret = bindings.KeysInterface_get_secure_random_bytes(this.ptr);
352 Reference.reachabilityFence(this);
357 * Reads a [`Signer`] for this [`KeysInterface`] from the given input stream.
358 * This is only called during deserialization of other objects which contain
359 * [`Sign`]-implementing objects (i.e., [`ChannelMonitor`]s and [`ChannelManager`]s).
360 * The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
361 * contain no versioning scheme. You may wish to include your own version prefix and ensure
362 * you've read all of the provided bytes to ensure no corruption occurred.
364 * This method is slowly being phased out -- it will only be called when reading objects
365 * written by LDK versions prior to 0.0.113.
367 * [`Signer`]: Self::Signer
368 * [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
369 * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
371 public Result_SignDecodeErrorZ read_chan_signer(byte[] reader) {
372 long ret = bindings.KeysInterface_read_chan_signer(this.ptr, reader);
373 Reference.reachabilityFence(this);
374 Reference.reachabilityFence(reader);
375 if (ret >= 0 && ret <= 4096) { return null; }
376 Result_SignDecodeErrorZ ret_hu_conv = Result_SignDecodeErrorZ.constr_from_ptr(ret);
382 * By parameterizing by the raw invoice bytes instead of the hash, we allow implementors of
383 * this trait to parse the invoice and make sure they're signing what they expect, rather than
384 * blindly signing the hash.
385 * The `hrp` is ASCII bytes, while the invoice data is base32-encoded.
387 * The secret key used to sign the invoice is dependent on the [`Recipient`].
389 * Errors if the [`Recipient`] variant is not supported by the implementation.
391 public Result_RecoverableSignatureNoneZ sign_invoice(byte[] hrp_bytes, UInt5[] invoice_data, org.ldk.enums.Recipient receipient) {
392 long ret = bindings.KeysInterface_sign_invoice(this.ptr, hrp_bytes, invoice_data != null ? InternalUtils.convUInt5Array(invoice_data) : null, receipient);
393 Reference.reachabilityFence(this);
394 Reference.reachabilityFence(hrp_bytes);
395 Reference.reachabilityFence(invoice_data);
396 Reference.reachabilityFence(receipient);
397 if (ret >= 0 && ret <= 4096) { return null; }
398 Result_RecoverableSignatureNoneZ ret_hu_conv = Result_RecoverableSignatureNoneZ.constr_from_ptr(ret);
403 * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
405 * If the implementor of this trait supports [phantom node payments], then every node that is
406 * intended to be included in the phantom invoice route hints must return the same value from
409 * This method must return the same value each time it is called.
411 * [phantom node payments]: PhantomKeysManager
413 public byte[] get_inbound_payment_key_material() {
414 byte[] ret = bindings.KeysInterface_get_inbound_payment_key_material(this.ptr);
415 Reference.reachabilityFence(this);