[TS] Slightly improve logging in TS trait calls that fail
[ldk-java] / src / main / java / org / ldk / structs / KeysInterface.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10 /**
11  * A trait to describe an object which can get user secrets and key material.
12  */
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;
21         }
22         @Override @SuppressWarnings("deprecation")
23         protected void finalize() throws Throwable {
24                 if (ptr != 0) { bindings.KeysInterface_free(ptr); } super.finalize();
25         }
26
27         public static interface KeysInterfaceInterface {
28                 /**
29                  * Get node secret key based on the provided [`Recipient`].
30                  * 
31                  * The `node_id`/`network_key` is the public key that corresponds to this secret key.
32                  * 
33                  * This method must return the same value each time it is called with a given [`Recipient`]
34                  * parameter.
35                  * 
36                  * Errors if the [`Recipient`] variant is not supported by the implementation.
37                  */
38                 Result_SecretKeyNoneZ get_node_secret(Recipient recipient);
39                 /**
40                  * Get node id based on the provided [`Recipient`]. This public key corresponds to the secret in
41                  * [`get_node_secret`].
42                  * 
43                  * This method must return the same value each time it is called with a given [`Recipient`]
44                  * parameter.
45                  * 
46                  * Errors if the [`Recipient`] variant is not supported by the implementation.
47                  * 
48                  * [`get_node_secret`]: Self::get_node_secret
49                  */
50                 Result_PublicKeyNoneZ get_node_id(Recipient recipient);
51                 /**
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.
55                  * 
56                  * Errors if the [`Recipient`] variant is not supported by the implementation.
57                  * 
58                  * [`node secret`]: Self::get_node_secret
59                  */
60                 Result_SharedSecretNoneZ ecdh(Recipient recipient, byte[] other_key, Option_ScalarZ tweak);
61                 /**
62                  * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
63                  * 
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.
66                  */
67                 byte[] get_destination_script();
68                 /**
69                  * Get a script pubkey which we will send funds to when closing a channel.
70                  * 
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.
73                  */
74                 ShutdownScript get_shutdown_scriptpubkey();
75                 /**
76                  * Get a new set of [`Sign`] for per-channel secrets. These MUST be unique even if you
77                  * restarted with some stale data!
78                  * 
79                  * This method must return a different value each time it is called.
80                  */
81                 byte[] generate_channel_keys_id(boolean inbound, long channel_value_satoshis, UInt128 user_channel_id);
82                 /**
83                  * Derives the private key material backing a `Signer`.
84                  * 
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`].
89                  */
90                 Sign derive_channel_signer(long channel_value_satoshis, byte[] channel_keys_id);
91                 /**
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.
95                  * 
96                  * This method must return a different value each time it is called.
97                  */
98                 byte[] get_secure_random_bytes();
99                 /**
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.
106                  * 
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.
109                  * 
110                  * [`Signer`]: Self::Signer
111                  * [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
112                  * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
113                  */
114                 Result_SignDecodeErrorZ read_chan_signer(byte[] reader);
115                 /**
116                  * Sign an invoice.
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.
121                  * 
122                  * The secret key used to sign the invoice is dependent on the [`Recipient`].
123                  * 
124                  * Errors if the [`Recipient`] variant is not supported by the implementation.
125                  */
126                 Result_RecoverableSignatureNoneZ sign_invoice(byte[] hrp_bytes, UInt5[] invoice_data, Recipient receipient);
127                 /**
128                  * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
129                  * 
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
132                  * this method.
133                  * 
134                  * This method must return the same value each time it is called.
135                  * 
136                  * [phantom node payments]: PhantomKeysManager
137                  */
138                 byte[] get_inbound_payment_key_material();
139         }
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();
148                                 return result;
149                         }
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();
154                                 return result;
155                         }
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();
162                                 return result;
163                         }
164                         @Override public byte[] get_destination_script() {
165                                 byte[] ret = arg.get_destination_script();
166                                 Reference.reachabilityFence(arg);
167                                 return ret;
168                         }
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();
173                                 return result;
174                         }
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);
180                                 return result;
181                         }
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); };
187                                 return result;
188                         }
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);
193                                 return result;
194                         }
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();
199                                 return result;
200                         }
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;
208                                 }
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();
212                                 return result;
213                         }
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);
218                                 return result;
219                         }
220                 });
221                 return impl_holder.held;
222         }
223         /**
224          * Get node secret key based on the provided [`Recipient`].
225          * 
226          * The `node_id`/`network_key` is the public key that corresponds to this secret key.
227          * 
228          * This method must return the same value each time it is called with a given [`Recipient`]
229          * parameter.
230          * 
231          * Errors if the [`Recipient`] variant is not supported by the implementation.
232          */
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);
239                 return ret_hu_conv;
240         }
241
242         /**
243          * Get node id based on the provided [`Recipient`]. This public key corresponds to the secret in
244          * [`get_node_secret`].
245          * 
246          * This method must return the same value each time it is called with a given [`Recipient`]
247          * parameter.
248          * 
249          * Errors if the [`Recipient`] variant is not supported by the implementation.
250          * 
251          * [`get_node_secret`]: Self::get_node_secret
252          */
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);
259                 return ret_hu_conv;
260         }
261
262         /**
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.
266          * 
267          * Errors if the [`Recipient`] variant is not supported by the implementation.
268          * 
269          * [`node secret`]: Self::get_node_secret
270          */
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);
279                 return ret_hu_conv;
280         }
281
282         /**
283          * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
284          * 
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.
287          */
288         public byte[] get_destination_script() {
289                 byte[] ret = bindings.KeysInterface_get_destination_script(this.ptr);
290                 Reference.reachabilityFence(this);
291                 return ret;
292         }
293
294         /**
295          * Get a script pubkey which we will send funds to when closing a channel.
296          * 
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.
299          */
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); };
306                 return ret_hu_conv;
307         }
308
309         /**
310          * Get a new set of [`Sign`] for per-channel secrets. These MUST be unique even if you
311          * restarted with some stale data!
312          * 
313          * This method must return a different value each time it is called.
314          */
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);
321                 return ret;
322         }
323
324         /**
325          * Derives the private key material backing a `Signer`.
326          * 
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`].
331          */
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); };
340                 return ret_hu_conv;
341         }
342
343         /**
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.
347          * 
348          * This method must return a different value each time it is called.
349          */
350         public byte[] get_secure_random_bytes() {
351                 byte[] ret = bindings.KeysInterface_get_secure_random_bytes(this.ptr);
352                 Reference.reachabilityFence(this);
353                 return ret;
354         }
355
356         /**
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.
363          * 
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.
366          * 
367          * [`Signer`]: Self::Signer
368          * [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
369          * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
370          */
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);
377                 return ret_hu_conv;
378         }
379
380         /**
381          * Sign an invoice.
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.
386          * 
387          * The secret key used to sign the invoice is dependent on the [`Recipient`].
388          * 
389          * Errors if the [`Recipient`] variant is not supported by the implementation.
390          */
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);
399                 return ret_hu_conv;
400         }
401
402         /**
403          * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
404          * 
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
407          * this method.
408          * 
409          * This method must return the same value each time it is called.
410          * 
411          * [phantom node payments]: PhantomKeysManager
412          */
413         public byte[] get_inbound_payment_key_material() {
414                 byte[] ret = bindings.KeysInterface_get_inbound_payment_key_material(this.ptr);
415                 Reference.reachabilityFence(this);
416                 return ret;
417         }
418
419 }