1 //! Wire messages, traits representing wire message handlers, and a few error types live here.
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
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.
18 use secp256k1::key::PublicKey;
19 use secp256k1::Signature;
21 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
22 use bitcoin::blockdata::script::Script;
24 use std::error::Error;
27 use std::result::Result;
28 use std::marker::PhantomData;
31 use util::ser::{Readable, Writeable, Writer};
33 use ln::channelmanager::{PaymentPreimage, PaymentHash};
35 /// An error in decoding a message or struct.
37 pub enum DecodeError {
38 /// A version byte specified something we don't know how to handle.
39 /// Includes unknown realm byte in an OnionHopData packet
41 /// Unknown feature mandating we fail to parse message
42 UnknownRequiredFeature,
43 /// Value was invalid, eg a byte which was supposed to be a bool was something other than a 0
44 /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, etc
48 /// node_announcement included more than one address of a given type!
49 ExtraAddressesPerType,
50 /// A length descriptor in the packet didn't describe the later data correctly
52 /// Error from std::io
56 /// The context in which a Feature object appears determines which bits of features the node
57 /// supports will be set. We use this when creating our own Feature objects to select which bits to
58 /// set and when passing around Feature objects to ensure the bits we're checking for are
61 /// This Context represents when the Feature appears in the init message, sent between peers and not
62 /// rumored around the P2P network.
63 pub struct FeatureContextInit {}
64 /// The context in which a Feature object appears determines which bits of features the node
65 /// supports will be set. We use this when creating our own Feature objects to select which bits to
66 /// set and when passing around Feature objects to ensure the bits we're checking for are
69 /// This Context represents when the Feature appears in the node_announcement message, as it is
70 /// rumored around the P2P network.
71 pub struct FeatureContextNode {}
72 /// The context in which a Feature object appears determines which bits of features the node
73 /// supports will be set. We use this when creating our own Feature objects to select which bits to
74 /// set and when passing around Feature objects to ensure the bits we're checking for are
77 /// This Context represents when the Feature appears in the ChannelAnnouncement message, as it is
78 /// rumored around the P2P network.
79 pub struct FeatureContextChannel {}
80 /// The context in which a Feature object appears determines which bits of features the node
81 /// supports will be set. We use this when creating our own Feature objects to select which bits to
82 /// set and when passing around Feature objects to ensure the bits we're checking for are
85 /// This Context represents when the Feature appears in an invoice, used to determine the different
86 /// options available for routing a payment.
88 /// Note that this is currently unused as invoices come to us via a different crate and are not
89 /// native to rust-lightning directly.
90 pub struct FeatureContextInvoice {}
92 /// An internal trait capturing the various future context types
93 pub trait FeatureContext {}
94 impl FeatureContext for FeatureContextInit {}
95 impl FeatureContext for FeatureContextNode {}
96 impl FeatureContext for FeatureContextChannel {}
97 impl FeatureContext for FeatureContextInvoice {}
99 /// An internal trait capturing FeatureContextInit and FeatureContextNode
100 pub trait FeatureContextInitNode : FeatureContext {}
101 impl FeatureContextInitNode for FeatureContextInit {}
102 impl FeatureContextInitNode for FeatureContextNode {}
104 /// Tracks the set of features which a node implements, templated by the context in which it
106 pub struct Features<T: FeatureContext> {
109 // Used to test encoding of diverse msgs
112 mark: PhantomData<T>,
115 impl<T: FeatureContext> Clone for Features<T> {
116 fn clone(&self) -> Self {
118 flags: self.flags.clone(),
123 impl<T: FeatureContext> PartialEq for Features<T> {
124 fn eq(&self, o: &Self) -> bool {
125 self.flags.eq(&o.flags)
128 impl<T: FeatureContext> fmt::Debug for Features<T> {
129 fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
134 /// A feature message as it appears in an init message
135 pub type InitFeatures = Features<FeatureContextInit>;
136 /// A feature message as it appears in a node_announcement message
137 pub type NodeFeatures = Features<FeatureContextNode>;
138 /// A feature message as it appears in a channel_announcement message
139 pub type ChannelFeatures = Features<FeatureContextChannel>;
141 impl<T: FeatureContextInitNode> Features<T> {
142 /// Create a blank Features flags (visibility extended for fuzz tests)
143 #[cfg(not(feature = "fuzztarget"))]
144 pub(crate) fn new() -> Features<T> {
146 flags: vec![2 | 1 << 5],
150 #[cfg(feature = "fuzztarget")]
151 pub fn new() -> Features<T> {
153 flags: vec![2 | 1 << 5],
159 impl Features<FeatureContextChannel> {
160 /// Create a blank Features flags (visibility extended for fuzz tests)
161 #[cfg(not(feature = "fuzztarget"))]
162 pub(crate) fn new() -> Features<FeatureContextChannel> {
168 #[cfg(feature = "fuzztarget")]
169 pub fn new() -> Features<FeatureContextChannel> {
177 impl<T: FeatureContext> Features<T> {
178 pub(crate) fn requires_unknown_bits(&self) -> bool {
179 self.flags.iter().enumerate().any(|(idx, &byte)| {
180 ( idx != 0 && (byte & 0x55) != 0 ) || ( idx == 0 && (byte & 0x14) != 0 )
184 pub(crate) fn supports_unknown_bits(&self) -> bool {
185 self.flags.iter().enumerate().any(|(idx, &byte)| {
186 ( idx != 0 && byte != 0 ) || ( idx == 0 && (byte & 0xc4) != 0 )
190 /// The number of bytes required to represent the feaature flags present. This does not include
191 /// the length bytes which are included in the serialized form.
192 pub(crate) fn byte_count(&self) -> usize {
197 impl<T: FeatureContextInitNode> Features<T> {
198 pub(crate) fn supports_data_loss_protect(&self) -> bool {
199 self.flags.len() > 0 && (self.flags[0] & 3) != 0
202 pub(crate) fn supports_upfront_shutdown_script(&self) -> bool {
203 self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0
206 pub(crate) fn unset_upfront_shutdown_script(&mut self) {
207 self.flags[0] ^= 1 << 5;
211 impl Features<FeatureContextInit> {
212 pub(crate) fn initial_routing_sync(&self) -> bool {
213 self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0
215 pub(crate) fn set_initial_routing_sync(&mut self) {
216 if self.flags.len() == 0 {
217 self.flags.resize(1, 1 << 3);
219 self.flags[0] |= 1 << 3;
223 /// Writes all features present up to, and including, 13.
224 pub(crate) fn write_up_to_13<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
225 let len = cmp::min(2, self.flags.len());
226 w.size_hint(len + 2);
227 (len as u16).write(w)?;
228 for i in (0..len).rev() {
230 self.flags[i].write(w)?;
232 (self.flags[i] & ((1 << (14 - 8)) - 1)).write(w)?;
238 /// or's another InitFeatures into this one.
239 pub(crate) fn or(&mut self, o: &InitFeatures) {
240 let total_feature_len = cmp::max(self.flags.len(), o.flags.len());
241 self.flags.resize(total_feature_len, 0u8);
242 for (feature, o_feature) in self.flags.iter_mut().zip(o.flags.iter()) {
243 *feature |= *o_feature;
248 impl<T: FeatureContext> Writeable for Features<T> {
249 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
250 w.size_hint(self.flags.len() + 2);
255 impl<R: ::std::io::Read, T: FeatureContext> Readable<R> for Features<T> {
256 fn read(r: &mut R) -> Result<Self, DecodeError> {
258 flags: Readable::read(r)?,
263 /// An init message to be sent or received from a peer
265 pub(crate) features: InitFeatures,
268 /// An error message to be sent or received from a peer
270 pub struct ErrorMessage {
271 pub(crate) channel_id: [u8; 32],
272 pub(crate) data: String,
275 /// A ping message to be sent or received from a peer
277 pub(crate) ponglen: u16,
278 pub(crate) byteslen: u16,
281 /// A pong message to be sent or received from a peer
283 pub(crate) byteslen: u16,
286 /// An open_channel message to be sent or received from a peer
288 pub struct OpenChannel {
289 pub(crate) chain_hash: Sha256dHash,
290 pub(crate) temporary_channel_id: [u8; 32],
291 pub(crate) funding_satoshis: u64,
292 pub(crate) push_msat: u64,
293 pub(crate) dust_limit_satoshis: u64,
294 pub(crate) max_htlc_value_in_flight_msat: u64,
295 pub(crate) channel_reserve_satoshis: u64,
296 pub(crate) htlc_minimum_msat: u64,
297 pub(crate) feerate_per_kw: u32,
298 pub(crate) to_self_delay: u16,
299 pub(crate) max_accepted_htlcs: u16,
300 pub(crate) funding_pubkey: PublicKey,
301 pub(crate) revocation_basepoint: PublicKey,
302 pub(crate) payment_basepoint: PublicKey,
303 pub(crate) delayed_payment_basepoint: PublicKey,
304 pub(crate) htlc_basepoint: PublicKey,
305 pub(crate) first_per_commitment_point: PublicKey,
306 pub(crate) channel_flags: u8,
307 pub(crate) shutdown_scriptpubkey: OptionalField<Script>,
310 /// An accept_channel message to be sent or received from a peer
312 pub struct AcceptChannel {
313 pub(crate) temporary_channel_id: [u8; 32],
314 pub(crate) dust_limit_satoshis: u64,
315 pub(crate) max_htlc_value_in_flight_msat: u64,
316 pub(crate) channel_reserve_satoshis: u64,
317 pub(crate) htlc_minimum_msat: u64,
318 pub(crate) minimum_depth: u32,
319 pub(crate) to_self_delay: u16,
320 pub(crate) max_accepted_htlcs: u16,
321 pub(crate) funding_pubkey: PublicKey,
322 pub(crate) revocation_basepoint: PublicKey,
323 pub(crate) payment_basepoint: PublicKey,
324 pub(crate) delayed_payment_basepoint: PublicKey,
325 pub(crate) htlc_basepoint: PublicKey,
326 pub(crate) first_per_commitment_point: PublicKey,
327 pub(crate) shutdown_scriptpubkey: OptionalField<Script>
330 /// A funding_created message to be sent or received from a peer
332 pub struct FundingCreated {
333 pub(crate) temporary_channel_id: [u8; 32],
334 pub(crate) funding_txid: Sha256dHash,
335 pub(crate) funding_output_index: u16,
336 pub(crate) signature: Signature,
339 /// A funding_signed message to be sent or received from a peer
341 pub struct FundingSigned {
342 pub(crate) channel_id: [u8; 32],
343 pub(crate) signature: Signature,
346 /// A funding_locked message to be sent or received from a peer
347 #[derive(Clone, PartialEq)]
348 pub struct FundingLocked {
349 pub(crate) channel_id: [u8; 32],
350 pub(crate) next_per_commitment_point: PublicKey,
353 /// A shutdown message to be sent or received from a peer
354 #[derive(Clone, PartialEq)]
355 pub struct Shutdown {
356 pub(crate) channel_id: [u8; 32],
357 pub(crate) scriptpubkey: Script,
360 /// A closing_signed message to be sent or received from a peer
361 #[derive(Clone, PartialEq)]
362 pub struct ClosingSigned {
363 pub(crate) channel_id: [u8; 32],
364 pub(crate) fee_satoshis: u64,
365 pub(crate) signature: Signature,
368 /// An update_add_htlc message to be sent or received from a peer
369 #[derive(Clone, PartialEq)]
370 pub struct UpdateAddHTLC {
371 pub(crate) channel_id: [u8; 32],
372 pub(crate) htlc_id: u64,
373 pub(crate) amount_msat: u64,
374 pub(crate) payment_hash: PaymentHash,
375 pub(crate) cltv_expiry: u32,
376 pub(crate) onion_routing_packet: OnionPacket,
379 /// An update_fulfill_htlc message to be sent or received from a peer
380 #[derive(Clone, PartialEq)]
381 pub struct UpdateFulfillHTLC {
382 pub(crate) channel_id: [u8; 32],
383 pub(crate) htlc_id: u64,
384 pub(crate) payment_preimage: PaymentPreimage,
387 /// An update_fail_htlc message to be sent or received from a peer
388 #[derive(Clone, PartialEq)]
389 pub struct UpdateFailHTLC {
390 pub(crate) channel_id: [u8; 32],
391 pub(crate) htlc_id: u64,
392 pub(crate) reason: OnionErrorPacket,
395 /// An update_fail_malformed_htlc message to be sent or received from a peer
396 #[derive(Clone, PartialEq)]
397 pub struct UpdateFailMalformedHTLC {
398 pub(crate) channel_id: [u8; 32],
399 pub(crate) htlc_id: u64,
400 pub(crate) sha256_of_onion: [u8; 32],
401 pub(crate) failure_code: u16,
404 /// A commitment_signed message to be sent or received from a peer
405 #[derive(Clone, PartialEq)]
406 pub struct CommitmentSigned {
407 pub(crate) channel_id: [u8; 32],
408 pub(crate) signature: Signature,
409 pub(crate) htlc_signatures: Vec<Signature>,
412 /// A revoke_and_ack message to be sent or received from a peer
413 #[derive(Clone, PartialEq)]
414 pub struct RevokeAndACK {
415 pub(crate) channel_id: [u8; 32],
416 pub(crate) per_commitment_secret: [u8; 32],
417 pub(crate) next_per_commitment_point: PublicKey,
420 /// An update_fee message to be sent or received from a peer
421 #[derive(PartialEq, Clone)]
422 pub struct UpdateFee {
423 pub(crate) channel_id: [u8; 32],
424 pub(crate) feerate_per_kw: u32,
427 #[derive(PartialEq, Clone)]
428 pub(crate) struct DataLossProtect {
429 pub(crate) your_last_per_commitment_secret: [u8; 32],
430 pub(crate) my_current_per_commitment_point: PublicKey,
433 /// A channel_reestablish message to be sent or received from a peer
434 #[derive(PartialEq, Clone)]
435 pub struct ChannelReestablish {
436 pub(crate) channel_id: [u8; 32],
437 pub(crate) next_local_commitment_number: u64,
438 pub(crate) next_remote_commitment_number: u64,
439 pub(crate) data_loss_protect: OptionalField<DataLossProtect>,
442 /// An announcement_signatures message to be sent or received from a peer
443 #[derive(PartialEq, Clone, Debug)]
444 pub struct AnnouncementSignatures {
445 pub(crate) channel_id: [u8; 32],
446 pub(crate) short_channel_id: u64,
447 pub(crate) node_signature: Signature,
448 pub(crate) bitcoin_signature: Signature,
451 /// An address which can be used to connect to a remote peer
452 #[derive(Clone, PartialEq, Debug)]
453 pub enum NetAddress {
454 /// An IPv4 address/port on which the peer is listening.
456 /// The 4-byte IPv4 address
458 /// The port on which the node is listening
461 /// An IPv6 address/port on which the peer is listening.
463 /// The 16-byte IPv6 address
465 /// The port on which the node is listening
468 /// An old-style Tor onion address/port on which the peer is listening.
470 /// The bytes (usually encoded in base32 with ".onion" appended)
472 /// The port on which the node is listening
475 /// A new-style Tor onion address/port on which the peer is listening.
476 /// To create the human-readable "hostname", concatenate ed25519_pubkey, checksum, and version,
477 /// wrap as base32 and append ".onion".
479 /// The ed25519 long-term public key of the peer
480 ed25519_pubkey: [u8; 32],
481 /// The checksum of the pubkey and version, as included in the onion address
483 /// The version byte, as defined by the Tor Onion v3 spec.
485 /// The port on which the node is listening
490 fn get_id(&self) -> u8 {
492 &NetAddress::IPv4 {..} => { 1 },
493 &NetAddress::IPv6 {..} => { 2 },
494 &NetAddress::OnionV2 {..} => { 3 },
495 &NetAddress::OnionV3 {..} => { 4 },
499 /// Strict byte-length of address descriptor, 1-byte type not recorded
500 fn len(&self) -> u16 {
502 &NetAddress::IPv4 { .. } => { 6 },
503 &NetAddress::IPv6 { .. } => { 18 },
504 &NetAddress::OnionV2 { .. } => { 12 },
505 &NetAddress::OnionV3 { .. } => { 37 },
510 impl Writeable for NetAddress {
511 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
513 &NetAddress::IPv4 { ref addr, ref port } => {
518 &NetAddress::IPv6 { ref addr, ref port } => {
523 &NetAddress::OnionV2 { ref addr, ref port } => {
528 &NetAddress::OnionV3 { ref ed25519_pubkey, ref checksum, ref version, ref port } => {
530 ed25519_pubkey.write(writer)?;
531 checksum.write(writer)?;
532 version.write(writer)?;
540 impl<R: ::std::io::Read> Readable<R> for Result<NetAddress, u8> {
541 fn read(reader: &mut R) -> Result<Result<NetAddress, u8>, DecodeError> {
542 let byte = <u8 as Readable<R>>::read(reader)?;
545 Ok(Ok(NetAddress::IPv4 {
546 addr: Readable::read(reader)?,
547 port: Readable::read(reader)?,
551 Ok(Ok(NetAddress::IPv6 {
552 addr: Readable::read(reader)?,
553 port: Readable::read(reader)?,
557 Ok(Ok(NetAddress::OnionV2 {
558 addr: Readable::read(reader)?,
559 port: Readable::read(reader)?,
563 Ok(Ok(NetAddress::OnionV3 {
564 ed25519_pubkey: Readable::read(reader)?,
565 checksum: Readable::read(reader)?,
566 version: Readable::read(reader)?,
567 port: Readable::read(reader)?,
570 _ => return Ok(Err(byte)),
575 // Only exposed as broadcast of node_announcement should be filtered by node_id
576 /// The unsigned part of a node_announcement
577 #[derive(PartialEq, Clone, Debug)]
578 pub struct UnsignedNodeAnnouncement {
579 pub(crate) features: NodeFeatures,
580 pub(crate) timestamp: u32,
581 /// The node_id this announcement originated from (don't rebroadcast the node_announcement back
583 pub node_id: PublicKey,
584 pub(crate) rgb: [u8; 3],
585 pub(crate) alias: [u8; 32],
586 /// List of addresses on which this node is reachable. Note that you may only have up to one
587 /// address of each type, if you have more, they may be silently discarded or we may panic!
588 pub(crate) addresses: Vec<NetAddress>,
589 pub(crate) excess_address_data: Vec<u8>,
590 pub(crate) excess_data: Vec<u8>,
592 #[derive(PartialEq, Clone)]
593 /// A node_announcement message to be sent or received from a peer
594 pub struct NodeAnnouncement {
595 pub(crate) signature: Signature,
596 pub(crate) contents: UnsignedNodeAnnouncement,
599 // Only exposed as broadcast of channel_announcement should be filtered by node_id
600 /// The unsigned part of a channel_announcement
601 #[derive(PartialEq, Clone, Debug)]
602 pub struct UnsignedChannelAnnouncement {
603 pub(crate) features: ChannelFeatures,
604 pub(crate) chain_hash: Sha256dHash,
605 pub(crate) short_channel_id: u64,
606 /// One of the two node_ids which are endpoints of this channel
607 pub node_id_1: PublicKey,
608 /// The other of the two node_ids which are endpoints of this channel
609 pub node_id_2: PublicKey,
610 pub(crate) bitcoin_key_1: PublicKey,
611 pub(crate) bitcoin_key_2: PublicKey,
612 pub(crate) excess_data: Vec<u8>,
614 /// A channel_announcement message to be sent or received from a peer
615 #[derive(PartialEq, Clone, Debug)]
616 pub struct ChannelAnnouncement {
617 pub(crate) node_signature_1: Signature,
618 pub(crate) node_signature_2: Signature,
619 pub(crate) bitcoin_signature_1: Signature,
620 pub(crate) bitcoin_signature_2: Signature,
621 pub(crate) contents: UnsignedChannelAnnouncement,
624 #[derive(PartialEq, Clone, Debug)]
625 pub(crate) struct UnsignedChannelUpdate {
626 pub(crate) chain_hash: Sha256dHash,
627 pub(crate) short_channel_id: u64,
628 pub(crate) timestamp: u32,
629 pub(crate) flags: u16,
630 pub(crate) cltv_expiry_delta: u16,
631 pub(crate) htlc_minimum_msat: u64,
632 pub(crate) fee_base_msat: u32,
633 pub(crate) fee_proportional_millionths: u32,
634 pub(crate) excess_data: Vec<u8>,
636 /// A channel_update message to be sent or received from a peer
637 #[derive(PartialEq, Clone, Debug)]
638 pub struct ChannelUpdate {
639 pub(crate) signature: Signature,
640 pub(crate) contents: UnsignedChannelUpdate,
643 /// Used to put an error message in a LightningError
645 pub enum ErrorAction {
646 /// The peer took some action which made us think they were useless. Disconnect them.
648 /// An error message which we should make an effort to send before we disconnect.
649 msg: Option<ErrorMessage>
651 /// The peer did something harmless that we weren't able to process, just log and ignore
653 /// The peer did something incorrect. Tell them.
655 /// The message to send.
660 /// An Err type for failure to process messages.
661 pub struct LightningError {
662 /// A human-readable message describing the error
663 pub err: &'static str,
664 /// The action which should be taken against the offending peer.
665 pub action: ErrorAction,
668 /// Struct used to return values from revoke_and_ack messages, containing a bunch of commitment
669 /// transaction updates if they were pending.
670 #[derive(PartialEq, Clone)]
671 pub struct CommitmentUpdate {
672 /// update_add_htlc messages which should be sent
673 pub update_add_htlcs: Vec<UpdateAddHTLC>,
674 /// update_fulfill_htlc messages which should be sent
675 pub update_fulfill_htlcs: Vec<UpdateFulfillHTLC>,
676 /// update_fail_htlc messages which should be sent
677 pub update_fail_htlcs: Vec<UpdateFailHTLC>,
678 /// update_fail_malformed_htlc messages which should be sent
679 pub update_fail_malformed_htlcs: Vec<UpdateFailMalformedHTLC>,
680 /// An update_fee message which should be sent
681 pub update_fee: Option<UpdateFee>,
682 /// Finally, the commitment_signed message which should be sent
683 pub commitment_signed: CommitmentSigned,
686 /// The information we received from a peer along the route of a payment we originated. This is
687 /// returned by ChannelMessageHandler::handle_update_fail_htlc to be passed into
688 /// RoutingMessageHandler::handle_htlc_fail_channel_update to update our network map.
690 pub enum HTLCFailChannelUpdate {
691 /// We received an error which included a full ChannelUpdate message.
692 ChannelUpdateMessage {
693 /// The unwrapped message we received
696 /// We received an error which indicated only that a channel has been closed
698 /// The short_channel_id which has now closed.
699 short_channel_id: u64,
700 /// when this true, this channel should be permanently removed from the
701 /// consideration. Otherwise, this channel can be restored as new channel_update is received
704 /// We received an error which indicated only that a node has failed
706 /// The node_id that has failed.
708 /// when this true, node should be permanently removed from the
709 /// consideration. Otherwise, the channels connected to this node can be
710 /// restored as new channel_update is received
715 /// Messages could have optional fields to use with extended features
716 /// As we wish to serialize these differently from Option<T>s (Options get a tag byte, but
717 /// OptionalFeild simply gets Present if there are enough bytes to read into it), we have a
718 /// separate enum type for them.
719 #[derive(Clone, PartialEq)]
720 pub enum OptionalField<T> {
721 /// Optional field is included in message
723 /// Optional field is absent in message
727 /// A trait to describe an object which can receive channel messages.
729 /// Messages MAY be called in parallel when they originate from different their_node_ids, however
730 /// they MUST NOT be called in parallel when the two calls have the same their_node_id.
731 pub trait ChannelMessageHandler : events::MessageSendEventsProvider + Send + Sync {
733 /// Handle an incoming open_channel message from the given peer.
734 fn handle_open_channel(&self, their_node_id: &PublicKey, their_features: InitFeatures, msg: &OpenChannel);
735 /// Handle an incoming accept_channel message from the given peer.
736 fn handle_accept_channel(&self, their_node_id: &PublicKey, their_features: InitFeatures, msg: &AcceptChannel);
737 /// Handle an incoming funding_created message from the given peer.
738 fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &FundingCreated);
739 /// Handle an incoming funding_signed message from the given peer.
740 fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &FundingSigned);
741 /// Handle an incoming funding_locked message from the given peer.
742 fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &FundingLocked);
745 /// Handle an incoming shutdown message from the given peer.
746 fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &Shutdown);
747 /// Handle an incoming closing_signed message from the given peer.
748 fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned);
751 /// Handle an incoming update_add_htlc message from the given peer.
752 fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &UpdateAddHTLC);
753 /// Handle an incoming update_fulfill_htlc message from the given peer.
754 fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFulfillHTLC);
755 /// Handle an incoming update_fail_htlc message from the given peer.
756 fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC);
757 /// Handle an incoming update_fail_malformed_htlc message from the given peer.
758 fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailMalformedHTLC);
759 /// Handle an incoming commitment_signed message from the given peer.
760 fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned);
761 /// Handle an incoming revoke_and_ack message from the given peer.
762 fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &RevokeAndACK);
764 /// Handle an incoming update_fee message from the given peer.
765 fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &UpdateFee);
767 // Channel-to-announce:
768 /// Handle an incoming announcement_signatures message from the given peer.
769 fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures);
771 // Connection loss/reestablish:
772 /// Indicates a connection to the peer failed/an existing connection was lost. If no connection
773 /// is believed to be possible in the future (eg they're sending us messages we don't
774 /// understand or indicate they require unknown feature bits), no_connection_possible is set
775 /// and any outstanding channels should be failed.
776 fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
778 /// Handle a peer reconnecting, possibly generating channel_reestablish message(s).
779 fn peer_connected(&self, their_node_id: &PublicKey);
780 /// Handle an incoming channel_reestablish message from the given peer.
781 fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish);
784 /// Handle an incoming error message from the given peer.
785 fn handle_error(&self, their_node_id: &PublicKey, msg: &ErrorMessage);
788 /// A trait to describe an object which can receive routing messages.
789 pub trait RoutingMessageHandler : Send + Sync {
790 /// Handle an incoming node_announcement message, returning true if it should be forwarded on,
791 /// false or returning an Err otherwise.
792 fn handle_node_announcement(&self, msg: &NodeAnnouncement) -> Result<bool, LightningError>;
793 /// Handle a channel_announcement message, returning true if it should be forwarded on, false
794 /// or returning an Err otherwise.
795 fn handle_channel_announcement(&self, msg: &ChannelAnnouncement) -> Result<bool, LightningError>;
796 /// Handle an incoming channel_update message, returning true if it should be forwarded on,
797 /// false or returning an Err otherwise.
798 fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result<bool, LightningError>;
799 /// Handle some updates to the route graph that we learned due to an outbound failed payment.
800 fn handle_htlc_fail_channel_update(&self, update: &HTLCFailChannelUpdate);
801 /// Gets a subset of the channel announcements and updates required to dump our routing table
802 /// to a remote node, starting at the short_channel_id indicated by starting_point and
803 /// including batch_amount entries.
804 fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(ChannelAnnouncement, ChannelUpdate, ChannelUpdate)>;
805 /// Gets a subset of the node announcements required to dump our routing table to a remote node,
806 /// starting at the node *after* the provided publickey and including batch_amount entries.
807 /// If None is provided for starting_point, we start at the first node.
808 fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<NodeAnnouncement>;
811 pub(crate) struct OnionRealm0HopData {
812 pub(crate) short_channel_id: u64,
813 pub(crate) amt_to_forward: u64,
814 pub(crate) outgoing_cltv_value: u32,
815 // 12 bytes of 0-padding
818 mod fuzzy_internal_msgs {
819 // These types aren't intended to be pub, but are exposed for direct fuzzing (as we deserialize
820 // them from untrusted input):
822 use super::OnionRealm0HopData;
823 pub struct OnionHopData {
824 pub(crate) realm: u8,
825 pub(crate) data: OnionRealm0HopData,
826 pub(crate) hmac: [u8; 32],
829 pub struct DecodedOnionErrorPacket {
830 pub(crate) hmac: [u8; 32],
831 pub(crate) failuremsg: Vec<u8>,
832 pub(crate) pad: Vec<u8>,
835 #[cfg(feature = "fuzztarget")]
836 pub use self::fuzzy_internal_msgs::*;
837 #[cfg(not(feature = "fuzztarget"))]
838 pub(crate) use self::fuzzy_internal_msgs::*;
841 pub(crate) struct OnionPacket {
842 pub(crate) version: u8,
843 /// In order to ensure we always return an error on Onion decode in compliance with BOLT 4, we
844 /// have to deserialize OnionPackets contained in UpdateAddHTLCs even if the ephemeral public
845 /// key (here) is bogus, so we hold a Result instead of a PublicKey as we'd like.
846 pub(crate) public_key: Result<PublicKey, secp256k1::Error>,
847 pub(crate) hop_data: [u8; 20*65],
848 pub(crate) hmac: [u8; 32],
851 impl PartialEq for OnionPacket {
852 fn eq(&self, other: &OnionPacket) -> bool {
853 for (i, j) in self.hop_data.iter().zip(other.hop_data.iter()) {
854 if i != j { return false; }
856 self.version == other.version &&
857 self.public_key == other.public_key &&
858 self.hmac == other.hmac
862 #[derive(Clone, PartialEq)]
863 pub(crate) struct OnionErrorPacket {
864 // This really should be a constant size slice, but the spec lets these things be up to 128KB?
865 // (TODO) We limit it in decode to much lower...
866 pub(crate) data: Vec<u8>,
869 impl Error for DecodeError {
870 fn description(&self) -> &str {
872 DecodeError::UnknownVersion => "Unknown realm byte in Onion packet",
873 DecodeError::UnknownRequiredFeature => "Unknown required feature preventing decode",
874 DecodeError::InvalidValue => "Nonsense bytes didn't map to the type they were interpreted as",
875 DecodeError::ShortRead => "Packet extended beyond the provided bytes",
876 DecodeError::ExtraAddressesPerType => "More than one address of a single type",
877 DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly",
878 DecodeError::Io(ref e) => e.description(),
882 impl fmt::Display for DecodeError {
883 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
884 f.write_str(self.description())
888 impl fmt::Debug for LightningError {
889 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
890 f.write_str(self.err)
894 impl From<::std::io::Error> for DecodeError {
895 fn from(e: ::std::io::Error) -> Self {
896 if e.kind() == ::std::io::ErrorKind::UnexpectedEof {
897 DecodeError::ShortRead
904 impl Writeable for OptionalField<Script> {
905 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
907 OptionalField::Present(ref script) => {
908 // Note that Writeable for script includes the 16-bit length tag for us
911 OptionalField::Absent => {}
917 impl<R: Read> Readable<R> for OptionalField<Script> {
918 fn read(r: &mut R) -> Result<Self, DecodeError> {
919 match <u16 as Readable<R>>::read(r) {
921 let mut buf = vec![0; len as usize];
922 r.read_exact(&mut buf)?;
923 Ok(OptionalField::Present(Script::from(buf)))
925 Err(DecodeError::ShortRead) => Ok(OptionalField::Absent),
931 impl_writeable_len_match!(AcceptChannel, {
932 {AcceptChannel{ shutdown_scriptpubkey: OptionalField::Present(ref script), .. }, 270 + 2 + script.len()},
935 temporary_channel_id,
937 max_htlc_value_in_flight_msat,
938 channel_reserve_satoshis,
944 revocation_basepoint,
946 delayed_payment_basepoint,
948 first_per_commitment_point,
949 shutdown_scriptpubkey
952 impl_writeable!(AnnouncementSignatures, 32+8+64*2, {
959 impl Writeable for ChannelReestablish {
960 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
961 w.size_hint(if let OptionalField::Present(..) = self.data_loss_protect { 32+2*8+33+32 } else { 32+2*8 });
962 self.channel_id.write(w)?;
963 self.next_local_commitment_number.write(w)?;
964 self.next_remote_commitment_number.write(w)?;
965 match self.data_loss_protect {
966 OptionalField::Present(ref data_loss_protect) => {
967 (*data_loss_protect).your_last_per_commitment_secret.write(w)?;
968 (*data_loss_protect).my_current_per_commitment_point.write(w)?;
970 OptionalField::Absent => {}
976 impl<R: Read> Readable<R> for ChannelReestablish{
977 fn read(r: &mut R) -> Result<Self, DecodeError> {
979 channel_id: Readable::read(r)?,
980 next_local_commitment_number: Readable::read(r)?,
981 next_remote_commitment_number: Readable::read(r)?,
983 match <[u8; 32] as Readable<R>>::read(r) {
984 Ok(your_last_per_commitment_secret) =>
985 OptionalField::Present(DataLossProtect {
986 your_last_per_commitment_secret,
987 my_current_per_commitment_point: Readable::read(r)?,
989 Err(DecodeError::ShortRead) => OptionalField::Absent,
990 Err(e) => return Err(e)
997 impl_writeable!(ClosingSigned, 32+8+64, {
1003 impl_writeable_len_match!(CommitmentSigned, {
1004 { CommitmentSigned { ref htlc_signatures, .. }, 32+64+2+htlc_signatures.len()*64 }
1011 impl_writeable_len_match!(DecodedOnionErrorPacket, {
1012 { DecodedOnionErrorPacket { ref failuremsg, ref pad, .. }, 32 + 4 + failuremsg.len() + pad.len() }
1019 impl_writeable!(FundingCreated, 32+32+2+64, {
1020 temporary_channel_id,
1022 funding_output_index,
1026 impl_writeable!(FundingSigned, 32+64, {
1031 impl_writeable!(FundingLocked, 32+33, {
1033 next_per_commitment_point
1036 impl Writeable for Init {
1037 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1038 // global_features gets the bottom 13 bits of our features, and local_features gets all of
1039 // our relevant feature bits. This keeps us compatible with old nodes.
1040 self.features.write_up_to_13(w)?;
1041 self.features.write(w)
1045 impl<R: Read> Readable<R> for Init {
1046 fn read(r: &mut R) -> Result<Self, DecodeError> {
1047 let global_features: InitFeatures = Readable::read(r)?;
1048 let mut features: InitFeatures = Readable::read(r)?;
1049 features.or(&global_features);
1056 impl_writeable_len_match!(OpenChannel, {
1057 { OpenChannel { shutdown_scriptpubkey: OptionalField::Present(ref script), .. }, 319 + 2 + script.len() },
1061 temporary_channel_id,
1064 dust_limit_satoshis,
1065 max_htlc_value_in_flight_msat,
1066 channel_reserve_satoshis,
1072 revocation_basepoint,
1074 delayed_payment_basepoint,
1076 first_per_commitment_point,
1078 shutdown_scriptpubkey
1081 impl_writeable!(RevokeAndACK, 32+32+33, {
1083 per_commitment_secret,
1084 next_per_commitment_point
1087 impl_writeable_len_match!(Shutdown, {
1088 { Shutdown { ref scriptpubkey, .. }, 32 + 2 + scriptpubkey.len() }
1094 impl_writeable_len_match!(UpdateFailHTLC, {
1095 { UpdateFailHTLC { ref reason, .. }, 32 + 10 + reason.data.len() }
1102 impl_writeable!(UpdateFailMalformedHTLC, 32+8+32+2, {
1109 impl_writeable!(UpdateFee, 32+4, {
1114 impl_writeable!(UpdateFulfillHTLC, 32+8+32, {
1120 impl_writeable_len_match!(OnionErrorPacket, {
1121 { OnionErrorPacket { ref data, .. }, 2 + data.len() }
1126 impl Writeable for OnionPacket {
1127 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1128 w.size_hint(1 + 33 + 20*65 + 32);
1129 self.version.write(w)?;
1130 match self.public_key {
1131 Ok(pubkey) => pubkey.write(w)?,
1132 Err(_) => [0u8;33].write(w)?,
1134 w.write_all(&self.hop_data)?;
1135 self.hmac.write(w)?;
1140 impl<R: Read> Readable<R> for OnionPacket {
1141 fn read(r: &mut R) -> Result<Self, DecodeError> {
1143 version: Readable::read(r)?,
1145 let mut buf = [0u8;33];
1146 r.read_exact(&mut buf)?;
1147 PublicKey::from_slice(&buf)
1149 hop_data: Readable::read(r)?,
1150 hmac: Readable::read(r)?,
1155 impl_writeable!(UpdateAddHTLC, 32+8+8+32+4+1366, {
1161 onion_routing_packet
1164 impl Writeable for OnionRealm0HopData {
1165 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1167 self.short_channel_id.write(w)?;
1168 self.amt_to_forward.write(w)?;
1169 self.outgoing_cltv_value.write(w)?;
1170 w.write_all(&[0;12])?;
1175 impl<R: Read> Readable<R> for OnionRealm0HopData {
1176 fn read(r: &mut R) -> Result<Self, DecodeError> {
1177 Ok(OnionRealm0HopData {
1178 short_channel_id: Readable::read(r)?,
1179 amt_to_forward: Readable::read(r)?,
1180 outgoing_cltv_value: {
1181 let v: u32 = Readable::read(r)?;
1182 r.read_exact(&mut [0; 12])?;
1189 impl Writeable for OnionHopData {
1190 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1192 self.realm.write(w)?;
1193 self.data.write(w)?;
1194 self.hmac.write(w)?;
1199 impl<R: Read> Readable<R> for OnionHopData {
1200 fn read(r: &mut R) -> Result<Self, DecodeError> {
1203 let r: u8 = Readable::read(r)?;
1205 return Err(DecodeError::UnknownVersion);
1209 data: Readable::read(r)?,
1210 hmac: Readable::read(r)?,
1215 impl Writeable for Ping {
1216 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1217 w.size_hint(self.byteslen as usize + 4);
1218 self.ponglen.write(w)?;
1219 vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
1224 impl<R: Read> Readable<R> for Ping {
1225 fn read(r: &mut R) -> Result<Self, DecodeError> {
1227 ponglen: Readable::read(r)?,
1229 let byteslen = Readable::read(r)?;
1230 r.read_exact(&mut vec![0u8; byteslen as usize][..])?;
1237 impl Writeable for Pong {
1238 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1239 w.size_hint(self.byteslen as usize + 2);
1240 vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
1245 impl<R: Read> Readable<R> for Pong {
1246 fn read(r: &mut R) -> Result<Self, DecodeError> {
1249 let byteslen = Readable::read(r)?;
1250 r.read_exact(&mut vec![0u8; byteslen as usize][..])?;
1257 impl Writeable for UnsignedChannelAnnouncement {
1258 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1259 w.size_hint(2 + 2*32 + 4*33 + self.features.byte_count() + self.excess_data.len());
1260 self.features.write(w)?;
1261 self.chain_hash.write(w)?;
1262 self.short_channel_id.write(w)?;
1263 self.node_id_1.write(w)?;
1264 self.node_id_2.write(w)?;
1265 self.bitcoin_key_1.write(w)?;
1266 self.bitcoin_key_2.write(w)?;
1267 w.write_all(&self.excess_data[..])?;
1272 impl<R: Read> Readable<R> for UnsignedChannelAnnouncement {
1273 fn read(r: &mut R) -> Result<Self, DecodeError> {
1276 let f: ChannelFeatures = Readable::read(r)?;
1277 if f.requires_unknown_bits() {
1278 return Err(DecodeError::UnknownRequiredFeature);
1282 chain_hash: Readable::read(r)?,
1283 short_channel_id: Readable::read(r)?,
1284 node_id_1: Readable::read(r)?,
1285 node_id_2: Readable::read(r)?,
1286 bitcoin_key_1: Readable::read(r)?,
1287 bitcoin_key_2: Readable::read(r)?,
1289 let mut excess_data = vec![];
1290 r.read_to_end(&mut excess_data)?;
1297 impl_writeable_len_match!(ChannelAnnouncement, {
1298 { ChannelAnnouncement { contents: UnsignedChannelAnnouncement {ref features, ref excess_data, ..}, .. },
1299 2 + 2*32 + 4*33 + features.byte_count() + excess_data.len() + 4*64 }
1303 bitcoin_signature_1,
1304 bitcoin_signature_2,
1308 impl Writeable for UnsignedChannelUpdate {
1309 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1310 w.size_hint(64 + self.excess_data.len());
1311 self.chain_hash.write(w)?;
1312 self.short_channel_id.write(w)?;
1313 self.timestamp.write(w)?;
1314 self.flags.write(w)?;
1315 self.cltv_expiry_delta.write(w)?;
1316 self.htlc_minimum_msat.write(w)?;
1317 self.fee_base_msat.write(w)?;
1318 self.fee_proportional_millionths.write(w)?;
1319 w.write_all(&self.excess_data[..])?;
1324 impl<R: Read> Readable<R> for UnsignedChannelUpdate {
1325 fn read(r: &mut R) -> Result<Self, DecodeError> {
1327 chain_hash: Readable::read(r)?,
1328 short_channel_id: Readable::read(r)?,
1329 timestamp: Readable::read(r)?,
1330 flags: Readable::read(r)?,
1331 cltv_expiry_delta: Readable::read(r)?,
1332 htlc_minimum_msat: Readable::read(r)?,
1333 fee_base_msat: Readable::read(r)?,
1334 fee_proportional_millionths: Readable::read(r)?,
1336 let mut excess_data = vec![];
1337 r.read_to_end(&mut excess_data)?;
1344 impl_writeable_len_match!(ChannelUpdate, {
1345 { ChannelUpdate { contents: UnsignedChannelUpdate {ref excess_data, ..}, .. },
1346 64 + excess_data.len() + 64 }
1352 impl Writeable for ErrorMessage {
1353 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1354 w.size_hint(32 + 2 + self.data.len());
1355 self.channel_id.write(w)?;
1356 (self.data.len() as u16).write(w)?;
1357 w.write_all(self.data.as_bytes())?;
1362 impl<R: Read> Readable<R> for ErrorMessage {
1363 fn read(r: &mut R) -> Result<Self, DecodeError> {
1365 channel_id: Readable::read(r)?,
1367 let mut sz: usize = <u16 as Readable<R>>::read(r)? as usize;
1368 let mut data = vec![];
1369 let data_len = r.read_to_end(&mut data)?;
1370 sz = cmp::min(data_len, sz);
1371 match String::from_utf8(data[..sz as usize].to_vec()) {
1373 Err(_) => return Err(DecodeError::InvalidValue),
1380 impl Writeable for UnsignedNodeAnnouncement {
1381 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1382 w.size_hint(64 + 76 + self.features.byte_count() + self.addresses.len()*38 + self.excess_address_data.len() + self.excess_data.len());
1383 self.features.write(w)?;
1384 self.timestamp.write(w)?;
1385 self.node_id.write(w)?;
1386 w.write_all(&self.rgb)?;
1387 self.alias.write(w)?;
1389 let mut addrs_to_encode = self.addresses.clone();
1390 addrs_to_encode.sort_unstable_by(|a, b| { a.get_id().cmp(&b.get_id()) });
1391 addrs_to_encode.dedup_by(|a, b| { a.get_id() == b.get_id() });
1392 let mut addr_len = 0;
1393 for addr in &addrs_to_encode {
1394 addr_len += 1 + addr.len();
1396 (addr_len + self.excess_address_data.len() as u16).write(w)?;
1397 for addr in addrs_to_encode {
1400 w.write_all(&self.excess_address_data[..])?;
1401 w.write_all(&self.excess_data[..])?;
1406 impl<R: Read> Readable<R> for UnsignedNodeAnnouncement {
1407 fn read(r: &mut R) -> Result<Self, DecodeError> {
1408 let features: NodeFeatures = Readable::read(r)?;
1409 if features.requires_unknown_bits() {
1410 return Err(DecodeError::UnknownRequiredFeature);
1412 let timestamp: u32 = Readable::read(r)?;
1413 let node_id: PublicKey = Readable::read(r)?;
1414 let mut rgb = [0; 3];
1415 r.read_exact(&mut rgb)?;
1416 let alias: [u8; 32] = Readable::read(r)?;
1418 let addr_len: u16 = Readable::read(r)?;
1419 let mut addresses: Vec<NetAddress> = Vec::with_capacity(4);
1420 let mut addr_readpos = 0;
1421 let mut excess = false;
1422 let mut excess_byte = 0;
1424 if addr_len <= addr_readpos { break; }
1425 match Readable::read(r) {
1428 NetAddress::IPv4 { .. } => {
1429 if addresses.len() > 0 {
1430 return Err(DecodeError::ExtraAddressesPerType);
1433 NetAddress::IPv6 { .. } => {
1434 if addresses.len() > 1 || (addresses.len() == 1 && addresses[0].get_id() != 1) {
1435 return Err(DecodeError::ExtraAddressesPerType);
1438 NetAddress::OnionV2 { .. } => {
1439 if addresses.len() > 2 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 2) {
1440 return Err(DecodeError::ExtraAddressesPerType);
1443 NetAddress::OnionV3 { .. } => {
1444 if addresses.len() > 3 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 3) {
1445 return Err(DecodeError::ExtraAddressesPerType);
1449 if addr_len < addr_readpos + 1 + addr.len() {
1450 return Err(DecodeError::BadLengthDescriptor);
1452 addr_readpos += (1 + addr.len()) as u16;
1453 addresses.push(addr);
1455 Ok(Err(unknown_descriptor)) => {
1457 excess_byte = unknown_descriptor;
1460 Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
1461 Err(e) => return Err(e),
1465 let mut excess_data = vec![];
1466 let excess_address_data = if addr_readpos < addr_len {
1467 let mut excess_address_data = vec![0; (addr_len - addr_readpos) as usize];
1468 r.read_exact(&mut excess_address_data[if excess { 1 } else { 0 }..])?;
1470 excess_address_data[0] = excess_byte;
1475 excess_data.push(excess_byte);
1479 r.read_to_end(&mut excess_data)?;
1480 Ok(UnsignedNodeAnnouncement {
1487 excess_address_data,
1493 impl_writeable_len_match!(NodeAnnouncement, {
1494 { NodeAnnouncement { contents: UnsignedNodeAnnouncement { ref features, ref addresses, ref excess_address_data, ref excess_data, ..}, .. },
1495 64 + 76 + features.byte_count() + addresses.len()*38 + excess_address_data.len() + excess_data.len() }
1505 use ln::msgs::{ChannelFeatures, InitFeatures, NodeFeatures, OptionalField, OnionErrorPacket};
1506 use ln::channelmanager::{PaymentPreimage, PaymentHash};
1507 use util::ser::Writeable;
1509 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
1510 use bitcoin_hashes::hex::FromHex;
1511 use bitcoin::util::address::Address;
1512 use bitcoin::network::constants::Network;
1513 use bitcoin::blockdata::script::Builder;
1514 use bitcoin::blockdata::opcodes;
1516 use secp256k1::key::{PublicKey,SecretKey};
1517 use secp256k1::{Secp256k1, Message};
1519 use std::marker::PhantomData;
1522 fn encoding_channel_reestablish_no_secret() {
1523 let cr = msgs::ChannelReestablish {
1524 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],
1525 next_local_commitment_number: 3,
1526 next_remote_commitment_number: 4,
1527 data_loss_protect: OptionalField::Absent,
1530 let encoded_value = cr.encode();
1533 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]
1538 fn encoding_channel_reestablish_with_secret() {
1540 let secp_ctx = Secp256k1::new();
1541 PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap())
1544 let cr = msgs::ChannelReestablish {
1545 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],
1546 next_local_commitment_number: 3,
1547 next_remote_commitment_number: 4,
1548 data_loss_protect: OptionalField::Present(msgs::DataLossProtect { your_last_per_commitment_secret: [9;32], my_current_per_commitment_point: public_key}),
1551 let encoded_value = cr.encode();
1554 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]
1558 macro_rules! get_keys_from {
1559 ($slice: expr, $secp_ctx: expr) => {
1561 let privkey = SecretKey::from_slice(&hex::decode($slice).unwrap()[..]).unwrap();
1562 let pubkey = PublicKey::from_secret_key(&$secp_ctx, &privkey);
1568 macro_rules! get_sig_on {
1569 ($privkey: expr, $ctx: expr, $string: expr) => {
1571 let sighash = Message::from_slice(&$string.into_bytes()[..]).unwrap();
1572 $ctx.sign(&sighash, &$privkey)
1578 fn encoding_announcement_signatures() {
1579 let secp_ctx = Secp256k1::new();
1580 let (privkey, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1581 let sig_1 = get_sig_on!(privkey, secp_ctx, String::from("01010101010101010101010101010101"));
1582 let sig_2 = get_sig_on!(privkey, secp_ctx, String::from("02020202020202020202020202020202"));
1583 let announcement_signatures = msgs::AnnouncementSignatures {
1584 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],
1585 short_channel_id: 2316138423780173,
1586 node_signature: sig_1,
1587 bitcoin_signature: sig_2,
1590 let encoded_value = announcement_signatures.encode();
1591 assert_eq!(encoded_value, hex::decode("040000000000000005000000000000000600000000000000070000000000000000083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073acf9953cef4700860f5967838eba2bae89288ad188ebf8b20bf995c3ea53a26df1876d0a3a0e13172ba286a673140190c02ba9da60a2e43a745188c8a83c7f3ef").unwrap());
1594 fn do_encoding_channel_announcement(unknown_features_bits: bool, non_bitcoin_chain_hash: bool, excess_data: bool) {
1595 let secp_ctx = Secp256k1::new();
1596 let (privkey_1, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1597 let (privkey_2, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
1598 let (privkey_3, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
1599 let (privkey_4, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
1600 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1601 let sig_2 = get_sig_on!(privkey_2, secp_ctx, String::from("01010101010101010101010101010101"));
1602 let sig_3 = get_sig_on!(privkey_3, secp_ctx, String::from("01010101010101010101010101010101"));
1603 let sig_4 = get_sig_on!(privkey_4, secp_ctx, String::from("01010101010101010101010101010101"));
1604 let mut features = ChannelFeatures::new();
1605 if unknown_features_bits {
1606 features.flags = vec![0xFF, 0xFF];
1608 let unsigned_channel_announcement = msgs::UnsignedChannelAnnouncement {
1610 chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
1611 short_channel_id: 2316138423780173,
1612 node_id_1: pubkey_1,
1613 node_id_2: pubkey_2,
1614 bitcoin_key_1: pubkey_3,
1615 bitcoin_key_2: pubkey_4,
1616 excess_data: if excess_data { vec![10, 0, 0, 20, 0, 0, 30, 0, 0, 40] } else { Vec::new() },
1618 let channel_announcement = msgs::ChannelAnnouncement {
1619 node_signature_1: sig_1,
1620 node_signature_2: sig_2,
1621 bitcoin_signature_1: sig_3,
1622 bitcoin_signature_2: sig_4,
1623 contents: unsigned_channel_announcement,
1625 let encoded_value = channel_announcement.encode();
1626 let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a1735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap();
1627 if unknown_features_bits {
1628 target_value.append(&mut hex::decode("0002ffff").unwrap());
1630 target_value.append(&mut hex::decode("0000").unwrap());
1632 if non_bitcoin_chain_hash {
1633 target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
1635 target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
1637 target_value.append(&mut hex::decode("00083a840000034d031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b").unwrap());
1639 target_value.append(&mut hex::decode("0a00001400001e000028").unwrap());
1641 assert_eq!(encoded_value, target_value);
1645 fn encoding_channel_announcement() {
1646 do_encoding_channel_announcement(false, false, false);
1647 do_encoding_channel_announcement(true, false, false);
1648 do_encoding_channel_announcement(true, true, false);
1649 do_encoding_channel_announcement(true, true, true);
1650 do_encoding_channel_announcement(false, true, true);
1651 do_encoding_channel_announcement(false, false, true);
1652 do_encoding_channel_announcement(false, true, false);
1653 do_encoding_channel_announcement(true, false, true);
1656 fn do_encoding_node_announcement(unknown_features_bits: bool, ipv4: bool, ipv6: bool, onionv2: bool, onionv3: bool, excess_address_data: bool, excess_data: bool) {
1657 let secp_ctx = Secp256k1::new();
1658 let (privkey_1, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1659 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1660 let mut features = NodeFeatures::new();
1661 if unknown_features_bits {
1662 features.flags = vec![0xFF, 0xFF];
1664 let mut addresses = Vec::new();
1666 addresses.push(msgs::NetAddress::IPv4 {
1667 addr: [255, 254, 253, 252],
1672 addresses.push(msgs::NetAddress::IPv6 {
1673 addr: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240],
1678 addresses.push(msgs::NetAddress::OnionV2 {
1679 addr: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246],
1684 addresses.push(msgs::NetAddress::OnionV3 {
1685 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],
1691 let mut addr_len = 0;
1692 for addr in &addresses {
1693 addr_len += addr.len() + 1;
1695 let unsigned_node_announcement = msgs::UnsignedNodeAnnouncement {
1697 timestamp: 20190119,
1702 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() },
1703 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() },
1705 addr_len += unsigned_node_announcement.excess_address_data.len() as u16;
1706 let node_announcement = msgs::NodeAnnouncement {
1708 contents: unsigned_node_announcement,
1710 let encoded_value = node_announcement.encode();
1711 let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1712 if unknown_features_bits {
1713 target_value.append(&mut hex::decode("0002ffff").unwrap());
1715 target_value.append(&mut hex::decode("000122").unwrap());
1717 target_value.append(&mut hex::decode("013413a7031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f2020201010101010101010101010101010101010101010101010101010101010101010").unwrap());
1718 target_value.append(&mut vec![(addr_len >> 8) as u8, addr_len as u8]);
1720 target_value.append(&mut hex::decode("01fffefdfc2607").unwrap());
1723 target_value.append(&mut hex::decode("02fffefdfcfbfaf9f8f7f6f5f4f3f2f1f02607").unwrap());
1726 target_value.append(&mut hex::decode("03fffefdfcfbfaf9f8f7f62607").unwrap());
1729 target_value.append(&mut hex::decode("04fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e00020102607").unwrap());
1731 if excess_address_data {
1732 target_value.append(&mut hex::decode("216c280b5395a2546e7e4b2663e04f811622f15a4f92e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d269").unwrap());
1735 target_value.append(&mut hex::decode("3b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap());
1737 assert_eq!(encoded_value, target_value);
1741 fn encoding_node_announcement() {
1742 do_encoding_node_announcement(true, true, true, true, true, true, true);
1743 do_encoding_node_announcement(false, false, false, false, false, false, false);
1744 do_encoding_node_announcement(false, true, false, false, false, false, false);
1745 do_encoding_node_announcement(false, false, true, false, false, false, false);
1746 do_encoding_node_announcement(false, false, false, true, false, false, false);
1747 do_encoding_node_announcement(false, false, false, false, true, false, false);
1748 do_encoding_node_announcement(false, false, false, false, false, true, false);
1749 do_encoding_node_announcement(false, true, false, true, false, true, false);
1750 do_encoding_node_announcement(false, false, true, false, true, false, false);
1753 fn do_encoding_channel_update(non_bitcoin_chain_hash: bool, direction: bool, disable: bool, htlc_maximum_msat: bool) {
1754 let secp_ctx = Secp256k1::new();
1755 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1756 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1757 let unsigned_channel_update = msgs::UnsignedChannelUpdate {
1758 chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
1759 short_channel_id: 2316138423780173,
1760 timestamp: 20190119,
1761 flags: if direction { 1 } else { 0 } | if disable { 1 << 1 } else { 0 } | if htlc_maximum_msat { 1 << 8 } else { 0 },
1762 cltv_expiry_delta: 144,
1763 htlc_minimum_msat: 1000000,
1764 fee_base_msat: 10000,
1765 fee_proportional_millionths: 20,
1766 excess_data: if htlc_maximum_msat { vec![0, 0, 0, 0, 59, 154, 202, 0] } else { Vec::new() }
1768 let channel_update = msgs::ChannelUpdate {
1770 contents: unsigned_channel_update
1772 let encoded_value = channel_update.encode();
1773 let mut target_value = hex::decode("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1774 if non_bitcoin_chain_hash {
1775 target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
1777 target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
1779 target_value.append(&mut hex::decode("00083a840000034d013413a7").unwrap());
1780 if htlc_maximum_msat {
1781 target_value.append(&mut hex::decode("01").unwrap());
1783 target_value.append(&mut hex::decode("00").unwrap());
1785 target_value.append(&mut hex::decode("00").unwrap());
1787 let flag = target_value.last_mut().unwrap();
1791 let flag = target_value.last_mut().unwrap();
1792 *flag = *flag | 1 << 1;
1794 target_value.append(&mut hex::decode("009000000000000f42400000271000000014").unwrap());
1795 if htlc_maximum_msat {
1796 target_value.append(&mut hex::decode("000000003b9aca00").unwrap());
1798 assert_eq!(encoded_value, target_value);
1802 fn encoding_channel_update() {
1803 do_encoding_channel_update(false, false, false, false);
1804 do_encoding_channel_update(true, false, false, false);
1805 do_encoding_channel_update(false, true, false, false);
1806 do_encoding_channel_update(false, false, true, false);
1807 do_encoding_channel_update(false, false, false, true);
1808 do_encoding_channel_update(true, true, true, true);
1811 fn do_encoding_open_channel(non_bitcoin_chain_hash: bool, random_bit: bool, shutdown: bool) {
1812 let secp_ctx = Secp256k1::new();
1813 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1814 let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
1815 let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
1816 let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
1817 let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
1818 let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
1819 let open_channel = msgs::OpenChannel {
1820 chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
1821 temporary_channel_id: [2; 32],
1822 funding_satoshis: 1311768467284833366,
1823 push_msat: 2536655962884945560,
1824 dust_limit_satoshis: 3608586615801332854,
1825 max_htlc_value_in_flight_msat: 8517154655701053848,
1826 channel_reserve_satoshis: 8665828695742877976,
1827 htlc_minimum_msat: 2316138423780173,
1828 feerate_per_kw: 821716,
1829 to_self_delay: 49340,
1830 max_accepted_htlcs: 49340,
1831 funding_pubkey: pubkey_1,
1832 revocation_basepoint: pubkey_2,
1833 payment_basepoint: pubkey_3,
1834 delayed_payment_basepoint: pubkey_4,
1835 htlc_basepoint: pubkey_5,
1836 first_per_commitment_point: pubkey_6,
1837 channel_flags: if random_bit { 1 << 5 } else { 0 },
1838 shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
1840 let encoded_value = open_channel.encode();
1841 let mut target_value = Vec::new();
1842 if non_bitcoin_chain_hash {
1843 target_value.append(&mut hex::decode("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000").unwrap());
1845 target_value.append(&mut hex::decode("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap());
1847 target_value.append(&mut hex::decode("02020202020202020202020202020202020202020202020202020202020202021234567890123456233403289122369832144668701144767633030896203198784335490624111800083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap());
1849 target_value.append(&mut hex::decode("20").unwrap());
1851 target_value.append(&mut hex::decode("00").unwrap());
1854 target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
1856 assert_eq!(encoded_value, target_value);
1860 fn encoding_open_channel() {
1861 do_encoding_open_channel(false, false, false);
1862 do_encoding_open_channel(true, false, false);
1863 do_encoding_open_channel(false, true, false);
1864 do_encoding_open_channel(false, false, true);
1865 do_encoding_open_channel(true, true, true);
1868 fn do_encoding_accept_channel(shutdown: bool) {
1869 let secp_ctx = Secp256k1::new();
1870 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1871 let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
1872 let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
1873 let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
1874 let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
1875 let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
1876 let accept_channel = msgs::AcceptChannel {
1877 temporary_channel_id: [2; 32],
1878 dust_limit_satoshis: 1311768467284833366,
1879 max_htlc_value_in_flight_msat: 2536655962884945560,
1880 channel_reserve_satoshis: 3608586615801332854,
1881 htlc_minimum_msat: 2316138423780173,
1882 minimum_depth: 821716,
1883 to_self_delay: 49340,
1884 max_accepted_htlcs: 49340,
1885 funding_pubkey: pubkey_1,
1886 revocation_basepoint: pubkey_2,
1887 payment_basepoint: pubkey_3,
1888 delayed_payment_basepoint: pubkey_4,
1889 htlc_basepoint: pubkey_5,
1890 first_per_commitment_point: pubkey_6,
1891 shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
1893 let encoded_value = accept_channel.encode();
1894 let mut target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020212345678901234562334032891223698321446687011447600083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap();
1896 target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
1898 assert_eq!(encoded_value, target_value);
1902 fn encoding_accept_channel() {
1903 do_encoding_accept_channel(false);
1904 do_encoding_accept_channel(true);
1908 fn encoding_funding_created() {
1909 let secp_ctx = Secp256k1::new();
1910 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1911 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1912 let funding_created = msgs::FundingCreated {
1913 temporary_channel_id: [2; 32],
1914 funding_txid: Sha256dHash::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap(),
1915 funding_output_index: 255,
1918 let encoded_value = funding_created.encode();
1919 let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202026e96fe9f8b0ddcd729ba03cfafa5a27b050b39d354dd980814268dfa9a44d4c200ffd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1920 assert_eq!(encoded_value, target_value);
1924 fn encoding_funding_signed() {
1925 let secp_ctx = Secp256k1::new();
1926 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1927 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1928 let funding_signed = msgs::FundingSigned {
1929 channel_id: [2; 32],
1932 let encoded_value = funding_signed.encode();
1933 let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1934 assert_eq!(encoded_value, target_value);
1938 fn encoding_funding_locked() {
1939 let secp_ctx = Secp256k1::new();
1940 let (_, pubkey_1,) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1941 let funding_locked = msgs::FundingLocked {
1942 channel_id: [2; 32],
1943 next_per_commitment_point: pubkey_1,
1945 let encoded_value = funding_locked.encode();
1946 let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
1947 assert_eq!(encoded_value, target_value);
1950 fn do_encoding_shutdown(script_type: u8) {
1951 let secp_ctx = Secp256k1::new();
1952 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1953 let script = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
1954 let shutdown = msgs::Shutdown {
1955 channel_id: [2; 32],
1956 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() },
1958 let encoded_value = shutdown.encode();
1959 let mut target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap();
1960 if script_type == 1 {
1961 target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
1962 } else if script_type == 2 {
1963 target_value.append(&mut hex::decode("0017a914da1745e9b549bd0bfa1a569971c77eba30cd5a4b87").unwrap());
1964 } else if script_type == 3 {
1965 target_value.append(&mut hex::decode("0016001479b000887626b294a914501a4cd226b58b235983").unwrap());
1966 } else if script_type == 4 {
1967 target_value.append(&mut hex::decode("002200204ae81572f06e1b88fd5ced7a1a000945432e83e1551e6f721ee9c00b8cc33260").unwrap());
1969 assert_eq!(encoded_value, target_value);
1973 fn encoding_shutdown() {
1974 do_encoding_shutdown(1);
1975 do_encoding_shutdown(2);
1976 do_encoding_shutdown(3);
1977 do_encoding_shutdown(4);
1981 fn encoding_closing_signed() {
1982 let secp_ctx = Secp256k1::new();
1983 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1984 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
1985 let closing_signed = msgs::ClosingSigned {
1986 channel_id: [2; 32],
1987 fee_satoshis: 2316138423780173,
1990 let encoded_value = closing_signed.encode();
1991 let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
1992 assert_eq!(encoded_value, target_value);
1996 fn encoding_update_add_htlc() {
1997 let secp_ctx = Secp256k1::new();
1998 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
1999 let onion_routing_packet = msgs::OnionPacket {
2001 public_key: Ok(pubkey_1),
2002 hop_data: [1; 20*65],
2005 let update_add_htlc = msgs::UpdateAddHTLC {
2006 channel_id: [2; 32],
2007 htlc_id: 2316138423780173,
2008 amount_msat: 3608586615801332854,
2009 payment_hash: PaymentHash([1; 32]),
2010 cltv_expiry: 821716,
2011 onion_routing_packet
2013 let encoded_value = update_add_htlc.encode();
2014 let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d32144668701144760101010101010101010101010101010101010101010101010101010101010101000c89d4ff031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202").unwrap();
2015 assert_eq!(encoded_value, target_value);
2019 fn encoding_update_fulfill_htlc() {
2020 let update_fulfill_htlc = msgs::UpdateFulfillHTLC {
2021 channel_id: [2; 32],
2022 htlc_id: 2316138423780173,
2023 payment_preimage: PaymentPreimage([1; 32]),
2025 let encoded_value = update_fulfill_htlc.encode();
2026 let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d0101010101010101010101010101010101010101010101010101010101010101").unwrap();
2027 assert_eq!(encoded_value, target_value);
2031 fn encoding_update_fail_htlc() {
2032 let reason = OnionErrorPacket {
2033 data: [1; 32].to_vec(),
2035 let update_fail_htlc = msgs::UpdateFailHTLC {
2036 channel_id: [2; 32],
2037 htlc_id: 2316138423780173,
2040 let encoded_value = update_fail_htlc.encode();
2041 let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d00200101010101010101010101010101010101010101010101010101010101010101").unwrap();
2042 assert_eq!(encoded_value, target_value);
2046 fn encoding_update_fail_malformed_htlc() {
2047 let update_fail_malformed_htlc = msgs::UpdateFailMalformedHTLC {
2048 channel_id: [2; 32],
2049 htlc_id: 2316138423780173,
2050 sha256_of_onion: [1; 32],
2053 let encoded_value = update_fail_malformed_htlc.encode();
2054 let target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020200083a840000034d010101010101010101010101010101010101010101010101010101010101010100ff").unwrap();
2055 assert_eq!(encoded_value, target_value);
2058 fn do_encoding_commitment_signed(htlcs: bool) {
2059 let secp_ctx = Secp256k1::new();
2060 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
2061 let (privkey_2, _) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
2062 let (privkey_3, _) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
2063 let (privkey_4, _) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
2064 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
2065 let sig_2 = get_sig_on!(privkey_2, secp_ctx, String::from("01010101010101010101010101010101"));
2066 let sig_3 = get_sig_on!(privkey_3, secp_ctx, String::from("01010101010101010101010101010101"));
2067 let sig_4 = get_sig_on!(privkey_4, secp_ctx, String::from("01010101010101010101010101010101"));
2068 let commitment_signed = msgs::CommitmentSigned {
2069 channel_id: [2; 32],
2071 htlc_signatures: if htlcs { vec![sig_2, sig_3, sig_4] } else { Vec::new() },
2073 let encoded_value = commitment_signed.encode();
2074 let mut target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
2076 target_value.append(&mut hex::decode("00031735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap());
2078 target_value.append(&mut hex::decode("0000").unwrap());
2080 assert_eq!(encoded_value, target_value);
2084 fn encoding_commitment_signed() {
2085 do_encoding_commitment_signed(true);
2086 do_encoding_commitment_signed(false);
2090 fn encoding_revoke_and_ack() {
2091 let secp_ctx = Secp256k1::new();
2092 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
2093 let raa = msgs::RevokeAndACK {
2094 channel_id: [2; 32],
2095 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],
2096 next_per_commitment_point: pubkey_1,
2098 let encoded_value = raa.encode();
2099 let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202020101010101010101010101010101010101010101010101010101010101010101031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
2100 assert_eq!(encoded_value, target_value);
2104 fn encoding_update_fee() {
2105 let update_fee = msgs::UpdateFee {
2106 channel_id: [2; 32],
2107 feerate_per_kw: 20190119,
2109 let encoded_value = update_fee.encode();
2110 let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202013413a7").unwrap();
2111 assert_eq!(encoded_value, target_value);
2115 fn encoding_init() {
2116 assert_eq!(msgs::Init {
2117 features: InitFeatures {
2118 flags: vec![0xFF, 0xFF, 0xFF],
2121 }.encode(), hex::decode("00023fff0003ffffff").unwrap());
2122 assert_eq!(msgs::Init {
2123 features: InitFeatures {
2127 }.encode(), hex::decode("0001ff0001ff").unwrap());
2128 assert_eq!(msgs::Init {
2129 features: InitFeatures {
2133 }.encode(), hex::decode("00000000").unwrap());
2137 fn encoding_error() {
2138 let error = msgs::ErrorMessage {
2139 channel_id: [2; 32],
2140 data: String::from("rust-lightning"),
2142 let encoded_value = error.encode();
2143 let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202000e727573742d6c696768746e696e67").unwrap();
2144 assert_eq!(encoded_value, target_value);
2148 fn encoding_ping() {
2149 let ping = msgs::Ping {
2153 let encoded_value = ping.encode();
2154 let target_value = hex::decode("0040004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
2155 assert_eq!(encoded_value, target_value);
2159 fn encoding_pong() {
2160 let pong = msgs::Pong {
2163 let encoded_value = pong.encode();
2164 let target_value = hex::decode("004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
2165 assert_eq!(encoded_value, target_value);