80a987eb146d57d241ff6d009b18241891f97da7
[ldk-java] / src / main / java / org / ldk / structs / Invoice.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10
11 /**
12  * Represents a syntactically and semantically correct lightning BOLT11 invoice.
13  * 
14  * There are three ways to construct an `Invoice`:
15  * 1. using `InvoiceBuilder`
16  * 2. using `Invoice::from_signed(SignedRawInvoice)`
17  * 3. using `str::parse::<Invoice>(&str)`
18  */
19 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
20 public class Invoice extends CommonBase {
21         Invoice(Object _dummy, long ptr) { super(ptr); }
22         @Override @SuppressWarnings("deprecation")
23         protected void finalize() throws Throwable {
24                 super.finalize();
25                 if (ptr != 0) { bindings.Invoice_free(ptr); }
26         }
27
28         /**
29          * Checks if two Invoices contain equal inner contents.
30          * This ignores pointers and is_owned flags and looks at the values in fields.
31          * Two objects with NULL inner values will be considered "equal" here.
32          */
33         public boolean eq(Invoice b) {
34                 boolean ret = bindings.Invoice_eq(this.ptr, b == null ? 0 : b.ptr & ~1);
35                 Reference.reachabilityFence(this);
36                 Reference.reachabilityFence(b);
37                 this.ptrs_to.add(b);
38                 return ret;
39         }
40
41         @Override public boolean equals(Object o) {
42                 if (!(o instanceof Invoice)) return false;
43                 return this.eq((Invoice)o);
44         }
45         long clone_ptr() {
46                 long ret = bindings.Invoice_clone_ptr(this.ptr);
47                 Reference.reachabilityFence(this);
48                 return ret;
49         }
50
51         /**
52          * Creates a copy of the Invoice
53          */
54         public Invoice clone() {
55                 long ret = bindings.Invoice_clone(this.ptr);
56                 Reference.reachabilityFence(this);
57                 if (ret >= 0 && ret <= 4096) { return null; }
58                 Invoice ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new Invoice(null, ret); }
59                 ret_hu_conv.ptrs_to.add(this);
60                 return ret_hu_conv;
61         }
62
63         /**
64          * Transform the `Invoice` into it's unchecked version
65          */
66         public SignedRawInvoice into_signed_raw() {
67                 long ret = bindings.Invoice_into_signed_raw(this.ptr);
68                 Reference.reachabilityFence(this);
69                 if (ret >= 0 && ret <= 4096) { return null; }
70                 SignedRawInvoice ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new SignedRawInvoice(null, ret); }
71                 ret_hu_conv.ptrs_to.add(this);
72                 ;
73                 return ret_hu_conv;
74         }
75
76         /**
77          * Check that the invoice is signed correctly and that key recovery works
78          */
79         public Result_NoneSemanticErrorZ check_signature() {
80                 long ret = bindings.Invoice_check_signature(this.ptr);
81                 Reference.reachabilityFence(this);
82                 if (ret >= 0 && ret <= 4096) { return null; }
83                 Result_NoneSemanticErrorZ ret_hu_conv = Result_NoneSemanticErrorZ.constr_from_ptr(ret);
84                 return ret_hu_conv;
85         }
86
87         /**
88          * Constructs an `Invoice` from a `SignedRawInvoice` by checking all its invariants.
89          * ```
90          * use lightning_invoice::*;
91          * 
92          * let invoice = \"lnbc100p1psj9jhxdqud3jxktt5w46x7unfv9kz6mn0v3jsnp4q0d3p2sfluzdx45tqcs\\
93          * h2pu5qc7lgq0xs578ngs6s0s68ua4h7cvspp5q6rmq35js88zp5dvwrv9m459tnk2zunwj5jalqtyxqulh0l\\
94          * 5gflssp5nf55ny5gcrfl30xuhzj3nphgj27rstekmr9fw3ny5989s300gyus9qyysgqcqpcrzjqw2sxwe993\\
95          * h5pcm4dxzpvttgza8zhkqxpgffcrf5v25nwpr3cmfg7z54kuqq8rgqqqqqqqq2qqqqq9qq9qrzjqd0ylaqcl\\
96          * j9424x9m8h2vcukcgnm6s56xfgu3j78zyqzhgs4hlpzvznlugqq9vsqqqqqqqlgqqqqqeqq9qrzjqwldmj9d\\
97          * ha74df76zhx6l9we0vjdquygcdt3kssupehe64g6yyp5yz5rhuqqwccqqyqqqqlgqqqqjcqq9qrzjqf9e58a\\
98          * guqr0rcun0ajlvmzq3ek63cw2w282gv3z5uupmuwvgjtq2z55qsqqg6qqqyqqqrtnqqqzq3cqygrzjqvphms\\
99          * ywntrrhqjcraumvc4y6r8v4z5v593trte429v4hredj7ms5z52usqq9ngqqqqqqqlgqqqqqqgq9qrzjq2v0v\\
100          * p62g49p7569ev48cmulecsxe59lvaw3wlxm7r982zxa9zzj7z5l0cqqxusqqyqqqqlgqqqqqzsqygarl9fh3\\
101          * 8s0gyuxjjgux34w75dnc6xp2l35j7es3jd4ugt3lu0xzre26yg5m7ke54n2d5sym4xcmxtl8238xxvw5h5h5\\
102          * j5r6drg6k6zcqj0fcwg\";
103          * 
104          * let signed = invoice.parse::<SignedRawInvoice>().unwrap();
105          * 
106          * assert!(Invoice::from_signed(signed).is_ok());
107          * ```
108          */
109         public static Result_InvoiceSemanticErrorZ from_signed(SignedRawInvoice signed_invoice) {
110                 long ret = bindings.Invoice_from_signed(signed_invoice == null ? 0 : signed_invoice.ptr & ~1);
111                 Reference.reachabilityFence(signed_invoice);
112                 if (ret >= 0 && ret <= 4096) { return null; }
113                 Result_InvoiceSemanticErrorZ ret_hu_conv = Result_InvoiceSemanticErrorZ.constr_from_ptr(ret);
114                 return ret_hu_conv;
115         }
116
117         /**
118          * Returns the `Invoice`'s timestamp (should equal it's creation time)
119          */
120         public long timestamp() {
121                 long ret = bindings.Invoice_timestamp(this.ptr);
122                 Reference.reachabilityFence(this);
123                 return ret;
124         }
125
126         /**
127          * Returns the hash to which we will receive the preimage on completion of the payment
128          */
129         public byte[] payment_hash() {
130                 byte[] ret = bindings.Invoice_payment_hash(this.ptr);
131                 Reference.reachabilityFence(this);
132                 return ret;
133         }
134
135         /**
136          * Get the payee's public key if one was included in the invoice
137          * 
138          * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
139          */
140         @Nullable
141         public byte[] payee_pub_key() {
142                 byte[] ret = bindings.Invoice_payee_pub_key(this.ptr);
143                 Reference.reachabilityFence(this);
144                 return ret;
145         }
146
147         /**
148          * Get the payment secret if one was included in the invoice
149          */
150         public byte[] payment_secret() {
151                 byte[] ret = bindings.Invoice_payment_secret(this.ptr);
152                 Reference.reachabilityFence(this);
153                 return ret;
154         }
155
156         /**
157          * Get the invoice features if they were included in the invoice
158          * 
159          * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
160          */
161         @Nullable
162         public InvoiceFeatures features() {
163                 long ret = bindings.Invoice_features(this.ptr);
164                 Reference.reachabilityFence(this);
165                 if (ret >= 0 && ret <= 4096) { return null; }
166                 InvoiceFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new InvoiceFeatures(null, ret); }
167                 ret_hu_conv.ptrs_to.add(this);
168                 return ret_hu_conv;
169         }
170
171         /**
172          * Recover the payee's public key (only to be used if none was included in the invoice)
173          */
174         public byte[] recover_payee_pub_key() {
175                 byte[] ret = bindings.Invoice_recover_payee_pub_key(this.ptr);
176                 Reference.reachabilityFence(this);
177                 return ret;
178         }
179
180         /**
181          * Returns the invoice's expiry time, if present, otherwise [`DEFAULT_EXPIRY_TIME`].
182          */
183         public long expiry_time() {
184                 long ret = bindings.Invoice_expiry_time(this.ptr);
185                 Reference.reachabilityFence(this);
186                 return ret;
187         }
188
189         /**
190          * Returns whether the invoice has expired.
191          */
192         public boolean is_expired() {
193                 boolean ret = bindings.Invoice_is_expired(this.ptr);
194                 Reference.reachabilityFence(this);
195                 return ret;
196         }
197
198         /**
199          * Returns the invoice's `min_final_cltv_expiry` time, if present, otherwise
200          * [`DEFAULT_MIN_FINAL_CLTV_EXPIRY`].
201          */
202         public long min_final_cltv_expiry() {
203                 long ret = bindings.Invoice_min_final_cltv_expiry(this.ptr);
204                 Reference.reachabilityFence(this);
205                 return ret;
206         }
207
208         /**
209          * Returns a list of all routes included in the invoice
210          */
211         public PrivateRoute[] private_routes() {
212                 long[] ret = bindings.Invoice_private_routes(this.ptr);
213                 Reference.reachabilityFence(this);
214                 int ret_conv_14_len = ret.length;
215                 PrivateRoute[] ret_conv_14_arr = new PrivateRoute[ret_conv_14_len];
216                 for (int o = 0; o < ret_conv_14_len; o++) {
217                         long ret_conv_14 = ret[o];
218                         PrivateRoute ret_conv_14_hu_conv = null; if (ret_conv_14 < 0 || ret_conv_14 > 4096) { ret_conv_14_hu_conv = new PrivateRoute(null, ret_conv_14); }
219                         ret_conv_14_hu_conv.ptrs_to.add(this);
220                         ret_conv_14_arr[o] = ret_conv_14_hu_conv;
221                 }
222                 return ret_conv_14_arr;
223         }
224
225         /**
226          * Returns a list of all routes included in the invoice as the underlying hints
227          */
228         public RouteHint[] route_hints() {
229                 long[] ret = bindings.Invoice_route_hints(this.ptr);
230                 Reference.reachabilityFence(this);
231                 int ret_conv_11_len = ret.length;
232                 RouteHint[] ret_conv_11_arr = new RouteHint[ret_conv_11_len];
233                 for (int l = 0; l < ret_conv_11_len; l++) {
234                         long ret_conv_11 = ret[l];
235                         RouteHint ret_conv_11_hu_conv = null; if (ret_conv_11 < 0 || ret_conv_11 > 4096) { ret_conv_11_hu_conv = new RouteHint(null, ret_conv_11); }
236                         ret_conv_11_hu_conv.ptrs_to.add(this);
237                         ret_conv_11_arr[l] = ret_conv_11_hu_conv;
238                 }
239                 return ret_conv_11_arr;
240         }
241
242         /**
243          * Returns the currency for which the invoice was issued
244          */
245         public Currency currency() {
246                 Currency ret = bindings.Invoice_currency(this.ptr);
247                 Reference.reachabilityFence(this);
248                 return ret;
249         }
250
251         /**
252          * Returns the amount if specified in the invoice as millisatoshis.
253          */
254         public Option_u64Z amount_milli_satoshis() {
255                 long ret = bindings.Invoice_amount_milli_satoshis(this.ptr);
256                 Reference.reachabilityFence(this);
257                 if (ret >= 0 && ret <= 4096) { return null; }
258                 Option_u64Z ret_hu_conv = Option_u64Z.constr_from_ptr(ret);
259                 ret_hu_conv.ptrs_to.add(this);
260                 return ret_hu_conv;
261         }
262
263         /**
264          * Read a Invoice object from a string
265          */
266         public static Result_InvoiceNoneZ from_str(java.lang.String s) {
267                 long ret = bindings.Invoice_from_str(s);
268                 Reference.reachabilityFence(s);
269                 if (ret >= 0 && ret <= 4096) { return null; }
270                 Result_InvoiceNoneZ ret_hu_conv = Result_InvoiceNoneZ.constr_from_ptr(ret);
271                 return ret_hu_conv;
272         }
273
274         /**
275          * Get the string representation of a Invoice object
276          */
277         public String to_str() {
278                 String ret = bindings.Invoice_to_str(this.ptr);
279                 Reference.reachabilityFence(this);
280                 return ret;
281         }
282
283 }