Merge pull request #120 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 org.ldk.enums.*;
4 import org.ldk.util.*;
5 import java.util.Arrays;
6 import java.lang.ref.Reference;
7 import javax.annotation.Nullable;
8
9 public class UtilMethods {
10         /**
11          * Constructs a new COption_NoneZ containing a
12          */
13         public static COption_NoneZ COption_NoneZ_some() {
14                 COption_NoneZ ret = bindings.COption_NoneZ_some();
15                 return ret;
16         }
17
18         /**
19          * Constructs a new COption_NoneZ containing nothing
20          */
21         public static COption_NoneZ COption_NoneZ_none() {
22                 COption_NoneZ ret = bindings.COption_NoneZ_none();
23                 return ret;
24         }
25
26         /**
27          * Read a ClosureReason from a byte array, created by ClosureReason_write
28          */
29         public static Result_COption_ClosureReasonZDecodeErrorZ ClosureReason_read(byte[] ser) {
30                 long ret = bindings.ClosureReason_read(ser);
31                 Reference.reachabilityFence(ser);
32                 if (ret >= 0 && ret <= 4096) { return null; }
33                 Result_COption_ClosureReasonZDecodeErrorZ ret_hu_conv = Result_COption_ClosureReasonZDecodeErrorZ.constr_from_ptr(ret);
34                 return ret_hu_conv;
35         }
36
37         /**
38          * Read a HTLCDestination from a byte array, created by HTLCDestination_write
39          */
40         public static Result_COption_HTLCDestinationZDecodeErrorZ HTLCDestination_read(byte[] ser) {
41                 long ret = bindings.HTLCDestination_read(ser);
42                 Reference.reachabilityFence(ser);
43                 if (ret >= 0 && ret <= 4096) { return null; }
44                 Result_COption_HTLCDestinationZDecodeErrorZ ret_hu_conv = Result_COption_HTLCDestinationZDecodeErrorZ.constr_from_ptr(ret);
45                 return ret_hu_conv;
46         }
47
48         /**
49          * Read a Event from a byte array, created by Event_write
50          */
51         public static Result_COption_EventZDecodeErrorZ Event_read(byte[] ser) {
52                 long ret = bindings.Event_read(ser);
53                 Reference.reachabilityFence(ser);
54                 if (ret >= 0 && ret <= 4096) { return null; }
55                 Result_COption_EventZDecodeErrorZ ret_hu_conv = Result_COption_EventZDecodeErrorZ.constr_from_ptr(ret);
56                 return ret_hu_conv;
57         }
58
59         /**
60          * Creates a digital signature of a message given a SecretKey, like the node's secret.
61          * 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.
62          * Signatures are EC recoverable, meaning that given the message and the signature the PublicKey of the signer can be extracted.
63          */
64         public static Result_StringErrorZ sign(byte[] msg, byte[] sk) {
65                 long ret = bindings.sign(msg, InternalUtils.check_arr_len(sk, 32));
66                 Reference.reachabilityFence(msg);
67                 Reference.reachabilityFence(sk);
68                 if (ret >= 0 && ret <= 4096) { return null; }
69                 Result_StringErrorZ ret_hu_conv = Result_StringErrorZ.constr_from_ptr(ret);
70                 return ret_hu_conv;
71         }
72
73         /**
74          * Recovers the PublicKey of the signer of the message given the message and the signature.
75          */
76         public static Result_PublicKeyErrorZ recover_pk(byte[] msg, java.lang.String sig) {
77                 long ret = bindings.recover_pk(msg, sig);
78                 Reference.reachabilityFence(msg);
79                 Reference.reachabilityFence(sig);
80                 if (ret >= 0 && ret <= 4096) { return null; }
81                 Result_PublicKeyErrorZ ret_hu_conv = Result_PublicKeyErrorZ.constr_from_ptr(ret);
82                 return ret_hu_conv;
83         }
84
85         /**
86          * Verifies a message was signed by a PrivateKey that derives to a given PublicKey, given a message, a signature,
87          * and the PublicKey.
88          */
89         public static boolean verify(byte[] msg, java.lang.String sig, byte[] pk) {
90                 boolean ret = bindings.verify(msg, sig, InternalUtils.check_arr_len(pk, 33));
91                 Reference.reachabilityFence(msg);
92                 Reference.reachabilityFence(sig);
93                 Reference.reachabilityFence(pk);
94                 return ret;
95         }
96
97         /**
98          * Construct the invoice's HRP and signatureless data into a preimage to be hashed.
99          */
100         public static byte[] construct_invoice_preimage(byte[] hrp_bytes, UInt5[] data_without_signature) {
101                 byte[] ret = bindings.construct_invoice_preimage(hrp_bytes, data_without_signature != null ? InternalUtils.convUInt5Array(data_without_signature) : null);
102                 Reference.reachabilityFence(hrp_bytes);
103                 Reference.reachabilityFence(data_without_signature);
104                 return ret;
105         }
106
107         /**
108          * Read a MonitorEvent from a byte array, created by MonitorEvent_write
109          */
110         public static Result_COption_MonitorEventZDecodeErrorZ MonitorEvent_read(byte[] ser) {
111                 long ret = bindings.MonitorEvent_read(ser);
112                 Reference.reachabilityFence(ser);
113                 if (ret >= 0 && ret <= 4096) { return null; }
114                 Result_COption_MonitorEventZDecodeErrorZ ret_hu_conv = Result_COption_MonitorEventZDecodeErrorZ.constr_from_ptr(ret);
115                 return ret_hu_conv;
116         }
117
118         /**
119          * Read a C2Tuple_BlockHashChannelMonitorZ from a byte array, created by C2Tuple_BlockHashChannelMonitorZ_write
120          */
121         public static Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ C2Tuple_BlockHashChannelMonitorZ_read(byte[] ser, KeysInterface arg) {
122                 long ret = bindings.C2Tuple_BlockHashChannelMonitorZ_read(ser, arg == null ? 0 : arg.ptr);
123                 Reference.reachabilityFence(ser);
124                 Reference.reachabilityFence(arg);
125                 if (ret >= 0 && ret <= 4096) { return null; }
126                 Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ ret_hu_conv = Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.constr_from_ptr(ret);
127                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg); };
128                 return ret_hu_conv;
129         }
130
131         /**
132          * Fetches the set of [`NodeFeatures`] flags which are provided by or required by
133          * [`ChannelManager`].
134          */
135         public static NodeFeatures provided_node_features() {
136                 long ret = bindings.provided_node_features();
137                 if (ret >= 0 && ret <= 4096) { return null; }
138                 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
139                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
140                 return ret_hu_conv;
141         }
142
143         /**
144          * Fetches the set of [`ChannelFeatures`] flags which are provided by or required by
145          * [`ChannelManager`].
146          */
147         public static ChannelFeatures provided_channel_features() {
148                 long ret = bindings.provided_channel_features();
149                 if (ret >= 0 && ret <= 4096) { return null; }
150                 org.ldk.structs.ChannelFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChannelFeatures(null, ret); }
151                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
152                 return ret_hu_conv;
153         }
154
155         /**
156          * Fetches the set of [`InitFeatures`] flags which are provided by or required by
157          * [`ChannelManager`].
158          */
159         public static InitFeatures provided_init_features() {
160                 long ret = bindings.provided_init_features();
161                 if (ret >= 0 && ret <= 4096) { return null; }
162                 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
163                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
164                 return ret_hu_conv;
165         }
166
167         /**
168          * Read a C2Tuple_BlockHashChannelManagerZ from a byte array, created by C2Tuple_BlockHashChannelManagerZ_write
169          */
170         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) {
171                 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, 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).toArray() : null));
172                 Reference.reachabilityFence(ser);
173                 Reference.reachabilityFence(arg_keys_manager);
174                 Reference.reachabilityFence(arg_fee_estimator);
175                 Reference.reachabilityFence(arg_chain_monitor);
176                 Reference.reachabilityFence(arg_tx_broadcaster);
177                 Reference.reachabilityFence(arg_logger);
178                 Reference.reachabilityFence(arg_default_config);
179                 Reference.reachabilityFence(arg_channel_monitors);
180                 if (ret >= 0 && ret <= 4096) { return null; }
181                 Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ ret_hu_conv = Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.constr_from_ptr(ret);
182                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_keys_manager); };
183                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_fee_estimator); };
184                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_chain_monitor); };
185                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_tx_broadcaster); };
186                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_logger); };
187                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_default_config); };
188                 for (ChannelMonitor arg_channel_monitors_conv_16: arg_channel_monitors) { if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_channel_monitors_conv_16); }; };
189                 return ret_hu_conv;
190         }
191
192         /**
193          * Equivalent to [`crate::ln::channelmanager::ChannelManager::create_inbound_payment`], but no
194          * `ChannelManager` is required. Useful for generating invoices for [phantom node payments] without
195          * a `ChannelManager`.
196          * 
197          * `keys` is generated by calling [`KeysInterface::get_inbound_payment_key_material`] and then
198          * calling [`ExpandedKey::new`] with its result. It is recommended to cache this value and not
199          * regenerate it for each new inbound payment.
200          * 
201          * `current_time` is a Unix timestamp representing the current time.
202          * 
203          * [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager
204          */
205         public static Result_C2Tuple_PaymentHashPaymentSecretZNoneZ create(ExpandedKey keys, Option_u64Z min_value_msat, int invoice_expiry_delta_secs, KeysInterface keys_manager, long current_time) {
206                 long ret = bindings.create(keys == null ? 0 : keys.ptr, min_value_msat.ptr, invoice_expiry_delta_secs, keys_manager == null ? 0 : keys_manager.ptr, current_time);
207                 Reference.reachabilityFence(keys);
208                 Reference.reachabilityFence(min_value_msat);
209                 Reference.reachabilityFence(invoice_expiry_delta_secs);
210                 Reference.reachabilityFence(keys_manager);
211                 Reference.reachabilityFence(current_time);
212                 if (ret >= 0 && ret <= 4096) { return null; }
213                 Result_C2Tuple_PaymentHashPaymentSecretZNoneZ ret_hu_conv = Result_C2Tuple_PaymentHashPaymentSecretZNoneZ.constr_from_ptr(ret);
214                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(keys); };
215                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(keys_manager); };
216                 return ret_hu_conv;
217         }
218
219         /**
220          * Equivalent to [`crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash`],
221          * but no `ChannelManager` is required. Useful for generating invoices for [phantom node payments]
222          * without a `ChannelManager`.
223          * 
224          * See [`create`] for information on the `keys` and `current_time` parameters.
225          * 
226          * [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager
227          */
228         public static Result_PaymentSecretNoneZ create_from_hash(ExpandedKey keys, Option_u64Z min_value_msat, byte[] payment_hash, int invoice_expiry_delta_secs, long current_time) {
229                 long ret = bindings.create_from_hash(keys == null ? 0 : keys.ptr, min_value_msat.ptr, InternalUtils.check_arr_len(payment_hash, 32), invoice_expiry_delta_secs, current_time);
230                 Reference.reachabilityFence(keys);
231                 Reference.reachabilityFence(min_value_msat);
232                 Reference.reachabilityFence(payment_hash);
233                 Reference.reachabilityFence(invoice_expiry_delta_secs);
234                 Reference.reachabilityFence(current_time);
235                 if (ret >= 0 && ret <= 4096) { return null; }
236                 Result_PaymentSecretNoneZ ret_hu_conv = Result_PaymentSecretNoneZ.constr_from_ptr(ret);
237                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(keys); };
238                 return ret_hu_conv;
239         }
240
241         /**
242          * Gets the weight for an HTLC-Success transaction.
243          */
244         public static long htlc_success_tx_weight(boolean opt_anchors) {
245                 long ret = bindings.htlc_success_tx_weight(opt_anchors);
246                 Reference.reachabilityFence(opt_anchors);
247                 return ret;
248         }
249
250         /**
251          * Gets the weight for an HTLC-Timeout transaction.
252          */
253         public static long htlc_timeout_tx_weight(boolean opt_anchors) {
254                 long ret = bindings.htlc_timeout_tx_weight(opt_anchors);
255                 Reference.reachabilityFence(opt_anchors);
256                 return ret;
257         }
258
259         /**
260          * Build the commitment secret from the seed and the commitment number
261          */
262         public static byte[] build_commitment_secret(byte[] commitment_seed, long idx) {
263                 byte[] ret = bindings.build_commitment_secret(InternalUtils.check_arr_len(commitment_seed, 32), idx);
264                 Reference.reachabilityFence(commitment_seed);
265                 Reference.reachabilityFence(idx);
266                 return ret;
267         }
268
269         /**
270          * Build a closing transaction
271          */
272         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) {
273                 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);
274                 Reference.reachabilityFence(to_holder_value_sat);
275                 Reference.reachabilityFence(to_counterparty_value_sat);
276                 Reference.reachabilityFence(to_holder_script);
277                 Reference.reachabilityFence(to_counterparty_script);
278                 Reference.reachabilityFence(funding_outpoint);
279                 return ret;
280         }
281
282         /**
283          * Derives a per-commitment-transaction private key (eg an htlc key or delayed_payment key)
284          * from the base secret and the per_commitment_point.
285          * 
286          * Note that this is infallible iff we trust that at least one of the two input keys are randomly
287          * generated (ie our own).
288          */
289         public static Result_SecretKeyErrorZ derive_private_key(byte[] per_commitment_point, byte[] base_secret) {
290                 long ret = bindings.derive_private_key(InternalUtils.check_arr_len(per_commitment_point, 33), InternalUtils.check_arr_len(base_secret, 32));
291                 Reference.reachabilityFence(per_commitment_point);
292                 Reference.reachabilityFence(base_secret);
293                 if (ret >= 0 && ret <= 4096) { return null; }
294                 Result_SecretKeyErrorZ ret_hu_conv = Result_SecretKeyErrorZ.constr_from_ptr(ret);
295                 return ret_hu_conv;
296         }
297
298         /**
299          * Derives a per-commitment-transaction public key (eg an htlc key or a delayed_payment key)
300          * from the base point and the per_commitment_key. This is the public equivalent of
301          * derive_private_key - using only public keys to derive a public key instead of private keys.
302          * 
303          * Note that this is infallible iff we trust that at least one of the two input keys are randomly
304          * generated (ie our own).
305          */
306         public static Result_PublicKeyErrorZ derive_public_key(byte[] per_commitment_point, byte[] base_point) {
307                 long ret = bindings.derive_public_key(InternalUtils.check_arr_len(per_commitment_point, 33), InternalUtils.check_arr_len(base_point, 33));
308                 Reference.reachabilityFence(per_commitment_point);
309                 Reference.reachabilityFence(base_point);
310                 if (ret >= 0 && ret <= 4096) { return null; }
311                 Result_PublicKeyErrorZ ret_hu_conv = Result_PublicKeyErrorZ.constr_from_ptr(ret);
312                 return ret_hu_conv;
313         }
314
315         /**
316          * Derives a per-commitment-transaction revocation key from its constituent parts.
317          * 
318          * Only the cheating participant owns a valid witness to propagate a revoked
319          * commitment transaction, thus per_commitment_secret always come from cheater
320          * and revocation_base_secret always come from punisher, which is the broadcaster
321          * of the transaction spending with this key knowledge.
322          * 
323          * Note that this is infallible iff we trust that at least one of the two input keys are randomly
324          * generated (ie our own).
325          */
326         public static Result_SecretKeyErrorZ derive_private_revocation_key(byte[] per_commitment_secret, byte[] countersignatory_revocation_base_secret) {
327                 long ret = bindings.derive_private_revocation_key(InternalUtils.check_arr_len(per_commitment_secret, 32), InternalUtils.check_arr_len(countersignatory_revocation_base_secret, 32));
328                 Reference.reachabilityFence(per_commitment_secret);
329                 Reference.reachabilityFence(countersignatory_revocation_base_secret);
330                 if (ret >= 0 && ret <= 4096) { return null; }
331                 Result_SecretKeyErrorZ ret_hu_conv = Result_SecretKeyErrorZ.constr_from_ptr(ret);
332                 return ret_hu_conv;
333         }
334
335         /**
336          * Derives a per-commitment-transaction revocation public key from its constituent parts. This is
337          * the public equivalend of derive_private_revocation_key - using only public keys to derive a
338          * public key instead of private keys.
339          * 
340          * Only the cheating participant owns a valid witness to propagate a revoked
341          * commitment transaction, thus per_commitment_point always come from cheater
342          * and revocation_base_point always come from punisher, which is the broadcaster
343          * of the transaction spending with this key knowledge.
344          * 
345          * Note that this is infallible iff we trust that at least one of the two input keys are randomly
346          * generated (ie our own).
347          */
348         public static Result_PublicKeyErrorZ derive_public_revocation_key(byte[] per_commitment_point, byte[] countersignatory_revocation_base_point) {
349                 long ret = bindings.derive_public_revocation_key(InternalUtils.check_arr_len(per_commitment_point, 33), InternalUtils.check_arr_len(countersignatory_revocation_base_point, 33));
350                 Reference.reachabilityFence(per_commitment_point);
351                 Reference.reachabilityFence(countersignatory_revocation_base_point);
352                 if (ret >= 0 && ret <= 4096) { return null; }
353                 Result_PublicKeyErrorZ ret_hu_conv = Result_PublicKeyErrorZ.constr_from_ptr(ret);
354                 return ret_hu_conv;
355         }
356
357         /**
358          * A script either spendable by the revocation
359          * key or the broadcaster_delayed_payment_key and satisfying the relative-locktime OP_CSV constrain.
360          * Encumbering a `to_holder` output on a commitment transaction or 2nd-stage HTLC transactions.
361          */
362         public static byte[] get_revokeable_redeemscript(byte[] revocation_key, short contest_delay, byte[] broadcaster_delayed_payment_key) {
363                 byte[] ret = bindings.get_revokeable_redeemscript(InternalUtils.check_arr_len(revocation_key, 33), contest_delay, InternalUtils.check_arr_len(broadcaster_delayed_payment_key, 33));
364                 Reference.reachabilityFence(revocation_key);
365                 Reference.reachabilityFence(contest_delay);
366                 Reference.reachabilityFence(broadcaster_delayed_payment_key);
367                 return ret;
368         }
369
370         /**
371          * Gets the witness redeemscript for an HTLC output in a commitment transaction. Note that htlc
372          * does not need to have its previous_output_index filled.
373          */
374         public static byte[] get_htlc_redeemscript(HTLCOutputInCommitment htlc, boolean opt_anchors, TxCreationKeys keys) {
375                 byte[] ret = bindings.get_htlc_redeemscript(htlc == null ? 0 : htlc.ptr, opt_anchors, keys == null ? 0 : keys.ptr);
376                 Reference.reachabilityFence(htlc);
377                 Reference.reachabilityFence(opt_anchors);
378                 Reference.reachabilityFence(keys);
379                 return ret;
380         }
381
382         /**
383          * Gets the redeemscript for a funding output from the two funding public keys.
384          * Note that the order of funding public keys does not matter.
385          */
386         public static byte[] make_funding_redeemscript(byte[] broadcaster, byte[] countersignatory) {
387                 byte[] ret = bindings.make_funding_redeemscript(InternalUtils.check_arr_len(broadcaster, 33), InternalUtils.check_arr_len(countersignatory, 33));
388                 Reference.reachabilityFence(broadcaster);
389                 Reference.reachabilityFence(countersignatory);
390                 return ret;
391         }
392
393         /**
394          * Builds an unsigned HTLC-Success or HTLC-Timeout transaction from the given channel and HTLC
395          * parameters. This is used by [`TrustedCommitmentTransaction::get_htlc_sigs`] to fetch the
396          * transaction which needs signing, and can be used to construct an HTLC transaction which is
397          * broadcastable given a counterparty HTLC signature.
398          * 
399          * Panics if htlc.transaction_output_index.is_none() (as such HTLCs do not appear in the
400          * commitment transaction).
401          */
402         public static byte[] build_htlc_transaction(byte[] commitment_txid, int feerate_per_kw, short contest_delay, HTLCOutputInCommitment htlc, boolean opt_anchors, byte[] broadcaster_delayed_payment_key, byte[] revocation_key) {
403                 byte[] ret = bindings.build_htlc_transaction(InternalUtils.check_arr_len(commitment_txid, 32), feerate_per_kw, contest_delay, htlc == null ? 0 : htlc.ptr, opt_anchors, InternalUtils.check_arr_len(broadcaster_delayed_payment_key, 33), InternalUtils.check_arr_len(revocation_key, 33));
404                 Reference.reachabilityFence(commitment_txid);
405                 Reference.reachabilityFence(feerate_per_kw);
406                 Reference.reachabilityFence(contest_delay);
407                 Reference.reachabilityFence(htlc);
408                 Reference.reachabilityFence(opt_anchors);
409                 Reference.reachabilityFence(broadcaster_delayed_payment_key);
410                 Reference.reachabilityFence(revocation_key);
411                 return ret;
412         }
413
414         /**
415          * Gets the witnessScript for an anchor output from the funding public key.
416          * The witness in the spending input must be:
417          * <BIP 143 funding_signature>
418          * After 16 blocks of confirmation, an alternative satisfying witness could be:
419          * <>
420          * (empty vector required to satisfy compliance with MINIMALIF-standard rule)
421          */
422         public static byte[] get_anchor_redeemscript(byte[] funding_pubkey) {
423                 byte[] ret = bindings.get_anchor_redeemscript(InternalUtils.check_arr_len(funding_pubkey, 33));
424                 Reference.reachabilityFence(funding_pubkey);
425                 return ret;
426         }
427
428         /**
429          * Commitment transaction numbers which appear in the transactions themselves are XOR'd with a
430          * shared secret first. This prevents on-chain observers from discovering how many commitment
431          * transactions occurred in a channel before it was closed.
432          * 
433          * This function gets the shared secret from relevant channel public keys and can be used to
434          * \"decrypt\" the commitment transaction number given a commitment transaction on-chain.
435          */
436         public static long get_commitment_transaction_number_obscure_factor(byte[] broadcaster_payment_basepoint, byte[] countersignatory_payment_basepoint, boolean outbound_from_broadcaster) {
437                 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);
438                 Reference.reachabilityFence(broadcaster_payment_basepoint);
439                 Reference.reachabilityFence(countersignatory_payment_basepoint);
440                 Reference.reachabilityFence(outbound_from_broadcaster);
441                 return ret;
442         }
443
444         /**
445          * Read a NetworkUpdate from a byte array, created by NetworkUpdate_write
446          */
447         public static Result_COption_NetworkUpdateZDecodeErrorZ NetworkUpdate_read(byte[] ser) {
448                 long ret = bindings.NetworkUpdate_read(ser);
449                 Reference.reachabilityFence(ser);
450                 if (ret >= 0 && ret <= 4096) { return null; }
451                 Result_COption_NetworkUpdateZDecodeErrorZ ret_hu_conv = Result_COption_NetworkUpdateZDecodeErrorZ.constr_from_ptr(ret);
452                 return ret_hu_conv;
453         }
454
455         /**
456          * Finds a route from us (payer) to the given target node (payee).
457          * 
458          * If the payee provided features in their invoice, they should be provided via `params.payee`.
459          * Without this, MPP will only be used if the payee's features are available in the network graph.
460          * 
461          * Private routing paths between a public node and the target may be included in `params.payee`.
462          * 
463          * If some channels aren't announced, it may be useful to fill in `first_hops` with the results
464          * from [`ChannelManager::list_usable_channels`]. If it is filled in, the view of these channels
465          * from `network_graph` will be ignored, and only those in `first_hops` will be used.
466          * 
467          * The fees on channels from us to the next hop are ignored as they are assumed to all be equal.
468          * However, the enabled/disabled bit on such channels as well as the `htlc_minimum_msat` /
469          * `htlc_maximum_msat` *are* checked as they may change based on the receiving node.
470          * 
471          * # Note
472          * 
473          * May be used to re-compute a [`Route`] when handling a [`Event::PaymentPathFailed`]. Any
474          * adjustments to the [`NetworkGraph`] and channel scores should be made prior to calling this
475          * function.
476          * 
477          * # Panics
478          * 
479          * Panics if first_hops contains channels without short_channel_ids;
480          * [`ChannelManager::list_usable_channels`] will never include such channels.
481          * 
482          * [`ChannelManager::list_usable_channels`]: crate::ln::channelmanager::ChannelManager::list_usable_channels
483          * [`Event::PaymentPathFailed`]: crate::util::events::Event::PaymentPathFailed
484          * [`NetworkGraph`]: crate::routing::gossip::NetworkGraph
485          * 
486          * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
487          */
488         public static Result_RouteLightningErrorZ find_route(byte[] our_node_pubkey, RouteParameters route_params, NetworkGraph network_graph, @Nullable ChannelDetails[] first_hops, Logger logger, Score scorer, byte[] random_seed_bytes) {
489                 long ret = bindings.find_route(InternalUtils.check_arr_len(our_node_pubkey, 33), route_params == null ? 0 : route_params.ptr, network_graph == null ? 0 : network_graph.ptr, first_hops != null ? Arrays.stream(first_hops).mapToLong(first_hops_conv_16 -> first_hops_conv_16 == null ? 0 : first_hops_conv_16.ptr).toArray() : null, logger == null ? 0 : logger.ptr, scorer == null ? 0 : scorer.ptr, InternalUtils.check_arr_len(random_seed_bytes, 32));
490                 Reference.reachabilityFence(our_node_pubkey);
491                 Reference.reachabilityFence(route_params);
492                 Reference.reachabilityFence(network_graph);
493                 Reference.reachabilityFence(first_hops);
494                 Reference.reachabilityFence(logger);
495                 Reference.reachabilityFence(scorer);
496                 Reference.reachabilityFence(random_seed_bytes);
497                 if (ret >= 0 && ret <= 4096) { return null; }
498                 Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
499                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(route_params); };
500                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(network_graph); };
501                 if (first_hops != null) { for (ChannelDetails first_hops_conv_16: first_hops) { if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(first_hops_conv_16); }; } };
502                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
503                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(scorer); };
504                 return ret_hu_conv;
505         }
506
507         /**
508          * Construct a route from us (payer) to the target node (payee) via the given hops (which should
509          * exclude the payer, but include the payee). This may be useful, e.g., for probing the chosen path.
510          * 
511          * Re-uses logic from `find_route`, so the restrictions described there also apply here.
512          */
513         public static Result_RouteLightningErrorZ build_route_from_hops(byte[] our_node_pubkey, byte[][] hops, RouteParameters route_params, NetworkGraph network_graph, Logger logger, byte[] random_seed_bytes) {
514                 long ret = bindings.build_route_from_hops(InternalUtils.check_arr_len(our_node_pubkey, 33), hops != null ? Arrays.stream(hops).map(hops_conv_8 -> InternalUtils.check_arr_len(hops_conv_8, 33)).toArray(byte[][]::new) : null, route_params == null ? 0 : route_params.ptr, network_graph == null ? 0 : network_graph.ptr, logger == null ? 0 : logger.ptr, InternalUtils.check_arr_len(random_seed_bytes, 32));
515                 Reference.reachabilityFence(our_node_pubkey);
516                 Reference.reachabilityFence(hops);
517                 Reference.reachabilityFence(route_params);
518                 Reference.reachabilityFence(network_graph);
519                 Reference.reachabilityFence(logger);
520                 Reference.reachabilityFence(random_seed_bytes);
521                 if (ret >= 0 && ret <= 4096) { return null; }
522                 Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
523                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(route_params); };
524                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(network_graph); };
525                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
526                 return ret_hu_conv;
527         }
528
529         /**
530          * Utility to create an invoice that can be paid to one of multiple nodes, or a \"phantom invoice.\"
531          * See [`PhantomKeysManager`] for more information on phantom node payments.
532          * 
533          * `phantom_route_hints` parameter:
534          * Contains channel info for all nodes participating in the phantom invoice
535          * Entries are retrieved from a call to [`ChannelManager::get_phantom_route_hints`] on each
536          * participating node
537          * It is fine to cache `phantom_route_hints` and reuse it across invoices, as long as the data is
538          * updated when a channel becomes disabled or closes
539          * Note that if too many channels are included in [`PhantomRouteHints::channels`], the invoice
540          * may be too long for QR code scanning. To fix this, `PhantomRouteHints::channels` may be pared
541          * down
542          * 
543          * `payment_hash` can be specified if you have a specific need for a custom payment hash (see the difference
544          * between [`ChannelManager::create_inbound_payment`] and [`ChannelManager::create_inbound_payment_for_hash`]).
545          * If `None` is provided for `payment_hash`, then one will be created.
546          * 
547          * `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
548          * in excess of the current time.
549          * 
550          * Note that the provided `keys_manager`'s `KeysInterface` implementation must support phantom
551          * invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this
552          * requirement).
553          * 
554          * [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager
555          * [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints
556          * [`ChannelManager::create_inbound_payment`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment
557          * [`ChannelManager::create_inbound_payment_for_hash`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
558          * [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels
559          * 
560          * Note that payment_hash (or a relevant inner pointer) may be NULL or all-0s to represent None
561          */
562         public static Result_InvoiceSignOrCreationErrorZ create_phantom_invoice(Option_u64Z amt_msat, @Nullable byte[] payment_hash, java.lang.String description, int invoice_expiry_delta_secs, PhantomRouteHints[] phantom_route_hints, KeysInterface keys_manager, Logger logger, org.ldk.enums.Currency network) {
563                 long ret = bindings.create_phantom_invoice(amt_msat.ptr, InternalUtils.check_arr_len(payment_hash, 32), description, invoice_expiry_delta_secs, phantom_route_hints != null ? Arrays.stream(phantom_route_hints).mapToLong(phantom_route_hints_conv_19 -> phantom_route_hints_conv_19 == null ? 0 : phantom_route_hints_conv_19.ptr).toArray() : null, keys_manager == null ? 0 : keys_manager.ptr, logger == null ? 0 : logger.ptr, network);
564                 Reference.reachabilityFence(amt_msat);
565                 Reference.reachabilityFence(payment_hash);
566                 Reference.reachabilityFence(description);
567                 Reference.reachabilityFence(invoice_expiry_delta_secs);
568                 Reference.reachabilityFence(phantom_route_hints);
569                 Reference.reachabilityFence(keys_manager);
570                 Reference.reachabilityFence(logger);
571                 Reference.reachabilityFence(network);
572                 if (ret >= 0 && ret <= 4096) { return null; }
573                 Result_InvoiceSignOrCreationErrorZ ret_hu_conv = Result_InvoiceSignOrCreationErrorZ.constr_from_ptr(ret);
574                 for (PhantomRouteHints phantom_route_hints_conv_19: phantom_route_hints) { if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(phantom_route_hints_conv_19); }; };
575                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(keys_manager); };
576                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
577                 return ret_hu_conv;
578         }
579
580         /**
581          * Utility to create an invoice that can be paid to one of multiple nodes, or a \"phantom invoice.\"
582          * See [`PhantomKeysManager`] for more information on phantom node payments.
583          * 
584          * `phantom_route_hints` parameter:
585          * Contains channel info for all nodes participating in the phantom invoice
586          * Entries are retrieved from a call to [`ChannelManager::get_phantom_route_hints`] on each
587          * participating node
588          * It is fine to cache `phantom_route_hints` and reuse it across invoices, as long as the data is
589          * updated when a channel becomes disabled or closes
590          * Note that if too many channels are included in [`PhantomRouteHints::channels`], the invoice
591          * may be too long for QR code scanning. To fix this, `PhantomRouteHints::channels` may be pared
592          * down
593          * 
594          * `description_hash` is a SHA-256 hash of the description text
595          * 
596          * `payment_hash` can be specified if you have a specific need for a custom payment hash (see the difference
597          * between [`ChannelManager::create_inbound_payment`] and [`ChannelManager::create_inbound_payment_for_hash`]).
598          * If `None` is provided for `payment_hash`, then one will be created.
599          * 
600          * `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
601          * in excess of the current time.
602          * 
603          * Note that the provided `keys_manager`'s `KeysInterface` implementation must support phantom
604          * invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this
605          * requirement).
606          * 
607          * [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager
608          * [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints
609          * [`ChannelManager::create_inbound_payment`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment
610          * [`ChannelManager::create_inbound_payment_for_hash`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
611          * [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels
612          * 
613          * Note that payment_hash (or a relevant inner pointer) may be NULL or all-0s to represent None
614          */
615         public static Result_InvoiceSignOrCreationErrorZ create_phantom_invoice_with_description_hash(Option_u64Z amt_msat, @Nullable byte[] payment_hash, int invoice_expiry_delta_secs, Sha256 description_hash, PhantomRouteHints[] phantom_route_hints, KeysInterface keys_manager, Logger logger, org.ldk.enums.Currency network) {
616                 long ret = bindings.create_phantom_invoice_with_description_hash(amt_msat.ptr, InternalUtils.check_arr_len(payment_hash, 32), invoice_expiry_delta_secs, description_hash == null ? 0 : description_hash.ptr, phantom_route_hints != null ? Arrays.stream(phantom_route_hints).mapToLong(phantom_route_hints_conv_19 -> phantom_route_hints_conv_19 == null ? 0 : phantom_route_hints_conv_19.ptr).toArray() : null, keys_manager == null ? 0 : keys_manager.ptr, logger == null ? 0 : logger.ptr, network);
617                 Reference.reachabilityFence(amt_msat);
618                 Reference.reachabilityFence(payment_hash);
619                 Reference.reachabilityFence(invoice_expiry_delta_secs);
620                 Reference.reachabilityFence(description_hash);
621                 Reference.reachabilityFence(phantom_route_hints);
622                 Reference.reachabilityFence(keys_manager);
623                 Reference.reachabilityFence(logger);
624                 Reference.reachabilityFence(network);
625                 if (ret >= 0 && ret <= 4096) { return null; }
626                 Result_InvoiceSignOrCreationErrorZ ret_hu_conv = Result_InvoiceSignOrCreationErrorZ.constr_from_ptr(ret);
627                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(description_hash); };
628                 for (PhantomRouteHints phantom_route_hints_conv_19: phantom_route_hints) { if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(phantom_route_hints_conv_19); }; };
629                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(keys_manager); };
630                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
631                 return ret_hu_conv;
632         }
633
634         /**
635          * Utility to construct an invoice. Generally, unless you want to do something like a custom
636          * cltv_expiry, this is what you should be using to create an invoice. The reason being, this
637          * method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user
638          * doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
639          * that the payment secret is valid when the invoice is paid.
640          * 
641          * `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
642          * in excess of the current time.
643          */
644         public static Result_InvoiceSignOrCreationErrorZ create_invoice_from_channelmanager(ChannelManager channelmanager, KeysInterface keys_manager, Logger logger, org.ldk.enums.Currency network, Option_u64Z amt_msat, java.lang.String description, int invoice_expiry_delta_secs) {
645                 long ret = bindings.create_invoice_from_channelmanager(channelmanager == null ? 0 : channelmanager.ptr, keys_manager == null ? 0 : keys_manager.ptr, logger == null ? 0 : logger.ptr, network, amt_msat.ptr, description, invoice_expiry_delta_secs);
646                 Reference.reachabilityFence(channelmanager);
647                 Reference.reachabilityFence(keys_manager);
648                 Reference.reachabilityFence(logger);
649                 Reference.reachabilityFence(network);
650                 Reference.reachabilityFence(amt_msat);
651                 Reference.reachabilityFence(description);
652                 Reference.reachabilityFence(invoice_expiry_delta_secs);
653                 if (ret >= 0 && ret <= 4096) { return null; }
654                 Result_InvoiceSignOrCreationErrorZ ret_hu_conv = Result_InvoiceSignOrCreationErrorZ.constr_from_ptr(ret);
655                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(channelmanager); };
656                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(keys_manager); };
657                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
658                 return ret_hu_conv;
659         }
660
661         /**
662          * Utility to construct an invoice. Generally, unless you want to do something like a custom
663          * cltv_expiry, this is what you should be using to create an invoice. The reason being, this
664          * method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user
665          * doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
666          * that the payment secret is valid when the invoice is paid.
667          * Use this variant if you want to pass the `description_hash` to the invoice.
668          * 
669          * `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
670          * in excess of the current time.
671          */
672         public static Result_InvoiceSignOrCreationErrorZ create_invoice_from_channelmanager_with_description_hash(ChannelManager channelmanager, KeysInterface keys_manager, Logger logger, org.ldk.enums.Currency network, Option_u64Z amt_msat, Sha256 description_hash, int invoice_expiry_delta_secs) {
673                 long ret = bindings.create_invoice_from_channelmanager_with_description_hash(channelmanager == null ? 0 : channelmanager.ptr, keys_manager == null ? 0 : keys_manager.ptr, logger == null ? 0 : logger.ptr, network, amt_msat.ptr, description_hash == null ? 0 : description_hash.ptr, invoice_expiry_delta_secs);
674                 Reference.reachabilityFence(channelmanager);
675                 Reference.reachabilityFence(keys_manager);
676                 Reference.reachabilityFence(logger);
677                 Reference.reachabilityFence(network);
678                 Reference.reachabilityFence(amt_msat);
679                 Reference.reachabilityFence(description_hash);
680                 Reference.reachabilityFence(invoice_expiry_delta_secs);
681                 if (ret >= 0 && ret <= 4096) { return null; }
682                 Result_InvoiceSignOrCreationErrorZ ret_hu_conv = Result_InvoiceSignOrCreationErrorZ.constr_from_ptr(ret);
683                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(channelmanager); };
684                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(keys_manager); };
685                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
686                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(description_hash); };
687                 return ret_hu_conv;
688         }
689
690         /**
691          * See [`create_invoice_from_channelmanager_with_description_hash`]
692          * This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not
693          * available and the current time is supplied by the caller.
694          */
695         public static Result_InvoiceSignOrCreationErrorZ create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch(ChannelManager channelmanager, KeysInterface keys_manager, Logger logger, org.ldk.enums.Currency network, Option_u64Z amt_msat, Sha256 description_hash, long duration_since_epoch, int invoice_expiry_delta_secs) {
696                 long ret = bindings.create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch(channelmanager == null ? 0 : channelmanager.ptr, keys_manager == null ? 0 : keys_manager.ptr, logger == null ? 0 : logger.ptr, network, amt_msat.ptr, description_hash == null ? 0 : description_hash.ptr, duration_since_epoch, invoice_expiry_delta_secs);
697                 Reference.reachabilityFence(channelmanager);
698                 Reference.reachabilityFence(keys_manager);
699                 Reference.reachabilityFence(logger);
700                 Reference.reachabilityFence(network);
701                 Reference.reachabilityFence(amt_msat);
702                 Reference.reachabilityFence(description_hash);
703                 Reference.reachabilityFence(duration_since_epoch);
704                 Reference.reachabilityFence(invoice_expiry_delta_secs);
705                 if (ret >= 0 && ret <= 4096) { return null; }
706                 Result_InvoiceSignOrCreationErrorZ ret_hu_conv = Result_InvoiceSignOrCreationErrorZ.constr_from_ptr(ret);
707                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(channelmanager); };
708                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(keys_manager); };
709                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
710                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(description_hash); };
711                 return ret_hu_conv;
712         }
713
714         /**
715          * See [`create_invoice_from_channelmanager`]
716          * This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not
717          * available and the current time is supplied by the caller.
718          */
719         public static Result_InvoiceSignOrCreationErrorZ create_invoice_from_channelmanager_and_duration_since_epoch(ChannelManager channelmanager, KeysInterface keys_manager, Logger logger, org.ldk.enums.Currency network, Option_u64Z amt_msat, java.lang.String description, long duration_since_epoch, int invoice_expiry_delta_secs) {
720                 long ret = bindings.create_invoice_from_channelmanager_and_duration_since_epoch(channelmanager == null ? 0 : channelmanager.ptr, keys_manager == null ? 0 : keys_manager.ptr, logger == null ? 0 : logger.ptr, network, amt_msat.ptr, description, duration_since_epoch, invoice_expiry_delta_secs);
721                 Reference.reachabilityFence(channelmanager);
722                 Reference.reachabilityFence(keys_manager);
723                 Reference.reachabilityFence(logger);
724                 Reference.reachabilityFence(network);
725                 Reference.reachabilityFence(amt_msat);
726                 Reference.reachabilityFence(description);
727                 Reference.reachabilityFence(duration_since_epoch);
728                 Reference.reachabilityFence(invoice_expiry_delta_secs);
729                 if (ret >= 0 && ret <= 4096) { return null; }
730                 Result_InvoiceSignOrCreationErrorZ ret_hu_conv = Result_InvoiceSignOrCreationErrorZ.constr_from_ptr(ret);
731                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(channelmanager); };
732                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(keys_manager); };
733                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
734                 return ret_hu_conv;
735         }
736
737 }