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 * While the behavior of [`InvoicePayer`] provides idempotency of duplicate `send_*payment` calls
14 * with the same [`PaymentHash`], it is up to the `Payer` to provide idempotency across restarts.
16 * [`ChannelManager`] provides idempotency for duplicate payments with the same [`PaymentId`].
18 * In order to trivially ensure idempotency for payments, the default `Payer` implementation
19 * reuses the [`PaymentHash`] bytes as the [`PaymentId`]. Custom implementations wishing to
20 * provide payment idempotency with a different idempotency key (i.e. [`PaymentId`]) should map
21 * the [`Invoice`] or spontaneous payment target pubkey to their own idempotency key.
23 * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
25 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
26 public class Payer extends CommonBase {
27 final bindings.LDKPayer bindings_instance;
28 Payer(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
29 private Payer(bindings.LDKPayer arg) {
30 super(bindings.LDKPayer_new(arg));
31 this.ptrs_to.add(arg);
32 this.bindings_instance = arg;
34 @Override @SuppressWarnings("deprecation")
35 protected void finalize() throws Throwable {
36 if (ptr != 0) { bindings.Payer_free(ptr); } super.finalize();
39 public static interface PayerInterface {
41 * Returns the payer's node id.
45 * Returns the payer's channels.
47 ChannelDetails[] first_hops();
49 * Sends a payment over the Lightning Network using the given [`Route`].
51 * Note that payment_secret (or a relevant inner pointer) may be NULL or all-0s to represent None
53 Result_NonePaymentSendFailureZ send_payment(Route route, byte[] payment_hash, byte[] payment_secret, byte[] payment_id);
55 * Sends a spontaneous payment over the Lightning Network using the given [`Route`].
57 Result_NonePaymentSendFailureZ send_spontaneous_payment(Route route, byte[] payment_preimage, byte[] payment_id);
59 * Retries a failed payment path for the [`PaymentId`] using the given [`Route`].
61 Result_NonePaymentSendFailureZ retry_payment(Route route, byte[] payment_id);
63 * Signals that no further retries for the given payment will occur.
65 void abandon_payment(byte[] payment_id);
67 * Construct an [`InFlightHtlcs`] containing information about currently used up liquidity
70 InFlightHtlcs inflight_htlcs();
72 private static class LDKPayerHolder { Payer held; }
73 public static Payer new_impl(PayerInterface arg) {
74 final LDKPayerHolder impl_holder = new LDKPayerHolder();
75 impl_holder.held = new Payer(new bindings.LDKPayer() {
76 @Override public byte[] node_id() {
77 byte[] ret = arg.node_id();
78 Reference.reachabilityFence(arg);
79 byte[] result = InternalUtils.check_arr_len(ret, 33);
82 @Override public long[] first_hops() {
83 ChannelDetails[] ret = arg.first_hops();
84 Reference.reachabilityFence(arg);
85 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_16 -> ret_conv_16 == null ? 0 : ret_conv_16.clone_ptr()).toArray() : null;
88 @Override public long send_payment(long route, byte[] payment_hash, byte[] payment_secret, byte[] payment_id) {
89 org.ldk.structs.Route route_hu_conv = null; if (route < 0 || route > 4096) { route_hu_conv = new org.ldk.structs.Route(null, route); }
90 Result_NonePaymentSendFailureZ ret = arg.send_payment(route_hu_conv, payment_hash, payment_secret, payment_id);
91 Reference.reachabilityFence(arg);
92 long result = ret == null ? 0 : ret.clone_ptr();
95 @Override public long send_spontaneous_payment(long route, byte[] payment_preimage, byte[] payment_id) {
96 org.ldk.structs.Route route_hu_conv = null; if (route < 0 || route > 4096) { route_hu_conv = new org.ldk.structs.Route(null, route); }
97 Result_NonePaymentSendFailureZ ret = arg.send_spontaneous_payment(route_hu_conv, payment_preimage, payment_id);
98 Reference.reachabilityFence(arg);
99 long result = ret == null ? 0 : ret.clone_ptr();
102 @Override public long retry_payment(long route, byte[] payment_id) {
103 org.ldk.structs.Route route_hu_conv = null; if (route < 0 || route > 4096) { route_hu_conv = new org.ldk.structs.Route(null, route); }
104 Result_NonePaymentSendFailureZ ret = arg.retry_payment(route_hu_conv, payment_id);
105 Reference.reachabilityFence(arg);
106 long result = ret == null ? 0 : ret.clone_ptr();
109 @Override public void abandon_payment(byte[] payment_id) {
110 arg.abandon_payment(payment_id);
111 Reference.reachabilityFence(arg);
113 @Override public long inflight_htlcs() {
114 InFlightHtlcs ret = arg.inflight_htlcs();
115 Reference.reachabilityFence(arg);
116 long result = ret == null ? 0 : ret.clone_ptr();
120 return impl_holder.held;
123 * Returns the payer's node id.
125 public byte[] node_id() {
126 byte[] ret = bindings.Payer_node_id(this.ptr);
127 Reference.reachabilityFence(this);
132 * Returns the payer's channels.
134 public ChannelDetails[] first_hops() {
135 long[] ret = bindings.Payer_first_hops(this.ptr);
136 Reference.reachabilityFence(this);
137 int ret_conv_16_len = ret.length;
138 ChannelDetails[] ret_conv_16_arr = new ChannelDetails[ret_conv_16_len];
139 for (int q = 0; q < ret_conv_16_len; q++) {
140 long ret_conv_16 = ret[q];
141 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); }
142 if (ret_conv_16_hu_conv != null) { ret_conv_16_hu_conv.ptrs_to.add(this); };
143 ret_conv_16_arr[q] = ret_conv_16_hu_conv;
145 return ret_conv_16_arr;
149 * Sends a payment over the Lightning Network using the given [`Route`].
151 * Note that payment_secret (or a relevant inner pointer) may be NULL or all-0s to represent None
153 public Result_NonePaymentSendFailureZ send_payment(org.ldk.structs.Route route, byte[] payment_hash, @Nullable byte[] payment_secret, byte[] payment_id) {
154 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), InternalUtils.check_arr_len(payment_id, 32));
155 Reference.reachabilityFence(this);
156 Reference.reachabilityFence(route);
157 Reference.reachabilityFence(payment_hash);
158 Reference.reachabilityFence(payment_secret);
159 Reference.reachabilityFence(payment_id);
160 if (ret >= 0 && ret <= 4096) { return null; }
161 Result_NonePaymentSendFailureZ ret_hu_conv = Result_NonePaymentSendFailureZ.constr_from_ptr(ret);
162 if (this != null) { this.ptrs_to.add(route); };
167 * Sends a spontaneous payment over the Lightning Network using the given [`Route`].
169 public Result_NonePaymentSendFailureZ send_spontaneous_payment(org.ldk.structs.Route route, byte[] payment_preimage, byte[] payment_id) {
170 long ret = bindings.Payer_send_spontaneous_payment(this.ptr, route == null ? 0 : route.ptr, InternalUtils.check_arr_len(payment_preimage, 32), InternalUtils.check_arr_len(payment_id, 32));
171 Reference.reachabilityFence(this);
172 Reference.reachabilityFence(route);
173 Reference.reachabilityFence(payment_preimage);
174 Reference.reachabilityFence(payment_id);
175 if (ret >= 0 && ret <= 4096) { return null; }
176 Result_NonePaymentSendFailureZ ret_hu_conv = Result_NonePaymentSendFailureZ.constr_from_ptr(ret);
177 if (this != null) { this.ptrs_to.add(route); };
182 * Retries a failed payment path for the [`PaymentId`] using the given [`Route`].
184 public Result_NonePaymentSendFailureZ retry_payment(org.ldk.structs.Route route, byte[] payment_id) {
185 long ret = bindings.Payer_retry_payment(this.ptr, route == null ? 0 : route.ptr, InternalUtils.check_arr_len(payment_id, 32));
186 Reference.reachabilityFence(this);
187 Reference.reachabilityFence(route);
188 Reference.reachabilityFence(payment_id);
189 if (ret >= 0 && ret <= 4096) { return null; }
190 Result_NonePaymentSendFailureZ ret_hu_conv = Result_NonePaymentSendFailureZ.constr_from_ptr(ret);
191 if (this != null) { this.ptrs_to.add(route); };
196 * Signals that no further retries for the given payment will occur.
198 public void abandon_payment(byte[] payment_id) {
199 bindings.Payer_abandon_payment(this.ptr, InternalUtils.check_arr_len(payment_id, 32));
200 Reference.reachabilityFence(this);
201 Reference.reachabilityFence(payment_id);
205 * Construct an [`InFlightHtlcs`] containing information about currently used up liquidity
208 public InFlightHtlcs inflight_htlcs() {
209 long ret = bindings.Payer_inflight_htlcs(this.ptr);
210 Reference.reachabilityFence(this);
211 if (ret >= 0 && ret <= 4096) { return null; }
212 org.ldk.structs.InFlightHtlcs ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InFlightHtlcs(null, ret); }
213 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };