1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
10 * A trait defining behavior of an [`Invoice`] payer.
12 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
13 public class Payer extends CommonBase {
14 final bindings.LDKPayer bindings_instance;
15 Payer(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
16 private Payer(bindings.LDKPayer arg) {
17 super(bindings.LDKPayer_new(arg));
18 this.ptrs_to.add(arg);
19 this.bindings_instance = arg;
21 @Override @SuppressWarnings("deprecation")
22 protected void finalize() throws Throwable {
23 if (ptr != 0) { bindings.Payer_free(ptr); } super.finalize();
26 public static interface PayerInterface {
28 * Returns the payer's node id.
32 * Returns the payer's channels.
34 ChannelDetails[] first_hops();
36 * Sends a payment over the Lightning Network using the given [`Route`].
38 * Note that payment_secret (or a relevant inner pointer) may be NULL or all-0s to represent None
40 Result_PaymentIdPaymentSendFailureZ send_payment(Route route, byte[] payment_hash, byte[] payment_secret);
42 * Retries a failed payment path for the [`PaymentId`] using the given [`Route`].
44 Result_NonePaymentSendFailureZ retry_payment(Route route, byte[] payment_id);
46 private static class LDKPayerHolder { Payer held; }
47 public static Payer new_impl(PayerInterface arg) {
48 final LDKPayerHolder impl_holder = new LDKPayerHolder();
49 impl_holder.held = new Payer(new bindings.LDKPayer() {
50 @Override public byte[] node_id() {
51 byte[] ret = arg.node_id();
54 @Override public long[] first_hops() {
55 ChannelDetails[] ret = arg.first_hops();
56 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_16 -> ret_conv_16 == null ? 0 : ret_conv_16.ptr & ~1).toArray() : null;
59 @Override public long send_payment(long route, byte[] payment_hash, byte[] payment_secret) {
60 Route route_hu_conv = null; if (route < 0 || route > 4096) { route_hu_conv = new Route(null, route); }
61 Result_PaymentIdPaymentSendFailureZ ret = arg.send_payment(route_hu_conv, payment_hash, payment_secret);
62 long result = ret != null ? ret.ptr : 0;
65 @Override public long retry_payment(long route, byte[] payment_id) {
66 Route route_hu_conv = null; if (route < 0 || route > 4096) { route_hu_conv = new Route(null, route); }
67 Result_NonePaymentSendFailureZ ret = arg.retry_payment(route_hu_conv, payment_id);
68 long result = ret != null ? ret.ptr : 0;
72 return impl_holder.held;
75 * Returns the payer's node id.
77 public byte[] node_id() {
78 byte[] ret = bindings.Payer_node_id(this.ptr);
83 * Returns the payer's channels.
85 public ChannelDetails[] first_hops() {
86 long[] ret = bindings.Payer_first_hops(this.ptr);
87 ChannelDetails[] ret_conv_16_arr = new ChannelDetails[ret.length];
88 for (int q = 0; q < ret.length; q++) {
89 long ret_conv_16 = ret[q];
90 ChannelDetails ret_conv_16_hu_conv = null; if (ret_conv_16 < 0 || ret_conv_16 > 4096) { ret_conv_16_hu_conv = new ChannelDetails(null, ret_conv_16); }
91 ret_conv_16_hu_conv.ptrs_to.add(this);
92 ret_conv_16_arr[q] = ret_conv_16_hu_conv;
94 return ret_conv_16_arr;
98 * Sends a payment over the Lightning Network using the given [`Route`].
100 * Note that payment_secret (or a relevant inner pointer) may be NULL or all-0s to represent None
102 public Result_PaymentIdPaymentSendFailureZ send_payment(Route route, byte[] payment_hash, @Nullable byte[] payment_secret) {
103 long ret = bindings.Payer_send_payment(this.ptr, route == null ? 0 : route.ptr & ~1, payment_hash, payment_secret);
104 if (ret >= 0 && ret <= 4096) { return null; }
105 Result_PaymentIdPaymentSendFailureZ ret_hu_conv = Result_PaymentIdPaymentSendFailureZ.constr_from_ptr(ret);
106 this.ptrs_to.add(route);
111 * Retries a failed payment path for the [`PaymentId`] using the given [`Route`].
113 public Result_NonePaymentSendFailureZ retry_payment(Route route, byte[] payment_id) {
114 long ret = bindings.Payer_retry_payment(this.ptr, route == null ? 0 : route.ptr & ~1, payment_id);
115 if (ret >= 0 && ret <= 4096) { return null; }
116 Result_NonePaymentSendFailureZ ret_hu_conv = Result_NonePaymentSendFailureZ.constr_from_ptr(ret);
117 this.ptrs_to.add(route);