1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
12 * Some information provided on receipt of payment depends on whether the payment received is a
13 * spontaneous payment or a \"conventional\" lightning payment that's paying an invoice.
15 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
16 public class PaymentPurpose extends CommonBase {
17 private PaymentPurpose(Object _dummy, long ptr) { super(ptr); }
18 @Override @SuppressWarnings("deprecation")
19 protected void finalize() throws Throwable {
21 if (ptr != 0) { bindings.PaymentPurpose_free(ptr); }
23 static PaymentPurpose constr_from_ptr(long ptr) {
24 bindings.LDKPaymentPurpose raw_val = bindings.LDKPaymentPurpose_ref_from_ptr(ptr);
25 if (raw_val.getClass() == bindings.LDKPaymentPurpose.Bolt11InvoicePayment.class) {
26 return new Bolt11InvoicePayment(ptr, (bindings.LDKPaymentPurpose.Bolt11InvoicePayment)raw_val);
28 if (raw_val.getClass() == bindings.LDKPaymentPurpose.Bolt12OfferPayment.class) {
29 return new Bolt12OfferPayment(ptr, (bindings.LDKPaymentPurpose.Bolt12OfferPayment)raw_val);
31 if (raw_val.getClass() == bindings.LDKPaymentPurpose.Bolt12RefundPayment.class) {
32 return new Bolt12RefundPayment(ptr, (bindings.LDKPaymentPurpose.Bolt12RefundPayment)raw_val);
34 if (raw_val.getClass() == bindings.LDKPaymentPurpose.SpontaneousPayment.class) {
35 return new SpontaneousPayment(ptr, (bindings.LDKPaymentPurpose.SpontaneousPayment)raw_val);
37 assert false; return null; // Unreachable without extending the (internal) bindings interface
41 * A payment for a BOLT 11 invoice.
43 public final static class Bolt11InvoicePayment extends PaymentPurpose {
45 * The preimage to the payment_hash, if the payment hash (and secret) were fetched via
46 * [`ChannelManager::create_inbound_payment`]. When handling [`Event::PaymentClaimable`],
47 * this can be passed directly to [`ChannelManager::claim_funds`] to claim the payment. No
48 * action is needed when seen in [`Event::PaymentClaimed`].
50 * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
51 * [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
53 public final org.ldk.structs.Option_ThirtyTwoBytesZ payment_preimage;
55 * The \"payment secret\". This authenticates the sender to the recipient, preventing a
56 * number of deanonymization attacks during the routing process.
57 * It is provided here for your reference, however its accuracy is enforced directly by
58 * [`ChannelManager`] using the values you previously provided to
59 * [`ChannelManager::create_inbound_payment`] or
60 * [`ChannelManager::create_inbound_payment_for_hash`].
62 * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
63 * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
64 * [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
66 public final byte[] payment_secret;
67 private Bolt11InvoicePayment(long ptr, bindings.LDKPaymentPurpose.Bolt11InvoicePayment obj) {
69 long payment_preimage = obj.payment_preimage;
70 org.ldk.structs.Option_ThirtyTwoBytesZ payment_preimage_hu_conv = org.ldk.structs.Option_ThirtyTwoBytesZ.constr_from_ptr(payment_preimage);
71 if (payment_preimage_hu_conv != null) { payment_preimage_hu_conv.ptrs_to.add(this); };
72 this.payment_preimage = payment_preimage_hu_conv;
73 this.payment_secret = obj.payment_secret;
77 * A payment for a BOLT 12 [`Offer`].
79 * [`Offer`]: crate::offers::offer::Offer
81 public final static class Bolt12OfferPayment extends PaymentPurpose {
83 * The preimage to the payment hash. When handling [`Event::PaymentClaimable`], this can be
84 * passed directly to [`ChannelManager::claim_funds`], if provided. No action is needed
85 * when seen in [`Event::PaymentClaimed`].
87 * [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
89 public final org.ldk.structs.Option_ThirtyTwoBytesZ payment_preimage;
91 * The secret used to authenticate the sender to the recipient, preventing a number of
92 * de-anonymization attacks while routing a payment.
94 * See [`PaymentPurpose::Bolt11InvoicePayment::payment_secret`] for further details.
96 public final byte[] payment_secret;
98 * The context of the payment such as information about the corresponding [`Offer`] and
101 * [`Offer`]: crate::offers::offer::Offer
102 * [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
104 public final org.ldk.structs.Bolt12OfferContext payment_context;
105 private Bolt12OfferPayment(long ptr, bindings.LDKPaymentPurpose.Bolt12OfferPayment obj) {
107 long payment_preimage = obj.payment_preimage;
108 org.ldk.structs.Option_ThirtyTwoBytesZ payment_preimage_hu_conv = org.ldk.structs.Option_ThirtyTwoBytesZ.constr_from_ptr(payment_preimage);
109 if (payment_preimage_hu_conv != null) { payment_preimage_hu_conv.ptrs_to.add(this); };
110 this.payment_preimage = payment_preimage_hu_conv;
111 this.payment_secret = obj.payment_secret;
112 long payment_context = obj.payment_context;
113 org.ldk.structs.Bolt12OfferContext payment_context_hu_conv = null; if (payment_context < 0 || payment_context > 4096) { payment_context_hu_conv = new org.ldk.structs.Bolt12OfferContext(null, payment_context); }
114 if (payment_context_hu_conv != null) { payment_context_hu_conv.ptrs_to.add(this); };
115 this.payment_context = payment_context_hu_conv;
119 * A payment for a BOLT 12 [`Refund`].
121 * [`Refund`]: crate::offers::refund::Refund
123 public final static class Bolt12RefundPayment extends PaymentPurpose {
125 * The preimage to the payment hash. When handling [`Event::PaymentClaimable`], this can be
126 * passed directly to [`ChannelManager::claim_funds`], if provided. No action is needed
127 * when seen in [`Event::PaymentClaimed`].
129 * [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
131 public final org.ldk.structs.Option_ThirtyTwoBytesZ payment_preimage;
133 * The secret used to authenticate the sender to the recipient, preventing a number of
134 * de-anonymization attacks while routing a payment.
136 * See [`PaymentPurpose::Bolt11InvoicePayment::payment_secret`] for further details.
138 public final byte[] payment_secret;
140 * The context of the payment such as information about the corresponding [`Refund`].
142 * [`Refund`]: crate::offers::refund::Refund
144 public final org.ldk.structs.Bolt12RefundContext payment_context;
145 private Bolt12RefundPayment(long ptr, bindings.LDKPaymentPurpose.Bolt12RefundPayment obj) {
147 long payment_preimage = obj.payment_preimage;
148 org.ldk.structs.Option_ThirtyTwoBytesZ payment_preimage_hu_conv = org.ldk.structs.Option_ThirtyTwoBytesZ.constr_from_ptr(payment_preimage);
149 if (payment_preimage_hu_conv != null) { payment_preimage_hu_conv.ptrs_to.add(this); };
150 this.payment_preimage = payment_preimage_hu_conv;
151 this.payment_secret = obj.payment_secret;
152 long payment_context = obj.payment_context;
153 org.ldk.structs.Bolt12RefundContext payment_context_hu_conv = null; if (payment_context < 0 || payment_context > 4096) { payment_context_hu_conv = new org.ldk.structs.Bolt12RefundContext(null, payment_context); }
154 if (payment_context_hu_conv != null) { payment_context_hu_conv.ptrs_to.add(this); };
155 this.payment_context = payment_context_hu_conv;
159 * Because this is a spontaneous payment, the payer generated their own preimage rather than us
160 * (the payee) providing a preimage.
162 public final static class SpontaneousPayment extends PaymentPurpose {
163 public final byte[] spontaneous_payment;
164 private SpontaneousPayment(long ptr, bindings.LDKPaymentPurpose.SpontaneousPayment obj) {
166 this.spontaneous_payment = obj.spontaneous_payment;
170 long ret = bindings.PaymentPurpose_clone_ptr(this.ptr);
171 Reference.reachabilityFence(this);
176 * Creates a copy of the PaymentPurpose
178 public PaymentPurpose clone() {
179 long ret = bindings.PaymentPurpose_clone(this.ptr);
180 Reference.reachabilityFence(this);
181 if (ret >= 0 && ret <= 4096) { return null; }
182 org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret);
183 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
188 * Utility method to constructs a new Bolt11InvoicePayment-variant PaymentPurpose
190 public static PaymentPurpose bolt11_invoice_payment(org.ldk.structs.Option_ThirtyTwoBytesZ payment_preimage, byte[] payment_secret) {
191 long ret = bindings.PaymentPurpose_bolt11_invoice_payment(payment_preimage.ptr, InternalUtils.check_arr_len(payment_secret, 32));
192 Reference.reachabilityFence(payment_preimage);
193 Reference.reachabilityFence(payment_secret);
194 if (ret >= 0 && ret <= 4096) { return null; }
195 org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret);
196 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
197 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(payment_preimage); };
202 * Utility method to constructs a new Bolt12OfferPayment-variant PaymentPurpose
204 public static PaymentPurpose bolt12_offer_payment(org.ldk.structs.Option_ThirtyTwoBytesZ payment_preimage, byte[] payment_secret, org.ldk.structs.Bolt12OfferContext payment_context) {
205 long ret = bindings.PaymentPurpose_bolt12_offer_payment(payment_preimage.ptr, InternalUtils.check_arr_len(payment_secret, 32), payment_context.ptr);
206 Reference.reachabilityFence(payment_preimage);
207 Reference.reachabilityFence(payment_secret);
208 Reference.reachabilityFence(payment_context);
209 if (ret >= 0 && ret <= 4096) { return null; }
210 org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret);
211 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
212 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(payment_preimage); };
213 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(payment_context); };
218 * Utility method to constructs a new Bolt12RefundPayment-variant PaymentPurpose
220 public static PaymentPurpose bolt12_refund_payment(org.ldk.structs.Option_ThirtyTwoBytesZ payment_preimage, byte[] payment_secret, org.ldk.structs.Bolt12RefundContext payment_context) {
221 long ret = bindings.PaymentPurpose_bolt12_refund_payment(payment_preimage.ptr, InternalUtils.check_arr_len(payment_secret, 32), payment_context.ptr);
222 Reference.reachabilityFence(payment_preimage);
223 Reference.reachabilityFence(payment_secret);
224 Reference.reachabilityFence(payment_context);
225 if (ret >= 0 && ret <= 4096) { return null; }
226 org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret);
227 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
228 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(payment_preimage); };
229 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(payment_context); };
234 * Utility method to constructs a new SpontaneousPayment-variant PaymentPurpose
236 public static PaymentPurpose spontaneous_payment(byte[] a) {
237 long ret = bindings.PaymentPurpose_spontaneous_payment(InternalUtils.check_arr_len(a, 32));
238 Reference.reachabilityFence(a);
239 if (ret >= 0 && ret <= 4096) { return null; }
240 org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret);
241 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
246 * Checks if two PaymentPurposes contain equal inner contents.
247 * This ignores pointers and is_owned flags and looks at the values in fields.
249 public boolean eq(org.ldk.structs.PaymentPurpose b) {
250 boolean ret = bindings.PaymentPurpose_eq(this.ptr, b.ptr);
251 Reference.reachabilityFence(this);
252 Reference.reachabilityFence(b);
256 @Override public boolean equals(Object o) {
257 if (!(o instanceof PaymentPurpose)) return false;
258 return this.eq((PaymentPurpose)o);
261 * Returns the preimage for this payment, if it is known.
263 public Option_ThirtyTwoBytesZ preimage() {
264 long ret = bindings.PaymentPurpose_preimage(this.ptr);
265 Reference.reachabilityFence(this);
266 if (ret >= 0 && ret <= 4096) { return null; }
267 org.ldk.structs.Option_ThirtyTwoBytesZ ret_hu_conv = org.ldk.structs.Option_ThirtyTwoBytesZ.constr_from_ptr(ret);
268 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
273 * Serialize the PaymentPurpose object into a byte array which can be read by PaymentPurpose_read
275 public byte[] write() {
276 byte[] ret = bindings.PaymentPurpose_write(this.ptr);
277 Reference.reachabilityFence(this);
282 * Read a PaymentPurpose from a byte array, created by PaymentPurpose_write
284 public static Result_PaymentPurposeDecodeErrorZ read(byte[] ser) {
285 long ret = bindings.PaymentPurpose_read(ser);
286 Reference.reachabilityFence(ser);
287 if (ret >= 0 && ret <= 4096) { return null; }
288 Result_PaymentPurposeDecodeErrorZ ret_hu_conv = Result_PaymentPurposeDecodeErrorZ.constr_from_ptr(ret);