1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
11 * Represents a syntactically and semantically correct lightning BOLT11 invoice.
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)`
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 {
24 if (ptr != 0) { bindings.Invoice_free(ptr); }
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.
32 public boolean eq(Invoice b) {
33 boolean ret = bindings.Invoice_eq(this.ptr, b == null ? 0 : b.ptr & ~1);
38 @Override public boolean equals(Object o) {
39 if (!(o instanceof Invoice)) return false;
40 return this.eq((Invoice)o);
43 long ret = bindings.Invoice_clone_ptr(this.ptr);
48 * Creates a copy of the Invoice
50 public Invoice clone() {
51 long ret = bindings.Invoice_clone(this.ptr);
52 if (ret >= 0 && ret <= 4096) { return null; }
53 Invoice ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new Invoice(null, ret); }
54 ret_hu_conv.ptrs_to.add(this);
59 * Transform the `Invoice` into it's unchecked version
61 public SignedRawInvoice into_signed_raw() {
62 long ret = bindings.Invoice_into_signed_raw(this.ptr);
63 if (ret >= 0 && ret <= 4096) { return null; }
64 SignedRawInvoice ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new SignedRawInvoice(null, ret); }
65 ret_hu_conv.ptrs_to.add(this);
71 * Check that the invoice is signed correctly and that key recovery works
73 public Result_NoneSemanticErrorZ check_signature() {
74 long ret = bindings.Invoice_check_signature(this.ptr);
75 if (ret >= 0 && ret <= 4096) { return null; }
76 Result_NoneSemanticErrorZ ret_hu_conv = Result_NoneSemanticErrorZ.constr_from_ptr(ret);
81 * Constructs an `Invoice` from a `SignedRawInvoice` by checking all its invariants.
83 * use lightning_invoice::*;
85 * let invoice = \"lnbc100p1psj9jhxdqud3jxktt5w46x7unfv9kz6mn0v3jsnp4q0d3p2sfluzdx45tqcs\\
86 * h2pu5qc7lgq0xs578ngs6s0s68ua4h7cvspp5q6rmq35js88zp5dvwrv9m459tnk2zunwj5jalqtyxqulh0l\\
87 * 5gflssp5nf55ny5gcrfl30xuhzj3nphgj27rstekmr9fw3ny5989s300gyus9qyysgqcqpcrzjqw2sxwe993\\
88 * h5pcm4dxzpvttgza8zhkqxpgffcrf5v25nwpr3cmfg7z54kuqq8rgqqqqqqqq2qqqqq9qq9qrzjqd0ylaqcl\\
89 * j9424x9m8h2vcukcgnm6s56xfgu3j78zyqzhgs4hlpzvznlugqq9vsqqqqqqqlgqqqqqeqq9qrzjqwldmj9d\\
90 * ha74df76zhx6l9we0vjdquygcdt3kssupehe64g6yyp5yz5rhuqqwccqqyqqqqlgqqqqjcqq9qrzjqf9e58a\\
91 * guqr0rcun0ajlvmzq3ek63cw2w282gv3z5uupmuwvgjtq2z55qsqqg6qqqyqqqrtnqqqzq3cqygrzjqvphms\\
92 * ywntrrhqjcraumvc4y6r8v4z5v593trte429v4hredj7ms5z52usqq9ngqqqqqqqlgqqqqqqgq9qrzjq2v0v\\
93 * p62g49p7569ev48cmulecsxe59lvaw3wlxm7r982zxa9zzj7z5l0cqqxusqqyqqqqlgqqqqqzsqygarl9fh3\\
94 * 8s0gyuxjjgux34w75dnc6xp2l35j7es3jd4ugt3lu0xzre26yg5m7ke54n2d5sym4xcmxtl8238xxvw5h5h5\\
95 * j5r6drg6k6zcqj0fcwg\";
97 * let signed = invoice.parse::<SignedRawInvoice>().unwrap();
99 * assert!(Invoice::from_signed(signed).is_ok());
102 public static Result_InvoiceSemanticErrorZ from_signed(SignedRawInvoice signed_invoice) {
103 long ret = bindings.Invoice_from_signed(signed_invoice == null ? 0 : signed_invoice.ptr & ~1);
104 if (ret >= 0 && ret <= 4096) { return null; }
105 Result_InvoiceSemanticErrorZ ret_hu_conv = Result_InvoiceSemanticErrorZ.constr_from_ptr(ret);
110 * Returns the `Invoice`'s timestamp (should equal it's creation time)
112 public long timestamp() {
113 long ret = bindings.Invoice_timestamp(this.ptr);
118 * Returns the hash to which we will receive the preimage on completion of the payment
120 public byte[] payment_hash() {
121 byte[] ret = bindings.Invoice_payment_hash(this.ptr);
126 * Get the payee's public key if one was included in the invoice
128 * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
131 public byte[] payee_pub_key() {
132 byte[] ret = bindings.Invoice_payee_pub_key(this.ptr);
137 * Get the payment secret if one was included in the invoice
139 public byte[] payment_secret() {
140 byte[] ret = bindings.Invoice_payment_secret(this.ptr);
145 * Get the invoice features if they were included in the invoice
147 * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
150 public InvoiceFeatures features() {
151 long ret = bindings.Invoice_features(this.ptr);
152 if (ret >= 0 && ret <= 4096) { return null; }
153 InvoiceFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new InvoiceFeatures(null, ret); }
154 ret_hu_conv.ptrs_to.add(this);
159 * Recover the payee's public key (only to be used if none was included in the invoice)
161 public byte[] recover_payee_pub_key() {
162 byte[] ret = bindings.Invoice_recover_payee_pub_key(this.ptr);
167 * Returns the invoice's expiry time, if present, otherwise [`DEFAULT_EXPIRY_TIME`].
169 public long expiry_time() {
170 long ret = bindings.Invoice_expiry_time(this.ptr);
175 * Returns whether the invoice has expired.
177 public boolean is_expired() {
178 boolean ret = bindings.Invoice_is_expired(this.ptr);
183 * Returns the invoice's `min_final_cltv_expiry` time, if present, otherwise
184 * [`DEFAULT_MIN_FINAL_CLTV_EXPIRY`].
186 public long min_final_cltv_expiry() {
187 long ret = bindings.Invoice_min_final_cltv_expiry(this.ptr);
192 * Returns a list of all routes included in the invoice
194 public PrivateRoute[] private_routes() {
195 long[] ret = bindings.Invoice_private_routes(this.ptr);
196 PrivateRoute[] ret_conv_14_arr = new PrivateRoute[ret.length];
197 for (int o = 0; o < ret.length; o++) {
198 long ret_conv_14 = ret[o];
199 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); }
200 ret_conv_14_hu_conv.ptrs_to.add(this);
201 ret_conv_14_arr[o] = ret_conv_14_hu_conv;
203 return ret_conv_14_arr;
207 * Returns a list of all routes included in the invoice as the underlying hints
209 public RouteHint[] route_hints() {
210 long[] ret = bindings.Invoice_route_hints(this.ptr);
211 RouteHint[] ret_conv_11_arr = new RouteHint[ret.length];
212 for (int l = 0; l < ret.length; l++) {
213 long ret_conv_11 = ret[l];
214 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); }
215 ret_conv_11_hu_conv.ptrs_to.add(this);
216 ret_conv_11_arr[l] = ret_conv_11_hu_conv;
218 return ret_conv_11_arr;
222 * Returns the currency for which the invoice was issued
224 public Currency currency() {
225 Currency ret = bindings.Invoice_currency(this.ptr);
230 * Returns the amount if specified in the invoice as millisatoshis.
232 public Option_u64Z amount_milli_satoshis() {
233 long ret = bindings.Invoice_amount_milli_satoshis(this.ptr);
234 if (ret >= 0 && ret <= 4096) { return null; }
235 Option_u64Z ret_hu_conv = Option_u64Z.constr_from_ptr(ret);
236 ret_hu_conv.ptrs_to.add(this);
241 * Read a Invoice object from a string
243 public static Result_InvoiceNoneZ from_str(java.lang.String s) {
244 long ret = bindings.Invoice_from_str(s);
245 if (ret >= 0 && ret <= 4096) { return null; }
246 Result_InvoiceNoneZ ret_hu_conv = Result_InvoiceNoneZ.constr_from_ptr(ret);
251 * Get the string representation of a Invoice object
253 public String to_str() {
254 String ret = bindings.Invoice_to_str(this.ptr);