3 import CommonBase from './CommonBase';
4 import * as bindings from '../bindings' // TODO: figure out location
8 export class ChannelMessageHandler extends CommonBase {
10 bindings_instance?: bindings.LDKChannelMessageHandler;
12 constructor(ptr?: number, arg?: bindings.LDKChannelMessageHandler, messageSendEventsProvider?: bindings.LDKMessageSendEventsProvider) {
13 if (Number.isFinite(ptr)) {
15 this.bindings_instance = null;
17 // TODO: private constructor instantiation
18 super(bindings.LDKChannelMessageHandler_new(arg, messageSendEventsProvider));
19 this.ptrs_to.push(arg);
20 this.ptrs_to.push(messageSendEventsProvider);
25 protected finalize() {
27 bindings.ChannelMessageHandler_free(this.ptr);
32 static new_impl(arg: ChannelMessageHandlerInterface, messageSendEventsProvider_impl: MessageSendEventsProvider.MessageSendEventsProviderInterface): ChannelMessageHandler {
33 const impl_holder: LDKChannelMessageHandlerHolder = new LDKChannelMessageHandlerHolder();
34 let structImplementation = <bindings.LDKChannelMessageHandler>{
35 // todo: in-line interface filling
36 handle_open_channel (their_node_id: Uint8Array, their_features: number, msg: number): void {
37 const their_features_hu_conv: InitFeatures = new InitFeatures(null, their_features);
38 their_features_hu_conv.ptrs_to.add(this);
39 const msg_hu_conv: OpenChannel = new OpenChannel(null, msg);
40 arg.handle_open_channel(their_node_id, their_features_hu_conv, msg_hu_conv);
43 handle_accept_channel (their_node_id: Uint8Array, their_features: number, msg: number): void {
44 const their_features_hu_conv: InitFeatures = new InitFeatures(null, their_features);
45 their_features_hu_conv.ptrs_to.add(this);
46 const msg_hu_conv: AcceptChannel = new AcceptChannel(null, msg);
47 arg.handle_accept_channel(their_node_id, their_features_hu_conv, msg_hu_conv);
50 handle_funding_created (their_node_id: Uint8Array, msg: number): void {
51 const msg_hu_conv: FundingCreated = new FundingCreated(null, msg);
52 arg.handle_funding_created(their_node_id, msg_hu_conv);
55 handle_funding_signed (their_node_id: Uint8Array, msg: number): void {
56 const msg_hu_conv: FundingSigned = new FundingSigned(null, msg);
57 arg.handle_funding_signed(their_node_id, msg_hu_conv);
60 handle_funding_locked (their_node_id: Uint8Array, msg: number): void {
61 const msg_hu_conv: FundingLocked = new FundingLocked(null, msg);
62 arg.handle_funding_locked(their_node_id, msg_hu_conv);
65 handle_shutdown (their_node_id: Uint8Array, msg: number): void {
66 const msg_hu_conv: Shutdown = new Shutdown(null, msg);
67 arg.handle_shutdown(their_node_id, msg_hu_conv);
70 handle_closing_signed (their_node_id: Uint8Array, msg: number): void {
71 const msg_hu_conv: ClosingSigned = new ClosingSigned(null, msg);
72 arg.handle_closing_signed(their_node_id, msg_hu_conv);
75 handle_update_add_htlc (their_node_id: Uint8Array, msg: number): void {
76 const msg_hu_conv: UpdateAddHTLC = new UpdateAddHTLC(null, msg);
77 arg.handle_update_add_htlc(their_node_id, msg_hu_conv);
80 handle_update_fulfill_htlc (their_node_id: Uint8Array, msg: number): void {
81 const msg_hu_conv: UpdateFulfillHTLC = new UpdateFulfillHTLC(null, msg);
82 arg.handle_update_fulfill_htlc(their_node_id, msg_hu_conv);
85 handle_update_fail_htlc (their_node_id: Uint8Array, msg: number): void {
86 const msg_hu_conv: UpdateFailHTLC = new UpdateFailHTLC(null, msg);
87 arg.handle_update_fail_htlc(their_node_id, msg_hu_conv);
90 handle_update_fail_malformed_htlc (their_node_id: Uint8Array, msg: number): void {
91 const msg_hu_conv: UpdateFailMalformedHTLC = new UpdateFailMalformedHTLC(null, msg);
92 arg.handle_update_fail_malformed_htlc(their_node_id, msg_hu_conv);
95 handle_commitment_signed (their_node_id: Uint8Array, msg: number): void {
96 const msg_hu_conv: CommitmentSigned = new CommitmentSigned(null, msg);
97 arg.handle_commitment_signed(their_node_id, msg_hu_conv);
100 handle_revoke_and_ack (their_node_id: Uint8Array, msg: number): void {
101 const msg_hu_conv: RevokeAndACK = new RevokeAndACK(null, msg);
102 arg.handle_revoke_and_ack(their_node_id, msg_hu_conv);
105 handle_update_fee (their_node_id: Uint8Array, msg: number): void {
106 const msg_hu_conv: UpdateFee = new UpdateFee(null, msg);
107 arg.handle_update_fee(their_node_id, msg_hu_conv);
110 handle_announcement_signatures (their_node_id: Uint8Array, msg: number): void {
111 const msg_hu_conv: AnnouncementSignatures = new AnnouncementSignatures(null, msg);
112 arg.handle_announcement_signatures(their_node_id, msg_hu_conv);
115 peer_disconnected (their_node_id: Uint8Array, no_connection_possible: boolean): void {
116 arg.peer_disconnected(their_node_id, no_connection_possible);
119 peer_connected (their_node_id: Uint8Array, msg: number): void {
120 const msg_hu_conv: Init = new Init(null, msg);
121 arg.peer_connected(their_node_id, msg_hu_conv);
124 handle_channel_reestablish (their_node_id: Uint8Array, msg: number): void {
125 const msg_hu_conv: ChannelReestablish = new ChannelReestablish(null, msg);
126 arg.handle_channel_reestablish(their_node_id, msg_hu_conv);
129 handle_error (their_node_id: Uint8Array, msg: number): void {
130 const msg_hu_conv: ErrorMessage = new ErrorMessage(null, msg);
131 arg.handle_error(their_node_id, msg_hu_conv);
136 impl_holder.held = new ChannelMessageHandler (null, structImplementation, MessageSendEventsProvider.new_impl(MessageSendEventsProvider_impl).bindings_instance);
140 export interface ChannelMessageHandlerInterface {
141 handle_open_channel(their_node_id: Uint8Array, their_features: InitFeatures, msg: OpenChannel): void;
142 handle_accept_channel(their_node_id: Uint8Array, their_features: InitFeatures, msg: AcceptChannel): void;
143 handle_funding_created(their_node_id: Uint8Array, msg: FundingCreated): void;
144 handle_funding_signed(their_node_id: Uint8Array, msg: FundingSigned): void;
145 handle_funding_locked(their_node_id: Uint8Array, msg: FundingLocked): void;
146 handle_shutdown(their_node_id: Uint8Array, msg: Shutdown): void;
147 handle_closing_signed(their_node_id: Uint8Array, msg: ClosingSigned): void;
148 handle_update_add_htlc(their_node_id: Uint8Array, msg: UpdateAddHTLC): void;
149 handle_update_fulfill_htlc(their_node_id: Uint8Array, msg: UpdateFulfillHTLC): void;
150 handle_update_fail_htlc(their_node_id: Uint8Array, msg: UpdateFailHTLC): void;
151 handle_update_fail_malformed_htlc(their_node_id: Uint8Array, msg: UpdateFailMalformedHTLC): void;
152 handle_commitment_signed(their_node_id: Uint8Array, msg: CommitmentSigned): void;
153 handle_revoke_and_ack(their_node_id: Uint8Array, msg: RevokeAndACK): void;
154 handle_update_fee(their_node_id: Uint8Array, msg: UpdateFee): void;
155 handle_announcement_signatures(their_node_id: Uint8Array, msg: AnnouncementSignatures): void;
156 peer_disconnected(their_node_id: Uint8Array, no_connection_possible: boolean): void;
157 peer_connected(their_node_id: Uint8Array, msg: Init): void;
158 handle_channel_reestablish(their_node_id: Uint8Array, msg: ChannelReestablish): void;
159 handle_error(their_node_id: Uint8Array, msg: ErrorMessage): void;
163 class LDKChannelMessageHandlerHolder {
164 held: ChannelMessageHandler;
166 public void handle_open_channel(Uint8Array their_node_id, InitFeatures their_features, OpenChannel msg) {
167 bindings.ChannelMessageHandler_handle_open_channel(this.ptr, their_node_id, their_features == null ? 0 : their_features.ptr & ~1, msg == null ? 0 : msg.ptr & ~1);
168 this.ptrs_to.add(their_features);
169 // Due to rust's strict-ownership memory model, in some cases we need to "move"
170 // an object to pass exclusive ownership to the function being called.
171 // In most cases, we avoid this being visible in GC'd languages by cloning the object
172 // at the FFI layer, creating a new object which Rust can claim ownership of
173 // However, in some cases (eg here), there is no way to clone an object, and thus
174 // we actually have to pass full ownership to Rust.
175 // Thus, after this call, their_features is reset to null and is now a dummy object.
176 their_features.ptr = 0;
177 this.ptrs_to.add(msg);
180 public void handle_accept_channel(Uint8Array their_node_id, InitFeatures their_features, AcceptChannel msg) {
181 bindings.ChannelMessageHandler_handle_accept_channel(this.ptr, their_node_id, their_features == null ? 0 : their_features.ptr & ~1, msg == null ? 0 : msg.ptr & ~1);
182 this.ptrs_to.add(their_features);
183 // Due to rust's strict-ownership memory model, in some cases we need to "move"
184 // an object to pass exclusive ownership to the function being called.
185 // In most cases, we avoid this being visible in GC'd languages by cloning the object
186 // at the FFI layer, creating a new object which Rust can claim ownership of
187 // However, in some cases (eg here), there is no way to clone an object, and thus
188 // we actually have to pass full ownership to Rust.
189 // Thus, after this call, their_features is reset to null and is now a dummy object.
190 their_features.ptr = 0;
191 this.ptrs_to.add(msg);
194 public void handle_funding_created(Uint8Array their_node_id, FundingCreated msg) {
195 bindings.ChannelMessageHandler_handle_funding_created(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
196 this.ptrs_to.add(msg);
199 public void handle_funding_signed(Uint8Array their_node_id, FundingSigned msg) {
200 bindings.ChannelMessageHandler_handle_funding_signed(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
201 this.ptrs_to.add(msg);
204 public void handle_funding_locked(Uint8Array their_node_id, FundingLocked msg) {
205 bindings.ChannelMessageHandler_handle_funding_locked(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
206 this.ptrs_to.add(msg);
209 public void handle_shutdown(Uint8Array their_node_id, Shutdown msg) {
210 bindings.ChannelMessageHandler_handle_shutdown(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
211 this.ptrs_to.add(msg);
214 public void handle_closing_signed(Uint8Array their_node_id, ClosingSigned msg) {
215 bindings.ChannelMessageHandler_handle_closing_signed(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
216 this.ptrs_to.add(msg);
219 public void handle_update_add_htlc(Uint8Array their_node_id, UpdateAddHTLC msg) {
220 bindings.ChannelMessageHandler_handle_update_add_htlc(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
221 this.ptrs_to.add(msg);
224 public void handle_update_fulfill_htlc(Uint8Array their_node_id, UpdateFulfillHTLC msg) {
225 bindings.ChannelMessageHandler_handle_update_fulfill_htlc(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
226 this.ptrs_to.add(msg);
229 public void handle_update_fail_htlc(Uint8Array their_node_id, UpdateFailHTLC msg) {
230 bindings.ChannelMessageHandler_handle_update_fail_htlc(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
231 this.ptrs_to.add(msg);
234 public void handle_update_fail_malformed_htlc(Uint8Array their_node_id, UpdateFailMalformedHTLC msg) {
235 bindings.ChannelMessageHandler_handle_update_fail_malformed_htlc(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
236 this.ptrs_to.add(msg);
239 public void handle_commitment_signed(Uint8Array their_node_id, CommitmentSigned msg) {
240 bindings.ChannelMessageHandler_handle_commitment_signed(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
241 this.ptrs_to.add(msg);
244 public void handle_revoke_and_ack(Uint8Array their_node_id, RevokeAndACK msg) {
245 bindings.ChannelMessageHandler_handle_revoke_and_ack(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
246 this.ptrs_to.add(msg);
249 public void handle_update_fee(Uint8Array their_node_id, UpdateFee msg) {
250 bindings.ChannelMessageHandler_handle_update_fee(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
251 this.ptrs_to.add(msg);
254 public void handle_announcement_signatures(Uint8Array their_node_id, AnnouncementSignatures msg) {
255 bindings.ChannelMessageHandler_handle_announcement_signatures(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
256 this.ptrs_to.add(msg);
259 public void peer_disconnected(Uint8Array their_node_id, boolean no_connection_possible) {
260 bindings.ChannelMessageHandler_peer_disconnected(this.ptr, their_node_id, no_connection_possible);
263 public void peer_connected(Uint8Array their_node_id, Init msg) {
264 bindings.ChannelMessageHandler_peer_connected(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
265 this.ptrs_to.add(msg);
268 public void handle_channel_reestablish(Uint8Array their_node_id, ChannelReestablish msg) {
269 bindings.ChannelMessageHandler_handle_channel_reestablish(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
270 this.ptrs_to.add(msg);
273 public void handle_error(Uint8Array their_node_id, ErrorMessage msg) {
274 bindings.ChannelMessageHandler_handle_error(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
275 this.ptrs_to.add(msg);