f9d70fcef0ee05a09b430a8a9562fa5532e5d1cf
[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 (aka node_id or network_key).
30                  * 
31                  * This method must return the same value each time it is called.
32                  */
33                 byte[] get_node_secret();
34                 /**
35                  * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
36                  * 
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.
39                  */
40                 byte[] get_destination_script();
41                 /**
42                  * Get a script pubkey which we will send funds to when closing a channel.
43                  * 
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.
46                  */
47                 ShutdownScript get_shutdown_scriptpubkey();
48                 /**
49                  * Get a new set of Sign for per-channel secrets. These MUST be unique even if you
50                  * restarted with some stale data!
51                  * 
52                  * This method must return a different value each time it is called.
53                  */
54                 Sign get_channel_signer(boolean inbound, long channel_value_satoshis);
55                 /**
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.
59                  * 
60                  * This method must return a different value each time it is called.
61                  */
62                 byte[] get_secure_random_bytes();
63                 /**
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.
70                  */
71                 Result_SignDecodeErrorZ read_chan_signer(byte[] reader);
72                 /**
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.
77                  */
78                 Result_RecoverableSignatureNoneZ sign_invoice(byte[] invoice_preimage);
79                 /**
80                  * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
81                  * 
82                  * This method must return the same value each time it is called.
83                  */
84                 byte[] get_inbound_payment_key_material();
85         }
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);
93                                 return result;
94                         }
95                         @Override public byte[] get_destination_script() {
96                                 byte[] ret = arg.get_destination_script();
97                                 return ret;
98                         }
99                         @Override public long get_shutdown_scriptpubkey() {
100                                 ShutdownScript ret = arg.get_shutdown_scriptpubkey();
101                                 long result = ret == null ? 0 : ret.clone_ptr();
102                                 return result;
103                         }
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);
108                                 return result;
109                         }
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);
113                                 return result;
114                         }
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();
118                                 return result;
119                         }
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();
123                                 return result;
124                         }
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);
128                                 return result;
129                         }
130                 });
131                 return impl_holder.held;
132         }
133         /**
134          * Get node secret key (aka node_id or network_key).
135          * 
136          * This method must return the same value each time it is called.
137          */
138         public byte[] get_node_secret() {
139                 byte[] ret = bindings.KeysInterface_get_node_secret(this.ptr);
140                 Reference.reachabilityFence(this);
141                 return ret;
142         }
143
144         /**
145          * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
146          * 
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.
149          */
150         public byte[] get_destination_script() {
151                 byte[] ret = bindings.KeysInterface_get_destination_script(this.ptr);
152                 Reference.reachabilityFence(this);
153                 return ret;
154         }
155
156         /**
157          * Get a script pubkey which we will send funds to when closing a channel.
158          * 
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.
161          */
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);
168                 return ret_hu_conv;
169         }
170
171         /**
172          * Get a new set of Sign for per-channel secrets. These MUST be unique even if you
173          * restarted with some stale data!
174          * 
175          * This method must return a different value each time it is called.
176          */
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);
185                 return ret_hu_conv;
186         }
187
188         /**
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.
192          * 
193          * This method must return a different value each time it is called.
194          */
195         public byte[] get_secure_random_bytes() {
196                 byte[] ret = bindings.KeysInterface_get_secure_random_bytes(this.ptr);
197                 Reference.reachabilityFence(this);
198                 return ret;
199         }
200
201         /**
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.
208          */
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);
215                 return ret_hu_conv;
216         }
217
218         /**
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.
223          */
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);
230                 return ret_hu_conv;
231         }
232
233         /**
234          * Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
235          * 
236          * This method must return the same value each time it is called.
237          */
238         public byte[] get_inbound_payment_key_material() {
239                 byte[] ret = bindings.KeysInterface_get_inbound_payment_key_material(this.ptr);
240                 Reference.reachabilityFence(this);
241                 return ret;
242         }
243
244 }