ebd6168e875bd450df98150a20a9d1eb7cb42984
[ldk-java] / c_sharp / src / org / ldk / structs / PaymentPurpose.cs
1 using org.ldk.impl;
2 using org.ldk.enums;
3 using org.ldk.util;
4 using System;
5
6 namespace org { namespace ldk { namespace structs {
7
8 /**
9  * Some information provided on receipt of payment depends on whether the payment received is a
10  * spontaneous payment or a \"conventional\" lightning payment that's paying an invoice.
11  */
12 public class PaymentPurpose : CommonBase {
13         protected PaymentPurpose(object _dummy, long ptr) : base(ptr) { }
14         ~PaymentPurpose() {
15                 if (ptr != 0) { bindings.PaymentPurpose_free(ptr); }
16         }
17
18         internal static PaymentPurpose constr_from_ptr(long ptr) {
19                 long raw_ty = bindings.LDKPaymentPurpose_ty_from_ptr(ptr);
20                 switch (raw_ty) {
21                         case 0: return new PaymentPurpose_InvoicePayment(ptr);
22                         case 1: return new PaymentPurpose_SpontaneousPayment(ptr);
23                         default:
24                                 throw new ArgumentException("Impossible enum variant");
25                 }
26         }
27
28         /** A PaymentPurpose of type InvoicePayment */
29         public class PaymentPurpose_InvoicePayment : PaymentPurpose {
30                 /**
31                  * The preimage to the payment_hash, if the payment hash (and secret) were fetched via
32                  * [`ChannelManager::create_inbound_payment`]. If provided, this can be handed directly to
33                  * [`ChannelManager::claim_funds`].
34                  * 
35                  * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
36                  * [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
37                  */
38                 public Option_ThirtyTwoBytesZ payment_preimage;
39                 /**
40                  * The \"payment secret\". This authenticates the sender to the recipient, preventing a
41                  * number of deanonymization attacks during the routing process.
42                  * It is provided here for your reference, however its accuracy is enforced directly by
43                  * [`ChannelManager`] using the values you previously provided to
44                  * [`ChannelManager::create_inbound_payment`] or
45                  * [`ChannelManager::create_inbound_payment_for_hash`].
46                  * 
47                  * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
48                  * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
49                  * [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
50                  */
51                 public byte[] payment_secret;
52                 internal PaymentPurpose_InvoicePayment(long ptr) : base(null, ptr) {
53                         long payment_preimage = bindings.LDKPaymentPurpose_InvoicePayment_get_payment_preimage(ptr);
54                         org.ldk.structs.Option_ThirtyTwoBytesZ payment_preimage_hu_conv = org.ldk.structs.Option_ThirtyTwoBytesZ.constr_from_ptr(payment_preimage);
55                         if (payment_preimage_hu_conv != null) { payment_preimage_hu_conv.ptrs_to.AddLast(this); };
56                         this.payment_preimage = payment_preimage_hu_conv;
57                         long payment_secret = bindings.LDKPaymentPurpose_InvoicePayment_get_payment_secret(ptr);
58                         byte[] payment_secret_conv = InternalUtils.decodeUint8Array(payment_secret);
59                         this.payment_secret = payment_secret_conv;
60                 }
61         }
62         /** A PaymentPurpose of type SpontaneousPayment */
63         public class PaymentPurpose_SpontaneousPayment : PaymentPurpose {
64                 public byte[] spontaneous_payment;
65                 internal PaymentPurpose_SpontaneousPayment(long ptr) : base(null, ptr) {
66                         long spontaneous_payment = bindings.LDKPaymentPurpose_SpontaneousPayment_get_spontaneous_payment(ptr);
67                         byte[] spontaneous_payment_conv = InternalUtils.decodeUint8Array(spontaneous_payment);
68                         this.spontaneous_payment = spontaneous_payment_conv;
69                 }
70         }
71         internal long clone_ptr() {
72                 long ret = bindings.PaymentPurpose_clone_ptr(this.ptr);
73                 GC.KeepAlive(this);
74                 return ret;
75         }
76
77         /**
78          * Creates a copy of the PaymentPurpose
79          */
80         public PaymentPurpose clone() {
81                 long ret = bindings.PaymentPurpose_clone(this.ptr);
82                 GC.KeepAlive(this);
83                 if (ret >= 0 && ret <= 4096) { return null; }
84                 org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret);
85                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
86                 return ret_hu_conv;
87         }
88
89         /**
90          * Utility method to constructs a new InvoicePayment-variant PaymentPurpose
91          */
92         public static PaymentPurpose invoice_payment(org.ldk.structs.Option_ThirtyTwoBytesZ payment_preimage, byte[] payment_secret) {
93                 long ret = bindings.PaymentPurpose_invoice_payment(payment_preimage.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(payment_secret, 32)));
94                 GC.KeepAlive(payment_preimage);
95                 GC.KeepAlive(payment_secret);
96                 if (ret >= 0 && ret <= 4096) { return null; }
97                 org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret);
98                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
99                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(payment_preimage); };
100                 return ret_hu_conv;
101         }
102
103         /**
104          * Utility method to constructs a new SpontaneousPayment-variant PaymentPurpose
105          */
106         public static PaymentPurpose spontaneous_payment(byte[] a) {
107                 long ret = bindings.PaymentPurpose_spontaneous_payment(InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(a, 32)));
108                 GC.KeepAlive(a);
109                 if (ret >= 0 && ret <= 4096) { return null; }
110                 org.ldk.structs.PaymentPurpose ret_hu_conv = org.ldk.structs.PaymentPurpose.constr_from_ptr(ret);
111                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
112                 return ret_hu_conv;
113         }
114
115         /**
116          * Checks if two PaymentPurposes contain equal inner contents.
117          * This ignores pointers and is_owned flags and looks at the values in fields.
118          */
119         public bool eq(org.ldk.structs.PaymentPurpose b) {
120                 bool ret = bindings.PaymentPurpose_eq(this.ptr, b == null ? 0 : b.ptr);
121                 GC.KeepAlive(this);
122                 GC.KeepAlive(b);
123                 return ret;
124         }
125
126         public override bool Equals(object o) {
127                 if (!(o is PaymentPurpose)) return false;
128                 return this.eq((PaymentPurpose)o);
129         }
130         /**
131          * Serialize the PaymentPurpose object into a byte array which can be read by PaymentPurpose_read
132          */
133         public byte[] write() {
134                 long ret = bindings.PaymentPurpose_write(this.ptr);
135                 GC.KeepAlive(this);
136                 if (ret >= 0 && ret <= 4096) { return null; }
137                 byte[] ret_conv = InternalUtils.decodeUint8Array(ret);
138                 return ret_conv;
139         }
140
141         /**
142          * Read a PaymentPurpose from a byte array, created by PaymentPurpose_write
143          */
144         public static Result_PaymentPurposeDecodeErrorZ read(byte[] ser) {
145                 long ret = bindings.PaymentPurpose_read(InternalUtils.encodeUint8Array(ser));
146                 GC.KeepAlive(ser);
147                 if (ret >= 0 && ret <= 4096) { return null; }
148                 Result_PaymentPurposeDecodeErrorZ ret_hu_conv = Result_PaymentPurposeDecodeErrorZ.constr_from_ptr(ret);
149                 return ret_hu_conv;
150         }
151
152 }
153 } } }