Merge pull request #39 from TheBlueMatt/main
[ldk-java] / src / main / java / org / ldk / structs / UtilMethods.java
index f2a7c07b8961eced7e6da82d3fa0bb7b682d6567..e7ea1861b68bc9923a92da15ea758402799de734 100644 (file)
@@ -1,6 +1,7 @@
 package org.ldk.structs;
 import org.ldk.impl.bindings;
 import java.util.Arrays;
+import javax.annotation.Nullable;
 import org.ldk.enums.*;
 
 public class UtilMethods {
@@ -11,6 +12,7 @@ public class UtilMethods {
         */
        public static Result_StringErrorZ sign(byte[] msg, byte[] sk) {
                long ret = bindings.sign(msg, sk);
+               if (ret < 1024) { return null; }
                Result_StringErrorZ ret_hu_conv = Result_StringErrorZ.constr_from_ptr(ret);
                return ret_hu_conv;
        }
@@ -18,8 +20,9 @@ public class UtilMethods {
        /**
         * Recovers the PublicKey of the signer of the message given the message and the signature.
         */
-       public static Result_PublicKeyErrorZ recover_pk(byte[] msg, String sig) {
+       public static Result_PublicKeyErrorZ recover_pk(byte[] msg, java.lang.String sig) {
                long ret = bindings.recover_pk(msg, sig);
+               if (ret < 1024) { return null; }
                Result_PublicKeyErrorZ ret_hu_conv = Result_PublicKeyErrorZ.constr_from_ptr(ret);
                return ret_hu_conv;
        }
@@ -28,7 +31,7 @@ public class UtilMethods {
         * Verifies a message was signed by a PrivateKey that derives to a given PublicKey, given a message, a signature,
         * and the PublicKey.
         */
-       public static boolean verify(byte[] msg, String sig, byte[] pk) {
+       public static boolean verify(byte[] msg, java.lang.String sig, byte[] pk) {
                boolean ret = bindings.verify(msg, sig, pk);
                return ret;
        }
@@ -38,6 +41,7 @@ public class UtilMethods {
         */
        public static Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ BlockHashChannelMonitorZ_read(byte[] ser, KeysInterface arg) {
                long ret = bindings.C2Tuple_BlockHashChannelMonitorZ_read(ser, arg == null ? 0 : arg.ptr);
+               if (ret < 1024) { return null; }
                Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ ret_hu_conv = Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.constr_from_ptr(ret);
                ret_hu_conv.ptrs_to.add(arg);
                return ret_hu_conv;
@@ -47,7 +51,8 @@ public class UtilMethods {
         * Read a C2Tuple_BlockHashChannelManagerZ from a byte array, created by C2Tuple_BlockHashChannelManagerZ_write
         */
        public static Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ BlockHashChannelManagerZ_read(byte[] ser, KeysInterface arg_keys_manager, FeeEstimator arg_fee_estimator, Watch arg_chain_monitor, BroadcasterInterface arg_tx_broadcaster, Logger arg_logger, UserConfig arg_default_config, ChannelMonitor[] arg_channel_monitors) {
-               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, 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()));
+               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));
+               if (ret < 1024) { return null; }
                Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ ret_hu_conv = Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.constr_from_ptr(ret);
                ret_hu_conv.ptrs_to.add(arg_keys_manager);
                ret_hu_conv.ptrs_to.add(arg_fee_estimator);
@@ -76,6 +81,7 @@ public class UtilMethods {
         */
        public static Result_SecretKeyErrorZ derive_private_key(byte[] per_commitment_point, byte[] base_secret) {
                long ret = bindings.derive_private_key(per_commitment_point, base_secret);
+               if (ret < 1024) { return null; }
                Result_SecretKeyErrorZ ret_hu_conv = Result_SecretKeyErrorZ.constr_from_ptr(ret);
                return ret_hu_conv;
        }
@@ -90,6 +96,7 @@ public class UtilMethods {
         */
        public static Result_PublicKeyErrorZ derive_public_key(byte[] per_commitment_point, byte[] base_point) {
                long ret = bindings.derive_public_key(per_commitment_point, base_point);
+               if (ret < 1024) { return null; }
                Result_PublicKeyErrorZ ret_hu_conv = Result_PublicKeyErrorZ.constr_from_ptr(ret);
                return ret_hu_conv;
        }
@@ -107,6 +114,7 @@ public class UtilMethods {
         */
        public static Result_SecretKeyErrorZ derive_private_revocation_key(byte[] per_commitment_secret, byte[] countersignatory_revocation_base_secret) {
                long ret = bindings.derive_private_revocation_key(per_commitment_secret, countersignatory_revocation_base_secret);
+               if (ret < 1024) { return null; }
                Result_SecretKeyErrorZ ret_hu_conv = Result_SecretKeyErrorZ.constr_from_ptr(ret);
                return ret_hu_conv;
        }
@@ -126,6 +134,7 @@ public class UtilMethods {
         */
        public static Result_PublicKeyErrorZ derive_public_revocation_key(byte[] per_commitment_point, byte[] countersignatory_revocation_base_point) {
                long ret = bindings.derive_public_revocation_key(per_commitment_point, countersignatory_revocation_base_point);
+               if (ret < 1024) { return null; }
                Result_PublicKeyErrorZ ret_hu_conv = Result_PublicKeyErrorZ.constr_from_ptr(ret);
                return ret_hu_conv;
        }
@@ -188,6 +197,24 @@ public class UtilMethods {
                return ret;
        }
 
+       /**
+        * Gets a keysend route from us (payer) to the given target node (payee). This is needed because
+        * keysend payments do not have an invoice from which to pull the payee's supported features, which
+        * makes it tricky to otherwise supply the `payee_features` parameter of `get_route`.
+        * 
+        * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
+        */
+       public static Result_RouteLightningErrorZ get_keysend_route(byte[] our_node_id, NetworkGraph network, byte[] payee, @Nullable ChannelDetails[] first_hops, RouteHint[] last_hops, long final_value_msat, int final_cltv, Logger logger) {
+               long ret = bindings.get_keysend_route(our_node_id, network == null ? 0 : network.ptr & ~1, payee, first_hops != null ? Arrays.stream(first_hops).mapToLong(first_hops_conv_16 -> first_hops_conv_16 == null ? 0 : first_hops_conv_16.ptr & ~1).toArray() : null, last_hops != null ? Arrays.stream(last_hops).mapToLong(last_hops_conv_11 -> last_hops_conv_11 == null ? 0 : last_hops_conv_11.ptr & ~1).toArray() : null, final_value_msat, final_cltv, logger == null ? 0 : logger.ptr);
+               if (ret < 1024) { return null; }
+               Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
+               ret_hu_conv.ptrs_to.add(network);
+               /* TODO 2 ChannelDetails  */;
+               /* TODO 2 RouteHint  */;
+               ret_hu_conv.ptrs_to.add(logger);
+               return ret_hu_conv;
+       }
+
        /**
         * Gets a route from us (payer) to the given target node (payee).
         * 
@@ -208,9 +235,13 @@ public class UtilMethods {
         * The fees on channels from us to next-hops are ignored (as they are assumed to all be
         * equal), however the enabled/disabled bit on such channels as well as the
         * htlc_minimum_msat/htlc_maximum_msat *are* checked as they may change based on the receiving node.
+        * 
+        * Note that payee_features (or a relevant inner pointer) may be NULL or all-0s to represent None
+        * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
         */
-       public static Result_RouteLightningErrorZ get_route(byte[] our_node_id, NetworkGraph network, byte[] payee, InvoiceFeatures payee_features, ChannelDetails[] first_hops, RouteHint[] last_hops, long final_value_msat, int final_cltv, Logger logger) {
-               long ret = bindings.get_route(our_node_id, network == null ? 0 : network.ptr & ~1, payee, payee_features == null ? 0 : payee_features.ptr & ~1, Arrays.stream(first_hops).mapToLong(first_hops_conv_16 -> first_hops_conv_16 == null ? 0 : first_hops_conv_16.ptr & ~1).toArray(), Arrays.stream(last_hops).mapToLong(last_hops_conv_11 -> last_hops_conv_11 == null ? 0 : last_hops_conv_11.ptr & ~1).toArray(), final_value_msat, final_cltv, logger == null ? 0 : logger.ptr);
+       public static Result_RouteLightningErrorZ get_route(byte[] our_node_id, NetworkGraph network, byte[] payee, @Nullable InvoiceFeatures payee_features, @Nullable ChannelDetails[] first_hops, RouteHint[] last_hops, long final_value_msat, int final_cltv, Logger logger) {
+               long ret = bindings.get_route(our_node_id, network == null ? 0 : network.ptr & ~1, payee, payee_features == null ? 0 : payee_features.ptr & ~1, first_hops != null ? Arrays.stream(first_hops).mapToLong(first_hops_conv_16 -> first_hops_conv_16 == null ? 0 : first_hops_conv_16.ptr & ~1).toArray() : null, last_hops != null ? Arrays.stream(last_hops).mapToLong(last_hops_conv_11 -> last_hops_conv_11 == null ? 0 : last_hops_conv_11.ptr & ~1).toArray() : null, final_value_msat, final_cltv, logger == null ? 0 : logger.ptr);
+               if (ret < 1024) { return null; }
                Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
                ret_hu_conv.ptrs_to.add(network);
                ret_hu_conv.ptrs_to.add(payee_features);
@@ -224,8 +255,9 @@ public class UtilMethods {
         * Writes the provided `ChannelManager` to the path provided at `FilesystemPersister`
         * initialization, within a file called \"manager\".
         */
-       public static Result_NoneErrorZ FilesystemPersister_persist_manager(String data_dir, ChannelManager manager) {
+       public static Result_NoneErrorZ FilesystemPersister_persist_manager(java.lang.String data_dir, ChannelManager manager) {
                long ret = bindings.FilesystemPersister_persist_manager(data_dir, manager == null ? 0 : manager.ptr & ~1);
+               if (ret < 1024) { return null; }
                Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
                ret_hu_conv.ptrs_to.add(manager);
                return ret_hu_conv;
@@ -238,8 +270,9 @@ public class UtilMethods {
         * doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
         * that the payment secret is valid when the invoice is paid.
         */
-       public static Result_InvoiceSignOrCreationErrorZ create_invoice_from_channelmanager(ChannelManager channelmanager, KeysInterface keys_manager, Currency network, Option_u64Z amt_msat, String description) {
+       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) {
                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);
+               if (ret < 1024) { return null; }
                Result_InvoiceSignOrCreationErrorZ ret_hu_conv = Result_InvoiceSignOrCreationErrorZ.constr_from_ptr(ret);
                ret_hu_conv.ptrs_to.add(channelmanager);
                ret_hu_conv.ptrs_to.add(keys_manager);