Merge pull request #39 from TheBlueMatt/main
[ldk-java] / src / main / java / org / ldk / structs / UtilMethods.java
1 package org.ldk.structs;
2 import org.ldk.impl.bindings;
3 import java.util.Arrays;
4 import javax.annotation.Nullable;
5 import org.ldk.enums.*;
6
7 public class UtilMethods {
8         /**
9          * Creates a digital signature of a message given a SecretKey, like the node's secret.
10          * A receiver knowing the PublicKey (e.g. the node's id) and the message can be sure that the signature was generated by the caller.
11          * Signatures are EC recoverable, meaning that given the message and the signature the PublicKey of the signer can be extracted.
12          */
13         public static Result_StringErrorZ sign(byte[] msg, byte[] sk) {
14                 long ret = bindings.sign(msg, sk);
15                 if (ret < 1024) { return null; }
16                 Result_StringErrorZ ret_hu_conv = Result_StringErrorZ.constr_from_ptr(ret);
17                 return ret_hu_conv;
18         }
19
20         /**
21          * Recovers the PublicKey of the signer of the message given the message and the signature.
22          */
23         public static Result_PublicKeyErrorZ recover_pk(byte[] msg, java.lang.String sig) {
24                 long ret = bindings.recover_pk(msg, sig);
25                 if (ret < 1024) { return null; }
26                 Result_PublicKeyErrorZ ret_hu_conv = Result_PublicKeyErrorZ.constr_from_ptr(ret);
27                 return ret_hu_conv;
28         }
29
30         /**
31          * Verifies a message was signed by a PrivateKey that derives to a given PublicKey, given a message, a signature,
32          * and the PublicKey.
33          */
34         public static boolean verify(byte[] msg, java.lang.String sig, byte[] pk) {
35                 boolean ret = bindings.verify(msg, sig, pk);
36                 return ret;
37         }
38
39         /**
40          * Read a C2Tuple_BlockHashChannelMonitorZ from a byte array, created by C2Tuple_BlockHashChannelMonitorZ_write
41          */
42         public static Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ BlockHashChannelMonitorZ_read(byte[] ser, KeysInterface arg) {
43                 long ret = bindings.C2Tuple_BlockHashChannelMonitorZ_read(ser, arg == null ? 0 : arg.ptr);
44                 if (ret < 1024) { return null; }
45                 Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ ret_hu_conv = Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.constr_from_ptr(ret);
46                 ret_hu_conv.ptrs_to.add(arg);
47                 return ret_hu_conv;
48         }
49
50         /**
51          * Read a C2Tuple_BlockHashChannelManagerZ from a byte array, created by C2Tuple_BlockHashChannelManagerZ_write
52          */
53         public static Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ BlockHashChannelManagerZ_read(byte[] ser, KeysInterface arg_keys_manager, FeeEstimator arg_fee_estimator, Watch arg_chain_monitor, BroadcasterInterface arg_tx_broadcaster, Logger arg_logger, UserConfig arg_default_config, ChannelMonitor[] arg_channel_monitors) {
54                 long ret = bindings.C2Tuple_BlockHashChannelManagerZ_read(ser, bindings.ChannelManagerReadArgs_new(arg_keys_manager == null ? 0 : arg_keys_manager.ptr, arg_fee_estimator == null ? 0 : arg_fee_estimator.ptr, arg_chain_monitor == null ? 0 : arg_chain_monitor.ptr, arg_tx_broadcaster == null ? 0 : arg_tx_broadcaster.ptr, arg_logger == null ? 0 : arg_logger.ptr, arg_default_config == null ? 0 : arg_default_config.ptr & ~1, arg_channel_monitors != null ? Arrays.stream(arg_channel_monitors).mapToLong(arg_channel_monitors_conv_16 -> arg_channel_monitors_conv_16 == null ? 0 : arg_channel_monitors_conv_16.ptr & ~1).toArray() : null));
55                 if (ret < 1024) { return null; }
56                 Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ ret_hu_conv = Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.constr_from_ptr(ret);
57                 ret_hu_conv.ptrs_to.add(arg_keys_manager);
58                 ret_hu_conv.ptrs_to.add(arg_fee_estimator);
59                 ret_hu_conv.ptrs_to.add(arg_chain_monitor);
60                 ret_hu_conv.ptrs_to.add(arg_tx_broadcaster);
61                 ret_hu_conv.ptrs_to.add(arg_logger);
62                 ret_hu_conv.ptrs_to.add(arg_default_config);
63                 /* TODO 2 ChannelMonitor  */;
64                 return ret_hu_conv;
65         }
66
67         /**
68          * Build the commitment secret from the seed and the commitment number
69          */
70         public static byte[] build_commitment_secret(byte[] commitment_seed, long idx) {
71                 byte[] ret = bindings.build_commitment_secret(commitment_seed, idx);
72                 return ret;
73         }
74
75         /**
76          * Derives a per-commitment-transaction private key (eg an htlc key or delayed_payment key)
77          * from the base secret and the per_commitment_point.
78          * 
79          * Note that this is infallible iff we trust that at least one of the two input keys are randomly
80          * generated (ie our own).
81          */
82         public static Result_SecretKeyErrorZ derive_private_key(byte[] per_commitment_point, byte[] base_secret) {
83                 long ret = bindings.derive_private_key(per_commitment_point, base_secret);
84                 if (ret < 1024) { return null; }
85                 Result_SecretKeyErrorZ ret_hu_conv = Result_SecretKeyErrorZ.constr_from_ptr(ret);
86                 return ret_hu_conv;
87         }
88
89         /**
90          * Derives a per-commitment-transaction public key (eg an htlc key or a delayed_payment key)
91          * from the base point and the per_commitment_key. This is the public equivalent of
92          * derive_private_key - using only public keys to derive a public key instead of private keys.
93          * 
94          * Note that this is infallible iff we trust that at least one of the two input keys are randomly
95          * generated (ie our own).
96          */
97         public static Result_PublicKeyErrorZ derive_public_key(byte[] per_commitment_point, byte[] base_point) {
98                 long ret = bindings.derive_public_key(per_commitment_point, base_point);
99                 if (ret < 1024) { return null; }
100                 Result_PublicKeyErrorZ ret_hu_conv = Result_PublicKeyErrorZ.constr_from_ptr(ret);
101                 return ret_hu_conv;
102         }
103
104         /**
105          * Derives a per-commitment-transaction revocation key from its constituent parts.
106          * 
107          * Only the cheating participant owns a valid witness to propagate a revoked
108          * commitment transaction, thus per_commitment_secret always come from cheater
109          * and revocation_base_secret always come from punisher, which is the broadcaster
110          * of the transaction spending with this key knowledge.
111          * 
112          * Note that this is infallible iff we trust that at least one of the two input keys are randomly
113          * generated (ie our own).
114          */
115         public static Result_SecretKeyErrorZ derive_private_revocation_key(byte[] per_commitment_secret, byte[] countersignatory_revocation_base_secret) {
116                 long ret = bindings.derive_private_revocation_key(per_commitment_secret, countersignatory_revocation_base_secret);
117                 if (ret < 1024) { return null; }
118                 Result_SecretKeyErrorZ ret_hu_conv = Result_SecretKeyErrorZ.constr_from_ptr(ret);
119                 return ret_hu_conv;
120         }
121
122         /**
123          * Derives a per-commitment-transaction revocation public key from its constituent parts. This is
124          * the public equivalend of derive_private_revocation_key - using only public keys to derive a
125          * public key instead of private keys.
126          * 
127          * Only the cheating participant owns a valid witness to propagate a revoked
128          * commitment transaction, thus per_commitment_point always come from cheater
129          * and revocation_base_point always come from punisher, which is the broadcaster
130          * of the transaction spending with this key knowledge.
131          * 
132          * Note that this is infallible iff we trust that at least one of the two input keys are randomly
133          * generated (ie our own).
134          */
135         public static Result_PublicKeyErrorZ derive_public_revocation_key(byte[] per_commitment_point, byte[] countersignatory_revocation_base_point) {
136                 long ret = bindings.derive_public_revocation_key(per_commitment_point, countersignatory_revocation_base_point);
137                 if (ret < 1024) { return null; }
138                 Result_PublicKeyErrorZ ret_hu_conv = Result_PublicKeyErrorZ.constr_from_ptr(ret);
139                 return ret_hu_conv;
140         }
141
142         /**
143          * A script either spendable by the revocation
144          * key or the broadcaster_delayed_payment_key and satisfying the relative-locktime OP_CSV constrain.
145          * Encumbering a `to_holder` output on a commitment transaction or 2nd-stage HTLC transactions.
146          */
147         public static byte[] get_revokeable_redeemscript(byte[] revocation_key, short contest_delay, byte[] broadcaster_delayed_payment_key) {
148                 byte[] ret = bindings.get_revokeable_redeemscript(revocation_key, contest_delay, broadcaster_delayed_payment_key);
149                 return ret;
150         }
151
152         /**
153          * Gets the witness redeemscript for an HTLC output in a commitment transaction. Note that htlc
154          * does not need to have its previous_output_index filled.
155          */
156         public static byte[] get_htlc_redeemscript(HTLCOutputInCommitment htlc, TxCreationKeys keys) {
157                 byte[] ret = bindings.get_htlc_redeemscript(htlc == null ? 0 : htlc.ptr & ~1, keys == null ? 0 : keys.ptr & ~1);
158                 // this.ptrs_to.add(htlc);
159                 // this.ptrs_to.add(keys);
160                 return ret;
161         }
162
163         /**
164          * Gets the redeemscript for a funding output from the two funding public keys.
165          * Note that the order of funding public keys does not matter.
166          */
167         public static byte[] make_funding_redeemscript(byte[] broadcaster, byte[] countersignatory) {
168                 byte[] ret = bindings.make_funding_redeemscript(broadcaster, countersignatory);
169                 return ret;
170         }
171
172         /**
173          * Builds an unsigned HTLC-Success or HTLC-Timeout transaction from the given channel and HTLC
174          * parameters. This is used by [`TrustedCommitmentTransaction::get_htlc_sigs`] to fetch the
175          * transaction which needs signing, and can be used to construct an HTLC transaction which is
176          * broadcastable given a counterparty HTLC signature.
177          * 
178          * Panics if htlc.transaction_output_index.is_none() (as such HTLCs do not appear in the
179          * commitment transaction).
180          */
181         public static byte[] build_htlc_transaction(byte[] commitment_txid, int feerate_per_kw, short contest_delay, HTLCOutputInCommitment htlc, byte[] broadcaster_delayed_payment_key, byte[] revocation_key) {
182                 byte[] ret = bindings.build_htlc_transaction(commitment_txid, feerate_per_kw, contest_delay, htlc == null ? 0 : htlc.ptr & ~1, broadcaster_delayed_payment_key, revocation_key);
183                 // this.ptrs_to.add(htlc);
184                 return ret;
185         }
186
187         /**
188          * Commitment transaction numbers which appear in the transactions themselves are XOR'd with a
189          * shared secret first. This prevents on-chain observers from discovering how many commitment
190          * transactions occurred in a channel before it was closed.
191          * 
192          * This function gets the shared secret from relevant channel public keys and can be used to
193          * \"decrypt\" the commitment transaction number given a commitment transaction on-chain.
194          */
195         public static long get_commitment_transaction_number_obscure_factor(byte[] broadcaster_payment_basepoint, byte[] countersignatory_payment_basepoint, boolean outbound_from_broadcaster) {
196                 long ret = bindings.get_commitment_transaction_number_obscure_factor(broadcaster_payment_basepoint, countersignatory_payment_basepoint, outbound_from_broadcaster);
197                 return ret;
198         }
199
200         /**
201          * Gets a keysend route from us (payer) to the given target node (payee). This is needed because
202          * keysend payments do not have an invoice from which to pull the payee's supported features, which
203          * makes it tricky to otherwise supply the `payee_features` parameter of `get_route`.
204          * 
205          * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
206          */
207         public static Result_RouteLightningErrorZ get_keysend_route(byte[] our_node_id, NetworkGraph network, byte[] payee, @Nullable ChannelDetails[] first_hops, RouteHint[] last_hops, long final_value_msat, int final_cltv, Logger logger) {
208                 long ret = bindings.get_keysend_route(our_node_id, network == null ? 0 : network.ptr & ~1, payee, first_hops != null ? Arrays.stream(first_hops).mapToLong(first_hops_conv_16 -> first_hops_conv_16 == null ? 0 : first_hops_conv_16.ptr & ~1).toArray() : null, last_hops != null ? Arrays.stream(last_hops).mapToLong(last_hops_conv_11 -> last_hops_conv_11 == null ? 0 : last_hops_conv_11.ptr & ~1).toArray() : null, final_value_msat, final_cltv, logger == null ? 0 : logger.ptr);
209                 if (ret < 1024) { return null; }
210                 Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
211                 ret_hu_conv.ptrs_to.add(network);
212                 /* TODO 2 ChannelDetails  */;
213                 /* TODO 2 RouteHint  */;
214                 ret_hu_conv.ptrs_to.add(logger);
215                 return ret_hu_conv;
216         }
217
218         /**
219          * Gets a route from us (payer) to the given target node (payee).
220          * 
221          * If the payee provided features in their invoice, they should be provided via payee_features.
222          * Without this, MPP will only be used if the payee's features are available in the network graph.
223          * 
224          * Private routing paths between a public node and the target may be included in `last_hops`.
225          * Currently, only the last hop in each path is considered.
226          * 
227          * If some channels aren't announced, it may be useful to fill in a first_hops with the
228          * results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
229          * view of our local channels (from net_graph_msg_handler) will be ignored, and only those
230          * in first_hops will be used.
231          * 
232          * Panics if first_hops contains channels without short_channel_ids
233          * (ChannelManager::list_usable_channels will never include such channels).
234          * 
235          * The fees on channels from us to next-hops are ignored (as they are assumed to all be
236          * equal), however the enabled/disabled bit on such channels as well as the
237          * htlc_minimum_msat/htlc_maximum_msat *are* checked as they may change based on the receiving node.
238          * 
239          * Note that payee_features (or a relevant inner pointer) may be NULL or all-0s to represent None
240          * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
241          */
242         public static Result_RouteLightningErrorZ get_route(byte[] our_node_id, NetworkGraph network, byte[] payee, @Nullable InvoiceFeatures payee_features, @Nullable ChannelDetails[] first_hops, RouteHint[] last_hops, long final_value_msat, int final_cltv, Logger logger) {
243                 long ret = bindings.get_route(our_node_id, network == null ? 0 : network.ptr & ~1, payee, payee_features == null ? 0 : payee_features.ptr & ~1, first_hops != null ? Arrays.stream(first_hops).mapToLong(first_hops_conv_16 -> first_hops_conv_16 == null ? 0 : first_hops_conv_16.ptr & ~1).toArray() : null, last_hops != null ? Arrays.stream(last_hops).mapToLong(last_hops_conv_11 -> last_hops_conv_11 == null ? 0 : last_hops_conv_11.ptr & ~1).toArray() : null, final_value_msat, final_cltv, logger == null ? 0 : logger.ptr);
244                 if (ret < 1024) { return null; }
245                 Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
246                 ret_hu_conv.ptrs_to.add(network);
247                 ret_hu_conv.ptrs_to.add(payee_features);
248                 /* TODO 2 ChannelDetails  */;
249                 /* TODO 2 RouteHint  */;
250                 ret_hu_conv.ptrs_to.add(logger);
251                 return ret_hu_conv;
252         }
253
254         /**
255          * Writes the provided `ChannelManager` to the path provided at `FilesystemPersister`
256          * initialization, within a file called \"manager\".
257          */
258         public static Result_NoneErrorZ FilesystemPersister_persist_manager(java.lang.String data_dir, ChannelManager manager) {
259                 long ret = bindings.FilesystemPersister_persist_manager(data_dir, manager == null ? 0 : manager.ptr & ~1);
260                 if (ret < 1024) { return null; }
261                 Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
262                 ret_hu_conv.ptrs_to.add(manager);
263                 return ret_hu_conv;
264         }
265
266         /**
267          * Utility to construct an invoice. Generally, unless you want to do something like a custom
268          * cltv_expiry, this is what you should be using to create an invoice. The reason being, this
269          * method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user
270          * doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
271          * that the payment secret is valid when the invoice is paid.
272          */
273         public static Result_InvoiceSignOrCreationErrorZ create_invoice_from_channelmanager(ChannelManager channelmanager, KeysInterface keys_manager, org.ldk.enums.Currency network, Option_u64Z amt_msat, java.lang.String description) {
274                 long ret = bindings.create_invoice_from_channelmanager(channelmanager == null ? 0 : channelmanager.ptr & ~1, keys_manager == null ? 0 : keys_manager.ptr, network, amt_msat.ptr, description);
275                 if (ret < 1024) { return null; }
276                 Result_InvoiceSignOrCreationErrorZ ret_hu_conv = Result_InvoiceSignOrCreationErrorZ.constr_from_ptr(ret);
277                 ret_hu_conv.ptrs_to.add(channelmanager);
278                 ret_hu_conv.ptrs_to.add(keys_manager);
279                 return ret_hu_conv;
280         }
281
282 }