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.FailedPayment.class) {
34 return new FailedPayment(ptr, (bindings.LDKHTLCDestination.FailedPayment)raw_val);
36 assert false; return null; // Unreachable without extending the (internal) bindings interface
40 * We tried forwarding to a channel but failed to do so. An example of such an instance is when
41 * there is insufficient capacity in our outbound channel.
43 public final static class NextHopChannel extends HTLCDestination {
45 * The `node_id` of the next node. For backwards compatibility, this field is
46 * marked as optional, versions prior to 0.0.110 may not always be able to provide
47 * counterparty node information.
49 * Note that this (or a relevant inner pointer) may be NULL or all-0s to represent None
51 @Nullable public final byte[] node_id;
53 * The outgoing `channel_id` between us and the next node.
55 public final byte[] channel_id;
56 private NextHopChannel(long ptr, bindings.LDKHTLCDestination.NextHopChannel obj) {
58 this.node_id = obj.node_id;
59 this.channel_id = obj.channel_id;
63 * Scenario where we are unsure of the next node to forward the HTLC to.
65 public final static class UnknownNextHop extends HTLCDestination {
67 * Short channel id we are requesting to forward an HTLC to.
69 public final long requested_forward_scid;
70 private UnknownNextHop(long ptr, bindings.LDKHTLCDestination.UnknownNextHop obj) {
72 this.requested_forward_scid = obj.requested_forward_scid;
76 * We couldn't forward to the outgoing scid. An example would be attempting to send a duplicate
79 public final static class InvalidForward extends HTLCDestination {
81 * Short channel id we are requesting to forward an HTLC to.
83 public final long requested_forward_scid;
84 private InvalidForward(long ptr, bindings.LDKHTLCDestination.InvalidForward obj) {
86 this.requested_forward_scid = obj.requested_forward_scid;
90 * Failure scenario where an HTLC may have been forwarded to be intended for us,
91 * but is invalid for some reason, so we reject it.
93 * Some of the reasons may include:
95 * Excess HTLCs for a payment that we have already fully received, over-paying for the
97 * The counterparty node modified the HTLC in transit,
98 * A probing attack where an intermediary node is trying to detect if we are the ultimate
99 * recipient for a payment.
101 public final static class FailedPayment extends HTLCDestination {
103 * The payment hash of the payment we attempted to process.
105 public final byte[] payment_hash;
106 private FailedPayment(long ptr, bindings.LDKHTLCDestination.FailedPayment obj) {
108 this.payment_hash = obj.payment_hash;
112 long ret = bindings.HTLCDestination_clone_ptr(this.ptr);
113 Reference.reachabilityFence(this);
118 * Creates a copy of the HTLCDestination
120 public HTLCDestination clone() {
121 long ret = bindings.HTLCDestination_clone(this.ptr);
122 Reference.reachabilityFence(this);
123 if (ret >= 0 && ret <= 4096) { return null; }
124 org.ldk.structs.HTLCDestination ret_hu_conv = org.ldk.structs.HTLCDestination.constr_from_ptr(ret);
125 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
130 * Utility method to constructs a new NextHopChannel-variant HTLCDestination
132 public static HTLCDestination next_hop_channel(byte[] node_id, byte[] channel_id) {
133 long ret = bindings.HTLCDestination_next_hop_channel(InternalUtils.check_arr_len(node_id, 33), InternalUtils.check_arr_len(channel_id, 32));
134 Reference.reachabilityFence(node_id);
135 Reference.reachabilityFence(channel_id);
136 if (ret >= 0 && ret <= 4096) { return null; }
137 org.ldk.structs.HTLCDestination ret_hu_conv = org.ldk.structs.HTLCDestination.constr_from_ptr(ret);
138 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
143 * Utility method to constructs a new UnknownNextHop-variant HTLCDestination
145 public static HTLCDestination unknown_next_hop(long requested_forward_scid) {
146 long ret = bindings.HTLCDestination_unknown_next_hop(requested_forward_scid);
147 Reference.reachabilityFence(requested_forward_scid);
148 if (ret >= 0 && ret <= 4096) { return null; }
149 org.ldk.structs.HTLCDestination ret_hu_conv = org.ldk.structs.HTLCDestination.constr_from_ptr(ret);
150 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
155 * Utility method to constructs a new InvalidForward-variant HTLCDestination
157 public static HTLCDestination invalid_forward(long requested_forward_scid) {
158 long ret = bindings.HTLCDestination_invalid_forward(requested_forward_scid);
159 Reference.reachabilityFence(requested_forward_scid);
160 if (ret >= 0 && ret <= 4096) { return null; }
161 org.ldk.structs.HTLCDestination ret_hu_conv = org.ldk.structs.HTLCDestination.constr_from_ptr(ret);
162 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
167 * Utility method to constructs a new FailedPayment-variant HTLCDestination
169 public static HTLCDestination failed_payment(byte[] payment_hash) {
170 long ret = bindings.HTLCDestination_failed_payment(InternalUtils.check_arr_len(payment_hash, 32));
171 Reference.reachabilityFence(payment_hash);
172 if (ret >= 0 && ret <= 4096) { return null; }
173 org.ldk.structs.HTLCDestination ret_hu_conv = org.ldk.structs.HTLCDestination.constr_from_ptr(ret);
174 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
179 * Checks if two HTLCDestinations contain equal inner contents.
180 * This ignores pointers and is_owned flags and looks at the values in fields.
182 public boolean eq(org.ldk.structs.HTLCDestination b) {
183 boolean ret = bindings.HTLCDestination_eq(this.ptr, b == null ? 0 : b.ptr);
184 Reference.reachabilityFence(this);
185 Reference.reachabilityFence(b);
189 @Override public boolean equals(Object o) {
190 if (!(o instanceof HTLCDestination)) return false;
191 return this.eq((HTLCDestination)o);
194 * Serialize the HTLCDestination object into a byte array which can be read by HTLCDestination_read
196 public byte[] write() {
197 byte[] ret = bindings.HTLCDestination_write(this.ptr);
198 Reference.reachabilityFence(this);