1 package org.ldk.structs;
2 import org.ldk.impl.bindings;
3 import java.util.Arrays;
4 import javax.annotation.Nullable;
5 import org.ldk.enums.*;
7 public class UtilMethods {
9 * Creates a digital signature of a message given a SecretKey, like the node's secret.
10 * A receiver knowing the PublicKey (e.g. the node's id) and the message can be sure that the signature was generated by the caller.
11 * Signatures are EC recoverable, meaning that given the message and the signature the PublicKey of the signer can be extracted.
13 public static Result_StringErrorZ sign(byte[] msg, byte[] sk) {
14 long ret = bindings.sign(msg, sk);
15 if (ret >= 0 && ret <= 4096) { return null; }
16 Result_StringErrorZ ret_hu_conv = Result_StringErrorZ.constr_from_ptr(ret);
21 * Recovers the PublicKey of the signer of the message given the message and the signature.
23 public static Result_PublicKeyErrorZ recover_pk(byte[] msg, java.lang.String sig) {
24 long ret = bindings.recover_pk(msg, sig);
25 if (ret >= 0 && ret <= 4096) { return null; }
26 Result_PublicKeyErrorZ ret_hu_conv = Result_PublicKeyErrorZ.constr_from_ptr(ret);
31 * Verifies a message was signed by a PrivateKey that derives to a given PublicKey, given a message, a signature,
34 public static boolean verify(byte[] msg, java.lang.String sig, byte[] pk) {
35 boolean ret = bindings.verify(msg, sig, pk);
40 * Read a C2Tuple_BlockHashChannelMonitorZ from a byte array, created by C2Tuple_BlockHashChannelMonitorZ_write
42 public static Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ C2Tuple_BlockHashChannelMonitorZ_read(byte[] ser, KeysInterface arg) {
43 long ret = bindings.C2Tuple_BlockHashChannelMonitorZ_read(ser, arg == null ? 0 : arg.ptr);
44 if (ret >= 0 && ret <= 4096) { return null; }
45 Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ ret_hu_conv = Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.constr_from_ptr(ret);
46 ret_hu_conv.ptrs_to.add(arg);
51 * Read a C2Tuple_BlockHashChannelManagerZ from a byte array, created by C2Tuple_BlockHashChannelManagerZ_write
53 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) {
54 long ret = bindings.C2Tuple_BlockHashChannelManagerZ_read(ser, bindings.ChannelManagerReadArgs_new(arg_keys_manager == null ? 0 : arg_keys_manager.ptr, arg_fee_estimator == null ? 0 : arg_fee_estimator.ptr, arg_chain_monitor == null ? 0 : arg_chain_monitor.ptr, arg_tx_broadcaster == null ? 0 : arg_tx_broadcaster.ptr, arg_logger == null ? 0 : arg_logger.ptr, arg_default_config == null ? 0 : arg_default_config.ptr & ~1, arg_channel_monitors != null ? Arrays.stream(arg_channel_monitors).mapToLong(arg_channel_monitors_conv_16 -> arg_channel_monitors_conv_16 == null ? 0 : arg_channel_monitors_conv_16.ptr & ~1).toArray() : null));
55 if (ret >= 0 && ret <= 4096) { return null; }
56 Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ ret_hu_conv = Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.constr_from_ptr(ret);
57 ret_hu_conv.ptrs_to.add(arg_keys_manager);
58 ret_hu_conv.ptrs_to.add(arg_fee_estimator);
59 ret_hu_conv.ptrs_to.add(arg_chain_monitor);
60 ret_hu_conv.ptrs_to.add(arg_tx_broadcaster);
61 ret_hu_conv.ptrs_to.add(arg_logger);
63 for (ChannelMonitor arg_channel_monitors_conv_16: arg_channel_monitors) { ret_hu_conv.ptrs_to.add(arg_channel_monitors_conv_16); };
68 * Build the commitment secret from the seed and the commitment number
70 public static byte[] build_commitment_secret(byte[] commitment_seed, long idx) {
71 byte[] ret = bindings.build_commitment_secret(commitment_seed, idx);
76 * Build a closing transaction
78 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) {
79 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);
84 * Derives a per-commitment-transaction private key (eg an htlc key or delayed_payment key)
85 * from the base secret and the per_commitment_point.
87 * Note that this is infallible iff we trust that at least one of the two input keys are randomly
88 * generated (ie our own).
90 public static Result_SecretKeyErrorZ derive_private_key(byte[] per_commitment_point, byte[] base_secret) {
91 long ret = bindings.derive_private_key(per_commitment_point, base_secret);
92 if (ret >= 0 && ret <= 4096) { return null; }
93 Result_SecretKeyErrorZ ret_hu_conv = Result_SecretKeyErrorZ.constr_from_ptr(ret);
98 * Derives a per-commitment-transaction public key (eg an htlc key or a delayed_payment key)
99 * from the base point and the per_commitment_key. This is the public equivalent of
100 * derive_private_key - using only public keys to derive a public key instead of private keys.
102 * Note that this is infallible iff we trust that at least one of the two input keys are randomly
103 * generated (ie our own).
105 public static Result_PublicKeyErrorZ derive_public_key(byte[] per_commitment_point, byte[] base_point) {
106 long ret = bindings.derive_public_key(per_commitment_point, base_point);
107 if (ret >= 0 && ret <= 4096) { return null; }
108 Result_PublicKeyErrorZ ret_hu_conv = Result_PublicKeyErrorZ.constr_from_ptr(ret);
113 * Derives a per-commitment-transaction revocation key from its constituent parts.
115 * Only the cheating participant owns a valid witness to propagate a revoked
116 * commitment transaction, thus per_commitment_secret always come from cheater
117 * and revocation_base_secret always come from punisher, which is the broadcaster
118 * of the transaction spending with this key knowledge.
120 * Note that this is infallible iff we trust that at least one of the two input keys are randomly
121 * generated (ie our own).
123 public static Result_SecretKeyErrorZ derive_private_revocation_key(byte[] per_commitment_secret, byte[] countersignatory_revocation_base_secret) {
124 long ret = bindings.derive_private_revocation_key(per_commitment_secret, countersignatory_revocation_base_secret);
125 if (ret >= 0 && ret <= 4096) { return null; }
126 Result_SecretKeyErrorZ ret_hu_conv = Result_SecretKeyErrorZ.constr_from_ptr(ret);
131 * Derives a per-commitment-transaction revocation public key from its constituent parts. This is
132 * the public equivalend of derive_private_revocation_key - using only public keys to derive a
133 * public key instead of private keys.
135 * Only the cheating participant owns a valid witness to propagate a revoked
136 * commitment transaction, thus per_commitment_point always come from cheater
137 * and revocation_base_point always come from punisher, which is the broadcaster
138 * of the transaction spending with this key knowledge.
140 * Note that this is infallible iff we trust that at least one of the two input keys are randomly
141 * generated (ie our own).
143 public static Result_PublicKeyErrorZ derive_public_revocation_key(byte[] per_commitment_point, byte[] countersignatory_revocation_base_point) {
144 long ret = bindings.derive_public_revocation_key(per_commitment_point, countersignatory_revocation_base_point);
145 if (ret >= 0 && ret <= 4096) { return null; }
146 Result_PublicKeyErrorZ ret_hu_conv = Result_PublicKeyErrorZ.constr_from_ptr(ret);
151 * A script either spendable by the revocation
152 * key or the broadcaster_delayed_payment_key and satisfying the relative-locktime OP_CSV constrain.
153 * Encumbering a `to_holder` output on a commitment transaction or 2nd-stage HTLC transactions.
155 public static byte[] get_revokeable_redeemscript(byte[] revocation_key, short contest_delay, byte[] broadcaster_delayed_payment_key) {
156 byte[] ret = bindings.get_revokeable_redeemscript(revocation_key, contest_delay, broadcaster_delayed_payment_key);
161 * Gets the witness redeemscript for an HTLC output in a commitment transaction. Note that htlc
162 * does not need to have its previous_output_index filled.
164 public static byte[] get_htlc_redeemscript(HTLCOutputInCommitment htlc, TxCreationKeys keys) {
165 byte[] ret = bindings.get_htlc_redeemscript(htlc == null ? 0 : htlc.ptr & ~1, keys == null ? 0 : keys.ptr & ~1);
166 // this.ptrs_to.add(htlc);
167 // this.ptrs_to.add(keys);
172 * Gets the redeemscript for a funding output from the two funding public keys.
173 * Note that the order of funding public keys does not matter.
175 public static byte[] make_funding_redeemscript(byte[] broadcaster, byte[] countersignatory) {
176 byte[] ret = bindings.make_funding_redeemscript(broadcaster, countersignatory);
181 * Builds an unsigned HTLC-Success or HTLC-Timeout transaction from the given channel and HTLC
182 * parameters. This is used by [`TrustedCommitmentTransaction::get_htlc_sigs`] to fetch the
183 * transaction which needs signing, and can be used to construct an HTLC transaction which is
184 * broadcastable given a counterparty HTLC signature.
186 * Panics if htlc.transaction_output_index.is_none() (as such HTLCs do not appear in the
187 * commitment transaction).
189 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) {
190 byte[] ret = bindings.build_htlc_transaction(commitment_txid, feerate_per_kw, contest_delay, htlc == null ? 0 : htlc.ptr & ~1, broadcaster_delayed_payment_key, revocation_key);
191 // this.ptrs_to.add(htlc);
196 * Commitment transaction numbers which appear in the transactions themselves are XOR'd with a
197 * shared secret first. This prevents on-chain observers from discovering how many commitment
198 * transactions occurred in a channel before it was closed.
200 * This function gets the shared secret from relevant channel public keys and can be used to
201 * \"decrypt\" the commitment transaction number given a commitment transaction on-chain.
203 public static long get_commitment_transaction_number_obscure_factor(byte[] broadcaster_payment_basepoint, byte[] countersignatory_payment_basepoint, boolean outbound_from_broadcaster) {
204 long ret = bindings.get_commitment_transaction_number_obscure_factor(broadcaster_payment_basepoint, countersignatory_payment_basepoint, outbound_from_broadcaster);
209 * Finds a route from us (payer) to the given target node (payee).
211 * If the payee provided features in their invoice, they should be provided via `params.payee`.
212 * Without this, MPP will only be used if the payee's features are available in the network graph.
214 * Private routing paths between a public node and the target may be included in `params.payee`.
216 * If some channels aren't announced, it may be useful to fill in `first_hops` with the results
217 * from [`ChannelManager::list_usable_channels`]. If it is filled in, the view of our local
218 * channels from [`NetworkGraph`] will be ignored, and only those in `first_hops` will be used.
220 * The fees on channels from us to the next hop are ignored as they are assumed to all be equal.
221 * However, the enabled/disabled bit on such channels as well as the `htlc_minimum_msat` /
222 * `htlc_maximum_msat` *are* checked as they may change based on the receiving node.
226 * May be used to re-compute a [`Route`] when handling a [`Event::PaymentPathFailed`]. Any
227 * adjustments to the [`NetworkGraph`] and channel scores should be made prior to calling this
232 * Panics if first_hops contains channels without short_channel_ids;
233 * [`ChannelManager::list_usable_channels`] will never include such channels.
235 * [`ChannelManager::list_usable_channels`]: crate::ln::channelmanager::ChannelManager::list_usable_channels
236 * [`Event::PaymentPathFailed`]: crate::util::events::Event::PaymentPathFailed
238 * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
240 public static Result_RouteLightningErrorZ find_route(byte[] our_node_pubkey, RouteParameters params, NetworkGraph network, @Nullable ChannelDetails[] first_hops, Logger logger, Score scorer) {
241 long ret = bindings.find_route(our_node_pubkey, 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);
242 if (ret >= 0 && ret <= 4096) { return null; }
243 Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
244 ret_hu_conv.ptrs_to.add(params);
245 ret_hu_conv.ptrs_to.add(network);
246 for (ChannelDetails first_hops_conv_16: first_hops) { ret_hu_conv.ptrs_to.add(first_hops_conv_16); };
247 ret_hu_conv.ptrs_to.add(logger);
248 ret_hu_conv.ptrs_to.add(scorer);
253 * Writes the provided `ChannelManager` to the path provided at `FilesystemPersister`
254 * initialization, within a file called \"manager\".
256 public static Result_NoneErrorZ FilesystemPersister_persist_manager(java.lang.String data_dir, ChannelManager manager) {
257 long ret = bindings.FilesystemPersister_persist_manager(data_dir, manager == null ? 0 : manager.ptr & ~1);
258 if (ret >= 0 && ret <= 4096) { return null; }
259 Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
260 ret_hu_conv.ptrs_to.add(manager);
265 * Utility to construct an invoice. Generally, unless you want to do something like a custom
266 * cltv_expiry, this is what you should be using to create an invoice. The reason being, this
267 * method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user
268 * doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
269 * that the payment secret is valid when the invoice is paid.
271 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) {
272 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);
273 if (ret >= 0 && ret <= 4096) { return null; }
274 Result_InvoiceSignOrCreationErrorZ ret_hu_conv = Result_InvoiceSignOrCreationErrorZ.constr_from_ptr(ret);
275 ret_hu_conv.ptrs_to.add(channelmanager);
276 ret_hu_conv.ptrs_to.add(keys_manager);