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 (aka node_id or network_key) based on the provided [`Recipient`].
31 * This method must return the same value each time it is called with a given `Recipient`
34 Result_SecretKeyNoneZ get_node_secret(Recipient recipient);
36 * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
38 * This method should return a different value each time it is called, to avoid linking
39 * on-chain funds across channels as controlled to the same user.
41 byte[] get_destination_script();
43 * Get a script pubkey which we will send funds to when closing a channel.
45 * This method should return a different value each time it is called, to avoid linking
46 * on-chain funds across channels as controlled to the same user.
48 ShutdownScript get_shutdown_scriptpubkey();
50 * Get a new set of Sign for per-channel secrets. These MUST be unique even if you
51 * restarted with some stale data!
53 * This method must return a different value each time it is called.
55 Sign get_channel_signer(boolean inbound, long channel_value_satoshis);
57 * Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
58 * onion packets and for temporary channel IDs. There is no requirement that these be
59 * persisted anywhere, though they must be unique across restarts.
61 * This method must return a different value each time it is called.
63 byte[] get_secure_random_bytes();
65 * Reads a `Signer` for this `KeysInterface` from the given input stream.
66 * This is only called during deserialization of other objects which contain
67 * `Sign`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s).
68 * The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
69 * contain no versioning scheme. You may wish to include your own version prefix and ensure
70 * you've read all of the provided bytes to ensure no corruption occurred.
72 Result_SignDecodeErrorZ read_chan_signer(byte[] reader);
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.
78 * The hrp is ascii bytes, while the invoice data is base32.
80 * The secret key used to sign the invoice is dependent on the [`Recipient`].
82 Result_RecoverableSignatureNoneZ sign_invoice(byte[] hrp_bytes, UInt5[] invoice_data, Recipient receipient);
84 * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
86 * If the implementor of this trait supports [phantom node payments], then every node that is
87 * intended to be included in the phantom invoice route hints must return the same value from
90 * This method must return the same value each time it is called.
92 * [phantom node payments]: PhantomKeysManager
94 byte[] get_inbound_payment_key_material();
96 private static class LDKKeysInterfaceHolder { KeysInterface held; }
97 public static KeysInterface new_impl(KeysInterfaceInterface arg) {
98 final LDKKeysInterfaceHolder impl_holder = new LDKKeysInterfaceHolder();
99 impl_holder.held = new KeysInterface(new bindings.LDKKeysInterface() {
100 @Override public long get_node_secret(Recipient recipient) {
101 Result_SecretKeyNoneZ ret = arg.get_node_secret(recipient);
102 Reference.reachabilityFence(arg);
103 long result = ret == null ? 0 : ret.clone_ptr();
106 @Override public byte[] get_destination_script() {
107 byte[] ret = arg.get_destination_script();
108 Reference.reachabilityFence(arg);
111 @Override public long get_shutdown_scriptpubkey() {
112 ShutdownScript ret = arg.get_shutdown_scriptpubkey();
113 Reference.reachabilityFence(arg);
114 long result = ret == null ? 0 : ret.clone_ptr();
117 @Override public long get_channel_signer(boolean inbound, long channel_value_satoshis) {
118 Sign ret = arg.get_channel_signer(inbound, channel_value_satoshis);
119 Reference.reachabilityFence(arg);
120 long result = ret == null ? 0 : ret.clone_ptr();
121 impl_holder.held.ptrs_to.add(ret);
124 @Override public byte[] get_secure_random_bytes() {
125 byte[] ret = arg.get_secure_random_bytes();
126 Reference.reachabilityFence(arg);
127 byte[] result = InternalUtils.check_arr_len(ret, 32);
130 @Override public long read_chan_signer(byte[] reader) {
131 Result_SignDecodeErrorZ ret = arg.read_chan_signer(reader);
132 Reference.reachabilityFence(arg);
133 long result = ret == null ? 0 : ret.clone_ptr();
136 @Override public long sign_invoice(byte[] hrp_bytes, byte[] invoice_data, Recipient receipient) {
137 int invoice_data_conv_7_len = invoice_data.length;
138 UInt5[] invoice_data_conv_7_arr = new UInt5[invoice_data_conv_7_len];
139 for (int h = 0; h < invoice_data_conv_7_len; h++) {
140 byte invoice_data_conv_7 = invoice_data[h];
141 UInt5 invoice_data_conv_7_conv = new UInt5(invoice_data_conv_7);
142 invoice_data_conv_7_arr[h] = invoice_data_conv_7_conv;
144 Result_RecoverableSignatureNoneZ ret = arg.sign_invoice(hrp_bytes, invoice_data_conv_7_arr, receipient);
145 Reference.reachabilityFence(arg);
146 long result = ret == null ? 0 : ret.clone_ptr();
149 @Override public byte[] get_inbound_payment_key_material() {
150 byte[] ret = arg.get_inbound_payment_key_material();
151 Reference.reachabilityFence(arg);
152 byte[] result = InternalUtils.check_arr_len(ret, 32);
156 return impl_holder.held;
159 * Get node secret key (aka node_id or network_key) based on the provided [`Recipient`].
161 * This method must return the same value each time it is called with a given `Recipient`
164 public Result_SecretKeyNoneZ get_node_secret(org.ldk.enums.Recipient recipient) {
165 long ret = bindings.KeysInterface_get_node_secret(this.ptr, recipient);
166 Reference.reachabilityFence(this);
167 Reference.reachabilityFence(recipient);
168 if (ret >= 0 && ret <= 4096) { return null; }
169 Result_SecretKeyNoneZ ret_hu_conv = Result_SecretKeyNoneZ.constr_from_ptr(ret);
174 * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
176 * This method should return a different value each time it is called, to avoid linking
177 * on-chain funds across channels as controlled to the same user.
179 public byte[] get_destination_script() {
180 byte[] ret = bindings.KeysInterface_get_destination_script(this.ptr);
181 Reference.reachabilityFence(this);
186 * Get a script pubkey which we will send funds to when closing a channel.
188 * This method should return a different value each time it is called, to avoid linking
189 * on-chain funds across channels as controlled to the same user.
191 public ShutdownScript get_shutdown_scriptpubkey() {
192 long ret = bindings.KeysInterface_get_shutdown_scriptpubkey(this.ptr);
193 Reference.reachabilityFence(this);
194 if (ret >= 0 && ret <= 4096) { return null; }
195 ShutdownScript ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new ShutdownScript(null, ret); }
196 ret_hu_conv.ptrs_to.add(this);
201 * Get a new set of Sign for per-channel secrets. These MUST be unique even if you
202 * restarted with some stale data!
204 * This method must return a different value each time it is called.
206 public Sign get_channel_signer(boolean inbound, long channel_value_satoshis) {
207 long ret = bindings.KeysInterface_get_channel_signer(this.ptr, inbound, channel_value_satoshis);
208 Reference.reachabilityFence(this);
209 Reference.reachabilityFence(inbound);
210 Reference.reachabilityFence(channel_value_satoshis);
211 if (ret >= 0 && ret <= 4096) { return null; }
212 Sign ret_hu_conv = new Sign(null, ret);
213 ret_hu_conv.ptrs_to.add(this);
218 * Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
219 * onion packets and for temporary channel IDs. There is no requirement that these be
220 * persisted anywhere, though they must be unique across restarts.
222 * This method must return a different value each time it is called.
224 public byte[] get_secure_random_bytes() {
225 byte[] ret = bindings.KeysInterface_get_secure_random_bytes(this.ptr);
226 Reference.reachabilityFence(this);
231 * Reads a `Signer` for this `KeysInterface` from the given input stream.
232 * This is only called during deserialization of other objects which contain
233 * `Sign`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s).
234 * The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
235 * contain no versioning scheme. You may wish to include your own version prefix and ensure
236 * you've read all of the provided bytes to ensure no corruption occurred.
238 public Result_SignDecodeErrorZ read_chan_signer(byte[] reader) {
239 long ret = bindings.KeysInterface_read_chan_signer(this.ptr, reader);
240 Reference.reachabilityFence(this);
241 Reference.reachabilityFence(reader);
242 if (ret >= 0 && ret <= 4096) { return null; }
243 Result_SignDecodeErrorZ ret_hu_conv = Result_SignDecodeErrorZ.constr_from_ptr(ret);
249 * By parameterizing by the raw invoice bytes instead of the hash, we allow implementors of
250 * this trait to parse the invoice and make sure they're signing what they expect, rather than
251 * blindly signing the hash.
252 * The hrp is ascii bytes, while the invoice data is base32.
254 * The secret key used to sign the invoice is dependent on the [`Recipient`].
256 public Result_RecoverableSignatureNoneZ sign_invoice(byte[] hrp_bytes, UInt5[] invoice_data, org.ldk.enums.Recipient receipient) {
257 long ret = bindings.KeysInterface_sign_invoice(this.ptr, hrp_bytes, invoice_data != null ? InternalUtils.convUInt5Array(invoice_data) : null, receipient);
258 Reference.reachabilityFence(this);
259 Reference.reachabilityFence(hrp_bytes);
260 Reference.reachabilityFence(invoice_data);
261 Reference.reachabilityFence(receipient);
262 if (ret >= 0 && ret <= 4096) { return null; }
263 Result_RecoverableSignatureNoneZ ret_hu_conv = Result_RecoverableSignatureNoneZ.constr_from_ptr(ret);
268 * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
270 * If the implementor of this trait supports [phantom node payments], then every node that is
271 * intended to be included in the phantom invoice route hints must return the same value from
274 * This method must return the same value each time it is called.
276 * [phantom node payments]: PhantomKeysManager
278 public byte[] get_inbound_payment_key_material() {
279 byte[] ret = bindings.KeysInterface_get_inbound_payment_key_material(this.ptr);
280 Reference.reachabilityFence(this);