1 package org.ldk.structs;
2 import org.ldk.impl.bindings;
3 import org.ldk.enums.*;
5 import java.util.Arrays;
6 import java.lang.ref.Reference;
7 import javax.annotation.Nullable;
9 public class UtilMethods {
11 * Constructs a new COption_NoneZ containing a
13 public static COption_NoneZ COption_NoneZ_some() {
14 COption_NoneZ ret = bindings.COption_NoneZ_some();
19 * Constructs a new COption_NoneZ containing nothing
21 public static COption_NoneZ COption_NoneZ_none() {
22 COption_NoneZ ret = bindings.COption_NoneZ_none();
27 * Read a ClosureReason from a byte array, created by ClosureReason_write
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);
38 * Read a Event from a byte array, created by Event_write
40 public static Result_COption_EventZDecodeErrorZ Event_read(byte[] ser) {
41 long ret = bindings.Event_read(ser);
42 Reference.reachabilityFence(ser);
43 if (ret >= 0 && ret <= 4096) { return null; }
44 Result_COption_EventZDecodeErrorZ ret_hu_conv = Result_COption_EventZDecodeErrorZ.constr_from_ptr(ret);
49 * Creates a digital signature of a message given a SecretKey, like the node's secret.
50 * 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.
51 * Signatures are EC recoverable, meaning that given the message and the signature the PublicKey of the signer can be extracted.
53 public static Result_StringErrorZ sign(byte[] msg, byte[] sk) {
54 long ret = bindings.sign(msg, InternalUtils.check_arr_len(sk, 32));
55 Reference.reachabilityFence(msg);
56 Reference.reachabilityFence(sk);
57 if (ret >= 0 && ret <= 4096) { return null; }
58 Result_StringErrorZ ret_hu_conv = Result_StringErrorZ.constr_from_ptr(ret);
63 * Recovers the PublicKey of the signer of the message given the message and the signature.
65 public static Result_PublicKeyErrorZ recover_pk(byte[] msg, java.lang.String sig) {
66 long ret = bindings.recover_pk(msg, sig);
67 Reference.reachabilityFence(msg);
68 Reference.reachabilityFence(sig);
69 if (ret >= 0 && ret <= 4096) { return null; }
70 Result_PublicKeyErrorZ ret_hu_conv = Result_PublicKeyErrorZ.constr_from_ptr(ret);
75 * Verifies a message was signed by a PrivateKey that derives to a given PublicKey, given a message, a signature,
78 public static boolean verify(byte[] msg, java.lang.String sig, byte[] pk) {
79 boolean ret = bindings.verify(msg, sig, InternalUtils.check_arr_len(pk, 33));
80 Reference.reachabilityFence(msg);
81 Reference.reachabilityFence(sig);
82 Reference.reachabilityFence(pk);
87 * Read a MonitorEvent from a byte array, created by MonitorEvent_write
89 public static Result_COption_MonitorEventZDecodeErrorZ MonitorEvent_read(byte[] ser) {
90 long ret = bindings.MonitorEvent_read(ser);
91 Reference.reachabilityFence(ser);
92 if (ret >= 0 && ret <= 4096) { return null; }
93 Result_COption_MonitorEventZDecodeErrorZ ret_hu_conv = Result_COption_MonitorEventZDecodeErrorZ.constr_from_ptr(ret);
98 * Read a C2Tuple_BlockHashChannelMonitorZ from a byte array, created by C2Tuple_BlockHashChannelMonitorZ_write
100 public static Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ C2Tuple_BlockHashChannelMonitorZ_read(byte[] ser, KeysInterface arg) {
101 long ret = bindings.C2Tuple_BlockHashChannelMonitorZ_read(ser, arg == null ? 0 : arg.ptr);
102 Reference.reachabilityFence(ser);
103 Reference.reachabilityFence(arg);
104 if (ret >= 0 && ret <= 4096) { return null; }
105 Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ ret_hu_conv = Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.constr_from_ptr(ret);
106 ret_hu_conv.ptrs_to.add(arg);
111 * Read a C2Tuple_BlockHashChannelManagerZ from a byte array, created by C2Tuple_BlockHashChannelManagerZ_write
113 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) {
114 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));
115 Reference.reachabilityFence(ser);
116 Reference.reachabilityFence(arg_keys_manager);
117 Reference.reachabilityFence(arg_fee_estimator);
118 Reference.reachabilityFence(arg_chain_monitor);
119 Reference.reachabilityFence(arg_tx_broadcaster);
120 Reference.reachabilityFence(arg_logger);
121 Reference.reachabilityFence(arg_default_config);
122 Reference.reachabilityFence(arg_channel_monitors);
123 if (ret >= 0 && ret <= 4096) { return null; }
124 Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ ret_hu_conv = Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.constr_from_ptr(ret);
125 ret_hu_conv.ptrs_to.add(arg_keys_manager);
126 ret_hu_conv.ptrs_to.add(arg_fee_estimator);
127 ret_hu_conv.ptrs_to.add(arg_chain_monitor);
128 ret_hu_conv.ptrs_to.add(arg_tx_broadcaster);
129 ret_hu_conv.ptrs_to.add(arg_logger);
131 for (ChannelMonitor arg_channel_monitors_conv_16: arg_channel_monitors) { ret_hu_conv.ptrs_to.add(arg_channel_monitors_conv_16); };
136 * Gets the weight for an HTLC-Success transaction.
138 public static long htlc_success_tx_weight(boolean opt_anchors) {
139 long ret = bindings.htlc_success_tx_weight(opt_anchors);
140 Reference.reachabilityFence(opt_anchors);
145 * Gets the weight for an HTLC-Timeout transaction.
147 public static long htlc_timeout_tx_weight(boolean opt_anchors) {
148 long ret = bindings.htlc_timeout_tx_weight(opt_anchors);
149 Reference.reachabilityFence(opt_anchors);
154 * Build the commitment secret from the seed and the commitment number
156 public static byte[] build_commitment_secret(byte[] commitment_seed, long idx) {
157 byte[] ret = bindings.build_commitment_secret(InternalUtils.check_arr_len(commitment_seed, 32), idx);
158 Reference.reachabilityFence(commitment_seed);
159 Reference.reachabilityFence(idx);
164 * Build a closing transaction
166 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) {
167 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);
168 Reference.reachabilityFence(to_holder_value_sat);
169 Reference.reachabilityFence(to_counterparty_value_sat);
170 Reference.reachabilityFence(to_holder_script);
171 Reference.reachabilityFence(to_counterparty_script);
172 Reference.reachabilityFence(funding_outpoint);
177 * Derives a per-commitment-transaction private key (eg an htlc key or delayed_payment key)
178 * from the base secret and the per_commitment_point.
180 * Note that this is infallible iff we trust that at least one of the two input keys are randomly
181 * generated (ie our own).
183 public static Result_SecretKeyErrorZ derive_private_key(byte[] per_commitment_point, byte[] base_secret) {
184 long ret = bindings.derive_private_key(InternalUtils.check_arr_len(per_commitment_point, 33), InternalUtils.check_arr_len(base_secret, 32));
185 Reference.reachabilityFence(per_commitment_point);
186 Reference.reachabilityFence(base_secret);
187 if (ret >= 0 && ret <= 4096) { return null; }
188 Result_SecretKeyErrorZ ret_hu_conv = Result_SecretKeyErrorZ.constr_from_ptr(ret);
193 * Derives a per-commitment-transaction public key (eg an htlc key or a delayed_payment key)
194 * from the base point and the per_commitment_key. This is the public equivalent of
195 * derive_private_key - using only public keys to derive a public key instead of private keys.
197 * Note that this is infallible iff we trust that at least one of the two input keys are randomly
198 * generated (ie our own).
200 public static Result_PublicKeyErrorZ derive_public_key(byte[] per_commitment_point, byte[] base_point) {
201 long ret = bindings.derive_public_key(InternalUtils.check_arr_len(per_commitment_point, 33), InternalUtils.check_arr_len(base_point, 33));
202 Reference.reachabilityFence(per_commitment_point);
203 Reference.reachabilityFence(base_point);
204 if (ret >= 0 && ret <= 4096) { return null; }
205 Result_PublicKeyErrorZ ret_hu_conv = Result_PublicKeyErrorZ.constr_from_ptr(ret);
210 * Derives a per-commitment-transaction revocation key from its constituent parts.
212 * Only the cheating participant owns a valid witness to propagate a revoked
213 * commitment transaction, thus per_commitment_secret always come from cheater
214 * and revocation_base_secret always come from punisher, which is the broadcaster
215 * of the transaction spending with this key knowledge.
217 * Note that this is infallible iff we trust that at least one of the two input keys are randomly
218 * generated (ie our own).
220 public static Result_SecretKeyErrorZ derive_private_revocation_key(byte[] per_commitment_secret, byte[] countersignatory_revocation_base_secret) {
221 long ret = bindings.derive_private_revocation_key(InternalUtils.check_arr_len(per_commitment_secret, 32), InternalUtils.check_arr_len(countersignatory_revocation_base_secret, 32));
222 Reference.reachabilityFence(per_commitment_secret);
223 Reference.reachabilityFence(countersignatory_revocation_base_secret);
224 if (ret >= 0 && ret <= 4096) { return null; }
225 Result_SecretKeyErrorZ ret_hu_conv = Result_SecretKeyErrorZ.constr_from_ptr(ret);
230 * Derives a per-commitment-transaction revocation public key from its constituent parts. This is
231 * the public equivalend of derive_private_revocation_key - using only public keys to derive a
232 * public key instead of private keys.
234 * Only the cheating participant owns a valid witness to propagate a revoked
235 * commitment transaction, thus per_commitment_point always come from cheater
236 * and revocation_base_point always come from punisher, which is the broadcaster
237 * of the transaction spending with this key knowledge.
239 * Note that this is infallible iff we trust that at least one of the two input keys are randomly
240 * generated (ie our own).
242 public static Result_PublicKeyErrorZ derive_public_revocation_key(byte[] per_commitment_point, byte[] countersignatory_revocation_base_point) {
243 long ret = bindings.derive_public_revocation_key(InternalUtils.check_arr_len(per_commitment_point, 33), InternalUtils.check_arr_len(countersignatory_revocation_base_point, 33));
244 Reference.reachabilityFence(per_commitment_point);
245 Reference.reachabilityFence(countersignatory_revocation_base_point);
246 if (ret >= 0 && ret <= 4096) { return null; }
247 Result_PublicKeyErrorZ ret_hu_conv = Result_PublicKeyErrorZ.constr_from_ptr(ret);
252 * A script either spendable by the revocation
253 * key or the broadcaster_delayed_payment_key and satisfying the relative-locktime OP_CSV constrain.
254 * Encumbering a `to_holder` output on a commitment transaction or 2nd-stage HTLC transactions.
256 public static byte[] get_revokeable_redeemscript(byte[] revocation_key, short contest_delay, byte[] broadcaster_delayed_payment_key) {
257 byte[] ret = bindings.get_revokeable_redeemscript(InternalUtils.check_arr_len(revocation_key, 33), contest_delay, InternalUtils.check_arr_len(broadcaster_delayed_payment_key, 33));
258 Reference.reachabilityFence(revocation_key);
259 Reference.reachabilityFence(contest_delay);
260 Reference.reachabilityFence(broadcaster_delayed_payment_key);
265 * Gets the witness redeemscript for an HTLC output in a commitment transaction. Note that htlc
266 * does not need to have its previous_output_index filled.
268 public static byte[] get_htlc_redeemscript(HTLCOutputInCommitment htlc, boolean opt_anchors, TxCreationKeys keys) {
269 byte[] ret = bindings.get_htlc_redeemscript(htlc == null ? 0 : htlc.ptr & ~1, opt_anchors, keys == null ? 0 : keys.ptr & ~1);
270 Reference.reachabilityFence(htlc);
271 Reference.reachabilityFence(opt_anchors);
272 Reference.reachabilityFence(keys);
273 // this.ptrs_to.add(htlc);
274 // this.ptrs_to.add(keys);
279 * Gets the redeemscript for a funding output from the two funding public keys.
280 * Note that the order of funding public keys does not matter.
282 public static byte[] make_funding_redeemscript(byte[] broadcaster, byte[] countersignatory) {
283 byte[] ret = bindings.make_funding_redeemscript(InternalUtils.check_arr_len(broadcaster, 33), InternalUtils.check_arr_len(countersignatory, 33));
284 Reference.reachabilityFence(broadcaster);
285 Reference.reachabilityFence(countersignatory);
290 * Builds an unsigned HTLC-Success or HTLC-Timeout transaction from the given channel and HTLC
291 * parameters. This is used by [`TrustedCommitmentTransaction::get_htlc_sigs`] to fetch the
292 * transaction which needs signing, and can be used to construct an HTLC transaction which is
293 * broadcastable given a counterparty HTLC signature.
295 * Panics if htlc.transaction_output_index.is_none() (as such HTLCs do not appear in the
296 * commitment transaction).
298 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) {
299 byte[] ret = bindings.build_htlc_transaction(InternalUtils.check_arr_len(commitment_txid, 32), feerate_per_kw, contest_delay, htlc == null ? 0 : htlc.ptr & ~1, opt_anchors, InternalUtils.check_arr_len(broadcaster_delayed_payment_key, 33), InternalUtils.check_arr_len(revocation_key, 33));
300 Reference.reachabilityFence(commitment_txid);
301 Reference.reachabilityFence(feerate_per_kw);
302 Reference.reachabilityFence(contest_delay);
303 Reference.reachabilityFence(htlc);
304 Reference.reachabilityFence(opt_anchors);
305 Reference.reachabilityFence(broadcaster_delayed_payment_key);
306 Reference.reachabilityFence(revocation_key);
307 // this.ptrs_to.add(htlc);
312 * Gets the witnessScript for an anchor output from the funding public key.
313 * The witness in the spending input must be:
314 * <BIP 143 funding_signature>
315 * After 16 blocks of confirmation, an alternative satisfying witness could be:
317 * (empty vector required to satisfy compliance with MINIMALIF-standard rule)
319 public static byte[] get_anchor_redeemscript(byte[] funding_pubkey) {
320 byte[] ret = bindings.get_anchor_redeemscript(InternalUtils.check_arr_len(funding_pubkey, 33));
321 Reference.reachabilityFence(funding_pubkey);
326 * Commitment transaction numbers which appear in the transactions themselves are XOR'd with a
327 * shared secret first. This prevents on-chain observers from discovering how many commitment
328 * transactions occurred in a channel before it was closed.
330 * This function gets the shared secret from relevant channel public keys and can be used to
331 * \"decrypt\" the commitment transaction number given a commitment transaction on-chain.
333 public static long get_commitment_transaction_number_obscure_factor(byte[] broadcaster_payment_basepoint, byte[] countersignatory_payment_basepoint, boolean outbound_from_broadcaster) {
334 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);
335 Reference.reachabilityFence(broadcaster_payment_basepoint);
336 Reference.reachabilityFence(countersignatory_payment_basepoint);
337 Reference.reachabilityFence(outbound_from_broadcaster);
342 * Read a NetworkUpdate from a byte array, created by NetworkUpdate_write
344 public static Result_COption_NetworkUpdateZDecodeErrorZ NetworkUpdate_read(byte[] ser) {
345 long ret = bindings.NetworkUpdate_read(ser);
346 Reference.reachabilityFence(ser);
347 if (ret >= 0 && ret <= 4096) { return null; }
348 Result_COption_NetworkUpdateZDecodeErrorZ ret_hu_conv = Result_COption_NetworkUpdateZDecodeErrorZ.constr_from_ptr(ret);
353 * Finds a route from us (payer) to the given target node (payee).
355 * If the payee provided features in their invoice, they should be provided via `params.payee`.
356 * Without this, MPP will only be used if the payee's features are available in the network graph.
358 * Private routing paths between a public node and the target may be included in `params.payee`.
360 * If some channels aren't announced, it may be useful to fill in `first_hops` with the results
361 * from [`ChannelManager::list_usable_channels`]. If it is filled in, the view of our local
362 * channels from [`NetworkGraph`] will be ignored, and only those in `first_hops` will be used.
364 * The fees on channels from us to the next hop are ignored as they are assumed to all be equal.
365 * However, the enabled/disabled bit on such channels as well as the `htlc_minimum_msat` /
366 * `htlc_maximum_msat` *are* checked as they may change based on the receiving node.
370 * May be used to re-compute a [`Route`] when handling a [`Event::PaymentPathFailed`]. Any
371 * adjustments to the [`NetworkGraph`] and channel scores should be made prior to calling this
376 * Panics if first_hops contains channels without short_channel_ids;
377 * [`ChannelManager::list_usable_channels`] will never include such channels.
379 * [`ChannelManager::list_usable_channels`]: crate::ln::channelmanager::ChannelManager::list_usable_channels
380 * [`Event::PaymentPathFailed`]: crate::util::events::Event::PaymentPathFailed
382 * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
384 public static Result_RouteLightningErrorZ find_route(byte[] our_node_pubkey, RouteParameters params, NetworkGraph network, @Nullable ChannelDetails[] first_hops, Logger logger, Score scorer) {
385 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);
386 Reference.reachabilityFence(our_node_pubkey);
387 Reference.reachabilityFence(params);
388 Reference.reachabilityFence(network);
389 Reference.reachabilityFence(first_hops);
390 Reference.reachabilityFence(logger);
391 Reference.reachabilityFence(scorer);
392 if (ret >= 0 && ret <= 4096) { return null; }
393 Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
394 ret_hu_conv.ptrs_to.add(params);
395 ret_hu_conv.ptrs_to.add(network);
396 for (ChannelDetails first_hops_conv_16: first_hops) { ret_hu_conv.ptrs_to.add(first_hops_conv_16); };
397 ret_hu_conv.ptrs_to.add(logger);
398 ret_hu_conv.ptrs_to.add(scorer);
403 * Writes the provided `ChannelManager` to the path provided at `FilesystemPersister`
404 * initialization, within a file called \"manager\".
406 public static Result_NoneErrorZ FilesystemPersister_persist_manager(java.lang.String data_dir, ChannelManager manager) {
407 long ret = bindings.FilesystemPersister_persist_manager(data_dir, manager == null ? 0 : manager.ptr & ~1);
408 Reference.reachabilityFence(data_dir);
409 Reference.reachabilityFence(manager);
410 if (ret >= 0 && ret <= 4096) { return null; }
411 Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
412 ret_hu_conv.ptrs_to.add(manager);
417 * Utility to construct an invoice. Generally, unless you want to do something like a custom
418 * cltv_expiry, this is what you should be using to create an invoice. The reason being, this
419 * method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user
420 * doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
421 * that the payment secret is valid when the invoice is paid.
423 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) {
424 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);
425 Reference.reachabilityFence(channelmanager);
426 Reference.reachabilityFence(keys_manager);
427 Reference.reachabilityFence(network);
428 Reference.reachabilityFence(amt_msat);
429 Reference.reachabilityFence(description);
430 if (ret >= 0 && ret <= 4096) { return null; }
431 Result_InvoiceSignOrCreationErrorZ ret_hu_conv = Result_InvoiceSignOrCreationErrorZ.constr_from_ptr(ret);
432 ret_hu_conv.ptrs_to.add(channelmanager);
433 ret_hu_conv.ptrs_to.add(keys_manager);