Merge pull request #33 from TheBlueMatt/main
[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 javax.annotation.Nullable;
8
9 /**
10  * A trait to describe an object which can get user secrets and key material.
11  */
12 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
13 public class KeysInterface extends CommonBase {
14         final bindings.LDKKeysInterface bindings_instance;
15         KeysInterface(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
16         private KeysInterface(bindings.LDKKeysInterface arg) {
17                 super(bindings.LDKKeysInterface_new(arg));
18                 this.ptrs_to.add(arg);
19                 this.bindings_instance = arg;
20         }
21         @Override @SuppressWarnings("deprecation")
22         protected void finalize() throws Throwable {
23                 if (ptr != 0) { bindings.KeysInterface_free(ptr); } super.finalize();
24         }
25
26         public static interface KeysInterfaceInterface {
27                 /**
28                  * Get node secret key (aka node_id or network_key).
29                  * 
30                  * This method must return the same value each time it is called.
31                  */
32                 byte[] get_node_secret();
33                 /**
34                  * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
35                  * 
36                  * This method should return a different value each time it is called, to avoid linking
37                  * on-chain funds across channels as controlled to the same user.
38                  */
39                 byte[] get_destination_script();
40                 /**
41                  * Get a public key which we will send funds to (in the form of a P2WPKH output) when closing
42                  * 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                 byte[] get_shutdown_pubkey();
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         private static class LDKKeysInterfaceHolder { KeysInterface held; }
81         public static KeysInterface new_impl(KeysInterfaceInterface arg) {
82                 final LDKKeysInterfaceHolder impl_holder = new LDKKeysInterfaceHolder();
83                 impl_holder.held = new KeysInterface(new bindings.LDKKeysInterface() {
84                         @Override public byte[] get_node_secret() {
85                                 byte[] ret = arg.get_node_secret();
86                                 return ret;
87                         }
88                         @Override public byte[] get_destination_script() {
89                                 byte[] ret = arg.get_destination_script();
90                                 return ret;
91                         }
92                         @Override public byte[] get_shutdown_pubkey() {
93                                 byte[] ret = arg.get_shutdown_pubkey();
94                                 return ret;
95                         }
96                         @Override public long get_channel_signer(boolean inbound, long channel_value_satoshis) {
97                                 Sign ret = arg.get_channel_signer(inbound, channel_value_satoshis);
98                                 long result = ret == null ? 0 : ret.ptr;
99                                 impl_holder.held.ptrs_to.add(ret);
100                                 return result;
101                         }
102                         @Override public byte[] get_secure_random_bytes() {
103                                 byte[] ret = arg.get_secure_random_bytes();
104                                 return ret;
105                         }
106                         @Override public long read_chan_signer(byte[] reader) {
107                                 Result_SignDecodeErrorZ ret = arg.read_chan_signer(reader);
108                                 long result = ret != null ? ret.ptr : 0;
109                                 return result;
110                         }
111                         @Override public long sign_invoice(byte[] invoice_preimage) {
112                                 Result_RecoverableSignatureNoneZ ret = arg.sign_invoice(invoice_preimage);
113                                 long result = ret != null ? ret.ptr : 0;
114                                 return result;
115                         }
116                 });
117                 return impl_holder.held;
118         }
119         /**
120          * Get node secret key (aka node_id or network_key).
121          * 
122          * This method must return the same value each time it is called.
123          */
124         public byte[] get_node_secret() {
125                 byte[] ret = bindings.KeysInterface_get_node_secret(this.ptr);
126                 return ret;
127         }
128
129         /**
130          * Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
131          * 
132          * This method should return a different value each time it is called, to avoid linking
133          * on-chain funds across channels as controlled to the same user.
134          */
135         public byte[] get_destination_script() {
136                 byte[] ret = bindings.KeysInterface_get_destination_script(this.ptr);
137                 return ret;
138         }
139
140         /**
141          * Get a public key which we will send funds to (in the form of a P2WPKH output) when closing
142          * a channel.
143          * 
144          * This method should return a different value each time it is called, to avoid linking
145          * on-chain funds across channels as controlled to the same user.
146          */
147         public byte[] get_shutdown_pubkey() {
148                 byte[] ret = bindings.KeysInterface_get_shutdown_pubkey(this.ptr);
149                 return ret;
150         }
151
152         /**
153          * Get a new set of Sign for per-channel secrets. These MUST be unique even if you
154          * restarted with some stale data!
155          * 
156          * This method must return a different value each time it is called.
157          */
158         public Sign get_channel_signer(boolean inbound, long channel_value_satoshis) {
159                 long ret = bindings.KeysInterface_get_channel_signer(this.ptr, inbound, channel_value_satoshis);
160                 if (ret < 1024) { return null; }
161                 Sign ret_hu_conv = new Sign(null, ret);
162                 ret_hu_conv.ptrs_to.add(this);
163                 return ret_hu_conv;
164         }
165
166         /**
167          * Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
168          * onion packets and for temporary channel IDs. There is no requirement that these be
169          * persisted anywhere, though they must be unique across restarts.
170          * 
171          * This method must return a different value each time it is called.
172          */
173         public byte[] get_secure_random_bytes() {
174                 byte[] ret = bindings.KeysInterface_get_secure_random_bytes(this.ptr);
175                 return ret;
176         }
177
178         /**
179          * Reads a `Signer` for this `KeysInterface` from the given input stream.
180          * This is only called during deserialization of other objects which contain
181          * `Sign`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s).
182          * The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
183          * contain no versioning scheme. You may wish to include your own version prefix and ensure
184          * you've read all of the provided bytes to ensure no corruption occurred.
185          */
186         public Result_SignDecodeErrorZ read_chan_signer(byte[] reader) {
187                 long ret = bindings.KeysInterface_read_chan_signer(this.ptr, reader);
188                 if (ret < 1024) { return null; }
189                 Result_SignDecodeErrorZ ret_hu_conv = Result_SignDecodeErrorZ.constr_from_ptr(ret);
190                 return ret_hu_conv;
191         }
192
193         /**
194          * Sign an invoice's preimage (note that this is the preimage of the invoice, not the HTLC's
195          * preimage). By parameterizing by the preimage instead of the hash, we allow implementors of
196          * this trait to parse the invoice and make sure they're signing what they expect, rather than
197          * blindly signing the hash.
198          */
199         public Result_RecoverableSignatureNoneZ sign_invoice(byte[] invoice_preimage) {
200                 long ret = bindings.KeysInterface_sign_invoice(this.ptr, invoice_preimage);
201                 if (ret < 1024) { return null; }
202                 Result_RecoverableSignatureNoneZ ret_hu_conv = Result_RecoverableSignatureNoneZ.constr_from_ptr(ret);
203                 return ret_hu_conv;
204         }
205
206 }