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