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 Result_SecretKeyNoneZ get_node_secret(Recipient recipient);
38 * Gets the ECDH shared secret of our [`node secret`] and `other_key`, multiplying by `tweak` if
39 * one is provided. Note that this tweak can be applied to `other_key` instead of our node
40 * secret, though this is less efficient.
42 * [`node secret`]: Self::get_node_secret
44 Result_SharedSecretNoneZ ecdh(Recipient recipient, byte[] other_key, Option_ScalarZ tweak);
46 * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
48 * This method should return a different value each time it is called, to avoid linking
49 * on-chain funds across channels as controlled to the same user.
51 byte[] get_destination_script();
53 * Get a script pubkey which we will send funds to when closing a channel.
55 * This method should return a different value each time it is called, to avoid linking
56 * on-chain funds across channels as controlled to the same user.
58 ShutdownScript get_shutdown_scriptpubkey();
60 * Get a new set of Sign for per-channel secrets. These MUST be unique even if you
61 * restarted with some stale data!
63 * This method must return a different value each time it is called.
65 Sign get_channel_signer(boolean inbound, long channel_value_satoshis);
67 * Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
68 * onion packets and for temporary channel IDs. There is no requirement that these be
69 * persisted anywhere, though they must be unique across restarts.
71 * This method must return a different value each time it is called.
73 byte[] get_secure_random_bytes();
75 * Reads a `Signer` for this `KeysInterface` from the given input stream.
76 * This is only called during deserialization of other objects which contain
77 * `Sign`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s).
78 * The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
79 * contain no versioning scheme. You may wish to include your own version prefix and ensure
80 * you've read all of the provided bytes to ensure no corruption occurred.
82 Result_SignDecodeErrorZ read_chan_signer(byte[] reader);
85 * By parameterizing by the raw invoice bytes instead of the hash, we allow implementors of
86 * this trait to parse the invoice and make sure they're signing what they expect, rather than
87 * blindly signing the hash.
88 * The hrp is ascii bytes, while the invoice data is base32.
90 * The secret key used to sign the invoice is dependent on the [`Recipient`].
92 Result_RecoverableSignatureNoneZ sign_invoice(byte[] hrp_bytes, UInt5[] invoice_data, Recipient receipient);
94 * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
96 * If the implementor of this trait supports [phantom node payments], then every node that is
97 * intended to be included in the phantom invoice route hints must return the same value from
100 * This method must return the same value each time it is called.
102 * [phantom node payments]: PhantomKeysManager
104 byte[] get_inbound_payment_key_material();
106 private static class LDKKeysInterfaceHolder { KeysInterface held; }
107 public static KeysInterface new_impl(KeysInterfaceInterface arg) {
108 final LDKKeysInterfaceHolder impl_holder = new LDKKeysInterfaceHolder();
109 impl_holder.held = new KeysInterface(new bindings.LDKKeysInterface() {
110 @Override public long get_node_secret(Recipient recipient) {
111 Result_SecretKeyNoneZ ret = arg.get_node_secret(recipient);
112 Reference.reachabilityFence(arg);
113 long result = ret == null ? 0 : ret.clone_ptr();
116 @Override public long ecdh(Recipient recipient, byte[] other_key, long tweak) {
117 org.ldk.structs.Option_ScalarZ tweak_hu_conv = org.ldk.structs.Option_ScalarZ.constr_from_ptr(tweak);
118 if (tweak_hu_conv != null) { tweak_hu_conv.ptrs_to.add(this); };
119 Result_SharedSecretNoneZ ret = arg.ecdh(recipient, other_key, tweak_hu_conv);
120 Reference.reachabilityFence(arg);
121 long result = ret == null ? 0 : ret.clone_ptr();
124 @Override public byte[] get_destination_script() {
125 byte[] ret = arg.get_destination_script();
126 Reference.reachabilityFence(arg);
129 @Override public long get_shutdown_scriptpubkey() {
130 ShutdownScript ret = arg.get_shutdown_scriptpubkey();
131 Reference.reachabilityFence(arg);
132 long result = ret == null ? 0 : ret.clone_ptr();
135 @Override public long get_channel_signer(boolean inbound, long channel_value_satoshis) {
136 Sign ret = arg.get_channel_signer(inbound, channel_value_satoshis);
137 Reference.reachabilityFence(arg);
138 long result = ret == null ? 0 : ret.clone_ptr();
139 if (impl_holder.held != null) { impl_holder.held.ptrs_to.add(ret); };
142 @Override public byte[] get_secure_random_bytes() {
143 byte[] ret = arg.get_secure_random_bytes();
144 Reference.reachabilityFence(arg);
145 byte[] result = InternalUtils.check_arr_len(ret, 32);
148 @Override public long read_chan_signer(byte[] reader) {
149 Result_SignDecodeErrorZ ret = arg.read_chan_signer(reader);
150 Reference.reachabilityFence(arg);
151 long result = ret == null ? 0 : ret.clone_ptr();
154 @Override public long sign_invoice(byte[] hrp_bytes, byte[] invoice_data, Recipient receipient) {
155 int invoice_data_conv_7_len = invoice_data.length;
156 UInt5[] invoice_data_conv_7_arr = new UInt5[invoice_data_conv_7_len];
157 for (int h = 0; h < invoice_data_conv_7_len; h++) {
158 byte invoice_data_conv_7 = invoice_data[h];
159 UInt5 invoice_data_conv_7_conv = new UInt5(invoice_data_conv_7);
160 invoice_data_conv_7_arr[h] = invoice_data_conv_7_conv;
162 Result_RecoverableSignatureNoneZ ret = arg.sign_invoice(hrp_bytes, invoice_data_conv_7_arr, receipient);
163 Reference.reachabilityFence(arg);
164 long result = ret == null ? 0 : ret.clone_ptr();
167 @Override public byte[] get_inbound_payment_key_material() {
168 byte[] ret = arg.get_inbound_payment_key_material();
169 Reference.reachabilityFence(arg);
170 byte[] result = InternalUtils.check_arr_len(ret, 32);
174 return impl_holder.held;
177 * Get node secret key based on the provided [`Recipient`].
179 * The node_id/network_key is the public key that corresponds to this secret key.
181 * This method must return the same value each time it is called with a given `Recipient`
184 public Result_SecretKeyNoneZ get_node_secret(org.ldk.enums.Recipient recipient) {
185 long ret = bindings.KeysInterface_get_node_secret(this.ptr, recipient);
186 Reference.reachabilityFence(this);
187 Reference.reachabilityFence(recipient);
188 if (ret >= 0 && ret <= 4096) { return null; }
189 Result_SecretKeyNoneZ ret_hu_conv = Result_SecretKeyNoneZ.constr_from_ptr(ret);
194 * Gets the ECDH shared secret of our [`node secret`] and `other_key`, multiplying by `tweak` if
195 * one is provided. Note that this tweak can be applied to `other_key` instead of our node
196 * secret, though this is less efficient.
198 * [`node secret`]: Self::get_node_secret
200 public Result_SharedSecretNoneZ ecdh(org.ldk.enums.Recipient recipient, byte[] other_key, Option_ScalarZ tweak) {
201 long ret = bindings.KeysInterface_ecdh(this.ptr, recipient, InternalUtils.check_arr_len(other_key, 33), tweak.ptr);
202 Reference.reachabilityFence(this);
203 Reference.reachabilityFence(recipient);
204 Reference.reachabilityFence(other_key);
205 Reference.reachabilityFence(tweak);
206 if (ret >= 0 && ret <= 4096) { return null; }
207 Result_SharedSecretNoneZ ret_hu_conv = Result_SharedSecretNoneZ.constr_from_ptr(ret);
212 * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
214 * This method should return a different value each time it is called, to avoid linking
215 * on-chain funds across channels as controlled to the same user.
217 public byte[] get_destination_script() {
218 byte[] ret = bindings.KeysInterface_get_destination_script(this.ptr);
219 Reference.reachabilityFence(this);
224 * Get a script pubkey which we will send funds to when closing a channel.
226 * This method should return a different value each time it is called, to avoid linking
227 * on-chain funds across channels as controlled to the same user.
229 public ShutdownScript get_shutdown_scriptpubkey() {
230 long ret = bindings.KeysInterface_get_shutdown_scriptpubkey(this.ptr);
231 Reference.reachabilityFence(this);
232 if (ret >= 0 && ret <= 4096) { return null; }
233 org.ldk.structs.ShutdownScript ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ShutdownScript(null, ret); }
234 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
239 * Get a new set of Sign for per-channel secrets. These MUST be unique even if you
240 * restarted with some stale data!
242 * This method must return a different value each time it is called.
244 public Sign get_channel_signer(boolean inbound, long channel_value_satoshis) {
245 long ret = bindings.KeysInterface_get_channel_signer(this.ptr, inbound, channel_value_satoshis);
246 Reference.reachabilityFence(this);
247 Reference.reachabilityFence(inbound);
248 Reference.reachabilityFence(channel_value_satoshis);
249 if (ret >= 0 && ret <= 4096) { return null; }
250 Sign ret_hu_conv = new Sign(null, ret);
251 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
256 * Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
257 * onion packets and for temporary channel IDs. There is no requirement that these be
258 * persisted anywhere, though they must be unique across restarts.
260 * This method must return a different value each time it is called.
262 public byte[] get_secure_random_bytes() {
263 byte[] ret = bindings.KeysInterface_get_secure_random_bytes(this.ptr);
264 Reference.reachabilityFence(this);
269 * Reads a `Signer` for this `KeysInterface` from the given input stream.
270 * This is only called during deserialization of other objects which contain
271 * `Sign`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s).
272 * The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
273 * contain no versioning scheme. You may wish to include your own version prefix and ensure
274 * you've read all of the provided bytes to ensure no corruption occurred.
276 public Result_SignDecodeErrorZ read_chan_signer(byte[] reader) {
277 long ret = bindings.KeysInterface_read_chan_signer(this.ptr, reader);
278 Reference.reachabilityFence(this);
279 Reference.reachabilityFence(reader);
280 if (ret >= 0 && ret <= 4096) { return null; }
281 Result_SignDecodeErrorZ ret_hu_conv = Result_SignDecodeErrorZ.constr_from_ptr(ret);
287 * By parameterizing by the raw invoice bytes instead of the hash, we allow implementors of
288 * this trait to parse the invoice and make sure they're signing what they expect, rather than
289 * blindly signing the hash.
290 * The hrp is ascii bytes, while the invoice data is base32.
292 * The secret key used to sign the invoice is dependent on the [`Recipient`].
294 public Result_RecoverableSignatureNoneZ sign_invoice(byte[] hrp_bytes, UInt5[] invoice_data, org.ldk.enums.Recipient receipient) {
295 long ret = bindings.KeysInterface_sign_invoice(this.ptr, hrp_bytes, invoice_data != null ? InternalUtils.convUInt5Array(invoice_data) : null, receipient);
296 Reference.reachabilityFence(this);
297 Reference.reachabilityFence(hrp_bytes);
298 Reference.reachabilityFence(invoice_data);
299 Reference.reachabilityFence(receipient);
300 if (ret >= 0 && ret <= 4096) { return null; }
301 Result_RecoverableSignatureNoneZ ret_hu_conv = Result_RecoverableSignatureNoneZ.constr_from_ptr(ret);
306 * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
308 * If the implementor of this trait supports [phantom node payments], then every node that is
309 * intended to be included in the phantom invoice route hints must return the same value from
312 * This method must return the same value each time it is called.
314 * [phantom node payments]: PhantomKeysManager
316 public byte[] get_inbound_payment_key_material() {
317 byte[] ret = bindings.KeysInterface_get_inbound_payment_key_material(this.ptr);
318 Reference.reachabilityFence(this);