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