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.InvoicePayment.class) {
26 return new InvoicePayment(ptr, (bindings.LDKPaymentPurpose.InvoicePayment)raw_val);
28 if (raw_val.getClass() == bindings.LDKPaymentPurpose.SpontaneousPayment.class) {
29 return new SpontaneousPayment(ptr, (bindings.LDKPaymentPurpose.SpontaneousPayment)raw_val);
31 assert false; return null; // Unreachable without extending the (internal) bindings interface
35 * Information for receiving a payment that we generated an invoice for.
37 public final static class InvoicePayment extends PaymentPurpose {
39 * The preimage to the payment_hash, if the payment hash (and secret) were fetched via
40 * [`ChannelManager::create_inbound_payment`]. If provided, this can be handed directly to
41 * [`ChannelManager::claim_funds`].
43 * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
44 * [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
46 public final org.ldk.structs.Option_ThirtyTwoBytesZ payment_preimage;
48 * The \"payment secret\". This authenticates the sender to the recipient, preventing a
49 * number of deanonymization attacks during the routing process.
50 * It is provided here for your reference, however its accuracy is enforced directly by
51 * [`ChannelManager`] using the values you previously provided to
52 * [`ChannelManager::create_inbound_payment`] or
53 * [`ChannelManager::create_inbound_payment_for_hash`].
55 * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
56 * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
57 * [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
59 public final byte[] payment_secret;
60 private InvoicePayment(long ptr, bindings.LDKPaymentPurpose.InvoicePayment obj) {
62 long payment_preimage = obj.payment_preimage;
63 org.ldk.structs.Option_ThirtyTwoBytesZ payment_preimage_hu_conv = org.ldk.structs.Option_ThirtyTwoBytesZ.constr_from_ptr(payment_preimage);
64 if (payment_preimage_hu_conv != null) { payment_preimage_hu_conv.ptrs_to.add(this); };
65 this.payment_preimage = payment_preimage_hu_conv;
66 this.payment_secret = obj.payment_secret;
70 * Because this is a spontaneous payment, the payer generated their own preimage rather than us
71 * (the payee) providing a preimage.
73 public final static class SpontaneousPayment extends PaymentPurpose {
74 public final byte[] spontaneous_payment;
75 private SpontaneousPayment(long ptr, bindings.LDKPaymentPurpose.SpontaneousPayment obj) {
77 this.spontaneous_payment = obj.spontaneous_payment;
81 long ret = bindings.PaymentPurpose_clone_ptr(this.ptr);
82 Reference.reachabilityFence(this);
87 * Creates a copy of the PaymentPurpose
89 public PaymentPurpose clone() {
90 long ret = bindings.PaymentPurpose_clone(this.ptr);
91 Reference.reachabilityFence(this);
92 if (ret >= 0 && ret <= 4096) { return null; }
93 org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret);
94 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
99 * Utility method to constructs a new InvoicePayment-variant PaymentPurpose
101 public static PaymentPurpose invoice_payment(org.ldk.structs.Option_ThirtyTwoBytesZ payment_preimage, byte[] payment_secret) {
102 long ret = bindings.PaymentPurpose_invoice_payment(payment_preimage.ptr, InternalUtils.check_arr_len(payment_secret, 32));
103 Reference.reachabilityFence(payment_preimage);
104 Reference.reachabilityFence(payment_secret);
105 if (ret >= 0 && ret <= 4096) { return null; }
106 org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret);
107 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
108 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(payment_preimage); };
113 * Utility method to constructs a new SpontaneousPayment-variant PaymentPurpose
115 public static PaymentPurpose spontaneous_payment(byte[] a) {
116 long ret = bindings.PaymentPurpose_spontaneous_payment(InternalUtils.check_arr_len(a, 32));
117 Reference.reachabilityFence(a);
118 if (ret >= 0 && ret <= 4096) { return null; }
119 org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret);
120 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
125 * Checks if two PaymentPurposes contain equal inner contents.
126 * This ignores pointers and is_owned flags and looks at the values in fields.
128 public boolean eq(org.ldk.structs.PaymentPurpose b) {
129 boolean ret = bindings.PaymentPurpose_eq(this.ptr, b == null ? 0 : b.ptr);
130 Reference.reachabilityFence(this);
131 Reference.reachabilityFence(b);
135 @Override public boolean equals(Object o) {
136 if (!(o instanceof PaymentPurpose)) return false;
137 return this.eq((PaymentPurpose)o);
140 * Serialize the PaymentPurpose object into a byte array which can be read by PaymentPurpose_read
142 public byte[] write() {
143 byte[] ret = bindings.PaymentPurpose_write(this.ptr);
144 Reference.reachabilityFence(this);
149 * Read a PaymentPurpose from a byte array, created by PaymentPurpose_write
151 public static Result_PaymentPurposeDecodeErrorZ read(byte[] ser) {
152 long ret = bindings.PaymentPurpose_read(ser);
153 Reference.reachabilityFence(ser);
154 if (ret >= 0 && ret <= 4096) { return null; }
155 Result_PaymentPurposeDecodeErrorZ ret_hu_conv = Result_PaymentPurposeDecodeErrorZ.constr_from_ptr(ret);