Work around lnd sending invalid messages
[rust-lightning] / src / ln / msgs.rs
1 use secp256k1::key::PublicKey;
2 use secp256k1::{Secp256k1, Signature};
3 use bitcoin::util::uint::Uint256;
4 use bitcoin::util::hash::Sha256dHash;
5 use bitcoin::network::serialize::{deserialize,serialize};
6 use bitcoin::blockdata::script::Script;
7
8 use std::error::Error;
9 use std::fmt;
10 use std::result::Result;
11
12 use util::{byte_utils, internal_traits, events};
13
14 pub trait MsgEncodable {
15         fn encode(&self) -> Vec<u8>;
16         #[inline]
17         fn encoded_len(&self) -> usize { self.encode().len() }
18         #[inline]
19         fn encode_with_len(&self) -> Vec<u8> {
20                 let enc = self.encode();
21                 let mut res = Vec::with_capacity(enc.len() + 2);
22                 res.extend_from_slice(&byte_utils::be16_to_array(enc.len() as u16));
23                 res.extend_from_slice(&enc);
24                 res
25         }
26 }
27 #[derive(Debug)]
28 pub enum DecodeError {
29         /// Unknown realm byte in an OnionHopData packet
30         UnknownRealmByte,
31         /// Failed to decode a public key (ie it's invalid)
32         BadPublicKey,
33         /// Failed to decode a signature (ie it's invalid)
34         BadSignature,
35         /// Buffer not of right length (either too short or too long)
36         WrongLength,
37         /// node_announcement included more than one address of a given type!
38         ExtraAddressesPerType,
39 }
40 pub trait MsgDecodable: Sized {
41         fn decode(v: &[u8]) -> Result<Self, DecodeError>;
42 }
43
44 /// Tracks localfeatures which are only in init messages
45 #[derive(Clone, PartialEq)]
46 pub struct LocalFeatures {
47         flags: Vec<u8>,
48 }
49
50 impl LocalFeatures {
51         pub fn new() -> LocalFeatures {
52                 LocalFeatures {
53                         flags: Vec::new(),
54                 }
55         }
56
57         pub fn supports_data_loss_protect(&self) -> bool {
58                 self.flags.len() > 0 && (self.flags[0] & 3) != 0
59         }
60         pub fn requires_data_loss_protect(&self) -> bool {
61                 self.flags.len() > 0 && (self.flags[0] & 1) != 0
62         }
63
64         pub fn supports_initial_routing_sync(&self) -> bool {
65                 self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0
66         }
67
68         pub fn supports_upfront_shutdown_script(&self) -> bool {
69                 self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0
70         }
71         pub fn requires_upfront_shutdown_script(&self) -> bool {
72                 self.flags.len() > 0 && (self.flags[0] & (1 << 4)) != 0
73         }
74
75         pub fn requires_unknown_bits(&self) -> bool {
76                 for (idx, &byte) in self.flags.iter().enumerate() {
77                         if idx != 0 && (byte & 0x55) != 0 {
78                                 return true;
79                         } else if idx == 0 && (byte & 0x14) != 0 {
80                                 return true;
81                         }
82                 }
83                 return false;
84         }
85
86         pub fn supports_unknown_bits(&self) -> bool {
87                 for (idx, &byte) in self.flags.iter().enumerate() {
88                         if idx != 0 && byte != 0 {
89                                 return true;
90                         } else if idx == 0 && (byte & 0xc4) != 0 {
91                                 return true;
92                         }
93                 }
94                 return false;
95         }
96 }
97
98 /// Tracks globalfeatures which are in init messages and routing announcements
99 #[derive(Clone, PartialEq)]
100 pub struct GlobalFeatures {
101         flags: Vec<u8>,
102 }
103
104 impl GlobalFeatures {
105         pub fn new() -> GlobalFeatures {
106                 GlobalFeatures {
107                         flags: Vec::new(),
108                 }
109         }
110
111         pub fn requires_unknown_bits(&self) -> bool {
112                 for &byte in self.flags.iter() {
113                         if (byte & 0x55) != 0 {
114                                 return true;
115                         }
116                 }
117                 return false;
118         }
119
120         pub fn supports_unknown_bits(&self) -> bool {
121                 for &byte in self.flags.iter() {
122                         if byte != 0 {
123                                 return true;
124                         }
125                 }
126                 return false;
127         }
128 }
129
130 pub struct Init {
131         pub global_features: GlobalFeatures,
132         pub local_features: LocalFeatures,
133 }
134
135 pub struct OpenChannel {
136         pub chain_hash: Sha256dHash,
137         pub temporary_channel_id: Uint256,
138         pub funding_satoshis: u64,
139         pub push_msat: u64,
140         pub dust_limit_satoshis: u64,
141         pub max_htlc_value_in_flight_msat: u64,
142         pub channel_reserve_satoshis: u64,
143         pub htlc_minimum_msat: u64,
144         pub feerate_per_kw: u32,
145         pub to_self_delay: u16,
146         pub max_accepted_htlcs: u16,
147         pub funding_pubkey: PublicKey,
148         pub revocation_basepoint: PublicKey,
149         pub payment_basepoint: PublicKey,
150         pub delayed_payment_basepoint: PublicKey,
151         pub htlc_basepoint: PublicKey,
152         pub first_per_commitment_point: PublicKey,
153         pub channel_flags: u8,
154         pub shutdown_scriptpubkey: Option<Script>,
155 }
156
157 pub struct AcceptChannel {
158         pub temporary_channel_id: Uint256,
159         pub dust_limit_satoshis: u64,
160         pub max_htlc_value_in_flight_msat: u64,
161         pub channel_reserve_satoshis: u64,
162         pub htlc_minimum_msat: u64,
163         pub minimum_depth: u32,
164         pub to_self_delay: u16,
165         pub max_accepted_htlcs: u16,
166         pub funding_pubkey: PublicKey,
167         pub revocation_basepoint: PublicKey,
168         pub payment_basepoint: PublicKey,
169         pub delayed_payment_basepoint: PublicKey,
170         pub htlc_basepoint: PublicKey,
171         pub first_per_commitment_point: PublicKey,
172         pub shutdown_scriptpubkey: Option<Script>,
173 }
174
175 pub struct FundingCreated {
176         pub temporary_channel_id: Uint256,
177         pub funding_txid: Sha256dHash,
178         pub funding_output_index: u16,
179         pub signature: Signature,
180 }
181
182 pub struct FundingSigned {
183         pub channel_id: Uint256,
184         pub signature: Signature,
185 }
186
187 pub struct FundingLocked {
188         pub channel_id: Uint256,
189         pub next_per_commitment_point: PublicKey,
190 }
191
192 pub struct Shutdown {
193         pub channel_id: Uint256,
194         pub scriptpubkey: Script,
195 }
196
197 pub struct ClosingSigned {
198         pub channel_id: Uint256,
199         pub fee_satoshis: u64,
200         pub signature: Signature,
201 }
202
203 #[derive(Clone)]
204 pub struct UpdateAddHTLC {
205         pub channel_id: Uint256,
206         pub htlc_id: u64,
207         pub amount_msat: u64,
208         pub payment_hash: [u8; 32],
209         pub cltv_expiry: u32,
210         pub onion_routing_packet: OnionPacket,
211 }
212
213 #[derive(Clone)]
214 pub struct UpdateFulfillHTLC {
215         pub channel_id: Uint256,
216         pub htlc_id: u64,
217         pub payment_preimage: [u8; 32],
218 }
219
220 #[derive(Clone)]
221 pub struct UpdateFailHTLC {
222         pub channel_id: Uint256,
223         pub htlc_id: u64,
224         pub reason: OnionErrorPacket,
225 }
226
227 #[derive(Clone)]
228 pub struct UpdateFailMalformedHTLC {
229         pub channel_id: Uint256,
230         pub htlc_id: u64,
231         pub sha256_of_onion: [u8; 32],
232         pub failure_code: u16,
233 }
234
235 #[derive(Clone)]
236 pub struct CommitmentSigned {
237         pub channel_id: Uint256,
238         pub signature: Signature,
239         pub htlc_signatures: Vec<Signature>,
240 }
241
242 pub struct RevokeAndACK {
243         pub channel_id: Uint256,
244         pub per_commitment_secret: [u8; 32],
245         pub next_per_commitment_point: PublicKey,
246 }
247
248 pub struct UpdateFee {
249         pub channel_id: Uint256,
250         pub feerate_per_kw: u32,
251 }
252
253 pub struct ChannelReestablish {
254         pub channel_id: Uint256,
255         pub next_local_commitment_number: u64,
256         pub next_remote_commitment_number: u64,
257         pub your_last_per_commitment_secret: Option<[u8; 32]>,
258         pub my_current_per_commitment_point: PublicKey,
259 }
260
261 #[derive(Clone)]
262 pub struct AnnouncementSignatures {
263         pub channel_id: Uint256,
264         pub short_channel_id: u64,
265         pub node_signature: Signature,
266         pub bitcoin_signature: Signature,
267 }
268
269 #[derive(Clone)]
270 pub enum NetAddress {
271         IPv4 {
272                 addr: [u8; 4],
273                 port: u16,
274         },
275         IPv6 {
276                 addr: [u8; 16],
277                 port: u16,
278         },
279         OnionV2 {
280                 addr: [u8; 10],
281                 port: u16,
282         },
283         OnionV3 {
284                 ed25519_pubkey: [u8; 32],
285                 checksum: u16,
286                 version: u8,
287                 port: u16,
288         },
289 }
290 impl NetAddress {
291         fn get_id(&self) -> u8 {
292                 match self {
293                         &NetAddress::IPv4 {..} => { 1 },
294                         &NetAddress::IPv6 {..} => { 2 },
295                         &NetAddress::OnionV2 {..} => { 3 },
296                         &NetAddress::OnionV3 {..} => { 4 },
297                 }
298         }
299 }
300
301 pub struct UnsignedNodeAnnouncement {
302         pub features: GlobalFeatures,
303         pub timestamp: u32,
304         pub node_id: PublicKey,
305         pub rgb: [u8; 3],
306         pub alias: [u8; 32],
307         /// List of addresses on which this node is reachable. Note that you may only have up to one
308         /// address of each type, if you have more, they may be silently discarded or we may panic!
309         pub addresses: Vec<NetAddress>,
310 }
311 pub struct NodeAnnouncement {
312         pub signature: Signature,
313         pub contents: UnsignedNodeAnnouncement,
314 }
315
316 #[derive(PartialEq, Clone)]
317 pub struct UnsignedChannelAnnouncement {
318         pub features: GlobalFeatures,
319         pub chain_hash: Sha256dHash,
320         pub short_channel_id: u64,
321         pub node_id_1: PublicKey,
322         pub node_id_2: PublicKey,
323         pub bitcoin_key_1: PublicKey,
324         pub bitcoin_key_2: PublicKey,
325 }
326 #[derive(PartialEq, Clone)]
327 pub struct ChannelAnnouncement {
328         pub node_signature_1: Signature,
329         pub node_signature_2: Signature,
330         pub bitcoin_signature_1: Signature,
331         pub bitcoin_signature_2: Signature,
332         pub contents: UnsignedChannelAnnouncement,
333 }
334
335 #[derive(PartialEq, Clone)]
336 pub struct UnsignedChannelUpdate {
337         pub chain_hash: Sha256dHash,
338         pub short_channel_id: u64,
339         pub timestamp: u32,
340         pub flags: u16,
341         pub cltv_expiry_delta: u16,
342         pub htlc_minimum_msat: u64,
343         pub fee_base_msat: u32,
344         pub fee_proportional_millionths: u32,
345 }
346 #[derive(PartialEq, Clone)]
347 pub struct ChannelUpdate {
348         pub signature: Signature,
349         pub contents: UnsignedChannelUpdate,
350 }
351
352 /// Used to put an error message in a HandleError
353 pub enum ErrorAction {
354         /// Indicates an inbound HTLC add resulted in a failure, and the UpdateFailHTLC provided in msg
355         /// should be sent back to the sender.
356         UpdateFailHTLC {
357                 msg: UpdateFailHTLC
358         },
359         /// The peer took some action which made us think they were useless. Disconnect them.
360         DisconnectPeer,
361         /// The peer did something harmless that we weren't able to process, just log and ignore
362         IgnoreError,
363 }
364
365 pub struct HandleError { //TODO: rename me
366         pub err: &'static str,
367         pub msg: Option<ErrorAction>, //TODO: Make this required and rename it
368 }
369
370 /// Struct used to return values from revoke_and_ack messages, containing a bunch of commitment
371 /// transaction updates if they were pending.
372 pub struct CommitmentUpdate {
373         pub update_add_htlcs: Vec<UpdateAddHTLC>,
374         pub update_fulfill_htlcs: Vec<UpdateFulfillHTLC>,
375         pub update_fail_htlcs: Vec<UpdateFailHTLC>,
376         pub commitment_signed: CommitmentSigned,
377 }
378
379 pub enum HTLCFailChannelUpdate {
380         ChannelUpdateMessage {
381                 msg: ChannelUpdate,
382         },
383         ChannelClosed {
384                 short_channel_id: u64,
385         },
386 }
387
388 /// A trait to describe an object which can receive channel messages. Messages MAY be called in
389 /// paralell when they originate from different their_node_ids, however they MUST NOT be called in
390 /// paralell when the two calls have the same their_node_id.
391 pub trait ChannelMessageHandler : events::EventsProvider + Send + Sync {
392         //Channel init:
393         fn handle_open_channel(&self, their_node_id: &PublicKey, msg: &OpenChannel) -> Result<AcceptChannel, HandleError>;
394         fn handle_accept_channel(&self, their_node_id: &PublicKey, msg: &AcceptChannel) -> Result<(), HandleError>;
395         fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &FundingCreated) -> Result<FundingSigned, HandleError>;
396         fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &FundingSigned) -> Result<(), HandleError>;
397         fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &FundingLocked) -> Result<Option<AnnouncementSignatures>, HandleError>;
398
399         // Channl close:
400         fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &Shutdown) -> Result<(Option<Shutdown>, Option<ClosingSigned>), HandleError>;
401         fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned) -> Result<Option<ClosingSigned>, HandleError>;
402
403         // HTLC handling:
404         fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &UpdateAddHTLC) -> Result<(), HandleError>;
405         fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFulfillHTLC) -> Result<(), HandleError>;
406         fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC) -> Result<Option<HTLCFailChannelUpdate>, HandleError>;
407         fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailMalformedHTLC) -> Result<(), HandleError>;
408         fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned) -> Result<(RevokeAndACK, Option<CommitmentSigned>), HandleError>;
409         fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &RevokeAndACK) -> Result<Option<CommitmentUpdate>, HandleError>;
410
411         fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &UpdateFee) -> Result<(), HandleError>;
412
413         // Channel-to-announce:
414         fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures) -> Result<(), HandleError>;
415
416         // Informational:
417         /// Indicates a connection to the peer failed/an existing connection was lost. If no connection
418         /// is believed to be possible in the future (eg they're sending us messages we don't
419         /// understand or indicate they require unknown feature bits), no_connection_possible is set
420         /// and any outstanding channels should be failed.
421         fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
422 }
423
424 pub trait RoutingMessageHandler : Send + Sync {
425         fn handle_node_announcement(&self, msg: &NodeAnnouncement) -> Result<(), HandleError>;
426         /// Handle a channel_announcement message, returning true if it should be forwarded on, false
427         /// or returning an Err otherwise.
428         fn handle_channel_announcement(&self, msg: &ChannelAnnouncement) -> Result<bool, HandleError>;
429         fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result<(), HandleError>;
430         fn handle_htlc_fail_channel_update(&self, update: &HTLCFailChannelUpdate);
431 }
432
433 pub struct OnionRealm0HopData {
434         pub short_channel_id: u64,
435         pub amt_to_forward: u64,
436         pub outgoing_cltv_value: u32,
437         // 12 bytes of 0-padding
438 }
439
440 pub struct OnionHopData {
441         pub realm: u8,
442         pub data: OnionRealm0HopData,
443         pub hmac: [u8; 32],
444 }
445 unsafe impl internal_traits::NoDealloc for OnionHopData{}
446
447 #[derive(Clone)]
448 pub struct OnionPacket {
449         pub version: u8,
450         pub public_key: PublicKey,
451         pub hop_data: [u8; 20*65],
452         pub hmac: [u8; 32],
453 }
454
455 pub struct DecodedOnionErrorPacket {
456         pub hmac: [u8; 32],
457         pub failuremsg: Vec<u8>,
458         pub pad: Vec<u8>,
459 }
460
461 #[derive(Clone)]
462 pub struct OnionErrorPacket {
463         // This really should be a constant size slice, but the spec lets these things be up to 128KB?
464         // (TODO) We limit it in decode to much lower...
465         pub data: Vec<u8>,
466 }
467
468 impl Error for DecodeError {
469         fn description(&self) -> &str {
470                 match *self {
471                         DecodeError::UnknownRealmByte => "Unknown realm byte in Onion packet",
472                         DecodeError::BadPublicKey => "Invalid public key in packet",
473                         DecodeError::BadSignature => "Invalid signature in packet",
474                         DecodeError::WrongLength => "Data was wrong length for packet",
475                         DecodeError::ExtraAddressesPerType => "More than one address of a single type",
476                 }
477         }
478 }
479 impl fmt::Display for DecodeError {
480         fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
481                 f.write_str(self.description())
482         }
483 }
484
485 impl fmt::Debug for HandleError {
486         fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
487                 f.write_str(self.err)
488         }
489 }
490
491 macro_rules! secp_pubkey {
492         ( $ctx: expr, $slice: expr ) => {
493                 match PublicKey::from_slice($ctx, $slice) {
494                         Ok(key) => key,
495                         Err(_) => return Err(DecodeError::BadPublicKey)
496                 }
497         };
498 }
499
500 macro_rules! secp_signature {
501         ( $ctx: expr, $slice: expr ) => {
502                 match Signature::from_compact($ctx, $slice) {
503                         Ok(sig) => sig,
504                         Err(_) => return Err(DecodeError::BadSignature)
505                 }
506         };
507 }
508
509 impl MsgDecodable for LocalFeatures {
510         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
511                 if v.len() < 2 { return Err(DecodeError::WrongLength); }
512                 let len = byte_utils::slice_to_be16(&v[0..2]) as usize;
513                 if v.len() < len + 2 { return Err(DecodeError::WrongLength); }
514                 let mut flags = Vec::with_capacity(len);
515                 flags.extend_from_slice(&v[2..2 + len]);
516                 Ok(Self {
517                         flags: flags
518                 })
519         }
520 }
521 impl MsgEncodable for LocalFeatures {
522         fn encode(&self) -> Vec<u8> {
523                 let mut res = Vec::with_capacity(self.flags.len() + 2);
524                 res.extend_from_slice(&byte_utils::be16_to_array(self.flags.len() as u16));
525                 res.extend_from_slice(&self.flags[..]);
526                 res
527         }
528         fn encoded_len(&self) -> usize { self.flags.len() + 2 }
529 }
530
531 impl MsgDecodable for GlobalFeatures {
532         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
533                 if v.len() < 2 { return Err(DecodeError::WrongLength); }
534                 let len = byte_utils::slice_to_be16(&v[0..2]) as usize;
535                 if v.len() < len + 2 { return Err(DecodeError::WrongLength); }
536                 let mut flags = Vec::with_capacity(len);
537                 flags.extend_from_slice(&v[2..2 + len]);
538                 Ok(Self {
539                         flags: flags
540                 })
541         }
542 }
543 impl MsgEncodable for GlobalFeatures {
544         fn encode(&self) -> Vec<u8> {
545                 let mut res = Vec::with_capacity(self.flags.len() + 2);
546                 res.extend_from_slice(&byte_utils::be16_to_array(self.flags.len() as u16));
547                 res.extend_from_slice(&self.flags[..]);
548                 res
549         }
550         fn encoded_len(&self) -> usize { self.flags.len() + 2 }
551 }
552
553 impl MsgDecodable for Init {
554         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
555                 let global_features = GlobalFeatures::decode(v)?;
556                 if v.len() < global_features.flags.len() + 4 {
557                         return Err(DecodeError::WrongLength);
558                 }
559                 let local_features = LocalFeatures::decode(&v[global_features.flags.len() + 2..])?;
560                 Ok(Self {
561                         global_features: global_features,
562                         local_features: local_features,
563                 })
564         }
565 }
566 impl MsgEncodable for Init {
567         fn encode(&self) -> Vec<u8> {
568                 let mut res = Vec::with_capacity(self.global_features.flags.len() + self.local_features.flags.len());
569                 res.extend_from_slice(&self.global_features.encode()[..]);
570                 res.extend_from_slice(&self.local_features.encode()[..]);
571                 res
572         }
573 }
574
575 impl MsgDecodable for OpenChannel {
576         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
577                 if v.len() < 2*32+6*8+4+2*2+6*33+1 {
578                         return Err(DecodeError::WrongLength);
579                 }
580                 let ctx = Secp256k1::without_caps();
581
582                 let mut shutdown_scriptpubkey = None;
583                 if v.len() >= 321 {
584                         let len = byte_utils::slice_to_be16(&v[319..321]) as usize;
585                         if v.len() < 321+len {
586                                 return Err(DecodeError::WrongLength);
587                         }
588                         shutdown_scriptpubkey = Some(Script::from(v[321..321+len].to_vec()));
589                 } else if v.len() != 2*32+6*8+4+2*2+6*33+1 { // Message cant have 1 extra byte
590                         return Err(DecodeError::WrongLength);
591                 }
592
593                 Ok(OpenChannel {
594                         chain_hash: deserialize(&v[0..32]).unwrap(),
595                         temporary_channel_id: deserialize(&v[32..64]).unwrap(),
596                         funding_satoshis: byte_utils::slice_to_be64(&v[64..72]),
597                         push_msat: byte_utils::slice_to_be64(&v[72..80]),
598                         dust_limit_satoshis: byte_utils::slice_to_be64(&v[80..88]),
599                         max_htlc_value_in_flight_msat: byte_utils::slice_to_be64(&v[88..96]),
600                         channel_reserve_satoshis: byte_utils::slice_to_be64(&v[96..104]),
601                         htlc_minimum_msat: byte_utils::slice_to_be64(&v[104..112]),
602                         feerate_per_kw: byte_utils::slice_to_be32(&v[112..116]),
603                         to_self_delay: byte_utils::slice_to_be16(&v[116..118]),
604                         max_accepted_htlcs: byte_utils::slice_to_be16(&v[118..120]),
605                         funding_pubkey: secp_pubkey!(&ctx, &v[120..153]),
606                         revocation_basepoint: secp_pubkey!(&ctx, &v[153..186]),
607                         payment_basepoint: secp_pubkey!(&ctx, &v[186..219]),
608                         delayed_payment_basepoint: secp_pubkey!(&ctx, &v[219..252]),
609                         htlc_basepoint: secp_pubkey!(&ctx, &v[252..285]),
610                         first_per_commitment_point: secp_pubkey!(&ctx, &v[285..318]),
611                         channel_flags: v[318],
612                         shutdown_scriptpubkey: shutdown_scriptpubkey
613                 })
614         }
615 }
616 impl MsgEncodable for OpenChannel {
617         fn encode(&self) -> Vec<u8> {
618                 let mut res = match &self.shutdown_scriptpubkey {
619                         &Some(ref script) => Vec::with_capacity(319 + 2 + script.len()),
620                         &None => Vec::with_capacity(319),
621                 };
622                 res.extend_from_slice(&serialize(&self.chain_hash).unwrap());
623                 res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap());
624                 res.extend_from_slice(&byte_utils::be64_to_array(self.funding_satoshis));
625                 res.extend_from_slice(&byte_utils::be64_to_array(self.push_msat));
626                 res.extend_from_slice(&byte_utils::be64_to_array(self.dust_limit_satoshis));
627                 res.extend_from_slice(&byte_utils::be64_to_array(self.max_htlc_value_in_flight_msat));
628                 res.extend_from_slice(&byte_utils::be64_to_array(self.channel_reserve_satoshis));
629                 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_minimum_msat));
630                 res.extend_from_slice(&byte_utils::be32_to_array(self.feerate_per_kw));
631                 res.extend_from_slice(&byte_utils::be16_to_array(self.to_self_delay));
632                 res.extend_from_slice(&byte_utils::be16_to_array(self.max_accepted_htlcs));
633                 res.extend_from_slice(&self.funding_pubkey.serialize());
634                 res.extend_from_slice(&self.revocation_basepoint.serialize());
635                 res.extend_from_slice(&self.payment_basepoint.serialize());
636                 res.extend_from_slice(&self.delayed_payment_basepoint.serialize());
637                 res.extend_from_slice(&self.htlc_basepoint.serialize());
638                 res.extend_from_slice(&self.first_per_commitment_point.serialize());
639                 res.push(self.channel_flags);
640                 if let &Some(ref script) = &self.shutdown_scriptpubkey {
641                         res.extend_from_slice(&byte_utils::be16_to_array(script.len() as u16));
642                         res.extend_from_slice(&script[..]);
643                 }
644                 res
645         }
646 }
647
648 impl MsgDecodable for AcceptChannel {
649         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
650                 if v.len() < 32+4*8+4+2*2+6*33 {
651                         return Err(DecodeError::WrongLength);
652                 }
653                 let ctx = Secp256k1::without_caps();
654
655                 let mut shutdown_scriptpubkey = None;
656                 if v.len() >= 272 {
657                         let len = byte_utils::slice_to_be16(&v[270..272]) as usize;
658                         if v.len() < 272+len {
659                                 return Err(DecodeError::WrongLength);
660                         }
661                         shutdown_scriptpubkey = Some(Script::from(v[272..272+len].to_vec()));
662                 } else if v.len() != 32+4*8+4+2*2+6*33 { // Message cant have 1 extra byte
663                         return Err(DecodeError::WrongLength);
664                 }
665
666                 Ok(Self {
667                         temporary_channel_id: deserialize(&v[0..32]).unwrap(),
668                         dust_limit_satoshis: byte_utils::slice_to_be64(&v[32..40]),
669                         max_htlc_value_in_flight_msat: byte_utils::slice_to_be64(&v[40..48]),
670                         channel_reserve_satoshis: byte_utils::slice_to_be64(&v[48..56]),
671                         htlc_minimum_msat: byte_utils::slice_to_be64(&v[56..64]),
672                         minimum_depth: byte_utils::slice_to_be32(&v[64..68]),
673                         to_self_delay: byte_utils::slice_to_be16(&v[68..70]),
674                         max_accepted_htlcs: byte_utils::slice_to_be16(&v[70..72]),
675                         funding_pubkey: secp_pubkey!(&ctx, &v[72..105]),
676                         revocation_basepoint: secp_pubkey!(&ctx, &v[105..138]),
677                         payment_basepoint: secp_pubkey!(&ctx, &v[138..171]),
678                         delayed_payment_basepoint: secp_pubkey!(&ctx, &v[171..204]),
679                         htlc_basepoint: secp_pubkey!(&ctx, &v[204..237]),
680                         first_per_commitment_point: secp_pubkey!(&ctx, &v[237..270]),
681                         shutdown_scriptpubkey: shutdown_scriptpubkey
682                 })
683         }
684 }
685 impl MsgEncodable for AcceptChannel {
686         fn encode(&self) -> Vec<u8> {
687                 let mut res = match &self.shutdown_scriptpubkey {
688                         &Some(ref script) => Vec::with_capacity(270 + 2 + script.len()),
689                         &None => Vec::with_capacity(270),
690                 };
691                 res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap()[..]);
692                 res.extend_from_slice(&byte_utils::be64_to_array(self.dust_limit_satoshis));
693                 res.extend_from_slice(&byte_utils::be64_to_array(self.max_htlc_value_in_flight_msat));
694                 res.extend_from_slice(&byte_utils::be64_to_array(self.channel_reserve_satoshis));
695                 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_minimum_msat));
696                 res.extend_from_slice(&byte_utils::be32_to_array(self.minimum_depth));
697                 res.extend_from_slice(&byte_utils::be16_to_array(self.to_self_delay));
698                 res.extend_from_slice(&byte_utils::be16_to_array(self.max_accepted_htlcs));
699                 res.extend_from_slice(&self.funding_pubkey.serialize());
700                 res.extend_from_slice(&self.revocation_basepoint.serialize());
701                 res.extend_from_slice(&self.payment_basepoint.serialize());
702                 res.extend_from_slice(&self.delayed_payment_basepoint.serialize());
703                 res.extend_from_slice(&self.htlc_basepoint.serialize());
704                 res.extend_from_slice(&self.first_per_commitment_point.serialize());
705                 if let &Some(ref script) = &self.shutdown_scriptpubkey {
706                         res.extend_from_slice(&byte_utils::be16_to_array(script.len() as u16));
707                         res.extend_from_slice(&script[..]);
708                 }
709                 res
710         }
711 }
712
713 impl MsgDecodable for FundingCreated {
714         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
715                 if v.len() < 32+32+2+64 {
716                         return Err(DecodeError::WrongLength);
717                 }
718                 let ctx = Secp256k1::without_caps();
719                 Ok(Self {
720                         temporary_channel_id: deserialize(&v[0..32]).unwrap(),
721                         funding_txid: deserialize(&v[32..64]).unwrap(),
722                         funding_output_index: byte_utils::slice_to_be16(&v[64..66]),
723                         signature: secp_signature!(&ctx, &v[66..130]),
724                 })
725         }
726 }
727 impl MsgEncodable for FundingCreated {
728         fn encode(&self) -> Vec<u8> {
729                 let mut res = Vec::with_capacity(32+32+2+64);
730                 res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap()[..]);
731                 res.extend_from_slice(&serialize(&self.funding_txid).unwrap()[..]);
732                 res.extend_from_slice(&byte_utils::be16_to_array(self.funding_output_index));
733                 let secp_ctx = Secp256k1::without_caps();
734                 res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx));
735                 res
736         }
737 }
738
739 impl MsgDecodable for FundingSigned {
740         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
741                 if v.len() < 32+64 {
742                         return Err(DecodeError::WrongLength);
743                 }
744                 let ctx = Secp256k1::without_caps();
745                 Ok(Self {
746                         channel_id: deserialize(&v[0..32]).unwrap(),
747                         signature: secp_signature!(&ctx, &v[32..96]),
748                 })
749         }
750 }
751 impl MsgEncodable for FundingSigned {
752         fn encode(&self) -> Vec<u8> {
753                 let mut res = Vec::with_capacity(32+64);
754                 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
755                 res.extend_from_slice(&self.signature.serialize_compact(&Secp256k1::without_caps()));
756                 res
757         }
758 }
759
760 impl MsgDecodable for FundingLocked {
761         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
762                 if v.len() < 32+33 {
763                         return Err(DecodeError::WrongLength);
764                 }
765                 let ctx = Secp256k1::without_caps();
766                 Ok(Self {
767                         channel_id: deserialize(&v[0..32]).unwrap(),
768                         next_per_commitment_point: secp_pubkey!(&ctx, &v[32..65]),
769                 })
770         }
771 }
772 impl MsgEncodable for FundingLocked {
773         fn encode(&self) -> Vec<u8> {
774                 let mut res = Vec::with_capacity(32+33);
775                 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
776                 res.extend_from_slice(&self.next_per_commitment_point.serialize());
777                 res
778         }
779 }
780
781 impl MsgDecodable for Shutdown {
782         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
783                 if v.len() < 32 + 2 {
784                         return Err(DecodeError::WrongLength);
785                 }
786                 let scriptlen = byte_utils::slice_to_be16(&v[32..34]) as usize;
787                 if v.len() < 32 + 2 + scriptlen {
788                         return Err(DecodeError::WrongLength);
789                 }
790                 Ok(Self {
791                         channel_id: deserialize(&v[0..32]).unwrap(),
792                         scriptpubkey: Script::from(v[34..34 + scriptlen].to_vec()),
793                 })
794         }
795 }
796 impl MsgEncodable for Shutdown {
797         fn encode(&self) -> Vec<u8> {
798                 let mut res = Vec::with_capacity(32 + 2 + self.scriptpubkey.len());
799                 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
800                 res.extend_from_slice(&byte_utils::be16_to_array(self.scriptpubkey.len() as u16));
801                 res.extend_from_slice(&self.scriptpubkey[..]);
802                 res
803         }
804 }
805
806 impl MsgDecodable for ClosingSigned {
807         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
808                 if v.len() < 32 + 8 + 64 {
809                         return Err(DecodeError::WrongLength);
810                 }
811                 let secp_ctx = Secp256k1::without_caps();
812                 Ok(Self {
813                         channel_id: deserialize(&v[0..32]).unwrap(),
814                         fee_satoshis: byte_utils::slice_to_be64(&v[32..40]),
815                         signature: secp_signature!(&secp_ctx, &v[40..104]),
816                 })
817         }
818 }
819 impl MsgEncodable for ClosingSigned {
820         fn encode(&self) -> Vec<u8> {
821                 let mut res = Vec::with_capacity(32+8+64);
822                 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
823                 res.extend_from_slice(&byte_utils::be64_to_array(self.fee_satoshis));
824                 let secp_ctx = Secp256k1::without_caps();
825                 res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx));
826                 res
827         }
828 }
829
830 impl MsgDecodable for UpdateAddHTLC {
831         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
832                 if v.len() < 32+8+8+32+4+1+33+20*65+32 {
833                         return Err(DecodeError::WrongLength);
834                 }
835                 let mut payment_hash = [0; 32];
836                 payment_hash.copy_from_slice(&v[48..80]);
837                 Ok(Self{
838                         channel_id: deserialize(&v[0..32]).unwrap(),
839                         htlc_id: byte_utils::slice_to_be64(&v[32..40]),
840                         amount_msat: byte_utils::slice_to_be64(&v[40..48]),
841                         payment_hash,
842                         cltv_expiry: byte_utils::slice_to_be32(&v[80..84]),
843                         onion_routing_packet: OnionPacket::decode(&v[84..84+1366])?,
844                 })
845         }
846 }
847 impl MsgEncodable for UpdateAddHTLC {
848         fn encode(&self) -> Vec<u8> {
849                 let mut res = Vec::with_capacity(32+8+8+32+4+1+1366);
850                 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
851                 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
852                 res.extend_from_slice(&byte_utils::be64_to_array(self.amount_msat));
853                 res.extend_from_slice(&self.payment_hash);
854                 res.extend_from_slice(&byte_utils::be32_to_array(self.cltv_expiry));
855                 res.extend_from_slice(&self.onion_routing_packet.encode()[..]);
856                 res
857         }
858 }
859
860 impl MsgDecodable for UpdateFulfillHTLC {
861         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
862                 if v.len() < 32+8+32 {
863                         return Err(DecodeError::WrongLength);
864                 }
865                 let mut payment_preimage = [0; 32];
866                 payment_preimage.copy_from_slice(&v[40..72]);
867                 Ok(Self{
868                         channel_id: deserialize(&v[0..32]).unwrap(),
869                         htlc_id: byte_utils::slice_to_be64(&v[32..40]),
870                         payment_preimage,
871                 })
872         }
873 }
874 impl MsgEncodable for UpdateFulfillHTLC {
875         fn encode(&self) -> Vec<u8> {
876                 let mut res = Vec::with_capacity(32+8+32);
877                 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
878                 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
879                 res.extend_from_slice(&self.payment_preimage);
880                 res
881         }
882 }
883
884 impl MsgDecodable for UpdateFailHTLC {
885         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
886                 if v.len() < 32+8 {
887                         return Err(DecodeError::WrongLength);
888                 }
889                 Ok(Self{
890                         channel_id: deserialize(&v[0..32]).unwrap(),
891                         htlc_id: byte_utils::slice_to_be64(&v[32..40]),
892                         reason: OnionErrorPacket::decode(&v[40..])?,
893                 })
894         }
895 }
896 impl MsgEncodable for UpdateFailHTLC {
897         fn encode(&self) -> Vec<u8> {
898                 let reason = self.reason.encode();
899                 let mut res = Vec::with_capacity(32+8+reason.len());
900                 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
901                 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
902                 res.extend_from_slice(&reason[..]);
903                 res
904         }
905 }
906
907 impl MsgDecodable for UpdateFailMalformedHTLC {
908         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
909                 if v.len() < 32+8+32+2 {
910                         return Err(DecodeError::WrongLength);
911                 }
912                 let mut sha256_of_onion = [0; 32];
913                 sha256_of_onion.copy_from_slice(&v[40..72]);
914                 Ok(Self{
915                         channel_id: deserialize(&v[0..32]).unwrap(),
916                         htlc_id: byte_utils::slice_to_be64(&v[32..40]),
917                         sha256_of_onion,
918                         failure_code: byte_utils::slice_to_be16(&v[72..74]),
919                 })
920         }
921 }
922 impl MsgEncodable for UpdateFailMalformedHTLC {
923         fn encode(&self) -> Vec<u8> {
924                 let mut res = Vec::with_capacity(32+8+32+2);
925                 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
926                 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
927                 res.extend_from_slice(&self.sha256_of_onion);
928                 res.extend_from_slice(&byte_utils::be16_to_array(self.failure_code));
929                 res
930         }
931 }
932
933 impl MsgDecodable for CommitmentSigned {
934         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
935                 if v.len() < 32+64+2 {
936                         return Err(DecodeError::WrongLength);
937                 }
938                 let htlcs = byte_utils::slice_to_be16(&v[96..98]) as usize;
939                 if v.len() < 32+64+2+htlcs*64 {
940                         return Err(DecodeError::WrongLength);
941                 }
942                 let mut htlc_signatures = Vec::with_capacity(htlcs);
943                 let secp_ctx = Secp256k1::without_caps();
944                 for i in 0..htlcs {
945                         htlc_signatures.push(secp_signature!(&secp_ctx, &v[98+i*64..98+(i+1)*64]));
946                 }
947                 Ok(Self {
948                         channel_id: deserialize(&v[0..32]).unwrap(),
949                         signature: secp_signature!(&secp_ctx, &v[32..96]),
950                         htlc_signatures,
951                 })
952         }
953 }
954 impl MsgEncodable for CommitmentSigned {
955         fn encode(&self) -> Vec<u8> {
956                 let mut res = Vec::with_capacity(32+64+2+self.htlc_signatures.len()*64);
957                 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
958                 let secp_ctx = Secp256k1::without_caps();
959                 res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx));
960                 res.extend_from_slice(&byte_utils::be16_to_array(self.htlc_signatures.len() as u16));
961                 for i in 0..self.htlc_signatures.len() {
962                         res.extend_from_slice(&self.htlc_signatures[i].serialize_compact(&secp_ctx));
963                 }
964                 res
965         }
966 }
967
968 impl MsgDecodable for RevokeAndACK {
969         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
970                 if v.len() < 32+32+33 {
971                         return Err(DecodeError::WrongLength);
972                 }
973                 let mut per_commitment_secret = [0; 32];
974                 per_commitment_secret.copy_from_slice(&v[32..64]);
975                 let secp_ctx = Secp256k1::without_caps();
976                 Ok(Self {
977                         channel_id: deserialize(&v[0..32]).unwrap(),
978                         per_commitment_secret,
979                         next_per_commitment_point: secp_pubkey!(&secp_ctx, &v[64..97]),
980                 })
981         }
982 }
983 impl MsgEncodable for RevokeAndACK {
984         fn encode(&self) -> Vec<u8> {
985                 let mut res = Vec::with_capacity(32+32+33);
986                 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
987                 res.extend_from_slice(&self.per_commitment_secret);
988                 res.extend_from_slice(&self.next_per_commitment_point.serialize());
989                 res
990         }
991 }
992
993 impl MsgDecodable for UpdateFee {
994         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
995                 if v.len() < 32+4 {
996                         return Err(DecodeError::WrongLength);
997                 }
998                 Ok(Self {
999                         channel_id: deserialize(&v[0..32]).unwrap(),
1000                         feerate_per_kw: byte_utils::slice_to_be32(&v[32..36]),
1001                 })
1002         }
1003 }
1004 impl MsgEncodable for UpdateFee {
1005         fn encode(&self) -> Vec<u8> {
1006                 let mut res = Vec::with_capacity(32+4);
1007                 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
1008                 res.extend_from_slice(&byte_utils::be32_to_array(self.feerate_per_kw));
1009                 res
1010         }
1011 }
1012
1013 impl MsgDecodable for ChannelReestablish {
1014         fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
1015                 unimplemented!();
1016         }
1017 }
1018 impl MsgEncodable for ChannelReestablish {
1019         fn encode(&self) -> Vec<u8> {
1020                 unimplemented!();
1021         }
1022 }
1023
1024 impl MsgDecodable for AnnouncementSignatures {
1025         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1026                 if v.len() < 32+8+64*2 {
1027                         return Err(DecodeError::WrongLength);
1028                 }
1029                 let secp_ctx = Secp256k1::without_caps();
1030                 Ok(Self {
1031                         channel_id: deserialize(&v[0..32]).unwrap(),
1032                         short_channel_id: byte_utils::slice_to_be64(&v[32..40]),
1033                         node_signature: secp_signature!(&secp_ctx, &v[40..104]),
1034                         bitcoin_signature: secp_signature!(&secp_ctx, &v[104..168]),
1035                 })
1036         }
1037 }
1038 impl MsgEncodable for AnnouncementSignatures {
1039         fn encode(&self) -> Vec<u8> {
1040                 let mut res = Vec::with_capacity(32+8+64*2);
1041                 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
1042                 res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id));
1043                 let secp_ctx = Secp256k1::without_caps();
1044                 res.extend_from_slice(&self.node_signature.serialize_compact(&secp_ctx));
1045                 res.extend_from_slice(&self.bitcoin_signature.serialize_compact(&secp_ctx));
1046                 res
1047         }
1048 }
1049
1050 impl MsgDecodable for UnsignedNodeAnnouncement {
1051         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1052                 let features = GlobalFeatures::decode(&v[..])?;
1053                 if v.len() < features.encoded_len() + 4 + 33 + 3 + 32 + 2 {
1054                         return Err(DecodeError::WrongLength);
1055                 }
1056                 let start = features.encoded_len();
1057
1058                 let mut rgb = [0; 3];
1059                 rgb.copy_from_slice(&v[start + 37..start + 40]);
1060
1061                 let mut alias = [0; 32];
1062                 alias.copy_from_slice(&v[start + 40..start + 72]);
1063
1064                 let addrlen = byte_utils::slice_to_be16(&v[start + 72..start + 74]) as usize;
1065                 if v.len() < start + 74 + addrlen {
1066                         return Err(DecodeError::WrongLength);
1067                 }
1068
1069                 let mut addresses = Vec::with_capacity(4);
1070                 let mut read_pos = start + 74;
1071                 loop {
1072                         if v.len() <= read_pos { break; }
1073                         match v[read_pos] {
1074                                 0 => { read_pos += 1; },
1075                                 1 => {
1076                                         if v.len() < read_pos + 1 + 6 {
1077                                                 return Err(DecodeError::WrongLength);
1078                                         }
1079                                         if addresses.len() > 0 {
1080                                                 return Err(DecodeError::ExtraAddressesPerType);
1081                                         }
1082                                         let mut addr = [0; 4];
1083                                         addr.copy_from_slice(&v[read_pos + 1..read_pos + 5]);
1084                                         addresses.push(NetAddress::IPv4 {
1085                                                 addr,
1086                                                 port: byte_utils::slice_to_be16(&v[read_pos + 5..read_pos + 7]),
1087                                         });
1088                                         read_pos += 1 + 6;
1089                                 },
1090                                 2 => {
1091                                         if v.len() < read_pos + 1 + 18 {
1092                                                 return Err(DecodeError::WrongLength);
1093                                         }
1094                                         if addresses.len() > 1 || (addresses.len() == 1 && addresses[0].get_id() != 1) {
1095                                                 return Err(DecodeError::ExtraAddressesPerType);
1096                                         }
1097                                         let mut addr = [0; 16];
1098                                         addr.copy_from_slice(&v[read_pos + 1..read_pos + 17]);
1099                                         addresses.push(NetAddress::IPv6 {
1100                                                 addr,
1101                                                 port: byte_utils::slice_to_be16(&v[read_pos + 17..read_pos + 19]),
1102                                         });
1103                                         read_pos += 1 + 18;
1104                                 },
1105                                 3 => {
1106                                         if v.len() < read_pos + 1 + 12 {
1107                                                 return Err(DecodeError::WrongLength);
1108                                         }
1109                                         if addresses.len() > 2 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 2) {
1110                                                 return Err(DecodeError::ExtraAddressesPerType);
1111                                         }
1112                                         let mut addr = [0; 10];
1113                                         addr.copy_from_slice(&v[read_pos + 1..read_pos + 11]);
1114                                         addresses.push(NetAddress::OnionV2 {
1115                                                 addr,
1116                                                 port: byte_utils::slice_to_be16(&v[read_pos + 11..read_pos + 13]),
1117                                         });
1118                                         read_pos += 1 + 12;
1119                                 },
1120                                 4 => {
1121                                         if v.len() < read_pos + 1 + 37 {
1122                                                 return Err(DecodeError::WrongLength);
1123                                         }
1124                                         if addresses.len() > 3 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 3) {
1125                                                 return Err(DecodeError::ExtraAddressesPerType);
1126                                         }
1127                                         let mut ed25519_pubkey = [0; 32];
1128                                         ed25519_pubkey.copy_from_slice(&v[read_pos + 1..read_pos + 33]);
1129                                         addresses.push(NetAddress::OnionV3 {
1130                                                 ed25519_pubkey,
1131                                                 checksum: byte_utils::slice_to_be16(&v[read_pos + 33..read_pos + 35]),
1132                                                 version: v[read_pos + 35],
1133                                                 port: byte_utils::slice_to_be16(&v[read_pos + 36..read_pos + 38]),
1134                                         });
1135                                         read_pos += 1 + 37;
1136                                 },
1137                                 _ => { break; } // We've read all we can, we dont understand anything higher (and they're sorted)
1138                         }
1139                 }
1140
1141                 let secp_ctx = Secp256k1::without_caps();
1142                 Ok(Self {
1143                         features,
1144                         timestamp: byte_utils::slice_to_be32(&v[start..start + 4]),
1145                         node_id: secp_pubkey!(&secp_ctx, &v[start + 4..start + 37]),
1146                         rgb,
1147                         alias,
1148                         addresses,
1149                 })
1150         }
1151 }
1152 impl MsgEncodable for UnsignedNodeAnnouncement {
1153         fn encode(&self) -> Vec<u8> {
1154                 let features = self.features.encode();
1155                 let mut res = Vec::with_capacity(74 + features.len() + self.addresses.len());
1156                 res.extend_from_slice(&features[..]);
1157                 res.extend_from_slice(&byte_utils::be32_to_array(self.timestamp));
1158                 res.extend_from_slice(&self.node_id.serialize());
1159                 res.extend_from_slice(&self.rgb);
1160                 res.extend_from_slice(&self.alias);
1161                 let mut addr_slice = Vec::with_capacity(self.addresses.len() * 18);
1162                 let mut addrs_to_encode = self.addresses.clone();
1163                 addrs_to_encode.sort_unstable_by(|a, b| { a.get_id().cmp(&b.get_id()) });
1164                 addrs_to_encode.dedup_by(|a, b| { a.get_id() == b.get_id() });
1165                 for addr in addrs_to_encode.iter() {
1166                         match addr {
1167                                 &NetAddress::IPv4{addr, port} => {
1168                                         addr_slice.push(1);
1169                                         addr_slice.extend_from_slice(&addr);
1170                                         addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
1171                                 },
1172                                 &NetAddress::IPv6{addr, port} => {
1173                                         addr_slice.push(2);
1174                                         addr_slice.extend_from_slice(&addr);
1175                                         addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
1176                                 },
1177                                 &NetAddress::OnionV2{addr, port} => {
1178                                         addr_slice.push(3);
1179                                         addr_slice.extend_from_slice(&addr);
1180                                         addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
1181                                 },
1182                                 &NetAddress::OnionV3{ed25519_pubkey, checksum, version, port} => {
1183                                         addr_slice.push(4);
1184                                         addr_slice.extend_from_slice(&ed25519_pubkey);
1185                                         addr_slice.extend_from_slice(&byte_utils::be16_to_array(checksum));
1186                                         addr_slice.push(version);
1187                                         addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
1188                                 },
1189                         }
1190                 }
1191                 res.extend_from_slice(&byte_utils::be16_to_array(addr_slice.len() as u16));
1192                 res.extend_from_slice(&addr_slice[..]);
1193                 res
1194         }
1195 }
1196
1197 impl MsgDecodable for NodeAnnouncement {
1198         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1199                 if v.len() < 64 {
1200                         return Err(DecodeError::WrongLength);
1201                 }
1202                 let secp_ctx = Secp256k1::without_caps();
1203                 Ok(Self {
1204                         signature: secp_signature!(&secp_ctx, &v[0..64]),
1205                         contents: UnsignedNodeAnnouncement::decode(&v[64..])?,
1206                 })
1207         }
1208 }
1209 impl MsgEncodable for NodeAnnouncement {
1210         fn encode(&self) -> Vec<u8> {
1211                 let contents = self.contents.encode();
1212                 let mut res = Vec::with_capacity(64 + contents.len());
1213                 let secp_ctx = Secp256k1::without_caps();
1214                 res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx));
1215                 res.extend_from_slice(&contents);
1216                 res
1217         }
1218 }
1219
1220 impl MsgDecodable for UnsignedChannelAnnouncement {
1221         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1222                 let features = GlobalFeatures::decode(&v[..])?;
1223                 if v.len() < features.encoded_len() + 32 + 8 + 33*4 {
1224                         return Err(DecodeError::WrongLength);
1225                 }
1226                 let start = features.encoded_len();
1227                 let secp_ctx = Secp256k1::without_caps();
1228                 Ok(Self {
1229                         features,
1230                         chain_hash: deserialize(&v[start..start + 32]).unwrap(),
1231                         short_channel_id: byte_utils::slice_to_be64(&v[start + 32..start + 40]),
1232                         node_id_1: secp_pubkey!(&secp_ctx, &v[start + 40..start + 73]),
1233                         node_id_2: secp_pubkey!(&secp_ctx, &v[start + 73..start + 106]),
1234                         bitcoin_key_1: secp_pubkey!(&secp_ctx, &v[start + 106..start + 139]),
1235                         bitcoin_key_2: secp_pubkey!(&secp_ctx, &v[start + 139..start + 172]),
1236                 })
1237         }
1238 }
1239 impl MsgEncodable for UnsignedChannelAnnouncement {
1240         fn encode(&self) -> Vec<u8> {
1241                 let features = self.features.encode();
1242                 let mut res = Vec::with_capacity(172 + features.len());
1243                 res.extend_from_slice(&features[..]);
1244                 res.extend_from_slice(&self.chain_hash[..]);
1245                 res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id));
1246                 res.extend_from_slice(&self.node_id_1.serialize());
1247                 res.extend_from_slice(&self.node_id_2.serialize());
1248                 res.extend_from_slice(&self.bitcoin_key_1.serialize());
1249                 res.extend_from_slice(&self.bitcoin_key_2.serialize());
1250                 res
1251         }
1252 }
1253
1254 impl MsgDecodable for ChannelAnnouncement {
1255         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1256                 if v.len() < 64*4 {
1257                         return Err(DecodeError::WrongLength);
1258                 }
1259                 let secp_ctx = Secp256k1::without_caps();
1260                 Ok(Self {
1261                         node_signature_1: secp_signature!(&secp_ctx, &v[0..64]),
1262                         node_signature_2: secp_signature!(&secp_ctx, &v[64..128]),
1263                         bitcoin_signature_1: secp_signature!(&secp_ctx, &v[128..192]),
1264                         bitcoin_signature_2: secp_signature!(&secp_ctx, &v[192..256]),
1265                         contents: UnsignedChannelAnnouncement::decode(&v[256..])?,
1266                 })
1267         }
1268 }
1269 impl MsgEncodable for ChannelAnnouncement {
1270         fn encode(&self) -> Vec<u8> {
1271                 let secp_ctx = Secp256k1::without_caps();
1272                 let contents = self.contents.encode();
1273                 let mut res = Vec::with_capacity(64 + contents.len());
1274                 res.extend_from_slice(&self.node_signature_1.serialize_compact(&secp_ctx));
1275                 res.extend_from_slice(&self.node_signature_2.serialize_compact(&secp_ctx));
1276                 res.extend_from_slice(&self.bitcoin_signature_1.serialize_compact(&secp_ctx));
1277                 res.extend_from_slice(&self.bitcoin_signature_2.serialize_compact(&secp_ctx));
1278                 res.extend_from_slice(&contents);
1279                 res
1280         }
1281 }
1282
1283 impl MsgDecodable for UnsignedChannelUpdate {
1284         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1285                 if v.len() < 32+8+4+2+2+8+4+4 {
1286                         return Err(DecodeError::WrongLength);
1287                 }
1288                 Ok(Self {
1289                         chain_hash: deserialize(&v[0..32]).unwrap(),
1290                         short_channel_id: byte_utils::slice_to_be64(&v[32..40]),
1291                         timestamp: byte_utils::slice_to_be32(&v[40..44]),
1292                         flags: byte_utils::slice_to_be16(&v[44..46]),
1293                         cltv_expiry_delta: byte_utils::slice_to_be16(&v[46..48]),
1294                         htlc_minimum_msat: byte_utils::slice_to_be64(&v[48..56]),
1295                         fee_base_msat: byte_utils::slice_to_be32(&v[56..60]),
1296                         fee_proportional_millionths: byte_utils::slice_to_be32(&v[60..64]),
1297                 })
1298         }
1299 }
1300 impl MsgEncodable for UnsignedChannelUpdate {
1301         fn encode(&self) -> Vec<u8> {
1302                 let mut res = Vec::with_capacity(64);
1303                 res.extend_from_slice(&self.chain_hash[..]);
1304                 res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id));
1305                 res.extend_from_slice(&byte_utils::be32_to_array(self.timestamp));
1306                 res.extend_from_slice(&byte_utils::be16_to_array(self.flags));
1307                 res.extend_from_slice(&byte_utils::be16_to_array(self.cltv_expiry_delta));
1308                 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_minimum_msat));
1309                 res.extend_from_slice(&byte_utils::be32_to_array(self.fee_base_msat));
1310                 res.extend_from_slice(&byte_utils::be32_to_array(self.fee_proportional_millionths));
1311                 res
1312         }
1313 }
1314
1315 impl MsgDecodable for ChannelUpdate {
1316         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1317                 if v.len() < 128 {
1318                         return Err(DecodeError::WrongLength);
1319                 }
1320                 let secp_ctx = Secp256k1::without_caps();
1321                 Ok(Self {
1322                         signature: secp_signature!(&secp_ctx, &v[0..64]),
1323                         contents: UnsignedChannelUpdate::decode(&v[64..])?,
1324                 })
1325         }
1326 }
1327 impl MsgEncodable for ChannelUpdate {
1328         fn encode(&self) -> Vec<u8> {
1329                 let mut res = Vec::with_capacity(128);
1330                 res.extend_from_slice(&self.signature.serialize_compact(&Secp256k1::without_caps())[..]);
1331                 res.extend_from_slice(&self.contents.encode()[..]);
1332                 res
1333         }
1334 }
1335
1336 impl MsgDecodable for OnionRealm0HopData {
1337         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1338                 if v.len() < 32 {
1339                         return Err(DecodeError::WrongLength);
1340                 }
1341                 Ok(OnionRealm0HopData {
1342                         short_channel_id: byte_utils::slice_to_be64(&v[0..8]),
1343                         amt_to_forward: byte_utils::slice_to_be64(&v[8..16]),
1344                         outgoing_cltv_value: byte_utils::slice_to_be32(&v[16..20]),
1345                 })
1346         }
1347 }
1348 impl MsgEncodable for OnionRealm0HopData {
1349         fn encode(&self) -> Vec<u8> {
1350                 let mut res = Vec::with_capacity(32);
1351                 res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id));
1352                 res.extend_from_slice(&byte_utils::be64_to_array(self.amt_to_forward));
1353                 res.extend_from_slice(&byte_utils::be32_to_array(self.outgoing_cltv_value));
1354                 res.resize(32, 0);
1355                 res
1356         }
1357 }
1358
1359 impl MsgDecodable for OnionHopData {
1360         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1361                 if v.len() < 65 {
1362                         return Err(DecodeError::WrongLength);
1363                 }
1364                 let realm = v[0];
1365                 if realm != 0 {
1366                         return Err(DecodeError::UnknownRealmByte);
1367                 }
1368                 let mut hmac = [0; 32];
1369                 hmac[..].copy_from_slice(&v[33..65]);
1370                 Ok(OnionHopData {
1371                         realm: realm,
1372                         data: OnionRealm0HopData::decode(&v[1..33])?,
1373                         hmac: hmac,
1374                 })
1375         }
1376 }
1377 impl MsgEncodable for OnionHopData {
1378         fn encode(&self) -> Vec<u8> {
1379                 let mut res = Vec::with_capacity(65);
1380                 res.push(self.realm);
1381                 res.extend_from_slice(&self.data.encode()[..]);
1382                 res.extend_from_slice(&self.hmac);
1383                 res
1384         }
1385 }
1386
1387 impl MsgDecodable for OnionPacket {
1388         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1389                 if v.len() < 1+33+20*65+32 {
1390                         return Err(DecodeError::WrongLength);
1391                 }
1392                 let mut hop_data = [0; 20*65];
1393                 hop_data.copy_from_slice(&v[34..1334]);
1394                 let mut hmac = [0; 32];
1395                 hmac.copy_from_slice(&v[1334..1366]);
1396                 let secp_ctx = Secp256k1::without_caps();
1397                 Ok(Self {
1398                         version: v[0],
1399                         public_key: secp_pubkey!(&secp_ctx, &v[1..34]),
1400                         hop_data,
1401                         hmac,
1402                 })
1403         }
1404 }
1405 impl MsgEncodable for OnionPacket {
1406         fn encode(&self) -> Vec<u8> {
1407                 let mut res = Vec::with_capacity(1 + 33 + 20*65 + 32);
1408                 res.push(self.version);
1409                 res.extend_from_slice(&self.public_key.serialize());
1410                 res.extend_from_slice(&self.hop_data);
1411                 res.extend_from_slice(&self.hmac);
1412                 res
1413         }
1414 }
1415
1416 impl MsgDecodable for DecodedOnionErrorPacket {
1417         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1418                 if v.len() < 32 + 4 {
1419                         return Err(DecodeError::WrongLength);
1420                 }
1421                 let failuremsg_len = byte_utils::slice_to_be16(&v[32..34]) as usize;
1422                 if v.len() < 32 + 4 + failuremsg_len {
1423                         return Err(DecodeError::WrongLength);
1424                 }
1425                 let padding_len = byte_utils::slice_to_be16(&v[34 + failuremsg_len..]) as usize;
1426                 if v.len() < 32 + 4 + failuremsg_len + padding_len {
1427                         return Err(DecodeError::WrongLength);
1428                 }
1429
1430                 let mut hmac = [0; 32];
1431                 hmac.copy_from_slice(&v[0..32]);
1432                 Ok(Self {
1433                         hmac,
1434                         failuremsg: v[34..34 + failuremsg_len].to_vec(),
1435                         pad: v[36 + failuremsg_len..36 + failuremsg_len + padding_len].to_vec(),
1436                 })
1437         }
1438 }
1439 impl MsgEncodable for DecodedOnionErrorPacket {
1440         fn encode(&self) -> Vec<u8> {
1441                 let mut res = Vec::with_capacity(32 + 4 + self.failuremsg.len() + self.pad.len());
1442                 res.extend_from_slice(&self.hmac);
1443                 res.extend_from_slice(&[((self.failuremsg.len() >> 8) & 0xff) as u8, (self.failuremsg.len() & 0xff) as u8]);
1444                 res.extend_from_slice(&self.failuremsg);
1445                 res.extend_from_slice(&[((self.pad.len() >> 8) & 0xff) as u8, (self.pad.len() & 0xff) as u8]);
1446                 res.extend_from_slice(&self.pad);
1447                 res
1448         }
1449 }
1450
1451 impl MsgDecodable for OnionErrorPacket {
1452         fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1453                 if v.len() < 2 {
1454                         return Err(DecodeError::WrongLength);
1455                 }
1456                 let len = byte_utils::slice_to_be16(&v[0..2]) as usize;
1457                 if v.len() < 2 + len {
1458                         return Err(DecodeError::WrongLength);
1459                 }
1460                 Ok(Self {
1461                         data: v[2..len+2].to_vec(),
1462                 })
1463         }
1464 }
1465 impl MsgEncodable for OnionErrorPacket {
1466         fn encode(&self) -> Vec<u8> {
1467                 let mut res = Vec::with_capacity(2 + self.data.len());
1468                 res.extend_from_slice(&byte_utils::be16_to_array(self.data.len() as u16));
1469                 res.extend_from_slice(&self.data);
1470                 res
1471         }
1472 }