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 * Some information provided on receipt of payment depends on whether the payment received is a
12 * spontaneous payment or a \"conventional\" lightning payment that's paying an invoice.
14 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
15 public class PaymentPurpose extends CommonBase {
16 private PaymentPurpose(Object _dummy, long ptr) { super(ptr); }
17 @Override @SuppressWarnings("deprecation")
18 protected void finalize() throws Throwable {
20 if (ptr != 0) { bindings.PaymentPurpose_free(ptr); }
22 static PaymentPurpose constr_from_ptr(long ptr) {
23 bindings.LDKPaymentPurpose raw_val = bindings.LDKPaymentPurpose_ref_from_ptr(ptr);
24 if (raw_val.getClass() == bindings.LDKPaymentPurpose.InvoicePayment.class) {
25 return new InvoicePayment(ptr, (bindings.LDKPaymentPurpose.InvoicePayment)raw_val);
27 if (raw_val.getClass() == bindings.LDKPaymentPurpose.SpontaneousPayment.class) {
28 return new SpontaneousPayment(ptr, (bindings.LDKPaymentPurpose.SpontaneousPayment)raw_val);
30 assert false; return null; // Unreachable without extending the (internal) bindings interface
34 * Information for receiving a payment that we generated an invoice for.
36 public final static class InvoicePayment extends PaymentPurpose {
38 * The preimage to the payment_hash, if the payment hash (and secret) were fetched via
39 * [`ChannelManager::create_inbound_payment`]. If provided, this can be handed directly to
40 * [`ChannelManager::claim_funds`].
42 * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
43 * [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
45 * Note that this (or a relevant inner pointer) may be NULL or all-0s to represent None
47 @Nullable public final byte[] payment_preimage;
49 * The \"payment secret\". This authenticates the sender to the recipient, preventing a
50 * number of deanonymization attacks during the routing process.
51 * It is provided here for your reference, however its accuracy is enforced directly by
52 * [`ChannelManager`] using the values you previously provided to
53 * [`ChannelManager::create_inbound_payment`] or
54 * [`ChannelManager::create_inbound_payment_for_hash`].
56 * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
57 * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
58 * [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
60 public final byte[] payment_secret;
62 * This is the `user_payment_id` which was provided to
63 * [`ChannelManager::create_inbound_payment_for_hash`] or
64 * [`ChannelManager::create_inbound_payment`]. It has no meaning inside of LDK and is
65 * simply copied here. It may be used to correlate PaymentReceived events with invoice
66 * metadata stored elsewhere.
68 * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
69 * [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
71 public final long user_payment_id;
72 private InvoicePayment(long ptr, bindings.LDKPaymentPurpose.InvoicePayment obj) {
74 this.payment_preimage = obj.payment_preimage;
75 this.payment_secret = obj.payment_secret;
76 this.user_payment_id = obj.user_payment_id;
80 * Because this is a spontaneous payment, the payer generated their own preimage rather than us
81 * (the payee) providing a preimage.
83 public final static class SpontaneousPayment extends PaymentPurpose {
84 public final byte[] spontaneous_payment;
85 private SpontaneousPayment(long ptr, bindings.LDKPaymentPurpose.SpontaneousPayment obj) {
87 this.spontaneous_payment = obj.spontaneous_payment;
91 long ret = bindings.PaymentPurpose_clone_ptr(this.ptr);
96 * Creates a copy of the PaymentPurpose
98 public PaymentPurpose clone() {
99 long ret = bindings.PaymentPurpose_clone(this.ptr);
100 if (ret >= 0 && ret <= 4096) { return null; }
101 PaymentPurpose ret_hu_conv = PaymentPurpose.constr_from_ptr(ret);
102 ret_hu_conv.ptrs_to.add(this);
107 * Utility method to constructs a new InvoicePayment-variant PaymentPurpose
109 public static PaymentPurpose invoice_payment(byte[] payment_preimage, byte[] payment_secret, long user_payment_id) {
110 long ret = bindings.PaymentPurpose_invoice_payment(InternalUtils.check_arr_len(payment_preimage, 32), InternalUtils.check_arr_len(payment_secret, 32), user_payment_id);
111 if (ret >= 0 && ret <= 4096) { return null; }
112 PaymentPurpose ret_hu_conv = PaymentPurpose.constr_from_ptr(ret);
113 ret_hu_conv.ptrs_to.add(ret_hu_conv);
118 * Utility method to constructs a new SpontaneousPayment-variant PaymentPurpose
120 public static PaymentPurpose spontaneous_payment(byte[] a) {
121 long ret = bindings.PaymentPurpose_spontaneous_payment(InternalUtils.check_arr_len(a, 32));
122 if (ret >= 0 && ret <= 4096) { return null; }
123 PaymentPurpose ret_hu_conv = PaymentPurpose.constr_from_ptr(ret);
124 ret_hu_conv.ptrs_to.add(ret_hu_conv);