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
33 public final static class InvoicePayment extends PaymentPurpose {
35 * The preimage to the payment_hash, if the payment hash (and secret) were fetched via
36 * [`ChannelManager::create_inbound_payment`]. If provided, this can be handed directly to
37 * [`ChannelManager::claim_funds`].
39 * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
40 * [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
42 * Note that this (or a relevant inner pointer) may be NULL or all-0s to represent None
44 @Nullable public final byte[] payment_preimage;
46 * The \"payment secret\". This authenticates the sender to the recipient, preventing a
47 * number of deanonymization attacks during the routing process.
48 * It is provided here for your reference, however its accuracy is enforced directly by
49 * [`ChannelManager`] using the values you previously provided to
50 * [`ChannelManager::create_inbound_payment`] or
51 * [`ChannelManager::create_inbound_payment_for_hash`].
53 * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
54 * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
55 * [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
57 public final byte[] payment_secret;
59 * This is the `user_payment_id` which was provided to
60 * [`ChannelManager::create_inbound_payment_for_hash`] or
61 * [`ChannelManager::create_inbound_payment`]. It has no meaning inside of LDK and is
62 * simply copied here. It may be used to correlate PaymentReceived events with invoice
63 * metadata stored elsewhere.
65 * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
66 * [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
68 public final long user_payment_id;
69 private InvoicePayment(long ptr, bindings.LDKPaymentPurpose.InvoicePayment obj) {
71 this.payment_preimage = obj.payment_preimage;
72 this.payment_secret = obj.payment_secret;
73 this.user_payment_id = obj.user_payment_id;
76 public final static class SpontaneousPayment extends PaymentPurpose {
77 public final byte[] spontaneous_payment;
78 private SpontaneousPayment(long ptr, bindings.LDKPaymentPurpose.SpontaneousPayment obj) {
80 this.spontaneous_payment = obj.spontaneous_payment;
84 * Creates a copy of the PaymentPurpose
86 public PaymentPurpose clone() {
87 long ret = bindings.PaymentPurpose_clone(this.ptr);
88 if (ret < 1024) { return null; }
89 PaymentPurpose ret_hu_conv = PaymentPurpose.constr_from_ptr(ret);
90 ret_hu_conv.ptrs_to.add(this);
95 * Utility method to constructs a new InvoicePayment-variant PaymentPurpose
97 public static PaymentPurpose invoice_payment(byte[] payment_preimage, byte[] payment_secret, long user_payment_id) {
98 long ret = bindings.PaymentPurpose_invoice_payment(payment_preimage, payment_secret, user_payment_id);
99 if (ret < 1024) { return null; }
100 PaymentPurpose ret_hu_conv = PaymentPurpose.constr_from_ptr(ret);
101 ret_hu_conv.ptrs_to.add(ret_hu_conv);
106 * Utility method to constructs a new SpontaneousPayment-variant PaymentPurpose
108 public static PaymentPurpose spontaneous_payment(byte[] a) {
109 long ret = bindings.PaymentPurpose_spontaneous_payment(a);
110 if (ret < 1024) { return null; }
111 PaymentPurpose ret_hu_conv = PaymentPurpose.constr_from_ptr(ret);
112 ret_hu_conv.ptrs_to.add(ret_hu_conv);