1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
12 * Intended destination of a failed HTLC as indicated in [`Event::HTLCHandlingFailed`].
14 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
15 public class HTLCDestination extends CommonBase {
16 private HTLCDestination(Object _dummy, long ptr) { super(ptr); }
17 @Override @SuppressWarnings("deprecation")
18 protected void finalize() throws Throwable {
20 if (ptr != 0) { bindings.HTLCDestination_free(ptr); }
22 static HTLCDestination constr_from_ptr(long ptr) {
23 bindings.LDKHTLCDestination raw_val = bindings.LDKHTLCDestination_ref_from_ptr(ptr);
24 if (raw_val.getClass() == bindings.LDKHTLCDestination.NextHopChannel.class) {
25 return new NextHopChannel(ptr, (bindings.LDKHTLCDestination.NextHopChannel)raw_val);
27 if (raw_val.getClass() == bindings.LDKHTLCDestination.UnknownNextHop.class) {
28 return new UnknownNextHop(ptr, (bindings.LDKHTLCDestination.UnknownNextHop)raw_val);
30 if (raw_val.getClass() == bindings.LDKHTLCDestination.InvalidForward.class) {
31 return new InvalidForward(ptr, (bindings.LDKHTLCDestination.InvalidForward)raw_val);
33 if (raw_val.getClass() == bindings.LDKHTLCDestination.InvalidOnion.class) {
34 return new InvalidOnion(ptr, (bindings.LDKHTLCDestination.InvalidOnion)raw_val);
36 if (raw_val.getClass() == bindings.LDKHTLCDestination.FailedPayment.class) {
37 return new FailedPayment(ptr, (bindings.LDKHTLCDestination.FailedPayment)raw_val);
39 assert false; return null; // Unreachable without extending the (internal) bindings interface
43 * We tried forwarding to a channel but failed to do so. An example of such an instance is when
44 * there is insufficient capacity in our outbound channel.
46 public final static class NextHopChannel extends HTLCDestination {
48 * The `node_id` of the next node. For backwards compatibility, this field is
49 * marked as optional, versions prior to 0.0.110 may not always be able to provide
50 * counterparty node information.
52 * Note that this (or a relevant inner pointer) may be NULL or all-0s to represent None
54 @Nullable public final byte[] node_id;
56 * The outgoing `channel_id` between us and the next node.
58 public final org.ldk.structs.ChannelId channel_id;
59 private NextHopChannel(long ptr, bindings.LDKHTLCDestination.NextHopChannel obj) {
61 this.node_id = obj.node_id;
62 long channel_id = obj.channel_id;
63 org.ldk.structs.ChannelId channel_id_hu_conv = null; if (channel_id < 0 || channel_id > 4096) { channel_id_hu_conv = new org.ldk.structs.ChannelId(null, channel_id); }
64 if (channel_id_hu_conv != null) { channel_id_hu_conv.ptrs_to.add(this); };
65 this.channel_id = channel_id_hu_conv;
69 * Scenario where we are unsure of the next node to forward the HTLC to.
71 public final static class UnknownNextHop extends HTLCDestination {
73 * Short channel id we are requesting to forward an HTLC to.
75 public final long requested_forward_scid;
76 private UnknownNextHop(long ptr, bindings.LDKHTLCDestination.UnknownNextHop obj) {
78 this.requested_forward_scid = obj.requested_forward_scid;
82 * We couldn't forward to the outgoing scid. An example would be attempting to send a duplicate
85 public final static class InvalidForward extends HTLCDestination {
87 * Short channel id we are requesting to forward an HTLC to.
89 public final long requested_forward_scid;
90 private InvalidForward(long ptr, bindings.LDKHTLCDestination.InvalidForward obj) {
92 this.requested_forward_scid = obj.requested_forward_scid;
96 * We couldn't decode the incoming onion to obtain the forwarding details.
98 public final static class InvalidOnion extends HTLCDestination {
99 private InvalidOnion(long ptr, bindings.LDKHTLCDestination.InvalidOnion obj) {
104 * Failure scenario where an HTLC may have been forwarded to be intended for us,
105 * but is invalid for some reason, so we reject it.
107 * Some of the reasons may include:
109 * Excess HTLCs for a payment that we have already fully received, over-paying for the
111 * The counterparty node modified the HTLC in transit,
112 * A probing attack where an intermediary node is trying to detect if we are the ultimate
113 * recipient for a payment.
115 public final static class FailedPayment extends HTLCDestination {
117 * The payment hash of the payment we attempted to process.
119 public final byte[] payment_hash;
120 private FailedPayment(long ptr, bindings.LDKHTLCDestination.FailedPayment obj) {
122 this.payment_hash = obj.payment_hash;
126 long ret = bindings.HTLCDestination_clone_ptr(this.ptr);
127 Reference.reachabilityFence(this);
132 * Creates a copy of the HTLCDestination
134 public HTLCDestination clone() {
135 long ret = bindings.HTLCDestination_clone(this.ptr);
136 Reference.reachabilityFence(this);
137 if (ret >= 0 && ret <= 4096) { return null; }
138 org.ldk.structs.HTLCDestination ret_hu_conv = org.ldk.structs.HTLCDestination.constr_from_ptr(ret);
139 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
144 * Utility method to constructs a new NextHopChannel-variant HTLCDestination
146 public static HTLCDestination next_hop_channel(byte[] node_id, org.ldk.structs.ChannelId channel_id) {
147 long ret = bindings.HTLCDestination_next_hop_channel(InternalUtils.check_arr_len(node_id, 33), channel_id.ptr);
148 Reference.reachabilityFence(node_id);
149 Reference.reachabilityFence(channel_id);
150 if (ret >= 0 && ret <= 4096) { return null; }
151 org.ldk.structs.HTLCDestination ret_hu_conv = org.ldk.structs.HTLCDestination.constr_from_ptr(ret);
152 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
153 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(channel_id); };
158 * Utility method to constructs a new UnknownNextHop-variant HTLCDestination
160 public static HTLCDestination unknown_next_hop(long requested_forward_scid) {
161 long ret = bindings.HTLCDestination_unknown_next_hop(requested_forward_scid);
162 Reference.reachabilityFence(requested_forward_scid);
163 if (ret >= 0 && ret <= 4096) { return null; }
164 org.ldk.structs.HTLCDestination ret_hu_conv = org.ldk.structs.HTLCDestination.constr_from_ptr(ret);
165 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
170 * Utility method to constructs a new InvalidForward-variant HTLCDestination
172 public static HTLCDestination invalid_forward(long requested_forward_scid) {
173 long ret = bindings.HTLCDestination_invalid_forward(requested_forward_scid);
174 Reference.reachabilityFence(requested_forward_scid);
175 if (ret >= 0 && ret <= 4096) { return null; }
176 org.ldk.structs.HTLCDestination ret_hu_conv = org.ldk.structs.HTLCDestination.constr_from_ptr(ret);
177 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
182 * Utility method to constructs a new InvalidOnion-variant HTLCDestination
184 public static HTLCDestination invalid_onion() {
185 long ret = bindings.HTLCDestination_invalid_onion();
186 if (ret >= 0 && ret <= 4096) { return null; }
187 org.ldk.structs.HTLCDestination ret_hu_conv = org.ldk.structs.HTLCDestination.constr_from_ptr(ret);
188 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
193 * Utility method to constructs a new FailedPayment-variant HTLCDestination
195 public static HTLCDestination failed_payment(byte[] payment_hash) {
196 long ret = bindings.HTLCDestination_failed_payment(InternalUtils.check_arr_len(payment_hash, 32));
197 Reference.reachabilityFence(payment_hash);
198 if (ret >= 0 && ret <= 4096) { return null; }
199 org.ldk.structs.HTLCDestination ret_hu_conv = org.ldk.structs.HTLCDestination.constr_from_ptr(ret);
200 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
205 * Checks if two HTLCDestinations contain equal inner contents.
206 * This ignores pointers and is_owned flags and looks at the values in fields.
208 public boolean eq(org.ldk.structs.HTLCDestination b) {
209 boolean ret = bindings.HTLCDestination_eq(this.ptr, b.ptr);
210 Reference.reachabilityFence(this);
211 Reference.reachabilityFence(b);
215 @Override public boolean equals(Object o) {
216 if (!(o instanceof HTLCDestination)) return false;
217 return this.eq((HTLCDestination)o);
220 * Serialize the HTLCDestination object into a byte array which can be read by HTLCDestination_read
222 public byte[] write() {
223 byte[] ret = bindings.HTLCDestination_write(this.ptr);
224 Reference.reachabilityFence(this);