1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
11 * A trait defining behavior of an [`Invoice`] payer.
13 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
14 public class Payer extends CommonBase {
15 final bindings.LDKPayer bindings_instance;
16 Payer(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
17 private Payer(bindings.LDKPayer arg) {
18 super(bindings.LDKPayer_new(arg));
19 this.ptrs_to.add(arg);
20 this.bindings_instance = arg;
22 @Override @SuppressWarnings("deprecation")
23 protected void finalize() throws Throwable {
24 if (ptr != 0) { bindings.Payer_free(ptr); } super.finalize();
27 public static interface PayerInterface {
29 * Returns the payer's node id.
33 * Returns the payer's channels.
35 ChannelDetails[] first_hops();
37 * Sends a payment over the Lightning Network using the given [`Route`].
39 * Note that payment_secret (or a relevant inner pointer) may be NULL or all-0s to represent None
41 Result_PaymentIdPaymentSendFailureZ send_payment(Route route, byte[] payment_hash, byte[] payment_secret);
43 * Sends a spontaneous payment over the Lightning Network using the given [`Route`].
45 Result_PaymentIdPaymentSendFailureZ send_spontaneous_payment(Route route, byte[] payment_preimage);
47 * Retries a failed payment path for the [`PaymentId`] using the given [`Route`].
49 Result_NonePaymentSendFailureZ retry_payment(Route route, byte[] payment_id);
51 * Signals that no further retries for the given payment will occur.
53 void abandon_payment(byte[] payment_id);
55 private static class LDKPayerHolder { Payer held; }
56 public static Payer new_impl(PayerInterface arg) {
57 final LDKPayerHolder impl_holder = new LDKPayerHolder();
58 impl_holder.held = new Payer(new bindings.LDKPayer() {
59 @Override public byte[] node_id() {
60 byte[] ret = arg.node_id();
61 Reference.reachabilityFence(arg);
62 byte[] result = InternalUtils.check_arr_len(ret, 33);
65 @Override public long[] first_hops() {
66 ChannelDetails[] ret = arg.first_hops();
67 Reference.reachabilityFence(arg);
68 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_16 -> ret_conv_16 == null ? 0 : ret_conv_16.clone_ptr()).toArray() : null;
71 @Override public long send_payment(long route, byte[] payment_hash, byte[] payment_secret) {
72 org.ldk.structs.Route route_hu_conv = null; if (route < 0 || route > 4096) { route_hu_conv = new org.ldk.structs.Route(null, route); }
73 Result_PaymentIdPaymentSendFailureZ ret = arg.send_payment(route_hu_conv, payment_hash, payment_secret);
74 Reference.reachabilityFence(arg);
75 long result = ret == null ? 0 : ret.clone_ptr();
78 @Override public long send_spontaneous_payment(long route, byte[] payment_preimage) {
79 org.ldk.structs.Route route_hu_conv = null; if (route < 0 || route > 4096) { route_hu_conv = new org.ldk.structs.Route(null, route); }
80 Result_PaymentIdPaymentSendFailureZ ret = arg.send_spontaneous_payment(route_hu_conv, payment_preimage);
81 Reference.reachabilityFence(arg);
82 long result = ret == null ? 0 : ret.clone_ptr();
85 @Override public long retry_payment(long route, byte[] payment_id) {
86 org.ldk.structs.Route route_hu_conv = null; if (route < 0 || route > 4096) { route_hu_conv = new org.ldk.structs.Route(null, route); }
87 Result_NonePaymentSendFailureZ ret = arg.retry_payment(route_hu_conv, payment_id);
88 Reference.reachabilityFence(arg);
89 long result = ret == null ? 0 : ret.clone_ptr();
92 @Override public void abandon_payment(byte[] payment_id) {
93 arg.abandon_payment(payment_id);
94 Reference.reachabilityFence(arg);
97 return impl_holder.held;
100 * Returns the payer's node id.
102 public byte[] node_id() {
103 byte[] ret = bindings.Payer_node_id(this.ptr);
104 Reference.reachabilityFence(this);
109 * Returns the payer's channels.
111 public ChannelDetails[] first_hops() {
112 long[] ret = bindings.Payer_first_hops(this.ptr);
113 Reference.reachabilityFence(this);
114 int ret_conv_16_len = ret.length;
115 ChannelDetails[] ret_conv_16_arr = new ChannelDetails[ret_conv_16_len];
116 for (int q = 0; q < ret_conv_16_len; q++) {
117 long ret_conv_16 = ret[q];
118 org.ldk.structs.ChannelDetails ret_conv_16_hu_conv = null; if (ret_conv_16 < 0 || ret_conv_16 > 4096) { ret_conv_16_hu_conv = new org.ldk.structs.ChannelDetails(null, ret_conv_16); }
119 if (ret_conv_16_hu_conv != null) { ret_conv_16_hu_conv.ptrs_to.add(this); };
120 ret_conv_16_arr[q] = ret_conv_16_hu_conv;
122 return ret_conv_16_arr;
126 * Sends a payment over the Lightning Network using the given [`Route`].
128 * Note that payment_secret (or a relevant inner pointer) may be NULL or all-0s to represent None
130 public Result_PaymentIdPaymentSendFailureZ send_payment(Route route, byte[] payment_hash, @Nullable byte[] payment_secret) {
131 long ret = bindings.Payer_send_payment(this.ptr, route == null ? 0 : route.ptr, InternalUtils.check_arr_len(payment_hash, 32), InternalUtils.check_arr_len(payment_secret, 32));
132 Reference.reachabilityFence(this);
133 Reference.reachabilityFence(route);
134 Reference.reachabilityFence(payment_hash);
135 Reference.reachabilityFence(payment_secret);
136 if (ret >= 0 && ret <= 4096) { return null; }
137 Result_PaymentIdPaymentSendFailureZ ret_hu_conv = Result_PaymentIdPaymentSendFailureZ.constr_from_ptr(ret);
138 if (this != null) { this.ptrs_to.add(route); };
143 * Sends a spontaneous payment over the Lightning Network using the given [`Route`].
145 public Result_PaymentIdPaymentSendFailureZ send_spontaneous_payment(Route route, byte[] payment_preimage) {
146 long ret = bindings.Payer_send_spontaneous_payment(this.ptr, route == null ? 0 : route.ptr, InternalUtils.check_arr_len(payment_preimage, 32));
147 Reference.reachabilityFence(this);
148 Reference.reachabilityFence(route);
149 Reference.reachabilityFence(payment_preimage);
150 if (ret >= 0 && ret <= 4096) { return null; }
151 Result_PaymentIdPaymentSendFailureZ ret_hu_conv = Result_PaymentIdPaymentSendFailureZ.constr_from_ptr(ret);
152 if (this != null) { this.ptrs_to.add(route); };
157 * Retries a failed payment path for the [`PaymentId`] using the given [`Route`].
159 public Result_NonePaymentSendFailureZ retry_payment(Route route, byte[] payment_id) {
160 long ret = bindings.Payer_retry_payment(this.ptr, route == null ? 0 : route.ptr, InternalUtils.check_arr_len(payment_id, 32));
161 Reference.reachabilityFence(this);
162 Reference.reachabilityFence(route);
163 Reference.reachabilityFence(payment_id);
164 if (ret >= 0 && ret <= 4096) { return null; }
165 Result_NonePaymentSendFailureZ ret_hu_conv = Result_NonePaymentSendFailureZ.constr_from_ptr(ret);
166 if (this != null) { this.ptrs_to.add(route); };
171 * Signals that no further retries for the given payment will occur.
173 public void abandon_payment(byte[] payment_id) {
174 bindings.Payer_abandon_payment(this.ptr, InternalUtils.check_arr_len(payment_id, 32));
175 Reference.reachabilityFence(this);
176 Reference.reachabilityFence(payment_id);