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 * Note that this (or a relevant inner pointer) may be NULL or all-0s to represent None
48 @Nullable public final byte[] payment_preimage;
50 * The \"payment secret\". This authenticates the sender to the recipient, preventing a
51 * number of deanonymization attacks during the routing process.
52 * It is provided here for your reference, however its accuracy is enforced directly by
53 * [`ChannelManager`] using the values you previously provided to
54 * [`ChannelManager::create_inbound_payment`] or
55 * [`ChannelManager::create_inbound_payment_for_hash`].
57 * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
58 * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
59 * [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
61 public final byte[] payment_secret;
62 private InvoicePayment(long ptr, bindings.LDKPaymentPurpose.InvoicePayment obj) {
64 this.payment_preimage = obj.payment_preimage;
65 this.payment_secret = obj.payment_secret;
69 * Because this is a spontaneous payment, the payer generated their own preimage rather than us
70 * (the payee) providing a preimage.
72 public final static class SpontaneousPayment extends PaymentPurpose {
73 public final byte[] spontaneous_payment;
74 private SpontaneousPayment(long ptr, bindings.LDKPaymentPurpose.SpontaneousPayment obj) {
76 this.spontaneous_payment = obj.spontaneous_payment;
80 long ret = bindings.PaymentPurpose_clone_ptr(this.ptr);
81 Reference.reachabilityFence(this);
86 * Creates a copy of the PaymentPurpose
88 public PaymentPurpose clone() {
89 long ret = bindings.PaymentPurpose_clone(this.ptr);
90 Reference.reachabilityFence(this);
91 if (ret >= 0 && ret <= 4096) { return null; }
92 org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret);
93 ret_hu_conv.ptrs_to.add(this);
98 * Utility method to constructs a new InvoicePayment-variant PaymentPurpose
100 public static PaymentPurpose invoice_payment(byte[] payment_preimage, byte[] payment_secret) {
101 long ret = bindings.PaymentPurpose_invoice_payment(InternalUtils.check_arr_len(payment_preimage, 32), InternalUtils.check_arr_len(payment_secret, 32));
102 Reference.reachabilityFence(payment_preimage);
103 Reference.reachabilityFence(payment_secret);
104 if (ret >= 0 && ret <= 4096) { return null; }
105 org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret);
106 ret_hu_conv.ptrs_to.add(ret_hu_conv);
111 * Utility method to constructs a new SpontaneousPayment-variant PaymentPurpose
113 public static PaymentPurpose spontaneous_payment(byte[] a) {
114 long ret = bindings.PaymentPurpose_spontaneous_payment(InternalUtils.check_arr_len(a, 32));
115 Reference.reachabilityFence(a);
116 if (ret >= 0 && ret <= 4096) { return null; }
117 org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret);
118 ret_hu_conv.ptrs_to.add(ret_hu_conv);
123 * Serialize the PaymentPurpose object into a byte array which can be read by PaymentPurpose_read
125 public byte[] write() {
126 byte[] ret = bindings.PaymentPurpose_write(this.ptr);
127 Reference.reachabilityFence(this);
132 * Read a PaymentPurpose from a byte array, created by PaymentPurpose_write
134 public static Result_PaymentPurposeDecodeErrorZ read(byte[] ser) {
135 long ret = bindings.PaymentPurpose_read(ser);
136 Reference.reachabilityFence(ser);
137 if (ret >= 0 && ret <= 4096) { return null; }
138 Result_PaymentPurposeDecodeErrorZ ret_hu_conv = Result_PaymentPurposeDecodeErrorZ.constr_from_ptr(ret);