1
+ //! Watchtower custom lightning messages that implement LDK's Readable & Writeable traits.
2
+
3
+ use crate :: appointment:: { Locator , LOCATOR_LEN } ;
1
4
use bitcoin:: secp256k1:: PublicKey ;
2
5
use bitcoin:: Txid ;
3
- use lightning:: ln:: msgs:: DecodeError ;
4
- use lightning:: ln:: peer_handler:: { CustomMessageHandler , PeerManager } ;
5
- use lightning:: ln:: wire:: { CustomMessageReader , Type } ;
6
+ use lightning:: ln:: msgs;
7
+ use lightning:: ln:: wire;
6
8
use lightning:: util:: ser;
7
-
8
- // To use (de)serialization macros.
9
- use lightning:: * ;
10
-
11
- use crate :: appointment:: { Locator , LOCATOR_LEN } ;
9
+ use std:: io;
12
10
13
11
/// A wrapper around a vector inside a lightning message.
14
12
/// This wrapper mainly exists because we cannot implement LDK's (de)serialization traits
@@ -24,13 +22,27 @@ impl<T> std::ops::Deref for LightningVec<T> {
24
22
}
25
23
}
26
24
27
- // fmt the inner Vec.
25
+ // Only format the inner Vec.
28
26
impl < T : std:: fmt:: Debug > std:: fmt:: Debug for LightningVec < T > {
29
27
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
30
28
self . 0 . fmt ( f)
31
29
}
32
30
}
33
31
32
+ /// A trait that associates a u16 [`Type::TYPE`] constant with a lightning message.
33
+ pub trait Type {
34
+ /// The type identifying the message payload.
35
+ const TYPE : u16 ;
36
+ }
37
+
38
+ macro_rules! set_msg_type {
39
+ ( $st: ident, $type: expr) => {
40
+ impl Type for $st {
41
+ const TYPE : u16 = $type;
42
+ }
43
+ } ;
44
+ }
45
+
34
46
/// The register message sent by the user to subscribe for the watching service.
35
47
#[ derive( Debug ) ]
36
48
pub struct Register {
@@ -122,11 +134,11 @@ pub struct UserSubscriptionInfo {
122
134
123
135
// Deserialization for a Locator inside a lightning message.
124
136
impl ser:: Readable for Locator {
125
- fn read < R : io:: Read > ( reader : & mut R ) -> Result < Self , DecodeError > {
137
+ fn read < R : io:: Read > ( reader : & mut R ) -> Result < Self , msgs :: DecodeError > {
126
138
let mut buf = [ 0 ; LOCATOR_LEN ] ;
127
139
reader. read_exact ( & mut buf) ?;
128
140
let locator =
129
- Self :: deserialize ( & buf) . map_err ( |_| DecodeError :: Io ( io:: ErrorKind :: InvalidData ) ) ?;
141
+ Self :: deserialize ( & buf) . map_err ( |_| msgs :: DecodeError :: Io ( io:: ErrorKind :: InvalidData ) ) ?;
130
142
Ok ( locator)
131
143
}
132
144
}
@@ -141,7 +153,7 @@ impl ser::Writeable for Locator {
141
153
142
154
// Deserialization for a vector of Locators inside a lightning message.
143
155
impl ser:: Readable for LightningVec < Locator > {
144
- fn read < R : io:: Read > ( reader : & mut R ) -> Result < Self , DecodeError > {
156
+ fn read < R : io:: Read > ( reader : & mut R ) -> Result < Self , msgs :: DecodeError > {
145
157
let len: u16 = ser:: Readable :: read ( reader) ?;
146
158
let mut locators = Vec :: with_capacity ( len as usize ) ;
147
159
for _ in 0 ..len {
@@ -162,70 +174,83 @@ impl ser::Writeable for LightningVec<Locator> {
162
174
}
163
175
}
164
176
165
- impl_writeable_msg ! ( Register , {
177
+ lightning :: impl_writeable_msg!( Register , {
166
178
pubkey,
167
179
appointment_slots,
168
180
subscription_period
169
181
} , { } ) ;
170
182
171
- impl_writeable_msg ! ( SubscriptionDetails , {
183
+ lightning :: impl_writeable_msg!( SubscriptionDetails , {
172
184
appointment_max_size,
173
185
amount_msat,
174
186
} , {
175
187
( 1 , signature, option) ,
176
188
} ) ;
177
189
178
- impl_writeable_msg ! ( AddUpdateAppointment , {
190
+ lightning :: impl_writeable_msg!( AddUpdateAppointment , {
179
191
locator,
180
192
encrypted_blob,
181
193
signature,
182
194
} , {
183
195
( 1 , to_self_delay, option) ,
184
196
} ) ;
185
197
186
- impl_writeable_msg ! ( AppointmentAccepted , {
198
+ lightning :: impl_writeable_msg!( AppointmentAccepted , {
187
199
locator,
188
200
start_block,
189
201
} , {
190
202
( 1 , receipt_signature, option) ,
191
203
} ) ;
192
204
193
- impl_writeable_msg ! ( AppointmentRejected , {
205
+ lightning :: impl_writeable_msg!( AppointmentRejected , {
194
206
locator,
195
207
rcode,
196
208
reason,
197
209
} , { } ) ;
198
210
199
- impl_writeable_msg ! ( GetAppointment , {
211
+ lightning :: impl_writeable_msg!( GetAppointment , {
200
212
locator,
201
213
signature,
202
214
} , { } ) ;
203
215
204
- impl_writeable_msg ! ( AppointmentFound , {
216
+ lightning :: impl_writeable_msg!( AppointmentFound , {
205
217
locator,
206
218
encrypted_blob,
207
219
} , { } ) ;
208
220
209
- impl_writeable_msg ! ( TrackerFound , {
221
+ lightning :: impl_writeable_msg!( TrackerFound , {
210
222
dispute_txid,
211
223
penalty_txid,
212
224
penalty_rawtx,
213
225
} , { } ) ;
214
226
215
- impl_writeable_msg ! ( AppointmentNotFound , {
227
+ lightning :: impl_writeable_msg!( AppointmentNotFound , {
216
228
locator,
217
229
} , { } ) ;
218
230
219
- impl_writeable_msg ! ( GetSubscriptionInfo , {
231
+ lightning :: impl_writeable_msg!( GetSubscriptionInfo , {
220
232
signature,
221
233
} , { } ) ;
222
234
223
- impl_writeable_msg ! ( UserSubscriptionInfo , {
235
+ lightning :: impl_writeable_msg!( UserSubscriptionInfo , {
224
236
available_slots,
225
237
subscription_expiry,
226
238
locators,
227
239
} , { } ) ;
228
240
241
+ set_msg_type ! ( Register , 48848 ) ;
242
+ set_msg_type ! ( SubscriptionDetails , 48850 ) ;
243
+ set_msg_type ! ( AddUpdateAppointment , 48852 ) ;
244
+ set_msg_type ! ( AppointmentAccepted , 48854 ) ;
245
+ set_msg_type ! ( AppointmentRejected , 48856 ) ;
246
+ set_msg_type ! ( GetAppointment , 48858 ) ;
247
+ set_msg_type ! ( AppointmentFound , 48860 ) ;
248
+ set_msg_type ! ( TrackerFound , 48862 ) ;
249
+ set_msg_type ! ( AppointmentNotFound , 48864 ) ;
250
+ // Let these messages get odd types since they are auxiliary messages.
251
+ set_msg_type ! ( GetSubscriptionInfo , 48865 ) ;
252
+ set_msg_type ! ( UserSubscriptionInfo , 48867 ) ;
253
+
229
254
#[ derive( Debug ) ]
230
255
pub enum TowerMessage {
231
256
// Register messages
@@ -245,21 +270,20 @@ pub enum TowerMessage {
245
270
UserSubscriptionInfo ( UserSubscriptionInfo ) ,
246
271
}
247
272
248
- impl Type for TowerMessage {
273
+ impl wire :: Type for TowerMessage {
249
274
fn type_id ( & self ) -> u16 {
250
275
match self {
251
- TowerMessage :: Register ( ..) => 48848 ,
252
- TowerMessage :: SubscriptionDetails ( ..) => 48850 ,
253
- TowerMessage :: AddUpdateAppointment ( ..) => 48852 ,
254
- TowerMessage :: AppointmentAccepted ( ..) => 48854 ,
255
- TowerMessage :: AppointmentRejected ( ..) => 48856 ,
256
- TowerMessage :: GetAppointment ( ..) => 48858 ,
257
- TowerMessage :: AppointmentFound ( ..) => 48860 ,
258
- TowerMessage :: TrackerFound ( ..) => 48862 ,
259
- TowerMessage :: AppointmentNotFound ( ..) => 48864 ,
260
- // Let these messages get odd types since they are auxiliary messages.
261
- TowerMessage :: GetSubscriptionInfo ( ..) => 48865 ,
262
- TowerMessage :: UserSubscriptionInfo ( ..) => 48867 ,
276
+ TowerMessage :: Register ( ..) => Register :: TYPE ,
277
+ TowerMessage :: SubscriptionDetails ( ..) => SubscriptionDetails :: TYPE ,
278
+ TowerMessage :: AddUpdateAppointment ( ..) => AddUpdateAppointment :: TYPE ,
279
+ TowerMessage :: AppointmentAccepted ( ..) => AppointmentAccepted :: TYPE ,
280
+ TowerMessage :: AppointmentRejected ( ..) => AppointmentRejected :: TYPE ,
281
+ TowerMessage :: GetAppointment ( ..) => GetAppointment :: TYPE ,
282
+ TowerMessage :: AppointmentFound ( ..) => AppointmentFound :: TYPE ,
283
+ TowerMessage :: TrackerFound ( ..) => TrackerFound :: TYPE ,
284
+ TowerMessage :: AppointmentNotFound ( ..) => AppointmentNotFound :: TYPE ,
285
+ TowerMessage :: GetSubscriptionInfo ( ..) => GetSubscriptionInfo :: TYPE ,
286
+ TowerMessage :: UserSubscriptionInfo ( ..) => UserSubscriptionInfo :: TYPE ,
263
287
}
264
288
}
265
289
}
0 commit comments