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`]: KeysInterface::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 Sign get_channel_signer(boolean inbound, long channel_value_satoshis);
83 * Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
84 * onion packets and for temporary channel IDs. There is no requirement that these be
85 * persisted anywhere, though they must be unique across restarts.
87 * This method must return a different value each time it is called.
89 byte[] get_secure_random_bytes();
91 * Reads a `Signer` for this `KeysInterface` from the given input stream.
92 * This is only called during deserialization of other objects which contain
93 * `Sign`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s).
94 * The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
95 * contain no versioning scheme. You may wish to include your own version prefix and ensure
96 * you've read all of the provided bytes to ensure no corruption occurred.
98 Result_SignDecodeErrorZ read_chan_signer(byte[] reader);
101 * By parameterizing by the raw invoice bytes instead of the hash, we allow implementors of
102 * this trait to parse the invoice and make sure they're signing what they expect, rather than
103 * blindly signing the hash.
104 * The hrp is ascii bytes, while the invoice data is base32.
106 * The secret key used to sign the invoice is dependent on the [`Recipient`].
108 * Errors if the `Recipient` variant is not supported by the implementation.
110 Result_RecoverableSignatureNoneZ sign_invoice(byte[] hrp_bytes, UInt5[] invoice_data, Recipient receipient);
112 * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
114 * If the implementor of this trait supports [phantom node payments], then every node that is
115 * intended to be included in the phantom invoice route hints must return the same value from
118 * This method must return the same value each time it is called.
120 * [phantom node payments]: PhantomKeysManager
122 byte[] get_inbound_payment_key_material();
124 private static class LDKKeysInterfaceHolder { KeysInterface held; }
125 public static KeysInterface new_impl(KeysInterfaceInterface arg) {
126 final LDKKeysInterfaceHolder impl_holder = new LDKKeysInterfaceHolder();
127 impl_holder.held = new KeysInterface(new bindings.LDKKeysInterface() {
128 @Override public long get_node_secret(Recipient recipient) {
129 Result_SecretKeyNoneZ ret = arg.get_node_secret(recipient);
130 Reference.reachabilityFence(arg);
131 long result = ret == null ? 0 : ret.clone_ptr();
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 == null ? 0 : ret.clone_ptr();
140 @Override public long ecdh(Recipient recipient, byte[] other_key, long tweak) {
141 org.ldk.structs.Option_ScalarZ tweak_hu_conv = org.ldk.structs.Option_ScalarZ.constr_from_ptr(tweak);
142 if (tweak_hu_conv != null) { tweak_hu_conv.ptrs_to.add(this); };
143 Result_SharedSecretNoneZ ret = arg.ecdh(recipient, other_key, tweak_hu_conv);
144 Reference.reachabilityFence(arg);
145 long result = ret == null ? 0 : ret.clone_ptr();
148 @Override public byte[] get_destination_script() {
149 byte[] ret = arg.get_destination_script();
150 Reference.reachabilityFence(arg);
153 @Override public long get_shutdown_scriptpubkey() {
154 ShutdownScript ret = arg.get_shutdown_scriptpubkey();
155 Reference.reachabilityFence(arg);
156 long result = ret == null ? 0 : ret.clone_ptr();
159 @Override public long get_channel_signer(boolean inbound, long channel_value_satoshis) {
160 Sign ret = arg.get_channel_signer(inbound, channel_value_satoshis);
161 Reference.reachabilityFence(arg);
162 long result = ret == null ? 0 : ret.clone_ptr();
163 if (impl_holder.held != null) { impl_holder.held.ptrs_to.add(ret); };
166 @Override public byte[] get_secure_random_bytes() {
167 byte[] ret = arg.get_secure_random_bytes();
168 Reference.reachabilityFence(arg);
169 byte[] result = InternalUtils.check_arr_len(ret, 32);
172 @Override public long read_chan_signer(byte[] reader) {
173 Result_SignDecodeErrorZ ret = arg.read_chan_signer(reader);
174 Reference.reachabilityFence(arg);
175 long result = ret == null ? 0 : ret.clone_ptr();
178 @Override public long sign_invoice(byte[] hrp_bytes, byte[] invoice_data, Recipient receipient) {
179 int invoice_data_conv_7_len = invoice_data.length;
180 UInt5[] invoice_data_conv_7_arr = new UInt5[invoice_data_conv_7_len];
181 for (int h = 0; h < invoice_data_conv_7_len; h++) {
182 byte invoice_data_conv_7 = invoice_data[h];
183 UInt5 invoice_data_conv_7_conv = new UInt5(invoice_data_conv_7);
184 invoice_data_conv_7_arr[h] = invoice_data_conv_7_conv;
186 Result_RecoverableSignatureNoneZ ret = arg.sign_invoice(hrp_bytes, invoice_data_conv_7_arr, receipient);
187 Reference.reachabilityFence(arg);
188 long result = ret == null ? 0 : ret.clone_ptr();
191 @Override public byte[] get_inbound_payment_key_material() {
192 byte[] ret = arg.get_inbound_payment_key_material();
193 Reference.reachabilityFence(arg);
194 byte[] result = InternalUtils.check_arr_len(ret, 32);
198 return impl_holder.held;
201 * Get node secret key based on the provided [`Recipient`].
203 * The node_id/network_key is the public key that corresponds to this secret key.
205 * This method must return the same value each time it is called with a given `Recipient`
208 * Errors if the `Recipient` variant is not supported by the implementation.
210 public Result_SecretKeyNoneZ get_node_secret(org.ldk.enums.Recipient recipient) {
211 long ret = bindings.KeysInterface_get_node_secret(this.ptr, recipient);
212 Reference.reachabilityFence(this);
213 Reference.reachabilityFence(recipient);
214 if (ret >= 0 && ret <= 4096) { return null; }
215 Result_SecretKeyNoneZ ret_hu_conv = Result_SecretKeyNoneZ.constr_from_ptr(ret);
220 * Get node id based on the provided [`Recipient`]. This public key corresponds to the secret in
221 * [`get_node_secret`].
223 * This method must return the same value each time it is called with a given `Recipient`
226 * Errors if the `Recipient` variant is not supported by the implementation.
228 * [`get_node_secret`]: KeysInterface::get_node_secret
230 public Result_PublicKeyNoneZ get_node_id(org.ldk.enums.Recipient recipient) {
231 long ret = bindings.KeysInterface_get_node_id(this.ptr, recipient);
232 Reference.reachabilityFence(this);
233 Reference.reachabilityFence(recipient);
234 if (ret >= 0 && ret <= 4096) { return null; }
235 Result_PublicKeyNoneZ ret_hu_conv = Result_PublicKeyNoneZ.constr_from_ptr(ret);
240 * Gets the ECDH shared secret of our [`node secret`] and `other_key`, multiplying by `tweak` if
241 * one is provided. Note that this tweak can be applied to `other_key` instead of our node
242 * secret, though this is less efficient.
244 * Errors if the `Recipient` variant is not supported by the implementation.
246 * [`node secret`]: Self::get_node_secret
248 public Result_SharedSecretNoneZ ecdh(org.ldk.enums.Recipient recipient, byte[] other_key, Option_ScalarZ tweak) {
249 long ret = bindings.KeysInterface_ecdh(this.ptr, recipient, InternalUtils.check_arr_len(other_key, 33), tweak.ptr);
250 Reference.reachabilityFence(this);
251 Reference.reachabilityFence(recipient);
252 Reference.reachabilityFence(other_key);
253 Reference.reachabilityFence(tweak);
254 if (ret >= 0 && ret <= 4096) { return null; }
255 Result_SharedSecretNoneZ ret_hu_conv = Result_SharedSecretNoneZ.constr_from_ptr(ret);
260 * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
262 * This method should return a different value each time it is called, to avoid linking
263 * on-chain funds across channels as controlled to the same user.
265 public byte[] get_destination_script() {
266 byte[] ret = bindings.KeysInterface_get_destination_script(this.ptr);
267 Reference.reachabilityFence(this);
272 * Get a script pubkey which we will send funds to when closing a channel.
274 * This method should return a different value each time it is called, to avoid linking
275 * on-chain funds across channels as controlled to the same user.
277 public ShutdownScript get_shutdown_scriptpubkey() {
278 long ret = bindings.KeysInterface_get_shutdown_scriptpubkey(this.ptr);
279 Reference.reachabilityFence(this);
280 if (ret >= 0 && ret <= 4096) { return null; }
281 org.ldk.structs.ShutdownScript ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ShutdownScript(null, ret); }
282 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
287 * Get a new set of Sign for per-channel secrets. These MUST be unique even if you
288 * restarted with some stale data!
290 * This method must return a different value each time it is called.
292 public Sign get_channel_signer(boolean inbound, long channel_value_satoshis) {
293 long ret = bindings.KeysInterface_get_channel_signer(this.ptr, inbound, channel_value_satoshis);
294 Reference.reachabilityFence(this);
295 Reference.reachabilityFence(inbound);
296 Reference.reachabilityFence(channel_value_satoshis);
297 if (ret >= 0 && ret <= 4096) { return null; }
298 Sign ret_hu_conv = new Sign(null, ret);
299 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
304 * Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
305 * onion packets and for temporary channel IDs. There is no requirement that these be
306 * persisted anywhere, though they must be unique across restarts.
308 * This method must return a different value each time it is called.
310 public byte[] get_secure_random_bytes() {
311 byte[] ret = bindings.KeysInterface_get_secure_random_bytes(this.ptr);
312 Reference.reachabilityFence(this);
317 * Reads a `Signer` for this `KeysInterface` from the given input stream.
318 * This is only called during deserialization of other objects which contain
319 * `Sign`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s).
320 * The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
321 * contain no versioning scheme. You may wish to include your own version prefix and ensure
322 * you've read all of the provided bytes to ensure no corruption occurred.
324 public Result_SignDecodeErrorZ read_chan_signer(byte[] reader) {
325 long ret = bindings.KeysInterface_read_chan_signer(this.ptr, reader);
326 Reference.reachabilityFence(this);
327 Reference.reachabilityFence(reader);
328 if (ret >= 0 && ret <= 4096) { return null; }
329 Result_SignDecodeErrorZ ret_hu_conv = Result_SignDecodeErrorZ.constr_from_ptr(ret);
335 * By parameterizing by the raw invoice bytes instead of the hash, we allow implementors of
336 * this trait to parse the invoice and make sure they're signing what they expect, rather than
337 * blindly signing the hash.
338 * The hrp is ascii bytes, while the invoice data is base32.
340 * The secret key used to sign the invoice is dependent on the [`Recipient`].
342 * Errors if the `Recipient` variant is not supported by the implementation.
344 public Result_RecoverableSignatureNoneZ sign_invoice(byte[] hrp_bytes, UInt5[] invoice_data, org.ldk.enums.Recipient receipient) {
345 long ret = bindings.KeysInterface_sign_invoice(this.ptr, hrp_bytes, invoice_data != null ? InternalUtils.convUInt5Array(invoice_data) : null, receipient);
346 Reference.reachabilityFence(this);
347 Reference.reachabilityFence(hrp_bytes);
348 Reference.reachabilityFence(invoice_data);
349 Reference.reachabilityFence(receipient);
350 if (ret >= 0 && ret <= 4096) { return null; }
351 Result_RecoverableSignatureNoneZ ret_hu_conv = Result_RecoverableSignatureNoneZ.constr_from_ptr(ret);
356 * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
358 * If the implementor of this trait supports [phantom node payments], then every node that is
359 * intended to be included in the phantom invoice route hints must return the same value from
362 * This method must return the same value each time it is called.
364 * [phantom node payments]: PhantomKeysManager
366 public byte[] get_inbound_payment_key_material() {
367 byte[] ret = bindings.KeysInterface_get_inbound_payment_key_material(this.ptr);
368 Reference.reachabilityFence(this);