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).
31 * This method must return the same value each time it is called.
33 byte[] get_node_secret();
35 * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
37 * This method should return a different value each time it is called, to avoid linking
38 * on-chain funds across channels as controlled to the same user.
40 byte[] get_destination_script();
42 * Get a script pubkey which we will send funds to when closing a channel.
44 * This method should return a different value each time it is called, to avoid linking
45 * on-chain funds across channels as controlled to the same user.
47 ShutdownScript get_shutdown_scriptpubkey();
49 * Get a new set of Sign for per-channel secrets. These MUST be unique even if you
50 * restarted with some stale data!
52 * This method must return a different value each time it is called.
54 Sign get_channel_signer(boolean inbound, long channel_value_satoshis);
56 * Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
57 * onion packets and for temporary channel IDs. There is no requirement that these be
58 * persisted anywhere, though they must be unique across restarts.
60 * This method must return a different value each time it is called.
62 byte[] get_secure_random_bytes();
64 * Reads a `Signer` for this `KeysInterface` from the given input stream.
65 * This is only called during deserialization of other objects which contain
66 * `Sign`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s).
67 * The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
68 * contain no versioning scheme. You may wish to include your own version prefix and ensure
69 * you've read all of the provided bytes to ensure no corruption occurred.
71 Result_SignDecodeErrorZ read_chan_signer(byte[] reader);
73 * Sign an invoice's preimage (note that this is the preimage of the invoice, not the HTLC's
74 * preimage). By parameterizing by the preimage instead of the hash, we allow implementors of
75 * this trait to parse the invoice and make sure they're signing what they expect, rather than
76 * blindly signing the hash.
78 Result_RecoverableSignatureNoneZ sign_invoice(byte[] invoice_preimage);
80 * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
82 * This method must return the same value each time it is called.
84 byte[] get_inbound_payment_key_material();
86 private static class LDKKeysInterfaceHolder { KeysInterface held; }
87 public static KeysInterface new_impl(KeysInterfaceInterface arg) {
88 final LDKKeysInterfaceHolder impl_holder = new LDKKeysInterfaceHolder();
89 impl_holder.held = new KeysInterface(new bindings.LDKKeysInterface() {
90 @Override public byte[] get_node_secret() {
91 byte[] ret = arg.get_node_secret();
92 byte[] result = InternalUtils.check_arr_len(ret, 32);
95 @Override public byte[] get_destination_script() {
96 byte[] ret = arg.get_destination_script();
99 @Override public long get_shutdown_scriptpubkey() {
100 ShutdownScript ret = arg.get_shutdown_scriptpubkey();
101 long result = ret == null ? 0 : ret.clone_ptr();
104 @Override public long get_channel_signer(boolean inbound, long channel_value_satoshis) {
105 Sign ret = arg.get_channel_signer(inbound, channel_value_satoshis);
106 long result = ret == null ? 0 : ret.clone_ptr();
107 impl_holder.held.ptrs_to.add(ret);
110 @Override public byte[] get_secure_random_bytes() {
111 byte[] ret = arg.get_secure_random_bytes();
112 byte[] result = InternalUtils.check_arr_len(ret, 32);
115 @Override public long read_chan_signer(byte[] reader) {
116 Result_SignDecodeErrorZ ret = arg.read_chan_signer(reader);
117 long result = ret == null ? 0 : ret.clone_ptr();
120 @Override public long sign_invoice(byte[] invoice_preimage) {
121 Result_RecoverableSignatureNoneZ ret = arg.sign_invoice(invoice_preimage);
122 long result = ret == null ? 0 : ret.clone_ptr();
125 @Override public byte[] get_inbound_payment_key_material() {
126 byte[] ret = arg.get_inbound_payment_key_material();
127 byte[] result = InternalUtils.check_arr_len(ret, 32);
131 return impl_holder.held;
134 * Get node secret key (aka node_id or network_key).
136 * This method must return the same value each time it is called.
138 public byte[] get_node_secret() {
139 byte[] ret = bindings.KeysInterface_get_node_secret(this.ptr);
140 Reference.reachabilityFence(this);
145 * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
147 * This method should return a different value each time it is called, to avoid linking
148 * on-chain funds across channels as controlled to the same user.
150 public byte[] get_destination_script() {
151 byte[] ret = bindings.KeysInterface_get_destination_script(this.ptr);
152 Reference.reachabilityFence(this);
157 * Get a script pubkey which we will send funds to when closing a channel.
159 * This method should return a different value each time it is called, to avoid linking
160 * on-chain funds across channels as controlled to the same user.
162 public ShutdownScript get_shutdown_scriptpubkey() {
163 long ret = bindings.KeysInterface_get_shutdown_scriptpubkey(this.ptr);
164 Reference.reachabilityFence(this);
165 if (ret >= 0 && ret <= 4096) { return null; }
166 ShutdownScript ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new ShutdownScript(null, ret); }
167 ret_hu_conv.ptrs_to.add(this);
172 * Get a new set of Sign for per-channel secrets. These MUST be unique even if you
173 * restarted with some stale data!
175 * This method must return a different value each time it is called.
177 public Sign get_channel_signer(boolean inbound, long channel_value_satoshis) {
178 long ret = bindings.KeysInterface_get_channel_signer(this.ptr, inbound, channel_value_satoshis);
179 Reference.reachabilityFence(this);
180 Reference.reachabilityFence(inbound);
181 Reference.reachabilityFence(channel_value_satoshis);
182 if (ret >= 0 && ret <= 4096) { return null; }
183 Sign ret_hu_conv = new Sign(null, ret);
184 ret_hu_conv.ptrs_to.add(this);
189 * Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
190 * onion packets and for temporary channel IDs. There is no requirement that these be
191 * persisted anywhere, though they must be unique across restarts.
193 * This method must return a different value each time it is called.
195 public byte[] get_secure_random_bytes() {
196 byte[] ret = bindings.KeysInterface_get_secure_random_bytes(this.ptr);
197 Reference.reachabilityFence(this);
202 * Reads a `Signer` for this `KeysInterface` from the given input stream.
203 * This is only called during deserialization of other objects which contain
204 * `Sign`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s).
205 * The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
206 * contain no versioning scheme. You may wish to include your own version prefix and ensure
207 * you've read all of the provided bytes to ensure no corruption occurred.
209 public Result_SignDecodeErrorZ read_chan_signer(byte[] reader) {
210 long ret = bindings.KeysInterface_read_chan_signer(this.ptr, reader);
211 Reference.reachabilityFence(this);
212 Reference.reachabilityFence(reader);
213 if (ret >= 0 && ret <= 4096) { return null; }
214 Result_SignDecodeErrorZ ret_hu_conv = Result_SignDecodeErrorZ.constr_from_ptr(ret);
219 * Sign an invoice's preimage (note that this is the preimage of the invoice, not the HTLC's
220 * preimage). By parameterizing by the preimage instead of the hash, we allow implementors of
221 * this trait to parse the invoice and make sure they're signing what they expect, rather than
222 * blindly signing the hash.
224 public Result_RecoverableSignatureNoneZ sign_invoice(byte[] invoice_preimage) {
225 long ret = bindings.KeysInterface_sign_invoice(this.ptr, invoice_preimage);
226 Reference.reachabilityFence(this);
227 Reference.reachabilityFence(invoice_preimage);
228 if (ret >= 0 && ret <= 4096) { return null; }
229 Result_RecoverableSignatureNoneZ ret_hu_conv = Result_RecoverableSignatureNoneZ.constr_from_ptr(ret);
234 * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
236 * This method must return the same value each time it is called.
238 public byte[] get_inbound_payment_key_material() {
239 byte[] ret = bindings.KeysInterface_get_inbound_payment_key_material(this.ptr);
240 Reference.reachabilityFence(this);