X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fldk%2Fstructs%2FPaymentPurpose.java;h=7c83eec9c0a479ac0b33f258f292a5de11b4de1b;hb=5cdb34f643d09f21c147c9d0b2f8888609e43429;hp=e2654da5c5cd28d6eb8f800242f94e0a8e15919a;hpb=1d0c372339e4d0148449728072c95964088412c4;p=ldk-java diff --git a/src/main/java/org/ldk/structs/PaymentPurpose.java b/src/main/java/org/ldk/structs/PaymentPurpose.java index e2654da5..7c83eec9 100644 --- a/src/main/java/org/ldk/structs/PaymentPurpose.java +++ b/src/main/java/org/ldk/structs/PaymentPurpose.java @@ -4,6 +4,7 @@ import org.ldk.impl.bindings; import org.ldk.enums.*; import org.ldk.util.*; import java.util.Arrays; +import java.lang.ref.Reference; import javax.annotation.Nullable; @@ -30,6 +31,9 @@ public class PaymentPurpose extends CommonBase { assert false; return null; // Unreachable without extending the (internal) bindings interface } + /** + * Information for receiving a payment that we generated an invoice for. + */ public final static class InvoicePayment extends PaymentPurpose { /** * The preimage to the payment_hash, if the payment hash (and secret) were fetched via @@ -55,24 +59,16 @@ public class PaymentPurpose extends CommonBase { * [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash */ public final byte[] payment_secret; - /** - * This is the `user_payment_id` which was provided to - * [`ChannelManager::create_inbound_payment_for_hash`] or - * [`ChannelManager::create_inbound_payment`]. It has no meaning inside of LDK and is - * simply copied here. It may be used to correlate PaymentReceived events with invoice - * metadata stored elsewhere. - * - * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment - * [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash - */ - public final long user_payment_id; private InvoicePayment(long ptr, bindings.LDKPaymentPurpose.InvoicePayment obj) { super(null, ptr); this.payment_preimage = obj.payment_preimage; this.payment_secret = obj.payment_secret; - this.user_payment_id = obj.user_payment_id; } } + /** + * Because this is a spontaneous payment, the payer generated their own preimage rather than us + * (the payee) providing a preimage. + */ public final static class SpontaneousPayment extends PaymentPurpose { public final byte[] spontaneous_payment; private SpontaneousPayment(long ptr, bindings.LDKPaymentPurpose.SpontaneousPayment obj) { @@ -80,25 +76,34 @@ public class PaymentPurpose extends CommonBase { this.spontaneous_payment = obj.spontaneous_payment; } } + long clone_ptr() { + long ret = bindings.PaymentPurpose_clone_ptr(this.ptr); + Reference.reachabilityFence(this); + return ret; + } + /** * Creates a copy of the PaymentPurpose */ public PaymentPurpose clone() { long ret = bindings.PaymentPurpose_clone(this.ptr); - if (ret < 1024) { return null; } - PaymentPurpose ret_hu_conv = PaymentPurpose.constr_from_ptr(ret); - ret_hu_conv.ptrs_to.add(this); + Reference.reachabilityFence(this); + if (ret >= 0 && ret <= 4096) { return null; } + org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret); + if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); }; return ret_hu_conv; } /** * Utility method to constructs a new InvoicePayment-variant PaymentPurpose */ - public static PaymentPurpose invoice_payment(byte[] payment_preimage, byte[] payment_secret, long user_payment_id) { - long ret = bindings.PaymentPurpose_invoice_payment(payment_preimage, payment_secret, user_payment_id); - if (ret < 1024) { return null; } - PaymentPurpose ret_hu_conv = PaymentPurpose.constr_from_ptr(ret); - ret_hu_conv.ptrs_to.add(ret_hu_conv); + public static PaymentPurpose invoice_payment(byte[] payment_preimage, byte[] payment_secret) { + long ret = bindings.PaymentPurpose_invoice_payment(InternalUtils.check_arr_len(payment_preimage, 32), InternalUtils.check_arr_len(payment_secret, 32)); + Reference.reachabilityFence(payment_preimage); + Reference.reachabilityFence(payment_secret); + if (ret >= 0 && ret <= 4096) { return null; } + org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret); + if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); }; return ret_hu_conv; } @@ -106,10 +111,46 @@ public class PaymentPurpose extends CommonBase { * Utility method to constructs a new SpontaneousPayment-variant PaymentPurpose */ public static PaymentPurpose spontaneous_payment(byte[] a) { - long ret = bindings.PaymentPurpose_spontaneous_payment(a); - if (ret < 1024) { return null; } - PaymentPurpose ret_hu_conv = PaymentPurpose.constr_from_ptr(ret); - ret_hu_conv.ptrs_to.add(ret_hu_conv); + long ret = bindings.PaymentPurpose_spontaneous_payment(InternalUtils.check_arr_len(a, 32)); + Reference.reachabilityFence(a); + if (ret >= 0 && ret <= 4096) { return null; } + org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret); + if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); }; + return ret_hu_conv; + } + + /** + * Checks if two PaymentPurposes contain equal inner contents. + * This ignores pointers and is_owned flags and looks at the values in fields. + */ + public boolean eq(org.ldk.structs.PaymentPurpose b) { + boolean ret = bindings.PaymentPurpose_eq(this.ptr, b == null ? 0 : b.ptr); + Reference.reachabilityFence(this); + Reference.reachabilityFence(b); + return ret; + } + + @Override public boolean equals(Object o) { + if (!(o instanceof PaymentPurpose)) return false; + return this.eq((PaymentPurpose)o); + } + /** + * Serialize the PaymentPurpose object into a byte array which can be read by PaymentPurpose_read + */ + public byte[] write() { + byte[] ret = bindings.PaymentPurpose_write(this.ptr); + Reference.reachabilityFence(this); + return ret; + } + + /** + * Read a PaymentPurpose from a byte array, created by PaymentPurpose_write + */ + public static Result_PaymentPurposeDecodeErrorZ read(byte[] ser) { + long ret = bindings.PaymentPurpose_read(ser); + Reference.reachabilityFence(ser); + if (ret >= 0 && ret <= 4096) { return null; } + Result_PaymentPurposeDecodeErrorZ ret_hu_conv = Result_PaymentPurposeDecodeErrorZ.constr_from_ptr(ret); return ret_hu_conv; }