1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
9 * A trait to describe an object which can get user secrets and key material.
11 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
12 public class KeysInterface extends CommonBase {
13 final bindings.LDKKeysInterface bindings_instance;
14 KeysInterface(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
15 private KeysInterface(bindings.LDKKeysInterface arg) {
16 super(bindings.LDKKeysInterface_new(arg));
17 this.ptrs_to.add(arg);
18 this.bindings_instance = arg;
20 @Override @SuppressWarnings("deprecation")
21 protected void finalize() throws Throwable {
22 if (ptr != 0) { bindings.KeysInterface_free(ptr); } super.finalize();
25 public static interface KeysInterfaceInterface {
27 * Get node secret key (aka node_id or network_key).
29 * This method must return the same value each time it is called.
31 byte[] get_node_secret();
33 * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
35 * This method should return a different value each time it is called, to avoid linking
36 * on-chain funds across channels as controlled to the same user.
38 byte[] get_destination_script();
40 * Get a public key which we will send funds to (in the form of a P2WPKH output) when closing
43 * This method should return a different value each time it is called, to avoid linking
44 * on-chain funds across channels as controlled to the same user.
46 byte[] get_shutdown_pubkey();
48 * Get a new set of Sign for per-channel secrets. These MUST be unique even if you
49 * restarted with some stale data!
51 * This method must return a different value each time it is called.
53 Sign get_channel_signer(boolean inbound, long channel_value_satoshis);
55 * Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
56 * onion packets and for temporary channel IDs. There is no requirement that these be
57 * persisted anywhere, though they must be unique across restarts.
59 * This method must return a different value each time it is called.
61 byte[] get_secure_random_bytes();
63 * Reads a `Signer` for this `KeysInterface` from the given input stream.
64 * This is only called during deserialization of other objects which contain
65 * `Sign`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s).
66 * The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
67 * contain no versioning scheme. You may wish to include your own version prefix and ensure
68 * you've read all of the provided bytes to ensure no corruption occurred.
70 Result_SignDecodeErrorZ read_chan_signer(byte[] reader);
72 * Sign an invoice's preimage (note that this is the preimage of the invoice, not the HTLC's
73 * preimage). By parameterizing by the preimage instead of the hash, we allow implementors of
74 * this trait to parse the invoice and make sure they're signing what they expect, rather than
75 * blindly signing the hash.
77 Result_RecoverableSignatureNoneZ sign_invoice(byte[] invoice_preimage);
79 private static class LDKKeysInterfaceHolder { KeysInterface held; }
80 public static KeysInterface new_impl(KeysInterfaceInterface arg) {
81 final LDKKeysInterfaceHolder impl_holder = new LDKKeysInterfaceHolder();
82 impl_holder.held = new KeysInterface(new bindings.LDKKeysInterface() {
83 @Override public byte[] get_node_secret() {
84 byte[] ret = arg.get_node_secret();
87 @Override public byte[] get_destination_script() {
88 byte[] ret = arg.get_destination_script();
91 @Override public byte[] get_shutdown_pubkey() {
92 byte[] ret = arg.get_shutdown_pubkey();
95 @Override public long get_channel_signer(boolean inbound, long channel_value_satoshis) {
96 Sign ret = arg.get_channel_signer(inbound, channel_value_satoshis);
97 long result = ret == null ? 0 : ret.ptr;
98 impl_holder.held.ptrs_to.add(ret);
101 @Override public byte[] get_secure_random_bytes() {
102 byte[] ret = arg.get_secure_random_bytes();
105 @Override public long read_chan_signer(byte[] reader) {
106 Result_SignDecodeErrorZ ret = arg.read_chan_signer(reader);
107 long result = ret != null ? ret.ptr : 0;
110 @Override public long sign_invoice(byte[] invoice_preimage) {
111 Result_RecoverableSignatureNoneZ ret = arg.sign_invoice(invoice_preimage);
112 long result = ret != null ? ret.ptr : 0;
116 return impl_holder.held;
119 * Get node secret key (aka node_id or network_key).
121 * This method must return the same value each time it is called.
123 public byte[] get_node_secret() {
124 byte[] ret = bindings.KeysInterface_get_node_secret(this.ptr);
129 * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
131 * This method should return a different value each time it is called, to avoid linking
132 * on-chain funds across channels as controlled to the same user.
134 public byte[] get_destination_script() {
135 byte[] ret = bindings.KeysInterface_get_destination_script(this.ptr);
140 * Get a public key which we will send funds to (in the form of a P2WPKH output) when closing
143 * This method should return a different value each time it is called, to avoid linking
144 * on-chain funds across channels as controlled to the same user.
146 public byte[] get_shutdown_pubkey() {
147 byte[] ret = bindings.KeysInterface_get_shutdown_pubkey(this.ptr);
152 * Get a new set of Sign for per-channel secrets. These MUST be unique even if you
153 * restarted with some stale data!
155 * This method must return a different value each time it is called.
157 public Sign get_channel_signer(boolean inbound, long channel_value_satoshis) {
158 long ret = bindings.KeysInterface_get_channel_signer(this.ptr, inbound, channel_value_satoshis);
159 Sign ret_hu_conv = new Sign(null, ret);
160 ret_hu_conv.ptrs_to.add(this);
165 * Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
166 * onion packets and for temporary channel IDs. There is no requirement that these be
167 * persisted anywhere, though they must be unique across restarts.
169 * This method must return a different value each time it is called.
171 public byte[] get_secure_random_bytes() {
172 byte[] ret = bindings.KeysInterface_get_secure_random_bytes(this.ptr);
177 * Reads a `Signer` for this `KeysInterface` from the given input stream.
178 * This is only called during deserialization of other objects which contain
179 * `Sign`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s).
180 * The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
181 * contain no versioning scheme. You may wish to include your own version prefix and ensure
182 * you've read all of the provided bytes to ensure no corruption occurred.
184 public Result_SignDecodeErrorZ read_chan_signer(byte[] reader) {
185 long ret = bindings.KeysInterface_read_chan_signer(this.ptr, reader);
186 Result_SignDecodeErrorZ ret_hu_conv = Result_SignDecodeErrorZ.constr_from_ptr(ret);
191 * Sign an invoice's preimage (note that this is the preimage of the invoice, not the HTLC's
192 * preimage). By parameterizing by the preimage instead of the hash, we allow implementors of
193 * this trait to parse the invoice and make sure they're signing what they expect, rather than
194 * blindly signing the hash.
196 public Result_RecoverableSignatureNoneZ sign_invoice(byte[] invoice_preimage) {
197 long ret = bindings.KeysInterface_sign_invoice(this.ptr, invoice_preimage);
198 Result_RecoverableSignatureNoneZ ret_hu_conv = Result_RecoverableSignatureNoneZ.constr_from_ptr(ret);