cb305f88c5cd86443207957f1529e4adbf3e9e4a
[ldk-java] / c_sharp / src / org / ldk / structs / Payer.cs
1 using org.ldk.impl;
2 using org.ldk.enums;
3 using org.ldk.util;
4 using System;
5
6 namespace org { namespace ldk { namespace structs {
7
8 /**
9  * A trait defining behavior of an [`Invoice`] payer.
10  * 
11  * While the behavior of [`InvoicePayer`] provides idempotency of duplicate `send_*payment` calls
12  * with the same [`PaymentHash`], it is up to the `Payer` to provide idempotency across restarts.
13  * 
14  * [`ChannelManager`] provides idempotency for duplicate payments with the same [`PaymentId`].
15  * 
16  * In order to trivially ensure idempotency for payments, the default `Payer` implementation
17  * reuses the [`PaymentHash`] bytes as the [`PaymentId`]. Custom implementations wishing to
18  * provide payment idempotency with a different idempotency key (i.e. [`PaymentId`]) should map
19  * the [`Invoice`] or spontaneous payment target pubkey to their own idempotency key.
20  * 
21  * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
22  */
23 public class Payer : CommonBase {
24         internal readonly bindings.LDKPayer bindings_instance;
25         internal Payer(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
26         private Payer(bindings.LDKPayer arg) : base(bindings.LDKPayer_new(arg)) {
27                 this.ptrs_to.AddLast(arg);
28                 this.bindings_instance = arg;
29         }
30         ~Payer() {
31                 if (ptr != 0) { bindings.Payer_free(ptr); }
32         }
33
34         public interface PayerInterface {
35                 /**
36                  * Returns the payer's node id.
37                  */
38                 byte[] node_id();
39                 /**
40                  * Returns the payer's channels.
41                  */
42                 ChannelDetails[] first_hops();
43                 /**
44                  * Sends a payment over the Lightning Network using the given [`Route`].
45                  * 
46                  * Note that payment_secret (or a relevant inner pointer) may be NULL or all-0s to represent None
47                  */
48                 Result_NonePaymentSendFailureZ send_payment(Route _route, byte[] _payment_hash, byte[] _payment_secret, byte[] _payment_id);
49                 /**
50                  * Sends a spontaneous payment over the Lightning Network using the given [`Route`].
51                  */
52                 Result_NonePaymentSendFailureZ send_spontaneous_payment(Route _route, byte[] _payment_preimage, byte[] _payment_id);
53                 /**
54                  * Retries a failed payment path for the [`PaymentId`] using the given [`Route`].
55                  */
56                 Result_NonePaymentSendFailureZ retry_payment(Route _route, byte[] _payment_id);
57                 /**
58                  * Signals that no further retries for the given payment will occur.
59                  */
60                 void abandon_payment(byte[] _payment_id);
61                 /**
62                  * Construct an [`InFlightHtlcs`] containing information about currently used up liquidity
63                  * across payments.
64                  */
65                 InFlightHtlcs inflight_htlcs();
66         }
67         private class LDKPayerHolder { internal Payer held; }
68         private class LDKPayerImpl : bindings.LDKPayer {
69                 internal LDKPayerImpl(PayerInterface arg, LDKPayerHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
70                 private PayerInterface arg;
71                 private LDKPayerHolder impl_holder;
72                 public byte[] node_id() {
73                         byte[] ret = arg.node_id();
74                                 GC.KeepAlive(arg);
75                         byte[] result = InternalUtils.check_arr_len(ret, 33);
76                         return result;
77                 }
78                 public long[] first_hops() {
79                         ChannelDetails[] ret = arg.first_hops();
80                                 GC.KeepAlive(arg);
81                         long[] result = ret != null ? InternalUtils.mapArray(ret, ret_conv_16 => ret_conv_16 == null ? 0 : ret_conv_16.clone_ptr()) : null;
82                         return result;
83                 }
84                 public long send_payment(long _route, byte[] _payment_hash, byte[] _payment_secret, byte[] _payment_id) {
85                         org.ldk.structs.Route _route_hu_conv = null; if (_route < 0 || _route > 4096) { _route_hu_conv = new org.ldk.structs.Route(null, _route); }
86                         Result_NonePaymentSendFailureZ ret = arg.send_payment(_route_hu_conv, _payment_hash, _payment_secret, _payment_id);
87                                 GC.KeepAlive(arg);
88                         long result = ret == null ? 0 : ret.clone_ptr();
89                         return result;
90                 }
91                 public long send_spontaneous_payment(long _route, byte[] _payment_preimage, byte[] _payment_id) {
92                         org.ldk.structs.Route _route_hu_conv = null; if (_route < 0 || _route > 4096) { _route_hu_conv = new org.ldk.structs.Route(null, _route); }
93                         Result_NonePaymentSendFailureZ ret = arg.send_spontaneous_payment(_route_hu_conv, _payment_preimage, _payment_id);
94                                 GC.KeepAlive(arg);
95                         long result = ret == null ? 0 : ret.clone_ptr();
96                         return result;
97                 }
98                 public long retry_payment(long _route, byte[] _payment_id) {
99                         org.ldk.structs.Route _route_hu_conv = null; if (_route < 0 || _route > 4096) { _route_hu_conv = new org.ldk.structs.Route(null, _route); }
100                         Result_NonePaymentSendFailureZ ret = arg.retry_payment(_route_hu_conv, _payment_id);
101                                 GC.KeepAlive(arg);
102                         long result = ret == null ? 0 : ret.clone_ptr();
103                         return result;
104                 }
105                 public void abandon_payment(byte[] _payment_id) {
106                         arg.abandon_payment(_payment_id);
107                                 GC.KeepAlive(arg);
108                 }
109                 public long inflight_htlcs() {
110                         InFlightHtlcs ret = arg.inflight_htlcs();
111                                 GC.KeepAlive(arg);
112                         long result = ret == null ? 0 : ret.clone_ptr();
113                         return result;
114                 }
115         }
116         public static Payer new_impl(PayerInterface arg) {
117                 LDKPayerHolder impl_holder = new LDKPayerHolder();
118                 impl_holder.held = new Payer(new LDKPayerImpl(arg, impl_holder));
119                 return impl_holder.held;
120         }
121         /**
122          * Returns the payer's node id.
123          */
124         public byte[] node_id() {
125                 byte[] ret = bindings.Payer_node_id(this.ptr);
126                 GC.KeepAlive(this);
127                 return ret;
128         }
129
130         /**
131          * Returns the payer's channels.
132          */
133         public ChannelDetails[] first_hops() {
134                 long[] ret = bindings.Payer_first_hops(this.ptr);
135                 GC.KeepAlive(this);
136                 int ret_conv_16_len = ret.Length;
137                 ChannelDetails[] ret_conv_16_arr = new ChannelDetails[ret_conv_16_len];
138                 for (int q = 0; q < ret_conv_16_len; q++) {
139                         long ret_conv_16 = ret[q];
140                         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); }
141                         if (ret_conv_16_hu_conv != null) { ret_conv_16_hu_conv.ptrs_to.AddLast(this); };
142                         ret_conv_16_arr[q] = ret_conv_16_hu_conv;
143                 }
144                 return ret_conv_16_arr;
145         }
146
147         /**
148          * Sends a payment over the Lightning Network using the given [`Route`].
149          * 
150          * Note that payment_secret (or a relevant inner pointer) may be NULL or all-0s to represent None
151          */
152         public Result_NonePaymentSendFailureZ send_payment(org.ldk.structs.Route route, byte[] payment_hash, byte[] payment_secret, byte[] payment_id) {
153                 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));
154                 GC.KeepAlive(this);
155                 GC.KeepAlive(route);
156                 GC.KeepAlive(payment_hash);
157                 GC.KeepAlive(payment_secret);
158                 GC.KeepAlive(payment_id);
159                 if (ret >= 0 && ret <= 4096) { return null; }
160                 Result_NonePaymentSendFailureZ ret_hu_conv = Result_NonePaymentSendFailureZ.constr_from_ptr(ret);
161                 if (this != null) { this.ptrs_to.AddLast(route); };
162                 return ret_hu_conv;
163         }
164
165         /**
166          * Sends a spontaneous payment over the Lightning Network using the given [`Route`].
167          */
168         public Result_NonePaymentSendFailureZ send_spontaneous_payment(org.ldk.structs.Route route, byte[] payment_preimage, byte[] payment_id) {
169                 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));
170                 GC.KeepAlive(this);
171                 GC.KeepAlive(route);
172                 GC.KeepAlive(payment_preimage);
173                 GC.KeepAlive(payment_id);
174                 if (ret >= 0 && ret <= 4096) { return null; }
175                 Result_NonePaymentSendFailureZ ret_hu_conv = Result_NonePaymentSendFailureZ.constr_from_ptr(ret);
176                 if (this != null) { this.ptrs_to.AddLast(route); };
177                 return ret_hu_conv;
178         }
179
180         /**
181          * Retries a failed payment path for the [`PaymentId`] using the given [`Route`].
182          */
183         public Result_NonePaymentSendFailureZ retry_payment(org.ldk.structs.Route route, byte[] payment_id) {
184                 long ret = bindings.Payer_retry_payment(this.ptr, route == null ? 0 : route.ptr, InternalUtils.check_arr_len(payment_id, 32));
185                 GC.KeepAlive(this);
186                 GC.KeepAlive(route);
187                 GC.KeepAlive(payment_id);
188                 if (ret >= 0 && ret <= 4096) { return null; }
189                 Result_NonePaymentSendFailureZ ret_hu_conv = Result_NonePaymentSendFailureZ.constr_from_ptr(ret);
190                 if (this != null) { this.ptrs_to.AddLast(route); };
191                 return ret_hu_conv;
192         }
193
194         /**
195          * Signals that no further retries for the given payment will occur.
196          */
197         public void abandon_payment(byte[] payment_id) {
198                 bindings.Payer_abandon_payment(this.ptr, InternalUtils.check_arr_len(payment_id, 32));
199                 GC.KeepAlive(this);
200                 GC.KeepAlive(payment_id);
201         }
202
203         /**
204          * Construct an [`InFlightHtlcs`] containing information about currently used up liquidity
205          * across payments.
206          */
207         public InFlightHtlcs inflight_htlcs() {
208                 long ret = bindings.Payer_inflight_htlcs(this.ptr);
209                 GC.KeepAlive(this);
210                 if (ret >= 0 && ret <= 4096) { return null; }
211                 org.ldk.structs.InFlightHtlcs ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InFlightHtlcs(null, ret); }
212                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
213                 return ret_hu_conv;
214         }
215
216 }
217 } } }