]> git.bitcoin.ninja Git - rust-lightning/blob - lightning/src/ln/msgs.rs
Bump versions to 0.0.10
[rust-lightning] / lightning / src / ln / msgs.rs
1 //! Wire messages, traits representing wire message handlers, and a few error types live here.
2 //!
3 //! For a normal node you probably don't need to use anything here, however, if you wish to split a
4 //! node into an internet-facing route/message socket handling daemon and a separate daemon (or
5 //! server entirely) which handles only channel-related messages you may wish to implement
6 //! ChannelMessageHandler yourself and use it to re-serialize messages and pass them across
7 //! daemons/servers.
8 //!
9 //! Note that if you go with such an architecture (instead of passing raw socket events to a
10 //! non-internet-facing system) you trust the frontend internet-facing system to not lie about the
11 //! source node_id of the message, however this does allow you to significantly reduce bandwidth
12 //! between the systems as routing messages can represent a significant chunk of bandwidth usage
13 //! (especially for non-channel-publicly-announcing nodes). As an alternate design which avoids
14 //! this issue, if you have sufficient bidirectional bandwidth between your systems, you may send
15 //! raw socket events into your non-internet-facing system and then send routing events back to
16 //! track the network on the less-secure system.
17
18 use secp256k1::key::PublicKey;
19 use secp256k1::Signature;
20 use secp256k1;
21 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
22 use bitcoin::blockdata::script::Script;
23
24 use std::error::Error;
25 use std::{cmp, fmt};
26 use std::io::Read;
27 use std::result::Result;
28
29 use util::events;
30 use util::ser::{Readable, Writeable, Writer};
31
32 use ln::channelmanager::{PaymentPreimage, PaymentHash};
33
34 /// An error in decoding a message or struct.
35 #[derive(Debug)]
36 pub enum DecodeError {
37         /// A version byte specified something we don't know how to handle.
38         /// Includes unknown realm byte in an OnionHopData packet
39         UnknownVersion,
40         /// Unknown feature mandating we fail to parse message
41         UnknownRequiredFeature,
42         /// Value was invalid, eg a byte which was supposed to be a bool was something other than a 0
43         /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, etc
44         InvalidValue,
45         /// Buffer too short
46         ShortRead,
47         /// node_announcement included more than one address of a given type!
48         ExtraAddressesPerType,
49         /// A length descriptor in the packet didn't describe the later data correctly
50         BadLengthDescriptor,
51         /// Error from std::io
52         Io(::std::io::Error),
53 }
54
55 /// Tracks localfeatures which are only in init messages
56 #[derive(Clone, PartialEq)]
57 pub struct LocalFeatures {
58         flags: Vec<u8>,
59 }
60
61 impl LocalFeatures {
62         /// Create a blank LocalFeatures flags (visibility extended for fuzz tests)
63         #[cfg(not(feature = "fuzztarget"))]
64         pub(crate) fn new() -> LocalFeatures {
65                 LocalFeatures {
66                         flags: vec![2 | 1 << 5],
67                 }
68         }
69         #[cfg(feature = "fuzztarget")]
70         pub fn new() -> LocalFeatures {
71                 LocalFeatures {
72                         flags: vec![2 | 1 << 5],
73                 }
74         }
75
76         pub(crate) fn supports_data_loss_protect(&self) -> bool {
77                 self.flags.len() > 0 && (self.flags[0] & 3) != 0
78         }
79         pub(crate) fn initial_routing_sync(&self) -> bool {
80                 self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0
81         }
82         pub(crate) fn set_initial_routing_sync(&mut self) {
83                 if self.flags.len() == 0 {
84                         self.flags.resize(1, 1 << 3);
85                 } else {
86                         self.flags[0] |= 1 << 3;
87                 }
88         }
89
90         pub(crate) fn supports_upfront_shutdown_script(&self) -> bool {
91                 self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0
92         }
93         #[cfg(test)]
94         pub(crate) fn unset_upfront_shutdown_script(&mut self) {
95                 self.flags[0] ^= 1 << 5;
96         }
97
98         pub(crate) fn requires_unknown_bits(&self) -> bool {
99                 self.flags.iter().enumerate().any(|(idx, &byte)| {
100                         ( idx != 0 && (byte & 0x55) != 0 ) || ( idx == 0 && (byte & 0x14) != 0 )
101                 })
102         }
103
104         pub(crate) fn supports_unknown_bits(&self) -> bool {
105                 self.flags.iter().enumerate().any(|(idx, &byte)| {
106                         ( idx != 0 && byte != 0 ) || ( idx == 0 && (byte & 0xc4) != 0 )
107                 })
108         }
109 }
110
111 /// Tracks globalfeatures which are in init messages and routing announcements
112 #[derive(Clone, PartialEq, Debug)]
113 pub struct GlobalFeatures {
114         #[cfg(not(test))]
115         flags: Vec<u8>,
116         // Used to test encoding of diverse msgs
117         #[cfg(test)]
118         pub flags: Vec<u8>
119 }
120
121 impl GlobalFeatures {
122         pub(crate) fn new() -> GlobalFeatures {
123                 GlobalFeatures {
124                         flags: Vec::new(),
125                 }
126         }
127
128         pub(crate) fn requires_unknown_bits(&self) -> bool {
129                 for &byte in self.flags.iter() {
130                         if (byte & 0x55) != 0 {
131                                 return true;
132                         }
133                 }
134                 return false;
135         }
136
137         pub(crate) fn supports_unknown_bits(&self) -> bool {
138                 for &byte in self.flags.iter() {
139                         if byte != 0 {
140                                 return true;
141                         }
142                 }
143                 return false;
144         }
145 }
146
147 /// An init message to be sent or received from a peer
148 pub struct Init {
149         pub(crate) global_features: GlobalFeatures,
150         pub(crate) local_features: LocalFeatures,
151 }
152
153 /// An error message to be sent or received from a peer
154 #[derive(Clone)]
155 pub struct ErrorMessage {
156         pub(crate) channel_id: [u8; 32],
157         pub(crate) data: String,
158 }
159
160 /// A ping message to be sent or received from a peer
161 pub struct Ping {
162         pub(crate) ponglen: u16,
163         pub(crate) byteslen: u16,
164 }
165
166 /// A pong message to be sent or received from a peer
167 pub struct Pong {
168         pub(crate) byteslen: u16,
169 }
170
171 /// An open_channel message to be sent or received from a peer
172 #[derive(Clone)]
173 pub struct OpenChannel {
174         pub(crate) chain_hash: Sha256dHash,
175         pub(crate) temporary_channel_id: [u8; 32],
176         pub(crate) funding_satoshis: u64,
177         pub(crate) push_msat: u64,
178         pub(crate) dust_limit_satoshis: u64,
179         pub(crate) max_htlc_value_in_flight_msat: u64,
180         pub(crate) channel_reserve_satoshis: u64,
181         pub(crate) htlc_minimum_msat: u64,
182         pub(crate) feerate_per_kw: u32,
183         pub(crate) to_self_delay: u16,
184         pub(crate) max_accepted_htlcs: u16,
185         pub(crate) funding_pubkey: PublicKey,
186         pub(crate) revocation_basepoint: PublicKey,
187         pub(crate) payment_basepoint: PublicKey,
188         pub(crate) delayed_payment_basepoint: PublicKey,
189         pub(crate) htlc_basepoint: PublicKey,
190         pub(crate) first_per_commitment_point: PublicKey,
191         pub(crate) channel_flags: u8,
192         pub(crate) shutdown_scriptpubkey: OptionalField<Script>,
193 }
194
195 /// An accept_channel message to be sent or received from a peer
196 #[derive(Clone)]
197 pub struct AcceptChannel {
198         pub(crate) temporary_channel_id: [u8; 32],
199         pub(crate) dust_limit_satoshis: u64,
200         pub(crate) max_htlc_value_in_flight_msat: u64,
201         pub(crate) channel_reserve_satoshis: u64,
202         pub(crate) htlc_minimum_msat: u64,
203         pub(crate) minimum_depth: u32,
204         pub(crate) to_self_delay: u16,
205         pub(crate) max_accepted_htlcs: u16,
206         pub(crate) funding_pubkey: PublicKey,
207         pub(crate) revocation_basepoint: PublicKey,
208         pub(crate) payment_basepoint: PublicKey,
209         pub(crate) delayed_payment_basepoint: PublicKey,
210         pub(crate) htlc_basepoint: PublicKey,
211         pub(crate) first_per_commitment_point: PublicKey,
212         pub(crate) shutdown_scriptpubkey: OptionalField<Script>
213 }
214
215 /// A funding_created message to be sent or received from a peer
216 #[derive(Clone)]
217 pub struct FundingCreated {
218         pub(crate) temporary_channel_id: [u8; 32],
219         pub(crate) funding_txid: Sha256dHash,
220         pub(crate) funding_output_index: u16,
221         pub(crate) signature: Signature,
222 }
223
224 /// A funding_signed message to be sent or received from a peer
225 #[derive(Clone)]
226 pub struct FundingSigned {
227         pub(crate) channel_id: [u8; 32],
228         pub(crate) signature: Signature,
229 }
230
231 /// A funding_locked message to be sent or received from a peer
232 #[derive(Clone, PartialEq)]
233 pub struct FundingLocked {
234         pub(crate) channel_id: [u8; 32],
235         pub(crate) next_per_commitment_point: PublicKey,
236 }
237
238 /// A shutdown message to be sent or received from a peer
239 #[derive(Clone, PartialEq)]
240 pub struct Shutdown {
241         pub(crate) channel_id: [u8; 32],
242         pub(crate) scriptpubkey: Script,
243 }
244
245 /// A closing_signed message to be sent or received from a peer
246 #[derive(Clone, PartialEq)]
247 pub struct ClosingSigned {
248         pub(crate) channel_id: [u8; 32],
249         pub(crate) fee_satoshis: u64,
250         pub(crate) signature: Signature,
251 }
252
253 /// An update_add_htlc message to be sent or received from a peer
254 #[derive(Clone, PartialEq)]
255 pub struct UpdateAddHTLC {
256         pub(crate) channel_id: [u8; 32],
257         pub(crate) htlc_id: u64,
258         pub(crate) amount_msat: u64,
259         pub(crate) payment_hash: PaymentHash,
260         pub(crate) cltv_expiry: u32,
261         pub(crate) onion_routing_packet: OnionPacket,
262 }
263
264 /// An update_fulfill_htlc message to be sent or received from a peer
265 #[derive(Clone, PartialEq)]
266 pub struct UpdateFulfillHTLC {
267         pub(crate) channel_id: [u8; 32],
268         pub(crate) htlc_id: u64,
269         pub(crate) payment_preimage: PaymentPreimage,
270 }
271
272 /// An update_fail_htlc message to be sent or received from a peer
273 #[derive(Clone, PartialEq)]
274 pub struct UpdateFailHTLC {
275         pub(crate) channel_id: [u8; 32],
276         pub(crate) htlc_id: u64,
277         pub(crate) reason: OnionErrorPacket,
278 }
279
280 /// An update_fail_malformed_htlc message to be sent or received from a peer
281 #[derive(Clone, PartialEq)]
282 pub struct UpdateFailMalformedHTLC {
283         pub(crate) channel_id: [u8; 32],
284         pub(crate) htlc_id: u64,
285         pub(crate) sha256_of_onion: [u8; 32],
286         pub(crate) failure_code: u16,
287 }
288
289 /// A commitment_signed message to be sent or received from a peer
290 #[derive(Clone, PartialEq)]
291 pub struct CommitmentSigned {
292         pub(crate) channel_id: [u8; 32],
293         pub(crate) signature: Signature,
294         pub(crate) htlc_signatures: Vec<Signature>,
295 }
296
297 /// A revoke_and_ack message to be sent or received from a peer
298 #[derive(Clone, PartialEq)]
299 pub struct RevokeAndACK {
300         pub(crate) channel_id: [u8; 32],
301         pub(crate) per_commitment_secret: [u8; 32],
302         pub(crate) next_per_commitment_point: PublicKey,
303 }
304
305 /// An update_fee message to be sent or received from a peer
306 #[derive(PartialEq, Clone)]
307 pub struct UpdateFee {
308         pub(crate) channel_id: [u8; 32],
309         pub(crate) feerate_per_kw: u32,
310 }
311
312 #[derive(PartialEq, Clone)]
313 pub(crate) struct DataLossProtect {
314         pub(crate) your_last_per_commitment_secret: [u8; 32],
315         pub(crate) my_current_per_commitment_point: PublicKey,
316 }
317
318 /// A channel_reestablish message to be sent or received from a peer
319 #[derive(PartialEq, Clone)]
320 pub struct ChannelReestablish {
321         pub(crate) channel_id: [u8; 32],
322         pub(crate) next_local_commitment_number: u64,
323         pub(crate) next_remote_commitment_number: u64,
324         pub(crate) data_loss_protect: OptionalField<DataLossProtect>,
325 }
326
327 /// An announcement_signatures message to be sent or received from a peer
328 #[derive(PartialEq, Clone, Debug)]
329 pub struct AnnouncementSignatures {
330         pub(crate) channel_id: [u8; 32],
331         pub(crate) short_channel_id: u64,
332         pub(crate) node_signature: Signature,
333         pub(crate) bitcoin_signature: Signature,
334 }
335
336 /// An address which can be used to connect to a remote peer
337 #[derive(Clone, PartialEq, Debug)]
338 pub enum NetAddress {
339         /// An IPv4 address/port on which the peer is listening.
340         IPv4 {
341                 /// The 4-byte IPv4 address
342                 addr: [u8; 4],
343                 /// The port on which the node is listening
344                 port: u16,
345         },
346         /// An IPv6 address/port on which the peer is listening.
347         IPv6 {
348                 /// The 16-byte IPv6 address
349                 addr: [u8; 16],
350                 /// The port on which the node is listening
351                 port: u16,
352         },
353         /// An old-style Tor onion address/port on which the peer is listening.
354         OnionV2 {
355                 /// The bytes (usually encoded in base32 with ".onion" appended)
356                 addr: [u8; 10],
357                 /// The port on which the node is listening
358                 port: u16,
359         },
360         /// A new-style Tor onion address/port on which the peer is listening.
361         /// To create the human-readable "hostname", concatenate ed25519_pubkey, checksum, and version,
362         /// wrap as base32 and append ".onion".
363         OnionV3 {
364                 /// The ed25519 long-term public key of the peer
365                 ed25519_pubkey: [u8; 32],
366                 /// The checksum of the pubkey and version, as included in the onion address
367                 checksum: u16,
368                 /// The version byte, as defined by the Tor Onion v3 spec.
369                 version: u8,
370                 /// The port on which the node is listening
371                 port: u16,
372         },
373 }
374 impl NetAddress {
375         fn get_id(&self) -> u8 {
376                 match self {
377                         &NetAddress::IPv4 {..} => { 1 },
378                         &NetAddress::IPv6 {..} => { 2 },
379                         &NetAddress::OnionV2 {..} => { 3 },
380                         &NetAddress::OnionV3 {..} => { 4 },
381                 }
382         }
383
384         /// Strict byte-length of address descriptor, 1-byte type not recorded
385         fn len(&self) -> u16 {
386                 match self {
387                         &NetAddress::IPv4 { .. } => { 6 },
388                         &NetAddress::IPv6 { .. } => { 18 },
389                         &NetAddress::OnionV2 { .. } => { 12 },
390                         &NetAddress::OnionV3 { .. } => { 37 },
391                 }
392         }
393 }
394
395 impl Writeable for NetAddress {
396         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
397                 match self {
398                         &NetAddress::IPv4 { ref addr, ref port } => {
399                                 1u8.write(writer)?;
400                                 addr.write(writer)?;
401                                 port.write(writer)?;
402                         },
403                         &NetAddress::IPv6 { ref addr, ref port } => {
404                                 2u8.write(writer)?;
405                                 addr.write(writer)?;
406                                 port.write(writer)?;
407                         },
408                         &NetAddress::OnionV2 { ref addr, ref port } => {
409                                 3u8.write(writer)?;
410                                 addr.write(writer)?;
411                                 port.write(writer)?;
412                         },
413                         &NetAddress::OnionV3 { ref ed25519_pubkey, ref checksum, ref version, ref port } => {
414                                 4u8.write(writer)?;
415                                 ed25519_pubkey.write(writer)?;
416                                 checksum.write(writer)?;
417                                 version.write(writer)?;
418                                 port.write(writer)?;
419                         }
420                 }
421                 Ok(())
422         }
423 }
424
425 impl<R: ::std::io::Read>  Readable<R> for Result<NetAddress, u8> {
426         fn read(reader: &mut R) -> Result<Result<NetAddress, u8>, DecodeError> {
427                 let byte = <u8 as Readable<R>>::read(reader)?;
428                 match byte {
429                         1 => {
430                                 Ok(Ok(NetAddress::IPv4 {
431                                         addr: Readable::read(reader)?,
432                                         port: Readable::read(reader)?,
433                                 }))
434                         },
435                         2 => {
436                                 Ok(Ok(NetAddress::IPv6 {
437                                         addr: Readable::read(reader)?,
438                                         port: Readable::read(reader)?,
439                                 }))
440                         },
441                         3 => {
442                                 Ok(Ok(NetAddress::OnionV2 {
443                                         addr: Readable::read(reader)?,
444                                         port: Readable::read(reader)?,
445                                 }))
446                         },
447                         4 => {
448                                 Ok(Ok(NetAddress::OnionV3 {
449                                         ed25519_pubkey: Readable::read(reader)?,
450                                         checksum: Readable::read(reader)?,
451                                         version: Readable::read(reader)?,
452                                         port: Readable::read(reader)?,
453                                 }))
454                         },
455                         _ => return Ok(Err(byte)),
456                 }
457         }
458 }
459
460 // Only exposed as broadcast of node_announcement should be filtered by node_id
461 /// The unsigned part of a node_announcement
462 #[derive(PartialEq, Clone, Debug)]
463 pub struct UnsignedNodeAnnouncement {
464         pub(crate) features: GlobalFeatures,
465         pub(crate) timestamp: u32,
466         /// The node_id this announcement originated from (don't rebroadcast the node_announcement back
467         /// to this node).
468         pub        node_id: PublicKey,
469         pub(crate) rgb: [u8; 3],
470         pub(crate) alias: [u8; 32],
471         /// List of addresses on which this node is reachable. Note that you may only have up to one
472         /// address of each type, if you have more, they may be silently discarded or we may panic!
473         pub(crate) addresses: Vec<NetAddress>,
474         pub(crate) excess_address_data: Vec<u8>,
475         pub(crate) excess_data: Vec<u8>,
476 }
477 #[derive(PartialEq, Clone)]
478 /// A node_announcement message to be sent or received from a peer
479 pub struct NodeAnnouncement {
480         pub(crate) signature: Signature,
481         pub(crate) contents: UnsignedNodeAnnouncement,
482 }
483
484 // Only exposed as broadcast of channel_announcement should be filtered by node_id
485 /// The unsigned part of a channel_announcement
486 #[derive(PartialEq, Clone, Debug)]
487 pub struct UnsignedChannelAnnouncement {
488         pub(crate) features: GlobalFeatures,
489         pub(crate) chain_hash: Sha256dHash,
490         pub(crate) short_channel_id: u64,
491         /// One of the two node_ids which are endpoints of this channel
492         pub        node_id_1: PublicKey,
493         /// The other of the two node_ids which are endpoints of this channel
494         pub        node_id_2: PublicKey,
495         pub(crate) bitcoin_key_1: PublicKey,
496         pub(crate) bitcoin_key_2: PublicKey,
497         pub(crate) excess_data: Vec<u8>,
498 }
499 /// A channel_announcement message to be sent or received from a peer
500 #[derive(PartialEq, Clone, Debug)]
501 pub struct ChannelAnnouncement {
502         pub(crate) node_signature_1: Signature,
503         pub(crate) node_signature_2: Signature,
504         pub(crate) bitcoin_signature_1: Signature,
505         pub(crate) bitcoin_signature_2: Signature,
506         pub(crate) contents: UnsignedChannelAnnouncement,
507 }
508
509 #[derive(PartialEq, Clone, Debug)]
510 pub(crate) struct UnsignedChannelUpdate {
511         pub(crate) chain_hash: Sha256dHash,
512         pub(crate) short_channel_id: u64,
513         pub(crate) timestamp: u32,
514         pub(crate) flags: u16,
515         pub(crate) cltv_expiry_delta: u16,
516         pub(crate) htlc_minimum_msat: u64,
517         pub(crate) fee_base_msat: u32,
518         pub(crate) fee_proportional_millionths: u32,
519         pub(crate) excess_data: Vec<u8>,
520 }
521 /// A channel_update message to be sent or received from a peer
522 #[derive(PartialEq, Clone, Debug)]
523 pub struct ChannelUpdate {
524         pub(crate) signature: Signature,
525         pub(crate) contents: UnsignedChannelUpdate,
526 }
527
528 /// Used to put an error message in a LightningError
529 #[derive(Clone)]
530 pub enum ErrorAction {
531         /// The peer took some action which made us think they were useless. Disconnect them.
532         DisconnectPeer {
533                 /// An error message which we should make an effort to send before we disconnect.
534                 msg: Option<ErrorMessage>
535         },
536         /// The peer did something harmless that we weren't able to process, just log and ignore
537         IgnoreError,
538         /// The peer did something incorrect. Tell them.
539         SendErrorMessage {
540                 /// The message to send.
541                 msg: ErrorMessage
542         },
543 }
544
545 /// An Err type for failure to process messages.
546 pub struct LightningError {
547         /// A human-readable message describing the error
548         pub err: &'static str,
549         /// The action which should be taken against the offending peer.
550         pub action: ErrorAction,
551 }
552
553 /// Struct used to return values from revoke_and_ack messages, containing a bunch of commitment
554 /// transaction updates if they were pending.
555 #[derive(PartialEq, Clone)]
556 pub struct CommitmentUpdate {
557         /// update_add_htlc messages which should be sent
558         pub update_add_htlcs: Vec<UpdateAddHTLC>,
559         /// update_fulfill_htlc messages which should be sent
560         pub update_fulfill_htlcs: Vec<UpdateFulfillHTLC>,
561         /// update_fail_htlc messages which should be sent
562         pub update_fail_htlcs: Vec<UpdateFailHTLC>,
563         /// update_fail_malformed_htlc messages which should be sent
564         pub update_fail_malformed_htlcs: Vec<UpdateFailMalformedHTLC>,
565         /// An update_fee message which should be sent
566         pub update_fee: Option<UpdateFee>,
567         /// Finally, the commitment_signed message which should be sent
568         pub commitment_signed: CommitmentSigned,
569 }
570
571 /// The information we received from a peer along the route of a payment we originated. This is
572 /// returned by ChannelMessageHandler::handle_update_fail_htlc to be passed into
573 /// RoutingMessageHandler::handle_htlc_fail_channel_update to update our network map.
574 #[derive(Clone)]
575 pub enum HTLCFailChannelUpdate {
576         /// We received an error which included a full ChannelUpdate message.
577         ChannelUpdateMessage {
578                 /// The unwrapped message we received
579                 msg: ChannelUpdate,
580         },
581         /// We received an error which indicated only that a channel has been closed
582         ChannelClosed {
583                 /// The short_channel_id which has now closed.
584                 short_channel_id: u64,
585                 /// when this true, this channel should be permanently removed from the
586                 /// consideration. Otherwise, this channel can be restored as new channel_update is received
587                 is_permanent: bool,
588         },
589         /// We received an error which indicated only that a node has failed
590         NodeFailure {
591                 /// The node_id that has failed.
592                 node_id: PublicKey,
593                 /// when this true, node should be permanently removed from the
594                 /// consideration. Otherwise, the channels connected to this node can be
595                 /// restored as new channel_update is received
596                 is_permanent: bool,
597         }
598 }
599
600 /// Messages could have optional fields to use with extended features
601 /// As we wish to serialize these differently from Option<T>s (Options get a tag byte, but
602 /// OptionalFeild simply gets Present if there are enough bytes to read into it), we have a
603 /// separate enum type for them.
604 #[derive(Clone, PartialEq)]
605 pub enum OptionalField<T> {
606         /// Optional field is included in message
607         Present(T),
608         /// Optional field is absent in message
609         Absent
610 }
611
612 /// A trait to describe an object which can receive channel messages.
613 ///
614 /// Messages MAY be called in parallel when they originate from different their_node_ids, however
615 /// they MUST NOT be called in parallel when the two calls have the same their_node_id.
616 pub trait ChannelMessageHandler : events::MessageSendEventsProvider + Send + Sync {
617         //Channel init:
618         /// Handle an incoming open_channel message from the given peer.
619         fn handle_open_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &OpenChannel) -> Result<(), LightningError>;
620         /// Handle an incoming accept_channel message from the given peer.
621         fn handle_accept_channel(&self, their_node_id: &PublicKey, their_local_features: LocalFeatures, msg: &AcceptChannel) -> Result<(), LightningError>;
622         /// Handle an incoming funding_created message from the given peer.
623         fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &FundingCreated) -> Result<(), LightningError>;
624         /// Handle an incoming funding_signed message from the given peer.
625         fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &FundingSigned) -> Result<(), LightningError>;
626         /// Handle an incoming funding_locked message from the given peer.
627         fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &FundingLocked) -> Result<(), LightningError>;
628
629         // Channl close:
630         /// Handle an incoming shutdown message from the given peer.
631         fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &Shutdown) -> Result<(), LightningError>;
632         /// Handle an incoming closing_signed message from the given peer.
633         fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned) -> Result<(), LightningError>;
634
635         // HTLC handling:
636         /// Handle an incoming update_add_htlc message from the given peer.
637         fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &UpdateAddHTLC) -> Result<(), LightningError>;
638         /// Handle an incoming update_fulfill_htlc message from the given peer.
639         fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFulfillHTLC) -> Result<(), LightningError>;
640         /// Handle an incoming update_fail_htlc message from the given peer.
641         fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC) -> Result<(), LightningError>;
642         /// Handle an incoming update_fail_malformed_htlc message from the given peer.
643         fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailMalformedHTLC) -> Result<(), LightningError>;
644         /// Handle an incoming commitment_signed message from the given peer.
645         fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned) -> Result<(), LightningError>;
646         /// Handle an incoming revoke_and_ack message from the given peer.
647         fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &RevokeAndACK) -> Result<(), LightningError>;
648
649         /// Handle an incoming update_fee message from the given peer.
650         fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &UpdateFee) -> Result<(), LightningError>;
651
652         // Channel-to-announce:
653         /// Handle an incoming announcement_signatures message from the given peer.
654         fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures) -> Result<(), LightningError>;
655
656         // Connection loss/reestablish:
657         /// Indicates a connection to the peer failed/an existing connection was lost. If no connection
658         /// is believed to be possible in the future (eg they're sending us messages we don't
659         /// understand or indicate they require unknown feature bits), no_connection_possible is set
660         /// and any outstanding channels should be failed.
661         fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
662
663         /// Handle a peer reconnecting, possibly generating channel_reestablish message(s).
664         fn peer_connected(&self, their_node_id: &PublicKey);
665         /// Handle an incoming channel_reestablish message from the given peer.
666         fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish) -> Result<(), LightningError>;
667
668         // Error:
669         /// Handle an incoming error message from the given peer.
670         fn handle_error(&self, their_node_id: &PublicKey, msg: &ErrorMessage);
671 }
672
673 /// A trait to describe an object which can receive routing messages.
674 pub trait RoutingMessageHandler : Send + Sync {
675         /// Handle an incoming node_announcement message, returning true if it should be forwarded on,
676         /// false or returning an Err otherwise.
677         fn handle_node_announcement(&self, msg: &NodeAnnouncement) -> Result<bool, LightningError>;
678         /// Handle a channel_announcement message, returning true if it should be forwarded on, false
679         /// or returning an Err otherwise.
680         fn handle_channel_announcement(&self, msg: &ChannelAnnouncement) -> Result<bool, LightningError>;
681         /// Handle an incoming channel_update message, returning true if it should be forwarded on,
682         /// false or returning an Err otherwise.
683         fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result<bool, LightningError>;
684         /// Handle some updates to the route graph that we learned due to an outbound failed payment.
685         fn handle_htlc_fail_channel_update(&self, update: &HTLCFailChannelUpdate);
686         /// Gets a subset of the channel announcements and updates required to dump our routing table
687         /// to a remote node, starting at the short_channel_id indicated by starting_point and
688         /// including batch_amount entries.
689         fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(ChannelAnnouncement, ChannelUpdate, ChannelUpdate)>;
690         /// Gets a subset of the node announcements required to dump our routing table to a remote node,
691         /// starting at the node *after* the provided publickey and including batch_amount entries.
692         /// If None is provided for starting_point, we start at the first node.
693         fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<NodeAnnouncement>;
694 }
695
696 pub(crate) struct OnionRealm0HopData {
697         pub(crate) short_channel_id: u64,
698         pub(crate) amt_to_forward: u64,
699         pub(crate) outgoing_cltv_value: u32,
700         // 12 bytes of 0-padding
701 }
702
703 mod fuzzy_internal_msgs {
704         // These types aren't intended to be pub, but are exposed for direct fuzzing (as we deserialize
705         // them from untrusted input):
706
707         use super::OnionRealm0HopData;
708         pub struct OnionHopData {
709                 pub(crate) realm: u8,
710                 pub(crate) data: OnionRealm0HopData,
711                 pub(crate) hmac: [u8; 32],
712         }
713
714         pub struct DecodedOnionErrorPacket {
715                 pub(crate) hmac: [u8; 32],
716                 pub(crate) failuremsg: Vec<u8>,
717                 pub(crate) pad: Vec<u8>,
718         }
719 }
720 #[cfg(feature = "fuzztarget")]
721 pub use self::fuzzy_internal_msgs::*;
722 #[cfg(not(feature = "fuzztarget"))]
723 pub(crate) use self::fuzzy_internal_msgs::*;
724
725 #[derive(Clone)]
726 pub(crate) struct OnionPacket {
727         pub(crate) version: u8,
728         /// In order to ensure we always return an error on Onion decode in compliance with BOLT 4, we
729         /// have to deserialize OnionPackets contained in UpdateAddHTLCs even if the ephemeral public
730         /// key (here) is bogus, so we hold a Result instead of a PublicKey as we'd like.
731         pub(crate) public_key: Result<PublicKey, secp256k1::Error>,
732         pub(crate) hop_data: [u8; 20*65],
733         pub(crate) hmac: [u8; 32],
734 }
735
736 impl PartialEq for OnionPacket {
737         fn eq(&self, other: &OnionPacket) -> bool {
738                 for (i, j) in self.hop_data.iter().zip(other.hop_data.iter()) {
739                         if i != j { return false; }
740                 }
741                 self.version == other.version &&
742                         self.public_key == other.public_key &&
743                         self.hmac == other.hmac
744         }
745 }
746
747 #[derive(Clone, PartialEq)]
748 pub(crate) struct OnionErrorPacket {
749         // This really should be a constant size slice, but the spec lets these things be up to 128KB?
750         // (TODO) We limit it in decode to much lower...
751         pub(crate) data: Vec<u8>,
752 }
753
754 impl Error for DecodeError {
755         fn description(&self) -> &str {
756                 match *self {
757                         DecodeError::UnknownVersion => "Unknown realm byte in Onion packet",
758                         DecodeError::UnknownRequiredFeature => "Unknown required feature preventing decode",
759                         DecodeError::InvalidValue => "Nonsense bytes didn't map to the type they were interpreted as",
760                         DecodeError::ShortRead => "Packet extended beyond the provided bytes",
761                         DecodeError::ExtraAddressesPerType => "More than one address of a single type",
762                         DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly",
763                         DecodeError::Io(ref e) => e.description(),
764                 }
765         }
766 }
767 impl fmt::Display for DecodeError {
768         fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
769                 f.write_str(self.description())
770         }
771 }
772
773 impl fmt::Debug for LightningError {
774         fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
775                 f.write_str(self.err)
776         }
777 }
778
779 impl From<::std::io::Error> for DecodeError {
780         fn from(e: ::std::io::Error) -> Self {
781                 if e.kind() == ::std::io::ErrorKind::UnexpectedEof {
782                         DecodeError::ShortRead
783                 } else {
784                         DecodeError::Io(e)
785                 }
786         }
787 }
788
789 impl Writeable for OptionalField<Script> {
790         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
791                 match *self {
792                         OptionalField::Present(ref script) => {
793                                 // Note that Writeable for script includes the 16-bit length tag for us
794                                 script.write(w)?;
795                         },
796                         OptionalField::Absent => {}
797                 }
798                 Ok(())
799         }
800 }
801
802 impl<R: Read> Readable<R> for OptionalField<Script> {
803         fn read(r: &mut R) -> Result<Self, DecodeError> {
804                 match <u16 as Readable<R>>::read(r) {
805                         Ok(len) => {
806                                 let mut buf = vec![0; len as usize];
807                                 r.read_exact(&mut buf)?;
808                                 Ok(OptionalField::Present(Script::from(buf)))
809                         },
810                         Err(DecodeError::ShortRead) => Ok(OptionalField::Absent),
811                         Err(e) => Err(e)
812                 }
813         }
814 }
815
816 impl_writeable_len_match!(AcceptChannel, {
817                 {AcceptChannel{ shutdown_scriptpubkey: OptionalField::Present(ref script), .. }, 270 + 2 + script.len()},
818                 {_, 270}
819         }, {
820         temporary_channel_id,
821         dust_limit_satoshis,
822         max_htlc_value_in_flight_msat,
823         channel_reserve_satoshis,
824         htlc_minimum_msat,
825         minimum_depth,
826         to_self_delay,
827         max_accepted_htlcs,
828         funding_pubkey,
829         revocation_basepoint,
830         payment_basepoint,
831         delayed_payment_basepoint,
832         htlc_basepoint,
833         first_per_commitment_point,
834         shutdown_scriptpubkey
835 });
836
837 impl_writeable!(AnnouncementSignatures, 32+8+64*2, {
838         channel_id,
839         short_channel_id,
840         node_signature,
841         bitcoin_signature
842 });
843
844 impl Writeable for ChannelReestablish {
845         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
846                 w.size_hint(if let OptionalField::Present(..) = self.data_loss_protect { 32+2*8+33+32 } else { 32+2*8 });
847                 self.channel_id.write(w)?;
848                 self.next_local_commitment_number.write(w)?;
849                 self.next_remote_commitment_number.write(w)?;
850                 match self.data_loss_protect {
851                         OptionalField::Present(ref data_loss_protect) => {
852                                 (*data_loss_protect).your_last_per_commitment_secret.write(w)?;
853                                 (*data_loss_protect).my_current_per_commitment_point.write(w)?;
854                         },
855                         OptionalField::Absent => {}
856                 }
857                 Ok(())
858         }
859 }
860
861 impl<R: Read> Readable<R> for ChannelReestablish{
862         fn read(r: &mut R) -> Result<Self, DecodeError> {
863                 Ok(Self {
864                         channel_id: Readable::read(r)?,
865                         next_local_commitment_number: Readable::read(r)?,
866                         next_remote_commitment_number: Readable::read(r)?,
867                         data_loss_protect: {
868                                 match <[u8; 32] as Readable<R>>::read(r) {
869                                         Ok(your_last_per_commitment_secret) =>
870                                                 OptionalField::Present(DataLossProtect {
871                                                         your_last_per_commitment_secret,
872                                                         my_current_per_commitment_point: Readable::read(r)?,
873                                                 }),
874                                         Err(DecodeError::ShortRead) => OptionalField::Absent,
875                                         Err(e) => return Err(e)
876                                 }
877                         }
878                 })
879         }
880 }
881
882 impl_writeable!(ClosingSigned, 32+8+64, {
883         channel_id,
884         fee_satoshis,
885         signature
886 });
887
888 impl_writeable_len_match!(CommitmentSigned, {
889                 { CommitmentSigned { ref htlc_signatures, .. }, 32+64+2+htlc_signatures.len()*64 }
890         }, {
891         channel_id,
892         signature,
893         htlc_signatures
894 });
895
896 impl_writeable_len_match!(DecodedOnionErrorPacket, {
897                 { DecodedOnionErrorPacket { ref failuremsg, ref pad, .. }, 32 + 4 + failuremsg.len() + pad.len() }
898         }, {
899         hmac,
900         failuremsg,
901         pad
902 });
903
904 impl_writeable!(FundingCreated, 32+32+2+64, {
905         temporary_channel_id,
906         funding_txid,
907         funding_output_index,
908         signature
909 });
910
911 impl_writeable!(FundingSigned, 32+64, {
912         channel_id,
913         signature
914 });
915
916 impl_writeable!(FundingLocked, 32+33, {
917         channel_id,
918         next_per_commitment_point
919 });
920
921 impl_writeable_len_match!(GlobalFeatures, {
922                 { GlobalFeatures { ref flags }, flags.len() + 2 }
923         }, {
924         flags
925 });
926
927 impl_writeable_len_match!(LocalFeatures, {
928                 { LocalFeatures { ref flags }, flags.len() + 2 }
929         }, {
930         flags
931 });
932
933 impl_writeable_len_match!(Init, {
934                 { Init { ref global_features, ref local_features }, global_features.flags.len() + local_features.flags.len() + 4 }
935         }, {
936         global_features,
937         local_features
938 });
939
940 impl_writeable_len_match!(OpenChannel, {
941                 { OpenChannel { shutdown_scriptpubkey: OptionalField::Present(ref script), .. }, 319 + 2 + script.len() },
942                 { _, 319 }
943         }, {
944         chain_hash,
945         temporary_channel_id,
946         funding_satoshis,
947         push_msat,
948         dust_limit_satoshis,
949         max_htlc_value_in_flight_msat,
950         channel_reserve_satoshis,
951         htlc_minimum_msat,
952         feerate_per_kw,
953         to_self_delay,
954         max_accepted_htlcs,
955         funding_pubkey,
956         revocation_basepoint,
957         payment_basepoint,
958         delayed_payment_basepoint,
959         htlc_basepoint,
960         first_per_commitment_point,
961         channel_flags,
962         shutdown_scriptpubkey
963 });
964
965 impl_writeable!(RevokeAndACK, 32+32+33, {
966         channel_id,
967         per_commitment_secret,
968         next_per_commitment_point
969 });
970
971 impl_writeable_len_match!(Shutdown, {
972                 { Shutdown { ref scriptpubkey, .. }, 32 + 2 + scriptpubkey.len() }
973         }, {
974         channel_id,
975         scriptpubkey
976 });
977
978 impl_writeable_len_match!(UpdateFailHTLC, {
979                 { UpdateFailHTLC { ref reason, .. }, 32 + 10 + reason.data.len() }
980         }, {
981         channel_id,
982         htlc_id,
983         reason
984 });
985
986 impl_writeable!(UpdateFailMalformedHTLC, 32+8+32+2, {
987         channel_id,
988         htlc_id,
989         sha256_of_onion,
990         failure_code
991 });
992
993 impl_writeable!(UpdateFee, 32+4, {
994         channel_id,
995         feerate_per_kw
996 });
997
998 impl_writeable!(UpdateFulfillHTLC, 32+8+32, {
999         channel_id,
1000         htlc_id,
1001         payment_preimage
1002 });
1003
1004 impl_writeable_len_match!(OnionErrorPacket, {
1005                 { OnionErrorPacket { ref data, .. }, 2 + data.len() }
1006         }, {
1007         data
1008 });
1009
1010 impl Writeable for OnionPacket {
1011         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1012                 w.size_hint(1 + 33 + 20*65 + 32);
1013                 self.version.write(w)?;
1014                 match self.public_key {
1015                         Ok(pubkey) => pubkey.write(w)?,
1016                         Err(_) => [0u8;33].write(w)?,
1017                 }
1018                 w.write_all(&self.hop_data)?;
1019                 self.hmac.write(w)?;
1020                 Ok(())
1021         }
1022 }
1023
1024 impl<R: Read> Readable<R> for OnionPacket {
1025         fn read(r: &mut R) -> Result<Self, DecodeError> {
1026                 Ok(OnionPacket {
1027                         version: Readable::read(r)?,
1028                         public_key: {
1029                                 let mut buf = [0u8;33];
1030                                 r.read_exact(&mut buf)?;
1031                                 PublicKey::from_slice(&buf)
1032                         },
1033                         hop_data: Readable::read(r)?,
1034                         hmac: Readable::read(r)?,
1035                 })
1036         }
1037 }
1038
1039 impl_writeable!(UpdateAddHTLC, 32+8+8+32+4+1366, {
1040         channel_id,
1041         htlc_id,
1042         amount_msat,
1043         payment_hash,
1044         cltv_expiry,
1045         onion_routing_packet
1046 });
1047
1048 impl Writeable for OnionRealm0HopData {
1049         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1050                 w.size_hint(32);
1051                 self.short_channel_id.write(w)?;
1052                 self.amt_to_forward.write(w)?;
1053                 self.outgoing_cltv_value.write(w)?;
1054                 w.write_all(&[0;12])?;
1055                 Ok(())
1056         }
1057 }
1058
1059 impl<R: Read> Readable<R> for OnionRealm0HopData {
1060         fn read(r: &mut R) -> Result<Self, DecodeError> {
1061                 Ok(OnionRealm0HopData {
1062                         short_channel_id: Readable::read(r)?,
1063                         amt_to_forward: Readable::read(r)?,
1064                         outgoing_cltv_value: {
1065                                 let v: u32 = Readable::read(r)?;
1066                                 r.read_exact(&mut [0; 12])?;
1067                                 v
1068                         }
1069                 })
1070         }
1071 }
1072
1073 impl Writeable for OnionHopData {
1074         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1075                 w.size_hint(65);
1076                 self.realm.write(w)?;
1077                 self.data.write(w)?;
1078                 self.hmac.write(w)?;
1079                 Ok(())
1080         }
1081 }
1082
1083 impl<R: Read> Readable<R> for OnionHopData {
1084         fn read(r: &mut R) -> Result<Self, DecodeError> {
1085                 Ok(OnionHopData {
1086                         realm: {
1087                                 let r: u8 = Readable::read(r)?;
1088                                 if r != 0 {
1089                                         return Err(DecodeError::UnknownVersion);
1090                                 }
1091                                 r
1092                         },
1093                         data: Readable::read(r)?,
1094                         hmac: Readable::read(r)?,
1095                 })
1096         }
1097 }
1098
1099 impl Writeable for Ping {
1100         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1101                 w.size_hint(self.byteslen as usize + 4);
1102                 self.ponglen.write(w)?;
1103                 vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
1104                 Ok(())
1105         }
1106 }
1107
1108 impl<R: Read> Readable<R> for Ping {
1109         fn read(r: &mut R) -> Result<Self, DecodeError> {
1110                 Ok(Ping {
1111                         ponglen: Readable::read(r)?,
1112                         byteslen: {
1113                                 let byteslen = Readable::read(r)?;
1114                                 r.read_exact(&mut vec![0u8; byteslen as usize][..])?;
1115                                 byteslen
1116                         }
1117                 })
1118         }
1119 }
1120
1121 impl Writeable for Pong {
1122         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1123                 w.size_hint(self.byteslen as usize + 2);
1124                 vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
1125                 Ok(())
1126         }
1127 }
1128
1129 impl<R: Read> Readable<R> for Pong {
1130         fn read(r: &mut R) -> Result<Self, DecodeError> {
1131                 Ok(Pong {
1132                         byteslen: {
1133                                 let byteslen = Readable::read(r)?;
1134                                 r.read_exact(&mut vec![0u8; byteslen as usize][..])?;
1135                                 byteslen
1136                         }
1137                 })
1138         }
1139 }
1140
1141 impl Writeable for UnsignedChannelAnnouncement {
1142         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1143                 w.size_hint(2 + 2*32 + 4*33 + self.features.flags.len() + self.excess_data.len());
1144                 self.features.write(w)?;
1145                 self.chain_hash.write(w)?;
1146                 self.short_channel_id.write(w)?;
1147                 self.node_id_1.write(w)?;
1148                 self.node_id_2.write(w)?;
1149                 self.bitcoin_key_1.write(w)?;
1150                 self.bitcoin_key_2.write(w)?;
1151                 w.write_all(&self.excess_data[..])?;
1152                 Ok(())
1153         }
1154 }
1155
1156 impl<R: Read> Readable<R> for UnsignedChannelAnnouncement {
1157         fn read(r: &mut R) -> Result<Self, DecodeError> {
1158                 Ok(Self {
1159                         features: {
1160                                 let f: GlobalFeatures = Readable::read(r)?;
1161                                 if f.requires_unknown_bits() {
1162                                         return Err(DecodeError::UnknownRequiredFeature);
1163                                 }
1164                                 f
1165                         },
1166                         chain_hash: Readable::read(r)?,
1167                         short_channel_id: Readable::read(r)?,
1168                         node_id_1: Readable::read(r)?,
1169                         node_id_2: Readable::read(r)?,
1170                         bitcoin_key_1: Readable::read(r)?,
1171                         bitcoin_key_2: Readable::read(r)?,
1172                         excess_data: {
1173                                 let mut excess_data = vec![];
1174                                 r.read_to_end(&mut excess_data)?;
1175                                 excess_data
1176                         },
1177                 })
1178         }
1179 }
1180
1181 impl_writeable_len_match!(ChannelAnnouncement, {
1182                 { ChannelAnnouncement { contents: UnsignedChannelAnnouncement {ref features, ref excess_data, ..}, .. },
1183                         2 + 2*32 + 4*33 + features.flags.len() + excess_data.len() + 4*64 }
1184         }, {
1185         node_signature_1,
1186         node_signature_2,
1187         bitcoin_signature_1,
1188         bitcoin_signature_2,
1189         contents
1190 });
1191
1192 impl Writeable for UnsignedChannelUpdate {
1193         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1194                 w.size_hint(64 + self.excess_data.len());
1195                 self.chain_hash.write(w)?;
1196                 self.short_channel_id.write(w)?;
1197                 self.timestamp.write(w)?;
1198                 self.flags.write(w)?;
1199                 self.cltv_expiry_delta.write(w)?;
1200                 self.htlc_minimum_msat.write(w)?;
1201                 self.fee_base_msat.write(w)?;
1202                 self.fee_proportional_millionths.write(w)?;
1203                 w.write_all(&self.excess_data[..])?;
1204                 Ok(())
1205         }
1206 }
1207
1208 impl<R: Read> Readable<R> for UnsignedChannelUpdate {
1209         fn read(r: &mut R) -> Result<Self, DecodeError> {
1210                 Ok(Self {
1211                         chain_hash: Readable::read(r)?,
1212                         short_channel_id: Readable::read(r)?,
1213                         timestamp: Readable::read(r)?,
1214                         flags: Readable::read(r)?,
1215                         cltv_expiry_delta: Readable::read(r)?,
1216                         htlc_minimum_msat: Readable::read(r)?,
1217                         fee_base_msat: Readable::read(r)?,
1218                         fee_proportional_millionths: Readable::read(r)?,
1219                         excess_data: {
1220                                 let mut excess_data = vec![];
1221                                 r.read_to_end(&mut excess_data)?;
1222                                 excess_data
1223                         },
1224                 })
1225         }
1226 }
1227
1228 impl_writeable_len_match!(ChannelUpdate, {
1229                 { ChannelUpdate { contents: UnsignedChannelUpdate {ref excess_data, ..}, .. },
1230                         64 + excess_data.len() + 64 }
1231         }, {
1232         signature,
1233         contents
1234 });
1235
1236 impl Writeable for ErrorMessage {
1237         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1238                 w.size_hint(32 + 2 + self.data.len());
1239                 self.channel_id.write(w)?;
1240                 (self.data.len() as u16).write(w)?;
1241                 w.write_all(self.data.as_bytes())?;
1242                 Ok(())
1243         }
1244 }
1245
1246 impl<R: Read> Readable<R> for ErrorMessage {
1247         fn read(r: &mut R) -> Result<Self, DecodeError> {
1248                 Ok(Self {
1249                         channel_id: Readable::read(r)?,
1250                         data: {
1251                                 let mut sz: usize = <u16 as Readable<R>>::read(r)? as usize;
1252                                 let mut data = vec![];
1253                                 let data_len = r.read_to_end(&mut data)?;
1254                                 sz = cmp::min(data_len, sz);
1255                                 match String::from_utf8(data[..sz as usize].to_vec()) {
1256                                         Ok(s) => s,
1257                                         Err(_) => return Err(DecodeError::InvalidValue),
1258                                 }
1259                         }
1260                 })
1261         }
1262 }
1263
1264 impl Writeable for UnsignedNodeAnnouncement {
1265         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1266                 w.size_hint(64 + 76 + self.features.flags.len() + self.addresses.len()*38 + self.excess_address_data.len() + self.excess_data.len());
1267                 self.features.write(w)?;
1268                 self.timestamp.write(w)?;
1269                 self.node_id.write(w)?;
1270                 w.write_all(&self.rgb)?;
1271                 self.alias.write(w)?;
1272
1273                 let mut addrs_to_encode = self.addresses.clone();
1274                 addrs_to_encode.sort_unstable_by(|a, b| { a.get_id().cmp(&b.get_id()) });
1275                 addrs_to_encode.dedup_by(|a, b| { a.get_id() == b.get_id() });
1276                 let mut addr_len = 0;
1277                 for addr in &addrs_to_encode {
1278                         addr_len += 1 + addr.len();
1279                 }
1280                 (addr_len + self.excess_address_data.len() as u16).write(w)?;
1281                 for addr in addrs_to_encode {
1282                         addr.write(w)?;
1283                 }
1284                 w.write_all(&self.excess_address_data[..])?;
1285                 w.write_all(&self.excess_data[..])?;
1286                 Ok(())
1287         }
1288 }
1289
1290 impl<R: Read> Readable<R> for UnsignedNodeAnnouncement {
1291         fn read(r: &mut R) -> Result<Self, DecodeError> {
1292                 let features: GlobalFeatures = Readable::read(r)?;
1293                 if features.requires_unknown_bits() {
1294                         return Err(DecodeError::UnknownRequiredFeature);
1295                 }
1296                 let timestamp: u32 = Readable::read(r)?;
1297                 let node_id: PublicKey = Readable::read(r)?;
1298                 let mut rgb = [0; 3];
1299                 r.read_exact(&mut rgb)?;
1300                 let alias: [u8; 32] = Readable::read(r)?;
1301
1302                 let addr_len: u16 = Readable::read(r)?;
1303                 let mut addresses: Vec<NetAddress> = Vec::with_capacity(4);
1304                 let mut addr_readpos = 0;
1305                 let mut excess = false;
1306                 let mut excess_byte = 0;
1307                 loop {
1308                         if addr_len <= addr_readpos { break; }
1309                         match Readable::read(r) {
1310                                 Ok(Ok(addr)) => {
1311                                         match addr {
1312                                                 NetAddress::IPv4 { .. } => {
1313                                                         if addresses.len() > 0 {
1314                                                                 return Err(DecodeError::ExtraAddressesPerType);
1315                                                         }
1316                                                 },
1317                                                 NetAddress::IPv6 { .. } => {
1318                                                         if addresses.len() > 1 || (addresses.len() == 1 && addresses[0].get_id() != 1) {
1319                                                                 return Err(DecodeError::ExtraAddressesPerType);
1320                                                         }
1321                                                 },
1322                                                 NetAddress::OnionV2 { .. } => {
1323                                                         if addresses.len() > 2 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 2) {
1324                                                                 return Err(DecodeError::ExtraAddressesPerType);
1325                                                         }
1326                                                 },
1327                                                 NetAddress::OnionV3 { .. } => {
1328                                                         if addresses.len() > 3 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 3) {
1329                                                                 return Err(DecodeError::ExtraAddressesPerType);
1330                                                         }
1331                                                 },
1332                                         }
1333                                         if addr_len < addr_readpos + 1 + addr.len() {
1334                                                 return Err(DecodeError::BadLengthDescriptor);
1335                                         }
1336                                         addr_readpos += (1 + addr.len()) as u16;
1337                                         addresses.push(addr);
1338                                 },
1339                                 Ok(Err(unknown_descriptor)) => {
1340                                         excess = true;
1341                                         excess_byte = unknown_descriptor;
1342                                         break;
1343                                 },
1344                                 Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
1345                                 Err(e) => return Err(e),
1346                         }
1347                 }
1348
1349                 let mut excess_data = vec![];
1350                 let excess_address_data = if addr_readpos < addr_len {
1351                         let mut excess_address_data = vec![0; (addr_len - addr_readpos) as usize];
1352                         r.read_exact(&mut excess_address_data[if excess { 1 } else { 0 }..])?;
1353                         if excess {
1354                                 excess_address_data[0] = excess_byte;
1355                         }
1356                         excess_address_data
1357                 } else {
1358                         if excess {
1359                                 excess_data.push(excess_byte);
1360                         }
1361                         Vec::new()
1362                 };
1363                 r.read_to_end(&mut excess_data)?;
1364                 Ok(UnsignedNodeAnnouncement {
1365                         features,
1366                         timestamp,
1367                         node_id,
1368                         rgb,
1369                         alias,
1370                         addresses,
1371                         excess_address_data,
1372                         excess_data,
1373                 })
1374         }
1375 }
1376
1377 impl_writeable_len_match!(NodeAnnouncement, {
1378                 { NodeAnnouncement { contents: UnsignedNodeAnnouncement { ref features, ref addresses, ref excess_address_data, ref excess_data, ..}, .. },
1379                         64 + 76 + features.flags.len() + addresses.len()*38 + excess_address_data.len() + excess_data.len() }
1380         }, {
1381         signature,
1382         contents
1383 });
1384
1385 #[cfg(test)]
1386 mod tests {
1387         use hex;
1388         use ln::msgs;
1389         use ln::msgs::{GlobalFeatures, LocalFeatures, OptionalField, OnionErrorPacket};
1390         use ln::channelmanager::{PaymentPreimage, PaymentHash};
1391         use util::ser::Writeable;
1392
1393         use bitcoin_hashes::sha256d::Hash as Sha256dHash;
1394         use bitcoin_hashes::hex::FromHex;
1395         use bitcoin::util::address::Address;
1396         use bitcoin::network::constants::Network;
1397         use bitcoin::blockdata::script::Builder;
1398         use bitcoin::blockdata::opcodes;
1399
1400         use secp256k1::key::{PublicKey,SecretKey};
1401         use secp256k1::{Secp256k1, Message};
1402
1403         #[test]
1404         fn encoding_channel_reestablish_no_secret() {
1405                 let cr = msgs::ChannelReestablish {
1406                         channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
1407                         next_local_commitment_number: 3,
1408                         next_remote_commitment_number: 4,
1409                         data_loss_protect: OptionalField::Absent,
1410                 };
1411
1412                 let encoded_value = cr.encode();
1413                 assert_eq!(
1414                         encoded_value,
1415                         vec![4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4]
1416                 );
1417         }
1418
1419         #[test]
1420         fn encoding_channel_reestablish_with_secret() {
1421                 let public_key = {
1422                         let secp_ctx = Secp256k1::new();
1423                         PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap())
1424                 };
1425
1426                 let cr = msgs::ChannelReestablish {
1427                         channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
1428                         next_local_commitment_number: 3,
1429                         next_remote_commitment_number: 4,
1430                         data_loss_protect: OptionalField::Present(msgs::DataLossProtect { your_last_per_commitment_secret: [9;32], my_current_per_commitment_point: public_key}),
1431                 };
1432
1433                 let encoded_value = cr.encode();
1434                 assert_eq!(
1435                         encoded_value,
1436                         vec![4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 3, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30, 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143]
1437                 );
1438         }
1439
1440         macro_rules! get_keys_from {
1441                 ($slice: expr, $secp_ctx: expr) => {
1442                         {
1443                                 let privkey = SecretKey::from_slice(&hex::decode($slice).unwrap()[..]).unwrap();
1444                                 let pubkey = PublicKey::from_secret_key(&$secp_ctx, &privkey);
1445                                 (privkey, pubkey)
1446                         }
1447                 }
1448         }
1449
1450         macro_rules! get_sig_on {
1451                 ($privkey: expr, $ctx: expr, $string: expr) => {
1452                         {
1453                                 let sighash = Message::from_slice(&$string.into_bytes()[..]).unwrap();
1454                                 $ctx.sign(&sighash, &$privkey)
1455                         }
1456                 }
1457         }
1458
1459         #[test]
1460         fn encoding_announcement_signatures() {
1461                 let secp_ctx = Secp256k1::new();
1462                 let (privkey, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1463                 let sig_1 = get_sig_on!(privkey, secp_ctx, String::from("01010101010101010101010101010101"));
1464                 let sig_2 = get_sig_on!(privkey, secp_ctx, String::from("02020202020202020202020202020202"));
1465                 let announcement_signatures = msgs::AnnouncementSignatures {
1466                         channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
1467                         short_channel_id: 2316138423780173,
1468                         node_signature: sig_1,
1469                         bitcoin_signature: sig_2,
1470                 };
1471
1472                 let encoded_value = announcement_signatures.encode();
1473                 assert_eq!(encoded_value, hex::decode("040000000000000005000000000000000600000000000000070000000000000000083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073acf9953cef4700860f5967838eba2bae89288ad188ebf8b20bf995c3ea53a26df1876d0a3a0e13172ba286a673140190c02ba9da60a2e43a745188c8a83c7f3ef").unwrap());
1474         }
1475
1476         fn do_encoding_channel_announcement(unknown_features_bits: bool, non_bitcoin_chain_hash: bool, excess_data: bool) {
1477                 let secp_ctx = Secp256k1::new();
1478                 let (privkey_1, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1479                 let (privkey_2, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
1480                 let (privkey_3, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
1481                 let (privkey_4, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
1482                 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1483                 let sig_2 = get_sig_on!(privkey_2, secp_ctx, String::from("01010101010101010101010101010101"));
1484                 let sig_3 = get_sig_on!(privkey_3, secp_ctx, String::from("01010101010101010101010101010101"));
1485                 let sig_4 = get_sig_on!(privkey_4, secp_ctx, String::from("01010101010101010101010101010101"));
1486                 let mut features = GlobalFeatures::new();
1487                 if unknown_features_bits {
1488                         features.flags = vec![0xFF, 0xFF];
1489                 }
1490                 let unsigned_channel_announcement = msgs::UnsignedChannelAnnouncement {
1491                         features,
1492                         chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
1493                         short_channel_id: 2316138423780173,
1494                         node_id_1: pubkey_1,
1495                         node_id_2: pubkey_2,
1496                         bitcoin_key_1: pubkey_3,
1497                         bitcoin_key_2: pubkey_4,
1498                         excess_data: if excess_data { vec![10, 0, 0, 20, 0, 0, 30, 0, 0, 40] } else { Vec::new() },
1499                 };
1500                 let channel_announcement = msgs::ChannelAnnouncement {
1501                         node_signature_1: sig_1,
1502                         node_signature_2: sig_2,
1503                         bitcoin_signature_1: sig_3,
1504                         bitcoin_signature_2: sig_4,
1505                         contents: unsigned_channel_announcement,
1506                 };
1507                 let encoded_value = channel_announcement.encode();
1508                 let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a1735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap();
1509                 if unknown_features_bits {
1510                         target_value.append(&mut hex::decode("0002ffff").unwrap());
1511                 } else {
1512                         target_value.append(&mut hex::decode("0000").unwrap());
1513                 }
1514                 if non_bitcoin_chain_hash {
1515                         target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
1516                 } else {
1517                         target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
1518                 }
1519                 target_value.append(&mut hex::decode("00083a840000034d031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b").unwrap());
1520                 if excess_data {
1521                         target_value.append(&mut hex::decode("0a00001400001e000028").unwrap());
1522                 }
1523                 assert_eq!(encoded_value, target_value);
1524         }
1525
1526         #[test]
1527         fn encoding_channel_announcement() {
1528                 do_encoding_channel_announcement(false, false, false);
1529                 do_encoding_channel_announcement(true, false, false);
1530                 do_encoding_channel_announcement(true, true, false);
1531                 do_encoding_channel_announcement(true, true, true);
1532                 do_encoding_channel_announcement(false, true, true);
1533                 do_encoding_channel_announcement(false, false, true);
1534                 do_encoding_channel_announcement(false, true, false);
1535                 do_encoding_channel_announcement(true, false, true);
1536         }
1537
1538         fn do_encoding_node_announcement(unknown_features_bits: bool, ipv4: bool, ipv6: bool, onionv2: bool, onionv3: bool, excess_address_data: bool, excess_data: bool) {
1539                 let secp_ctx = Secp256k1::new();
1540                 let (privkey_1, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1541                 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1542                 let mut features = GlobalFeatures::new();
1543                 if unknown_features_bits {
1544                         features.flags = vec![0xFF, 0xFF];
1545                 }
1546                 let mut addresses = Vec::new();
1547                 if ipv4 {
1548                         addresses.push(msgs::NetAddress::IPv4 {
1549                                 addr: [255, 254, 253, 252],
1550                                 port: 9735
1551                         });
1552                 }
1553                 if ipv6 {
1554                         addresses.push(msgs::NetAddress::IPv6 {
1555                                 addr: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240],
1556                                 port: 9735
1557                         });
1558                 }
1559                 if onionv2 {
1560                         addresses.push(msgs::NetAddress::OnionV2 {
1561                                 addr: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246],
1562                                 port: 9735
1563                         });
1564                 }
1565                 if onionv3 {
1566                         addresses.push(msgs::NetAddress::OnionV3 {
1567                                 ed25519_pubkey: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 237, 236, 235, 234, 233, 232, 231, 230, 229, 228, 227, 226, 225, 224],
1568                                 checksum: 32,
1569                                 version: 16,
1570                                 port: 9735
1571                         });
1572                 }
1573                 let mut addr_len = 0;
1574                 for addr in &addresses {
1575                         addr_len += addr.len() + 1;
1576                 }
1577                 let unsigned_node_announcement = msgs::UnsignedNodeAnnouncement {
1578                         features,
1579                         timestamp: 20190119,
1580                         node_id: pubkey_1,
1581                         rgb: [32; 3],
1582                         alias: [16;32],
1583                         addresses,
1584                         excess_address_data: if excess_address_data { vec![33, 108, 40, 11, 83, 149, 162, 84, 110, 126, 75, 38, 99, 224, 79, 129, 22, 34, 241, 90, 79, 146, 232, 58, 162, 233, 43, 162, 165, 115, 193, 57, 20, 44, 84, 174, 99, 7, 42, 30, 193, 238, 125, 192, 192, 75, 222, 92, 132, 120, 6, 23, 42, 160, 92, 146, 194, 42, 232, 227, 8, 209, 210, 105] } else { Vec::new() },
1585                         excess_data: if excess_data { vec![59, 18, 204, 25, 92, 224, 162, 209, 189, 166, 168, 139, 239, 161, 159, 160, 127, 81, 202, 167, 92, 232, 56, 55, 242, 137, 101, 96, 11, 138, 172, 171, 8, 85, 255, 176, 231, 65, 236, 95, 124, 65, 66, 30, 152, 41, 169, 212, 134, 17, 200, 200, 49, 247, 27, 229, 234, 115, 230, 101, 148, 151, 127, 253] } else { Vec::new() },
1586                 };
1587                 addr_len += unsigned_node_announcement.excess_address_data.len() as u16;
1588                 let node_announcement = msgs::NodeAnnouncement {
1589                         signature: sig_1,
1590                         contents: unsigned_node_announcement,
1591                 };
1592                 let encoded_value = node_announcement.encode();
1593                 let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1594                 if unknown_features_bits {
1595                         target_value.append(&mut hex::decode("0002ffff").unwrap());
1596                 } else {
1597                         target_value.append(&mut hex::decode("0000").unwrap());
1598                 }
1599                 target_value.append(&mut hex::decode("013413a7031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f2020201010101010101010101010101010101010101010101010101010101010101010").unwrap());
1600                 target_value.append(&mut vec![(addr_len >> 8) as u8, addr_len as u8]);
1601                 if ipv4 {
1602                         target_value.append(&mut hex::decode("01fffefdfc2607").unwrap());
1603                 }
1604                 if ipv6 {
1605                         target_value.append(&mut hex::decode("02fffefdfcfbfaf9f8f7f6f5f4f3f2f1f02607").unwrap());
1606                 }
1607                 if onionv2 {
1608                         target_value.append(&mut hex::decode("03fffefdfcfbfaf9f8f7f62607").unwrap());
1609                 }
1610                 if onionv3 {
1611                         target_value.append(&mut hex::decode("04fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e00020102607").unwrap());
1612                 }
1613                 if excess_address_data {
1614                         target_value.append(&mut hex::decode("216c280b5395a2546e7e4b2663e04f811622f15a4f92e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d269").unwrap());
1615                 }
1616                 if excess_data {
1617                         target_value.append(&mut hex::decode("3b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap());
1618                 }
1619                 assert_eq!(encoded_value, target_value);
1620         }
1621
1622         #[test]
1623         fn encoding_node_announcement() {
1624                 do_encoding_node_announcement(true, true, true, true, true, true, true);
1625                 do_encoding_node_announcement(false, false, false, false, false, false, false);
1626                 do_encoding_node_announcement(false, true, false, false, false, false, false);
1627                 do_encoding_node_announcement(false, false, true, false, false, false, false);
1628                 do_encoding_node_announcement(false, false, false, true, false, false, false);
1629                 do_encoding_node_announcement(false, false, false, false, true, false, false);
1630                 do_encoding_node_announcement(false, false, false, false, false, true, false);
1631                 do_encoding_node_announcement(false, true, false, true, false, true, false);
1632                 do_encoding_node_announcement(false, false, true, false, true, false, false);
1633         }
1634
1635         fn do_encoding_channel_update(non_bitcoin_chain_hash: bool, direction: bool, disable: bool, htlc_maximum_msat: bool) {
1636                 let secp_ctx = Secp256k1::new();
1637                 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1638                 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1639                 let unsigned_channel_update = msgs::UnsignedChannelUpdate {
1640                         chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
1641                         short_channel_id: 2316138423780173,
1642                         timestamp: 20190119,
1643                         flags: if direction { 1 } else { 0 } | if disable { 1 << 1 } else { 0 } | if htlc_maximum_msat { 1 << 8 } else { 0 },
1644                         cltv_expiry_delta: 144,
1645                         htlc_minimum_msat: 1000000,
1646                         fee_base_msat: 10000,
1647                         fee_proportional_millionths: 20,
1648                         excess_data: if htlc_maximum_msat { vec![0, 0, 0, 0, 59, 154, 202, 0] } else { Vec::new() }
1649                 };
1650                 let channel_update = msgs::ChannelUpdate {
1651                         signature: sig_1,
1652                         contents: unsigned_channel_update
1653                 };
1654                 let encoded_value = channel_update.encode();
1655                 let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1656                 if non_bitcoin_chain_hash {
1657                         target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
1658                 } else {
1659                         target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
1660                 }
1661                 target_value.append(&mut hex::decode("00083a840000034d013413a7").unwrap());
1662                 if htlc_maximum_msat {
1663                         target_value.append(&mut hex::decode("01").unwrap());
1664                 } else {
1665                         target_value.append(&mut hex::decode("00").unwrap());
1666                 }
1667                 target_value.append(&mut hex::decode("00").unwrap());
1668                 if direction {
1669                         let flag = target_value.last_mut().unwrap();
1670                         *flag = 1;
1671                 }
1672                 if disable {
1673                         let flag = target_value.last_mut().unwrap();
1674                         *flag = *flag | 1 << 1;
1675                 }
1676                 target_value.append(&mut hex::decode("009000000000000f42400000271000000014").unwrap());
1677                 if htlc_maximum_msat {
1678                         target_value.append(&mut hex::decode("000000003b9aca00").unwrap());
1679                 }
1680                 assert_eq!(encoded_value, target_value);
1681         }
1682
1683         #[test]
1684         fn encoding_channel_update() {
1685                 do_encoding_channel_update(false, false, false, false);
1686                 do_encoding_channel_update(true, false, false, false);
1687                 do_encoding_channel_update(false, true, false, false);
1688                 do_encoding_channel_update(false, false, true, false);
1689                 do_encoding_channel_update(false, false, false, true);
1690                 do_encoding_channel_update(true, true, true, true);
1691         }
1692
1693         fn do_encoding_open_channel(non_bitcoin_chain_hash: bool, random_bit: bool, shutdown: bool) {
1694                 let secp_ctx = Secp256k1::new();
1695                 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1696                 let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
1697                 let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
1698                 let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
1699                 let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
1700                 let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
1701                 let open_channel = msgs::OpenChannel {
1702                         chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
1703                         temporary_channel_id: [2; 32],
1704                         funding_satoshis: 1311768467284833366,
1705                         push_msat: 2536655962884945560,
1706                         dust_limit_satoshis: 3608586615801332854,
1707                         max_htlc_value_in_flight_msat: 8517154655701053848,
1708                         channel_reserve_satoshis: 8665828695742877976,
1709                         htlc_minimum_msat: 2316138423780173,
1710                         feerate_per_kw: 821716,
1711                         to_self_delay: 49340,
1712                         max_accepted_htlcs: 49340,
1713                         funding_pubkey: pubkey_1,
1714                         revocation_basepoint: pubkey_2,
1715                         payment_basepoint: pubkey_3,
1716                         delayed_payment_basepoint: pubkey_4,
1717                         htlc_basepoint: pubkey_5,
1718                         first_per_commitment_point: pubkey_6,
1719                         channel_flags: if random_bit { 1 << 5 } else { 0 },
1720                         shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
1721                 };
1722                 let encoded_value = open_channel.encode();
1723                 let mut target_value = Vec::new();
1724                 if non_bitcoin_chain_hash {
1725                         target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
1726                 } else {
1727                         target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
1728                 }
1729                 target_value.append(&mut hex::decode("02020202020202020202020202020202020202020202020202020202020202021234567890123456233403289122369832144668701144767633030896203198784335490624111800083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap());
1730                 if random_bit {
1731                         target_value.append(&mut hex::decode("20").unwrap());
1732                 } else {
1733                         target_value.append(&mut hex::decode("00").unwrap());
1734                 }
1735                 if shutdown {
1736                         target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
1737                 }
1738                 assert_eq!(encoded_value, target_value);
1739         }
1740
1741         #[test]
1742         fn encoding_open_channel() {
1743                 do_encoding_open_channel(false, false, false);
1744                 do_encoding_open_channel(true, false, false);
1745                 do_encoding_open_channel(false, true, false);
1746                 do_encoding_open_channel(false, false, true);
1747                 do_encoding_open_channel(true, true, true);
1748         }
1749
1750         fn do_encoding_accept_channel(shutdown: bool) {
1751                 let secp_ctx = Secp256k1::new();
1752                 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1753                 let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
1754                 let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
1755                 let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
1756                 let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
1757                 let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
1758                 let accept_channel = msgs::AcceptChannel {
1759                         temporary_channel_id: [2; 32],
1760                         dust_limit_satoshis: 1311768467284833366,
1761                         max_htlc_value_in_flight_msat: 2536655962884945560,
1762                         channel_reserve_satoshis: 3608586615801332854,
1763                         htlc_minimum_msat: 2316138423780173,
1764                         minimum_depth: 821716,
1765                         to_self_delay: 49340,
1766                         max_accepted_htlcs: 49340,
1767                         funding_pubkey: pubkey_1,
1768                         revocation_basepoint: pubkey_2,
1769                         payment_basepoint: pubkey_3,
1770                         delayed_payment_basepoint: pubkey_4,
1771                         htlc_basepoint: pubkey_5,
1772                         first_per_commitment_point: pubkey_6,
1773                         shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
1774                 };
1775                 let encoded_value = accept_channel.encode();
1776                 let mut target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020212345678901234562334032891223698321446687011447600083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap();
1777                 if shutdown {
1778                         target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
1779                 }
1780                 assert_eq!(encoded_value, target_value);
1781         }
1782
1783         #[test]
1784         fn encoding_accept_channel() {
1785                 do_encoding_accept_channel(false);
1786                 do_encoding_accept_channel(true);
1787         }
1788
1789         #[test]
1790         fn encoding_funding_created() {
1791                 let secp_ctx = Secp256k1::new();
1792                 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1793                 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1794                 let funding_created = msgs::FundingCreated {
1795                         temporary_channel_id: [2; 32],
1796                         funding_txid: Sha256dHash::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap(),
1797                         funding_output_index: 255,
1798                         signature: sig_1,
1799                 };
1800                 let encoded_value = funding_created.encode();
1801                 let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202026e96fe9f8b0ddcd729ba03cfafa5a27b050b39d354dd980814268dfa9a44d4c200ffd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1802                 assert_eq!(encoded_value, target_value);
1803         }
1804
1805         #[test]
1806         fn encoding_funding_signed() {
1807                 let secp_ctx = Secp256k1::new();
1808                 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1809                 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1810                 let funding_signed = msgs::FundingSigned {
1811                         channel_id: [2; 32],
1812                         signature: sig_1,
1813                 };
1814                 let encoded_value = funding_signed.encode();
1815                 let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1816                 assert_eq!(encoded_value, target_value);
1817         }
1818
1819         #[test]
1820         fn encoding_funding_locked() {
1821                 let secp_ctx = Secp256k1::new();
1822                 let (_, pubkey_1,) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1823                 let funding_locked = msgs::FundingLocked {
1824                         channel_id: [2; 32],
1825                         next_per_commitment_point: pubkey_1,
1826                 };
1827                 let encoded_value = funding_locked.encode();
1828                 let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
1829                 assert_eq!(encoded_value, target_value);
1830         }
1831
1832         fn do_encoding_shutdown(script_type: u8) {
1833                 let secp_ctx = Secp256k1::new();
1834                 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1835                 let script = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
1836                 let shutdown = msgs::Shutdown {
1837                         channel_id: [2; 32],
1838                         scriptpubkey: if script_type == 1 { Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey() } else if script_type == 2 { Address::p2sh(&script, Network::Testnet).script_pubkey() } else if script_type == 3 { Address::p2wpkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey() } else { Address::p2wsh(&script, Network::Testnet).script_pubkey() },
1839                 };
1840                 let encoded_value = shutdown.encode();
1841                 let mut target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap();
1842                 if script_type == 1 {
1843                         target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
1844                 } else if script_type == 2 {
1845                         target_value.append(&mut hex::decode("0017a914da1745e9b549bd0bfa1a569971c77eba30cd5a4b87").unwrap());
1846                 } else if script_type == 3 {
1847                         target_value.append(&mut hex::decode("0016001479b000887626b294a914501a4cd226b58b235983").unwrap());
1848                 } else if script_type == 4 {
1849                         target_value.append(&mut hex::decode("002200204ae81572f06e1b88fd5ced7a1a000945432e83e1551e6f721ee9c00b8cc33260").unwrap());
1850                 }
1851                 assert_eq!(encoded_value, target_value);
1852         }
1853
1854         #[test]
1855         fn encoding_shutdown() {
1856                 do_encoding_shutdown(1);
1857                 do_encoding_shutdown(2);
1858                 do_encoding_shutdown(3);
1859                 do_encoding_shutdown(4);
1860         }
1861
1862         #[test]
1863         fn encoding_closing_signed() {
1864                 let secp_ctx = Secp256k1::new();
1865                 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1866                 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1867                 let closing_signed = msgs::ClosingSigned {
1868                         channel_id: [2; 32],
1869                         fee_satoshis: 2316138423780173,
1870                         signature: sig_1,
1871                 };
1872                 let encoded_value = closing_signed.encode();
1873                 let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1874                 assert_eq!(encoded_value, target_value);
1875         }
1876
1877         #[test]
1878         fn encoding_update_add_htlc() {
1879                 let secp_ctx = Secp256k1::new();
1880                 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1881                 let onion_routing_packet = msgs::OnionPacket {
1882                         version: 255,
1883                         public_key: Ok(pubkey_1),
1884                         hop_data: [1; 20*65],
1885                         hmac: [2; 32]
1886                 };
1887                 let update_add_htlc = msgs::UpdateAddHTLC {
1888                         channel_id: [2; 32],
1889                         htlc_id: 2316138423780173,
1890                         amount_msat: 3608586615801332854,
1891                         payment_hash: PaymentHash([1; 32]),
1892                         cltv_expiry: 821716,
1893                         onion_routing_packet
1894                 };
1895                 let encoded_value = update_add_htlc.encode();
1896                 let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d32144668701144760101010101010101010101010101010101010101010101010101010101010101000c89d4ff031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202").unwrap();
1897                 assert_eq!(encoded_value, target_value);
1898         }
1899
1900         #[test]
1901         fn encoding_update_fulfill_htlc() {
1902                 let update_fulfill_htlc = msgs::UpdateFulfillHTLC {
1903                         channel_id: [2; 32],
1904                         htlc_id: 2316138423780173,
1905                         payment_preimage: PaymentPreimage([1; 32]),
1906                 };
1907                 let encoded_value = update_fulfill_htlc.encode();
1908                 let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d0101010101010101010101010101010101010101010101010101010101010101").unwrap();
1909                 assert_eq!(encoded_value, target_value);
1910         }
1911
1912         #[test]
1913         fn encoding_update_fail_htlc() {
1914                 let reason = OnionErrorPacket {
1915                         data: [1; 32].to_vec(),
1916                 };
1917                 let update_fail_htlc = msgs::UpdateFailHTLC {
1918                         channel_id: [2; 32],
1919                         htlc_id: 2316138423780173,
1920                         reason
1921                 };
1922                 let encoded_value = update_fail_htlc.encode();
1923                 let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d00200101010101010101010101010101010101010101010101010101010101010101").unwrap();
1924                 assert_eq!(encoded_value, target_value);
1925         }
1926
1927         #[test]
1928         fn encoding_update_fail_malformed_htlc() {
1929                 let update_fail_malformed_htlc = msgs::UpdateFailMalformedHTLC {
1930                         channel_id: [2; 32],
1931                         htlc_id: 2316138423780173,
1932                         sha256_of_onion: [1; 32],
1933                         failure_code: 255
1934                 };
1935                 let encoded_value = update_fail_malformed_htlc.encode();
1936                 let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d010101010101010101010101010101010101010101010101010101010101010100ff").unwrap();
1937                 assert_eq!(encoded_value, target_value);
1938         }
1939
1940         fn do_encoding_commitment_signed(htlcs: bool) {
1941                 let secp_ctx = Secp256k1::new();
1942                 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1943                 let (privkey_2, _) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
1944                 let (privkey_3, _) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
1945                 let (privkey_4, _) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
1946                 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1947                 let sig_2 = get_sig_on!(privkey_2, secp_ctx, String::from("01010101010101010101010101010101"));
1948                 let sig_3 = get_sig_on!(privkey_3, secp_ctx, String::from("01010101010101010101010101010101"));
1949                 let sig_4 = get_sig_on!(privkey_4, secp_ctx, String::from("01010101010101010101010101010101"));
1950                 let commitment_signed = msgs::CommitmentSigned {
1951                         channel_id: [2; 32],
1952                         signature: sig_1,
1953                         htlc_signatures: if htlcs { vec![sig_2, sig_3, sig_4] } else { Vec::new() },
1954                 };
1955                 let encoded_value = commitment_signed.encode();
1956                 let mut target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1957                 if htlcs {
1958                         target_value.append(&mut hex::decode("00031735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap());
1959                 } else {
1960                         target_value.append(&mut hex::decode("0000").unwrap());
1961                 }
1962                 assert_eq!(encoded_value, target_value);
1963         }
1964
1965         #[test]
1966         fn encoding_commitment_signed() {
1967                 do_encoding_commitment_signed(true);
1968                 do_encoding_commitment_signed(false);
1969         }
1970
1971         #[test]
1972         fn encoding_revoke_and_ack() {
1973                 let secp_ctx = Secp256k1::new();
1974                 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1975                 let raa = msgs::RevokeAndACK {
1976                         channel_id: [2; 32],
1977                         per_commitment_secret: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
1978                         next_per_commitment_point: pubkey_1,
1979                 };
1980                 let encoded_value = raa.encode();
1981                 let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202020101010101010101010101010101010101010101010101010101010101010101031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
1982                 assert_eq!(encoded_value, target_value);
1983         }
1984
1985         #[test]
1986         fn encoding_update_fee() {
1987                 let update_fee = msgs::UpdateFee {
1988                         channel_id: [2; 32],
1989                         feerate_per_kw: 20190119,
1990                 };
1991                 let encoded_value = update_fee.encode();
1992                 let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202013413a7").unwrap();
1993                 assert_eq!(encoded_value, target_value);
1994         }
1995
1996         fn do_encoding_init(unknown_global_bits: bool, initial_routing_sync: bool) {
1997                 let mut global = GlobalFeatures::new();
1998                 if unknown_global_bits {
1999                         global.flags = vec![0xFF, 0xFF];
2000                 }
2001                 let mut local = LocalFeatures::new();
2002                 if initial_routing_sync {
2003                         local.set_initial_routing_sync();
2004                 }
2005                 let init = msgs::Init {
2006                         global_features: global,
2007                         local_features: local,
2008                 };
2009                 let encoded_value = init.encode();
2010                 let mut target_value = Vec::new();
2011                 if unknown_global_bits {
2012                         target_value.append(&mut hex::decode("0002ffff").unwrap());
2013                 } else {
2014                         target_value.append(&mut hex::decode("0000").unwrap());
2015                 }
2016                 if initial_routing_sync {
2017                         target_value.append(&mut hex::decode("00012a").unwrap());
2018                 } else {
2019                         target_value.append(&mut hex::decode("000122").unwrap());
2020                 }
2021                 assert_eq!(encoded_value, target_value);
2022         }
2023
2024         #[test]
2025         fn encoding_init() {
2026                 do_encoding_init(false, false);
2027                 do_encoding_init(true, false);
2028                 do_encoding_init(false, true);
2029                 do_encoding_init(true, true);
2030         }
2031
2032         #[test]
2033         fn encoding_error() {
2034                 let error = msgs::ErrorMessage {
2035                         channel_id: [2; 32],
2036                         data: String::from("rust-lightning"),
2037                 };
2038                 let encoded_value = error.encode();
2039                 let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202000e727573742d6c696768746e696e67").unwrap();
2040                 assert_eq!(encoded_value, target_value);
2041         }
2042
2043         #[test]
2044         fn encoding_ping() {
2045                 let ping = msgs::Ping {
2046                         ponglen: 64,
2047                         byteslen: 64
2048                 };
2049                 let encoded_value = ping.encode();
2050                 let target_value = hex::decode("0040004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
2051                 assert_eq!(encoded_value, target_value);
2052         }
2053
2054         #[test]
2055         fn encoding_pong() {
2056                 let pong = msgs::Pong {
2057                         byteslen: 64
2058                 };
2059                 let encoded_value = pong.encode();
2060                 let target_value = hex::decode("004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
2061                 assert_eq!(encoded_value, target_value);
2062         }
2063 }