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 * An Event which you should probably take some action in response to.
13 * Note that while Writeable and Readable are implemented for Event, you probably shouldn't use
14 * them directly as they don't round-trip exactly (for example FundingGenerationReady is never
15 * written as it makes no sense to respond to it after reconnecting to peers).
17 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
18 public class Event extends CommonBase {
19 private Event(Object _dummy, long ptr) { super(ptr); }
20 @Override @SuppressWarnings("deprecation")
21 protected void finalize() throws Throwable {
23 if (ptr != 0) { bindings.Event_free(ptr); }
25 static Event constr_from_ptr(long ptr) {
26 bindings.LDKEvent raw_val = bindings.LDKEvent_ref_from_ptr(ptr);
27 if (raw_val.getClass() == bindings.LDKEvent.FundingGenerationReady.class) {
28 return new FundingGenerationReady(ptr, (bindings.LDKEvent.FundingGenerationReady)raw_val);
30 if (raw_val.getClass() == bindings.LDKEvent.PaymentReceived.class) {
31 return new PaymentReceived(ptr, (bindings.LDKEvent.PaymentReceived)raw_val);
33 if (raw_val.getClass() == bindings.LDKEvent.PaymentSent.class) {
34 return new PaymentSent(ptr, (bindings.LDKEvent.PaymentSent)raw_val);
36 if (raw_val.getClass() == bindings.LDKEvent.PaymentFailed.class) {
37 return new PaymentFailed(ptr, (bindings.LDKEvent.PaymentFailed)raw_val);
39 if (raw_val.getClass() == bindings.LDKEvent.PendingHTLCsForwardable.class) {
40 return new PendingHTLCsForwardable(ptr, (bindings.LDKEvent.PendingHTLCsForwardable)raw_val);
42 if (raw_val.getClass() == bindings.LDKEvent.SpendableOutputs.class) {
43 return new SpendableOutputs(ptr, (bindings.LDKEvent.SpendableOutputs)raw_val);
45 assert false; return null; // Unreachable without extending the (internal) bindings interface
48 public final static class FundingGenerationReady extends Event {
50 * The random channel_id we picked which you'll need to pass into
51 * ChannelManager::funding_transaction_generated.
53 public final byte[] temporary_channel_id;
55 * The value, in satoshis, that the output should have.
57 public final long channel_value_satoshis;
59 * The script which should be used in the transaction output.
61 public final byte[] output_script;
63 * The value passed in to ChannelManager::create_channel
65 public final long user_channel_id;
66 private FundingGenerationReady(long ptr, bindings.LDKEvent.FundingGenerationReady obj) {
68 this.temporary_channel_id = obj.temporary_channel_id;
69 this.channel_value_satoshis = obj.channel_value_satoshis;
70 this.output_script = obj.output_script;
71 this.user_channel_id = obj.user_channel_id;
74 public final static class PaymentReceived extends Event {
76 * The hash for which the preimage should be handed to the ChannelManager.
78 public final byte[] payment_hash;
80 * The preimage to the payment_hash, if the payment hash (and secret) were fetched via
81 * [`ChannelManager::create_inbound_payment`]. If provided, this can be handed directly to
82 * [`ChannelManager::claim_funds`].
84 * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
85 * [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
87 * Note that this (or a relevant inner pointer) may be NULL or all-0s to represent None
89 @Nullable public final byte[] payment_preimage;
91 * The \"payment secret\". This authenticates the sender to the recipient, preventing a
92 * number of deanonymization attacks during the routing process.
93 * It is provided here for your reference, however its accuracy is enforced directly by
94 * [`ChannelManager`] using the values you previously provided to
95 * [`ChannelManager::create_inbound_payment`] or
96 * [`ChannelManager::create_inbound_payment_for_hash`].
98 * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
99 * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
100 * [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
102 public final byte[] payment_secret;
104 * The value, in thousandths of a satoshi, that this payment is for. Note that you must
105 * compare this to the expected value before accepting the payment (as otherwise you are
106 * providing proof-of-payment for less than the value you expected!).
108 public final long amt;
110 * This is the `user_payment_id` which was provided to
111 * [`ChannelManager::create_inbound_payment_for_hash`] or
112 * [`ChannelManager::create_inbound_payment`]. It has no meaning inside of LDK and is
113 * simply copied here. It may be used to correlate PaymentReceived events with invoice
114 * metadata stored elsewhere.
116 * [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
117 * [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
119 public final long user_payment_id;
120 private PaymentReceived(long ptr, bindings.LDKEvent.PaymentReceived obj) {
122 this.payment_hash = obj.payment_hash;
123 this.payment_preimage = obj.payment_preimage;
124 this.payment_secret = obj.payment_secret;
126 this.user_payment_id = obj.user_payment_id;
129 public final static class PaymentSent extends Event {
131 * The preimage to the hash given to ChannelManager::send_payment.
132 * Note that this serves as a payment receipt, if you wish to have such a thing, you must
135 public final byte[] payment_preimage;
136 private PaymentSent(long ptr, bindings.LDKEvent.PaymentSent obj) {
138 this.payment_preimage = obj.payment_preimage;
141 public final static class PaymentFailed extends Event {
143 * The hash which was given to ChannelManager::send_payment.
145 public final byte[] payment_hash;
147 * Indicates the payment was rejected for some reason by the recipient. This implies that
148 * the payment has failed, not just the route in question. If this is not set, you may
149 * retry the payment via a different route.
151 public final boolean rejected_by_dest;
152 private PaymentFailed(long ptr, bindings.LDKEvent.PaymentFailed obj) {
154 this.payment_hash = obj.payment_hash;
155 this.rejected_by_dest = obj.rejected_by_dest;
158 public final static class PendingHTLCsForwardable extends Event {
160 * The minimum amount of time that should be waited prior to calling
161 * process_pending_htlc_forwards. To increase the effort required to correlate payments,
162 * you should wait a random amount of time in roughly the range (now + time_forwardable,
163 * now + 5*time_forwardable).
165 public final long time_forwardable;
166 private PendingHTLCsForwardable(long ptr, bindings.LDKEvent.PendingHTLCsForwardable obj) {
168 this.time_forwardable = obj.time_forwardable;
171 public final static class SpendableOutputs extends Event {
173 * The outputs which you should store as spendable by you.
175 public final SpendableOutputDescriptor[] outputs;
176 private SpendableOutputs(long ptr, bindings.LDKEvent.SpendableOutputs obj) {
178 long[] outputs = obj.outputs;
179 SpendableOutputDescriptor[] outputs_conv_27_arr = new SpendableOutputDescriptor[outputs.length];
180 for (int b = 0; b < outputs.length; b++) {
181 long outputs_conv_27 = outputs[b];
182 SpendableOutputDescriptor outputs_conv_27_hu_conv = SpendableOutputDescriptor.constr_from_ptr(outputs_conv_27);
183 outputs_conv_27_hu_conv.ptrs_to.add(this);
184 outputs_conv_27_arr[b] = outputs_conv_27_hu_conv;
186 this.outputs = outputs_conv_27_arr;
190 * Creates a copy of the Event
192 public Event clone() {
193 long ret = bindings.Event_clone(this.ptr);
194 if (ret < 1024) { return null; }
195 Event ret_hu_conv = Event.constr_from_ptr(ret);
196 ret_hu_conv.ptrs_to.add(this);
201 * Utility method to constructs a new FundingGenerationReady-variant Event
203 public static Event funding_generation_ready(byte[] temporary_channel_id, long channel_value_satoshis, byte[] output_script, long user_channel_id) {
204 long ret = bindings.Event_funding_generation_ready(temporary_channel_id, channel_value_satoshis, output_script, user_channel_id);
205 if (ret < 1024) { return null; }
206 Event ret_hu_conv = Event.constr_from_ptr(ret);
207 ret_hu_conv.ptrs_to.add(ret_hu_conv);
212 * Utility method to constructs a new PaymentReceived-variant Event
214 public static Event payment_received(byte[] payment_hash, byte[] payment_preimage, byte[] payment_secret, long amt, long user_payment_id) {
215 long ret = bindings.Event_payment_received(payment_hash, payment_preimage, payment_secret, amt, user_payment_id);
216 if (ret < 1024) { return null; }
217 Event ret_hu_conv = Event.constr_from_ptr(ret);
218 ret_hu_conv.ptrs_to.add(ret_hu_conv);
223 * Utility method to constructs a new PaymentSent-variant Event
225 public static Event payment_sent(byte[] payment_preimage) {
226 long ret = bindings.Event_payment_sent(payment_preimage);
227 if (ret < 1024) { return null; }
228 Event ret_hu_conv = Event.constr_from_ptr(ret);
229 ret_hu_conv.ptrs_to.add(ret_hu_conv);
234 * Utility method to constructs a new PaymentFailed-variant Event
236 public static Event payment_failed(byte[] payment_hash, boolean rejected_by_dest) {
237 long ret = bindings.Event_payment_failed(payment_hash, rejected_by_dest);
238 if (ret < 1024) { return null; }
239 Event ret_hu_conv = Event.constr_from_ptr(ret);
240 ret_hu_conv.ptrs_to.add(ret_hu_conv);
245 * Utility method to constructs a new PendingHTLCsForwardable-variant Event
247 public static Event pending_htlcs_forwardable(long time_forwardable) {
248 long ret = bindings.Event_pending_htlcs_forwardable(time_forwardable);
249 if (ret < 1024) { return null; }
250 Event ret_hu_conv = Event.constr_from_ptr(ret);
251 ret_hu_conv.ptrs_to.add(ret_hu_conv);
256 * Utility method to constructs a new SpendableOutputs-variant Event
258 public static Event spendable_outputs(SpendableOutputDescriptor[] outputs) {
259 long ret = bindings.Event_spendable_outputs(outputs != null ? Arrays.stream(outputs).mapToLong(outputs_conv_27 -> outputs_conv_27.ptr).toArray() : null);
260 if (ret < 1024) { return null; }
261 Event ret_hu_conv = Event.constr_from_ptr(ret);
262 ret_hu_conv.ptrs_to.add(ret_hu_conv);
263 /* TODO 2 SpendableOutputDescriptor */;
268 * Serialize the Event object into a byte array which can be read by Event_read
270 public byte[] write() {
271 byte[] ret = bindings.Event_write(this.ptr);