Merge pull request #139 from TheBlueMatt/main
[ldk-java] / c_sharp / src / org / ldk / structs / Bolt11Invoice.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 /**
10  * Represents a syntactically and semantically correct lightning BOLT11 invoice.
11  * 
12  * There are three ways to construct a `Bolt11Invoice`:
13  * 1. using [`InvoiceBuilder`]
14  * 2. using [`Bolt11Invoice::from_signed`]
15  * 3. using `str::parse::<Bolt11Invoice>(&str)` (see [`Bolt11Invoice::from_str`])
16  * 
17  * [`Bolt11Invoice::from_str`]: crate::Bolt11Invoice#impl-FromStr
18  */
19 public class Bolt11Invoice : CommonBase {
20         internal Bolt11Invoice(object _dummy, long ptr) : base(ptr) { }
21         ~Bolt11Invoice() {
22                 if (ptr != 0) { bindings.Bolt11Invoice_free(ptr); }
23         }
24
25         /**
26          * Checks if two Bolt11Invoices contain equal inner contents.
27          * This ignores pointers and is_owned flags and looks at the values in fields.
28          * Two objects with NULL inner values will be considered "equal" here.
29          */
30         public bool eq(org.ldk.structs.Bolt11Invoice b) {
31                 bool ret = bindings.Bolt11Invoice_eq(this.ptr, b == null ? 0 : b.ptr);
32                 GC.KeepAlive(this);
33                 GC.KeepAlive(b);
34                 if (this != null) { this.ptrs_to.AddLast(b); };
35                 return ret;
36         }
37
38         public override bool Equals(object o) {
39                 if (!(o is Bolt11Invoice)) return false;
40                 return this.eq((Bolt11Invoice)o);
41         }
42         internal long clone_ptr() {
43                 long ret = bindings.Bolt11Invoice_clone_ptr(this.ptr);
44                 GC.KeepAlive(this);
45                 return ret;
46         }
47
48         /**
49          * Creates a copy of the Bolt11Invoice
50          */
51         public Bolt11Invoice clone() {
52                 long ret = bindings.Bolt11Invoice_clone(this.ptr);
53                 GC.KeepAlive(this);
54                 if (ret >= 0 && ret <= 4096) { return null; }
55                 org.ldk.structs.Bolt11Invoice ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.Bolt11Invoice(null, ret); }
56                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
57                 return ret_hu_conv;
58         }
59
60         /**
61          * Generates a non-cryptographic 64-bit hash of the Bolt11Invoice.
62          */
63         public long hash() {
64                 long ret = bindings.Bolt11Invoice_hash(this.ptr);
65                 GC.KeepAlive(this);
66                 return ret;
67         }
68
69         public override int GetHashCode() {
70                 return (int)this.hash();
71         }
72         /**
73          * The hash of the [`RawBolt11Invoice`] that was signed.
74          */
75         public byte[] signable_hash() {
76                 byte[] ret = bindings.Bolt11Invoice_signable_hash(this.ptr);
77                 GC.KeepAlive(this);
78                 return ret;
79         }
80
81         /**
82          * Transform the `Bolt11Invoice` into its unchecked version.
83          */
84         public SignedRawBolt11Invoice into_signed_raw() {
85                 long ret = bindings.Bolt11Invoice_into_signed_raw(this.ptr);
86                 GC.KeepAlive(this);
87                 if (ret >= 0 && ret <= 4096) { return null; }
88                 org.ldk.structs.SignedRawBolt11Invoice ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.SignedRawBolt11Invoice(null, ret); }
89                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
90                 if (this != null) { this.ptrs_to.AddLast(this); };
91                 return ret_hu_conv;
92         }
93
94         /**
95          * Check that the invoice is signed correctly and that key recovery works
96          */
97         public Result_NoneBolt11SemanticErrorZ check_signature() {
98                 long ret = bindings.Bolt11Invoice_check_signature(this.ptr);
99                 GC.KeepAlive(this);
100                 if (ret >= 0 && ret <= 4096) { return null; }
101                 Result_NoneBolt11SemanticErrorZ ret_hu_conv = Result_NoneBolt11SemanticErrorZ.constr_from_ptr(ret);
102                 return ret_hu_conv;
103         }
104
105         /**
106          * Constructs a `Bolt11Invoice` from a [`SignedRawBolt11Invoice`] by checking all its invariants.
107          * ```
108          * use lightning_invoice::*;
109          * 
110          * let invoice = \"lnbc100p1psj9jhxdqud3jxktt5w46x7unfv9kz6mn0v3jsnp4q0d3p2sfluzdx45tqcs\\
111          * h2pu5qc7lgq0xs578ngs6s0s68ua4h7cvspp5q6rmq35js88zp5dvwrv9m459tnk2zunwj5jalqtyxqulh0l\\
112          * 5gflssp5nf55ny5gcrfl30xuhzj3nphgj27rstekmr9fw3ny5989s300gyus9qyysgqcqpcrzjqw2sxwe993\\
113          * h5pcm4dxzpvttgza8zhkqxpgffcrf5v25nwpr3cmfg7z54kuqq8rgqqqqqqqq2qqqqq9qq9qrzjqd0ylaqcl\\
114          * j9424x9m8h2vcukcgnm6s56xfgu3j78zyqzhgs4hlpzvznlugqq9vsqqqqqqqlgqqqqqeqq9qrzjqwldmj9d\\
115          * ha74df76zhx6l9we0vjdquygcdt3kssupehe64g6yyp5yz5rhuqqwccqqyqqqqlgqqqqjcqq9qrzjqf9e58a\\
116          * guqr0rcun0ajlvmzq3ek63cw2w282gv3z5uupmuwvgjtq2z55qsqqg6qqqyqqqrtnqqqzq3cqygrzjqvphms\\
117          * ywntrrhqjcraumvc4y6r8v4z5v593trte429v4hredj7ms5z52usqq9ngqqqqqqqlgqqqqqqgq9qrzjq2v0v\\
118          * p62g49p7569ev48cmulecsxe59lvaw3wlxm7r982zxa9zzj7z5l0cqqxusqqyqqqqlgqqqqqzsqygarl9fh3\\
119          * 8s0gyuxjjgux34w75dnc6xp2l35j7es3jd4ugt3lu0xzre26yg5m7ke54n2d5sym4xcmxtl8238xxvw5h5h5\\
120          * j5r6drg6k6zcqj0fcwg\";
121          * 
122          * let signed = invoice.parse::<SignedRawBolt11Invoice>().unwrap();
123          * 
124          * assert!(Bolt11Invoice::from_signed(signed).is_ok());
125          * ```
126          */
127         public static Result_Bolt11InvoiceBolt11SemanticErrorZ from_signed(org.ldk.structs.SignedRawBolt11Invoice signed_invoice) {
128                 long ret = bindings.Bolt11Invoice_from_signed(signed_invoice == null ? 0 : signed_invoice.ptr);
129                 GC.KeepAlive(signed_invoice);
130                 if (ret >= 0 && ret <= 4096) { return null; }
131                 Result_Bolt11InvoiceBolt11SemanticErrorZ ret_hu_conv = Result_Bolt11InvoiceBolt11SemanticErrorZ.constr_from_ptr(ret);
132                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(signed_invoice); };
133                 return ret_hu_conv;
134         }
135
136         /**
137          * Returns the `Bolt11Invoice`'s timestamp (should equal its creation time)
138          */
139         public long timestamp() {
140                 long ret = bindings.Bolt11Invoice_timestamp(this.ptr);
141                 GC.KeepAlive(this);
142                 return ret;
143         }
144
145         /**
146          * Returns the `Bolt11Invoice`'s timestamp as a duration since the Unix epoch
147          */
148         public long duration_since_epoch() {
149                 long ret = bindings.Bolt11Invoice_duration_since_epoch(this.ptr);
150                 GC.KeepAlive(this);
151                 return ret;
152         }
153
154         /**
155          * Returns the hash to which we will receive the preimage on completion of the payment
156          */
157         public byte[] payment_hash() {
158                 byte[] ret = bindings.Bolt11Invoice_payment_hash(this.ptr);
159                 GC.KeepAlive(this);
160                 return ret;
161         }
162
163         /**
164          * Get the payee's public key if one was included in the invoice
165          * 
166          * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
167          */
168         public byte[] payee_pub_key() {
169                 byte[] ret = bindings.Bolt11Invoice_payee_pub_key(this.ptr);
170                 GC.KeepAlive(this);
171                 return ret;
172         }
173
174         /**
175          * Get the payment secret if one was included in the invoice
176          */
177         public byte[] payment_secret() {
178                 byte[] ret = bindings.Bolt11Invoice_payment_secret(this.ptr);
179                 GC.KeepAlive(this);
180                 return ret;
181         }
182
183         /**
184          * Get the payment metadata blob if one was included in the invoice
185          */
186         public Option_CVec_u8ZZ payment_metadata() {
187                 long ret = bindings.Bolt11Invoice_payment_metadata(this.ptr);
188                 GC.KeepAlive(this);
189                 if (ret >= 0 && ret <= 4096) { return null; }
190                 org.ldk.structs.Option_CVec_u8ZZ ret_hu_conv = org.ldk.structs.Option_CVec_u8ZZ.constr_from_ptr(ret);
191                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
192                 return ret_hu_conv;
193         }
194
195         /**
196          * Get the invoice features if they were included in the invoice
197          * 
198          * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
199          */
200         public Bolt11InvoiceFeatures features() {
201                 long ret = bindings.Bolt11Invoice_features(this.ptr);
202                 GC.KeepAlive(this);
203                 if (ret >= 0 && ret <= 4096) { return null; }
204                 org.ldk.structs.Bolt11InvoiceFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.Bolt11InvoiceFeatures(null, ret); }
205                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
206                 return ret_hu_conv;
207         }
208
209         /**
210          * Recover the payee's public key (only to be used if none was included in the invoice)
211          */
212         public byte[] recover_payee_pub_key() {
213                 byte[] ret = bindings.Bolt11Invoice_recover_payee_pub_key(this.ptr);
214                 GC.KeepAlive(this);
215                 return ret;
216         }
217
218         /**
219          * Returns the Duration since the Unix epoch at which the invoice expires.
220          * Returning None if overflow occurred.
221          */
222         public Option_DurationZ expires_at() {
223                 long ret = bindings.Bolt11Invoice_expires_at(this.ptr);
224                 GC.KeepAlive(this);
225                 if (ret >= 0 && ret <= 4096) { return null; }
226                 org.ldk.structs.Option_DurationZ ret_hu_conv = org.ldk.structs.Option_DurationZ.constr_from_ptr(ret);
227                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
228                 return ret_hu_conv;
229         }
230
231         /**
232          * Returns the invoice's expiry time, if present, otherwise [`DEFAULT_EXPIRY_TIME`].
233          */
234         public long expiry_time() {
235                 long ret = bindings.Bolt11Invoice_expiry_time(this.ptr);
236                 GC.KeepAlive(this);
237                 return ret;
238         }
239
240         /**
241          * Returns whether the invoice has expired.
242          */
243         public bool is_expired() {
244                 bool ret = bindings.Bolt11Invoice_is_expired(this.ptr);
245                 GC.KeepAlive(this);
246                 return ret;
247         }
248
249         /**
250          * Returns the Duration remaining until the invoice expires.
251          */
252         public long duration_until_expiry() {
253                 long ret = bindings.Bolt11Invoice_duration_until_expiry(this.ptr);
254                 GC.KeepAlive(this);
255                 return ret;
256         }
257
258         /**
259          * Returns the Duration remaining until the invoice expires given the current time.
260          * `time` is the timestamp as a duration since the Unix epoch.
261          */
262         public long expiration_remaining_from_epoch(long time) {
263                 long ret = bindings.Bolt11Invoice_expiration_remaining_from_epoch(this.ptr, time);
264                 GC.KeepAlive(this);
265                 GC.KeepAlive(time);
266                 return ret;
267         }
268
269         /**
270          * Returns whether the expiry time would pass at the given point in time.
271          * `at_time` is the timestamp as a duration since the Unix epoch.
272          */
273         public bool would_expire(long at_time) {
274                 bool ret = bindings.Bolt11Invoice_would_expire(this.ptr, at_time);
275                 GC.KeepAlive(this);
276                 GC.KeepAlive(at_time);
277                 return ret;
278         }
279
280         /**
281          * Returns the invoice's `min_final_cltv_expiry_delta` time, if present, otherwise
282          * [`DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA`].
283          */
284         public long min_final_cltv_expiry_delta() {
285                 long ret = bindings.Bolt11Invoice_min_final_cltv_expiry_delta(this.ptr);
286                 GC.KeepAlive(this);
287                 return ret;
288         }
289
290         /**
291          * Returns a list of all fallback addresses as [`Address`]es
292          */
293         public string[] fallback_addresses() {
294                 string[] ret = bindings.Bolt11Invoice_fallback_addresses(this.ptr);
295                 GC.KeepAlive(this);
296                 return ret;
297         }
298
299         /**
300          * Returns a list of all routes included in the invoice
301          */
302         public PrivateRoute[] private_routes() {
303                 long[] ret = bindings.Bolt11Invoice_private_routes(this.ptr);
304                 GC.KeepAlive(this);
305                 int ret_conv_14_len = ret.Length;
306                 PrivateRoute[] ret_conv_14_arr = new PrivateRoute[ret_conv_14_len];
307                 for (int o = 0; o < ret_conv_14_len; o++) {
308                         long ret_conv_14 = ret[o];
309                         org.ldk.structs.PrivateRoute ret_conv_14_hu_conv = null; if (ret_conv_14 < 0 || ret_conv_14 > 4096) { ret_conv_14_hu_conv = new org.ldk.structs.PrivateRoute(null, ret_conv_14); }
310                         if (ret_conv_14_hu_conv != null) { ret_conv_14_hu_conv.ptrs_to.AddLast(this); };
311                         ret_conv_14_arr[o] = ret_conv_14_hu_conv;
312                 }
313                 return ret_conv_14_arr;
314         }
315
316         /**
317          * Returns a list of all routes included in the invoice as the underlying hints
318          */
319         public RouteHint[] route_hints() {
320                 long[] ret = bindings.Bolt11Invoice_route_hints(this.ptr);
321                 GC.KeepAlive(this);
322                 int ret_conv_11_len = ret.Length;
323                 RouteHint[] ret_conv_11_arr = new RouteHint[ret_conv_11_len];
324                 for (int l = 0; l < ret_conv_11_len; l++) {
325                         long ret_conv_11 = ret[l];
326                         org.ldk.structs.RouteHint ret_conv_11_hu_conv = null; if (ret_conv_11 < 0 || ret_conv_11 > 4096) { ret_conv_11_hu_conv = new org.ldk.structs.RouteHint(null, ret_conv_11); }
327                         if (ret_conv_11_hu_conv != null) { ret_conv_11_hu_conv.ptrs_to.AddLast(this); };
328                         ret_conv_11_arr[l] = ret_conv_11_hu_conv;
329                 }
330                 return ret_conv_11_arr;
331         }
332
333         /**
334          * Returns the currency for which the invoice was issued
335          */
336         public Currency currency() {
337                 Currency ret = bindings.Bolt11Invoice_currency(this.ptr);
338                 GC.KeepAlive(this);
339                 return ret;
340         }
341
342         /**
343          * Returns the amount if specified in the invoice as millisatoshis.
344          */
345         public Option_u64Z amount_milli_satoshis() {
346                 long ret = bindings.Bolt11Invoice_amount_milli_satoshis(this.ptr);
347                 GC.KeepAlive(this);
348                 if (ret >= 0 && ret <= 4096) { return null; }
349                 org.ldk.structs.Option_u64Z ret_hu_conv = org.ldk.structs.Option_u64Z.constr_from_ptr(ret);
350                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
351                 return ret_hu_conv;
352         }
353
354         /**
355          * Read a Bolt11Invoice object from a string
356          */
357         public static Result_Bolt11InvoiceParseOrSemanticErrorZ from_str(string s) {
358                 long ret = bindings.Bolt11Invoice_from_str(s);
359                 GC.KeepAlive(s);
360                 if (ret >= 0 && ret <= 4096) { return null; }
361                 Result_Bolt11InvoiceParseOrSemanticErrorZ ret_hu_conv = Result_Bolt11InvoiceParseOrSemanticErrorZ.constr_from_ptr(ret);
362                 return ret_hu_conv;
363         }
364
365         /**
366          * Get the string representation of a Bolt11Invoice object
367          */
368         public string to_str() {
369                 string ret = bindings.Bolt11Invoice_to_str(this.ptr);
370                 GC.KeepAlive(this);
371                 return ret;
372         }
373
374 }
375 } } }