1 use secp256k1::key::PublicKey;
2 use secp256k1::{Secp256k1, Signature};
4 use bitcoin::util::hash::Sha256dHash;
5 use bitcoin::network::serialize::{deserialize,serialize};
6 use bitcoin::blockdata::script::Script;
11 use std::result::Result;
13 use util::{byte_utils, internal_traits, events};
14 use util::ser::{Readable, Writeable, Writer};
16 pub trait MsgEncodable {
17 fn encode(&self) -> Vec<u8>;
19 fn encoded_len(&self) -> usize { self.encode().len() }
21 fn encode_with_len(&self) -> Vec<u8> {
22 let enc = self.encode();
23 let mut res = Vec::with_capacity(enc.len() + 2);
24 res.extend_from_slice(&byte_utils::be16_to_array(enc.len() as u16));
25 res.extend_from_slice(&enc);
30 pub enum DecodeError {
31 /// Unknown realm byte in an OnionHopData packet
33 /// Unknown feature mandating we fail to parse message
34 UnknownRequiredFeature,
35 /// Failed to decode a public key (ie it's invalid)
37 /// Failed to decode a signature (ie it's invalid)
39 /// Value expected to be text wasn't decodable as text
43 /// node_announcement included more than one address of a given type!
44 ExtraAddressesPerType,
45 /// A length descriptor in the packet didn't describe the later data correctly
46 /// (currently only generated in node_announcement)
48 /// Error from std::io
50 /// 1 or 0 is not found for boolean value
53 pub trait MsgDecodable: Sized {
54 fn decode(v: &[u8]) -> Result<Self, DecodeError>;
57 /// Tracks localfeatures which are only in init messages
58 #[derive(Clone, PartialEq)]
59 pub struct LocalFeatures {
64 pub fn new() -> LocalFeatures {
70 pub fn supports_data_loss_protect(&self) -> bool {
71 self.flags.len() > 0 && (self.flags[0] & 3) != 0
73 pub fn requires_data_loss_protect(&self) -> bool {
74 self.flags.len() > 0 && (self.flags[0] & 1) != 0
77 pub fn initial_routing_sync(&self) -> bool {
78 self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0
80 pub fn set_initial_routing_sync(&mut self) {
81 if self.flags.len() == 0 {
82 self.flags.resize(1, 1 << 3);
84 self.flags[0] |= 1 << 3;
88 pub fn supports_upfront_shutdown_script(&self) -> bool {
89 self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0
91 pub fn requires_upfront_shutdown_script(&self) -> bool {
92 self.flags.len() > 0 && (self.flags[0] & (1 << 4)) != 0
95 pub fn requires_unknown_bits(&self) -> bool {
96 for (idx, &byte) in self.flags.iter().enumerate() {
97 if idx != 0 && (byte & 0x55) != 0 {
99 } else if idx == 0 && (byte & 0x14) != 0 {
106 pub fn supports_unknown_bits(&self) -> bool {
107 for (idx, &byte) in self.flags.iter().enumerate() {
108 if idx != 0 && byte != 0 {
110 } else if idx == 0 && (byte & 0xc4) != 0 {
118 /// Tracks globalfeatures which are in init messages and routing announcements
119 #[derive(Clone, PartialEq)]
120 pub struct GlobalFeatures {
124 impl GlobalFeatures {
125 pub fn new() -> GlobalFeatures {
131 pub fn requires_unknown_bits(&self) -> bool {
132 for &byte in self.flags.iter() {
133 if (byte & 0x55) != 0 {
140 pub fn supports_unknown_bits(&self) -> bool {
141 for &byte in self.flags.iter() {
151 pub global_features: GlobalFeatures,
152 pub local_features: LocalFeatures,
155 pub struct ErrorMessage {
156 pub channel_id: [u8; 32],
169 pub struct OpenChannel {
170 pub chain_hash: Sha256dHash,
171 pub temporary_channel_id: [u8; 32],
172 pub funding_satoshis: u64,
174 pub dust_limit_satoshis: u64,
175 pub max_htlc_value_in_flight_msat: u64,
176 pub channel_reserve_satoshis: u64,
177 pub htlc_minimum_msat: u64,
178 pub feerate_per_kw: u32,
179 pub to_self_delay: u16,
180 pub max_accepted_htlcs: u16,
181 pub funding_pubkey: PublicKey,
182 pub revocation_basepoint: PublicKey,
183 pub payment_basepoint: PublicKey,
184 pub delayed_payment_basepoint: PublicKey,
185 pub htlc_basepoint: PublicKey,
186 pub first_per_commitment_point: PublicKey,
187 pub channel_flags: u8,
188 pub shutdown_scriptpubkey: Option<Script>,
191 pub struct AcceptChannel {
192 pub temporary_channel_id: [u8; 32],
193 pub dust_limit_satoshis: u64,
194 pub max_htlc_value_in_flight_msat: u64,
195 pub channel_reserve_satoshis: u64,
196 pub htlc_minimum_msat: u64,
197 pub minimum_depth: u32,
198 pub to_self_delay: u16,
199 pub max_accepted_htlcs: u16,
200 pub funding_pubkey: PublicKey,
201 pub revocation_basepoint: PublicKey,
202 pub payment_basepoint: PublicKey,
203 pub delayed_payment_basepoint: PublicKey,
204 pub htlc_basepoint: PublicKey,
205 pub first_per_commitment_point: PublicKey,
206 pub shutdown_scriptpubkey: Option<Script>,
209 pub struct FundingCreated {
210 pub temporary_channel_id: [u8; 32],
211 pub funding_txid: Sha256dHash,
212 pub funding_output_index: u16,
213 pub signature: Signature,
216 pub struct FundingSigned {
217 pub channel_id: [u8; 32],
218 pub signature: Signature,
221 pub struct FundingLocked {
222 pub channel_id: [u8; 32],
223 pub next_per_commitment_point: PublicKey,
226 pub struct Shutdown {
227 pub channel_id: [u8; 32],
228 pub scriptpubkey: Script,
231 pub struct ClosingSigned {
232 pub channel_id: [u8; 32],
233 pub fee_satoshis: u64,
234 pub signature: Signature,
238 pub struct UpdateAddHTLC {
239 pub channel_id: [u8; 32],
241 pub amount_msat: u64,
242 pub payment_hash: [u8; 32],
243 pub cltv_expiry: u32,
244 pub onion_routing_packet: OnionPacket,
248 pub struct UpdateFulfillHTLC {
249 pub channel_id: [u8; 32],
251 pub payment_preimage: [u8; 32],
255 pub struct UpdateFailHTLC {
256 pub channel_id: [u8; 32],
258 pub reason: OnionErrorPacket,
262 pub struct UpdateFailMalformedHTLC {
263 pub channel_id: [u8; 32],
265 pub sha256_of_onion: [u8; 32],
266 pub failure_code: u16,
270 pub struct CommitmentSigned {
271 pub channel_id: [u8; 32],
272 pub signature: Signature,
273 pub htlc_signatures: Vec<Signature>,
276 pub struct RevokeAndACK {
277 pub channel_id: [u8; 32],
278 pub per_commitment_secret: [u8; 32],
279 pub next_per_commitment_point: PublicKey,
282 pub struct UpdateFee {
283 pub channel_id: [u8; 32],
284 pub feerate_per_kw: u32,
287 pub struct DataLossProtect {
288 pub your_last_per_commitment_secret: [u8; 32],
289 pub my_current_per_commitment_point: PublicKey,
292 pub struct ChannelReestablish {
293 pub channel_id: [u8; 32],
294 pub next_local_commitment_number: u64,
295 pub next_remote_commitment_number: u64,
296 pub data_loss_protect: Option<DataLossProtect>,
300 pub struct AnnouncementSignatures {
301 pub channel_id: [u8; 32],
302 pub short_channel_id: u64,
303 pub node_signature: Signature,
304 pub bitcoin_signature: Signature,
308 pub enum NetAddress {
322 ed25519_pubkey: [u8; 32],
329 fn get_id(&self) -> u8 {
331 &NetAddress::IPv4 {..} => { 1 },
332 &NetAddress::IPv6 {..} => { 2 },
333 &NetAddress::OnionV2 {..} => { 3 },
334 &NetAddress::OnionV3 {..} => { 4 },
339 pub struct UnsignedNodeAnnouncement {
340 pub features: GlobalFeatures,
342 pub node_id: PublicKey,
345 /// List of addresses on which this node is reachable. Note that you may only have up to one
346 /// address of each type, if you have more, they may be silently discarded or we may panic!
347 pub addresses: Vec<NetAddress>,
348 pub excess_address_data: Vec<u8>,
349 pub excess_data: Vec<u8>,
351 pub struct NodeAnnouncement {
352 pub signature: Signature,
353 pub contents: UnsignedNodeAnnouncement,
356 #[derive(PartialEq, Clone)]
357 pub struct UnsignedChannelAnnouncement {
358 pub features: GlobalFeatures,
359 pub chain_hash: Sha256dHash,
360 pub short_channel_id: u64,
361 pub node_id_1: PublicKey,
362 pub node_id_2: PublicKey,
363 pub bitcoin_key_1: PublicKey,
364 pub bitcoin_key_2: PublicKey,
365 pub excess_data: Vec<u8>,
367 #[derive(PartialEq, Clone)]
368 pub struct ChannelAnnouncement {
369 pub node_signature_1: Signature,
370 pub node_signature_2: Signature,
371 pub bitcoin_signature_1: Signature,
372 pub bitcoin_signature_2: Signature,
373 pub contents: UnsignedChannelAnnouncement,
376 #[derive(PartialEq, Clone)]
377 pub struct UnsignedChannelUpdate {
378 pub chain_hash: Sha256dHash,
379 pub short_channel_id: u64,
382 pub cltv_expiry_delta: u16,
383 pub htlc_minimum_msat: u64,
384 pub fee_base_msat: u32,
385 pub fee_proportional_millionths: u32,
386 pub excess_data: Vec<u8>,
388 #[derive(PartialEq, Clone)]
389 pub struct ChannelUpdate {
390 pub signature: Signature,
391 pub contents: UnsignedChannelUpdate,
394 /// Used to put an error message in a HandleError
395 pub enum ErrorAction {
396 /// The peer took some action which made us think they were useless. Disconnect them.
398 msg: Option<ErrorMessage>
400 /// The peer did something harmless that we weren't able to process, just log and ignore
402 /// The peer did something incorrect. Tell them.
408 pub struct HandleError { //TODO: rename me
409 pub err: &'static str,
410 pub action: Option<ErrorAction>, //TODO: Make this required
413 /// Struct used to return values from revoke_and_ack messages, containing a bunch of commitment
414 /// transaction updates if they were pending.
415 pub struct CommitmentUpdate {
416 pub update_add_htlcs: Vec<UpdateAddHTLC>,
417 pub update_fulfill_htlcs: Vec<UpdateFulfillHTLC>,
418 pub update_fail_htlcs: Vec<UpdateFailHTLC>,
419 pub update_fail_malformed_htlcs: Vec<UpdateFailMalformedHTLC>,
420 pub commitment_signed: CommitmentSigned,
423 pub enum HTLCFailChannelUpdate {
424 ChannelUpdateMessage {
428 short_channel_id: u64,
432 /// A trait to describe an object which can receive channel messages. Messages MAY be called in
433 /// paralell when they originate from different their_node_ids, however they MUST NOT be called in
434 /// paralell when the two calls have the same their_node_id.
435 pub trait ChannelMessageHandler : events::EventsProvider + Send + Sync {
437 fn handle_open_channel(&self, their_node_id: &PublicKey, msg: &OpenChannel) -> Result<AcceptChannel, HandleError>;
438 fn handle_accept_channel(&self, their_node_id: &PublicKey, msg: &AcceptChannel) -> Result<(), HandleError>;
439 fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &FundingCreated) -> Result<FundingSigned, HandleError>;
440 fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &FundingSigned) -> Result<(), HandleError>;
441 fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &FundingLocked) -> Result<Option<AnnouncementSignatures>, HandleError>;
444 fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &Shutdown) -> Result<(Option<Shutdown>, Option<ClosingSigned>), HandleError>;
445 fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned) -> Result<Option<ClosingSigned>, HandleError>;
448 fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &UpdateAddHTLC) -> Result<(), HandleError>;
449 fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFulfillHTLC) -> Result<(), HandleError>;
450 fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC) -> Result<Option<HTLCFailChannelUpdate>, HandleError>;
451 fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailMalformedHTLC) -> Result<(), HandleError>;
452 fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned) -> Result<(RevokeAndACK, Option<CommitmentSigned>), HandleError>;
453 fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &RevokeAndACK) -> Result<Option<CommitmentUpdate>, HandleError>;
455 fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &UpdateFee) -> Result<(), HandleError>;
457 // Channel-to-announce:
458 fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures) -> Result<(), HandleError>;
460 // Connection loss/reestablish:
461 /// Indicates a connection to the peer failed/an existing connection was lost. If no connection
462 /// is believed to be possible in the future (eg they're sending us messages we don't
463 /// understand or indicate they require unknown feature bits), no_connection_possible is set
464 /// and any outstanding channels should be failed.
465 fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
467 fn peer_connected(&self, their_node_id: &PublicKey) -> Vec<ChannelReestablish>;
468 fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish) -> Result<(Option<FundingLocked>, Option<RevokeAndACK>, Option<CommitmentUpdate>), HandleError>;
471 fn handle_error(&self, their_node_id: &PublicKey, msg: &ErrorMessage);
474 pub trait RoutingMessageHandler : Send + Sync {
475 fn handle_node_announcement(&self, msg: &NodeAnnouncement) -> Result<bool, HandleError>;
476 /// Handle a channel_announcement message, returning true if it should be forwarded on, false
477 /// or returning an Err otherwise.
478 fn handle_channel_announcement(&self, msg: &ChannelAnnouncement) -> Result<bool, HandleError>;
479 fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result<bool, HandleError>;
480 fn handle_htlc_fail_channel_update(&self, update: &HTLCFailChannelUpdate);
483 pub struct OnionRealm0HopData {
484 pub short_channel_id: u64,
485 pub amt_to_forward: u64,
486 pub outgoing_cltv_value: u32,
487 // 12 bytes of 0-padding
490 pub struct OnionHopData {
492 pub data: OnionRealm0HopData,
495 unsafe impl internal_traits::NoDealloc for OnionHopData{}
498 pub struct OnionPacket {
500 /// In order to ensure we always return an error on Onion decode in compliance with BOLT 4, we
501 /// have to deserialize OnionPackets contained in UpdateAddHTLCs even if the ephemeral public
502 /// key (here) is bogus, so we hold a Result instead of a PublicKey as we'd like.
503 pub public_key: Result<PublicKey, secp256k1::Error>,
504 pub hop_data: [u8; 20*65],
508 pub struct DecodedOnionErrorPacket {
510 pub failuremsg: Vec<u8>,
515 pub struct OnionErrorPacket {
516 // This really should be a constant size slice, but the spec lets these things be up to 128KB?
517 // (TODO) We limit it in decode to much lower...
521 impl Error for DecodeError {
522 fn description(&self) -> &str {
524 DecodeError::UnknownRealmByte => "Unknown realm byte in Onion packet",
525 DecodeError::UnknownRequiredFeature => "Unknown required feature preventing decode",
526 DecodeError::BadPublicKey => "Invalid public key in packet",
527 DecodeError::BadSignature => "Invalid signature in packet",
528 DecodeError::BadText => "Invalid text in packet",
529 DecodeError::ShortRead => "Packet extended beyond the provided bytes",
530 DecodeError::ExtraAddressesPerType => "More than one address of a single type",
531 DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly",
532 DecodeError::Io(ref e) => e.description(),
533 DecodeError::InvalidValue => "0 or 1 is not found for boolean",
537 impl fmt::Display for DecodeError {
538 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
539 f.write_str(self.description())
543 impl fmt::Debug for HandleError {
544 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
545 f.write_str(self.err)
549 impl From<::std::io::Error> for DecodeError {
550 fn from(e: ::std::io::Error) -> Self {
551 if e.kind() == ::std::io::ErrorKind::UnexpectedEof {
552 DecodeError::ShortRead
559 macro_rules! secp_pubkey {
560 ( $ctx: expr, $slice: expr ) => {
561 match PublicKey::from_slice($ctx, $slice) {
563 Err(_) => return Err(DecodeError::BadPublicKey)
568 macro_rules! secp_signature {
569 ( $ctx: expr, $slice: expr ) => {
570 match Signature::from_compact($ctx, $slice) {
572 Err(_) => return Err(DecodeError::BadSignature)
577 impl MsgDecodable for LocalFeatures {
578 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
579 if v.len() < 2 { return Err(DecodeError::ShortRead); }
580 let len = byte_utils::slice_to_be16(&v[0..2]) as usize;
581 if v.len() < len + 2 { return Err(DecodeError::ShortRead); }
582 let mut flags = Vec::with_capacity(len);
583 flags.extend_from_slice(&v[2..2 + len]);
589 impl MsgEncodable for LocalFeatures {
590 fn encode(&self) -> Vec<u8> {
591 let mut res = Vec::with_capacity(self.flags.len() + 2);
592 res.extend_from_slice(&byte_utils::be16_to_array(self.flags.len() as u16));
593 res.extend_from_slice(&self.flags[..]);
596 fn encoded_len(&self) -> usize { self.flags.len() + 2 }
599 impl MsgDecodable for GlobalFeatures {
600 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
601 if v.len() < 2 { return Err(DecodeError::ShortRead); }
602 let len = byte_utils::slice_to_be16(&v[0..2]) as usize;
603 if v.len() < len + 2 { return Err(DecodeError::ShortRead); }
604 let mut flags = Vec::with_capacity(len);
605 flags.extend_from_slice(&v[2..2 + len]);
611 impl MsgEncodable for GlobalFeatures {
612 fn encode(&self) -> Vec<u8> {
613 let mut res = Vec::with_capacity(self.flags.len() + 2);
614 res.extend_from_slice(&byte_utils::be16_to_array(self.flags.len() as u16));
615 res.extend_from_slice(&self.flags[..]);
618 fn encoded_len(&self) -> usize { self.flags.len() + 2 }
621 impl MsgDecodable for Init {
622 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
623 let global_features = GlobalFeatures::decode(v)?;
624 if v.len() < global_features.flags.len() + 4 {
625 return Err(DecodeError::ShortRead);
627 let local_features = LocalFeatures::decode(&v[global_features.flags.len() + 2..])?;
629 global_features: global_features,
630 local_features: local_features,
634 impl MsgEncodable for Init {
635 fn encode(&self) -> Vec<u8> {
636 let mut res = Vec::with_capacity(self.global_features.flags.len() + self.local_features.flags.len());
637 res.extend_from_slice(&self.global_features.encode()[..]);
638 res.extend_from_slice(&self.local_features.encode()[..]);
643 impl MsgDecodable for Ping {
644 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
646 return Err(DecodeError::ShortRead);
648 let ponglen = byte_utils::slice_to_be16(&v[0..2]);
649 let byteslen = byte_utils::slice_to_be16(&v[2..4]);
650 if v.len() < 4 + byteslen as usize {
651 return Err(DecodeError::ShortRead);
659 impl MsgEncodable for Ping {
660 fn encode(&self) -> Vec<u8> {
661 let mut res = Vec::with_capacity(self.byteslen as usize + 2);
662 res.extend_from_slice(&byte_utils::be16_to_array(self.byteslen));
663 res.resize(2 + self.byteslen as usize, 0);
668 impl MsgDecodable for Pong {
669 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
671 return Err(DecodeError::ShortRead);
673 let byteslen = byte_utils::slice_to_be16(&v[0..2]);
674 if v.len() < 2 + byteslen as usize {
675 return Err(DecodeError::ShortRead);
682 impl MsgEncodable for Pong {
683 fn encode(&self) -> Vec<u8> {
684 let mut res = Vec::with_capacity(self.byteslen as usize + 2);
685 res.extend_from_slice(&byte_utils::be16_to_array(self.byteslen));
686 res.resize(2 + self.byteslen as usize, 0);
691 impl MsgDecodable for OpenChannel {
692 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
693 if v.len() < 2*32+6*8+4+2*2+6*33+1 {
694 return Err(DecodeError::ShortRead);
696 let ctx = Secp256k1::without_caps();
698 let mut shutdown_scriptpubkey = None;
700 let len = byte_utils::slice_to_be16(&v[319..321]) as usize;
701 if v.len() < 321+len {
702 return Err(DecodeError::ShortRead);
704 shutdown_scriptpubkey = Some(Script::from(v[321..321+len].to_vec()));
706 let mut temp_channel_id = [0; 32];
707 temp_channel_id[..].copy_from_slice(&v[32..64]);
709 chain_hash: deserialize(&v[0..32]).unwrap(),
710 temporary_channel_id: temp_channel_id,
711 funding_satoshis: byte_utils::slice_to_be64(&v[64..72]),
712 push_msat: byte_utils::slice_to_be64(&v[72..80]),
713 dust_limit_satoshis: byte_utils::slice_to_be64(&v[80..88]),
714 max_htlc_value_in_flight_msat: byte_utils::slice_to_be64(&v[88..96]),
715 channel_reserve_satoshis: byte_utils::slice_to_be64(&v[96..104]),
716 htlc_minimum_msat: byte_utils::slice_to_be64(&v[104..112]),
717 feerate_per_kw: byte_utils::slice_to_be32(&v[112..116]),
718 to_self_delay: byte_utils::slice_to_be16(&v[116..118]),
719 max_accepted_htlcs: byte_utils::slice_to_be16(&v[118..120]),
720 funding_pubkey: secp_pubkey!(&ctx, &v[120..153]),
721 revocation_basepoint: secp_pubkey!(&ctx, &v[153..186]),
722 payment_basepoint: secp_pubkey!(&ctx, &v[186..219]),
723 delayed_payment_basepoint: secp_pubkey!(&ctx, &v[219..252]),
724 htlc_basepoint: secp_pubkey!(&ctx, &v[252..285]),
725 first_per_commitment_point: secp_pubkey!(&ctx, &v[285..318]),
726 channel_flags: v[318],
727 shutdown_scriptpubkey: shutdown_scriptpubkey
731 impl MsgEncodable for OpenChannel {
732 fn encode(&self) -> Vec<u8> {
733 let mut res = match &self.shutdown_scriptpubkey {
734 &Some(ref script) => Vec::with_capacity(319 + 2 + script.len()),
735 &None => Vec::with_capacity(319),
737 res.extend_from_slice(&serialize(&self.chain_hash).unwrap());
738 res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap());
739 res.extend_from_slice(&byte_utils::be64_to_array(self.funding_satoshis));
740 res.extend_from_slice(&byte_utils::be64_to_array(self.push_msat));
741 res.extend_from_slice(&byte_utils::be64_to_array(self.dust_limit_satoshis));
742 res.extend_from_slice(&byte_utils::be64_to_array(self.max_htlc_value_in_flight_msat));
743 res.extend_from_slice(&byte_utils::be64_to_array(self.channel_reserve_satoshis));
744 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_minimum_msat));
745 res.extend_from_slice(&byte_utils::be32_to_array(self.feerate_per_kw));
746 res.extend_from_slice(&byte_utils::be16_to_array(self.to_self_delay));
747 res.extend_from_slice(&byte_utils::be16_to_array(self.max_accepted_htlcs));
748 res.extend_from_slice(&self.funding_pubkey.serialize());
749 res.extend_from_slice(&self.revocation_basepoint.serialize());
750 res.extend_from_slice(&self.payment_basepoint.serialize());
751 res.extend_from_slice(&self.delayed_payment_basepoint.serialize());
752 res.extend_from_slice(&self.htlc_basepoint.serialize());
753 res.extend_from_slice(&self.first_per_commitment_point.serialize());
754 res.push(self.channel_flags);
755 if let &Some(ref script) = &self.shutdown_scriptpubkey {
756 res.extend_from_slice(&byte_utils::be16_to_array(script.len() as u16));
757 res.extend_from_slice(&script[..]);
763 impl MsgDecodable for AcceptChannel {
764 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
765 if v.len() < 32+4*8+4+2*2+6*33 {
766 return Err(DecodeError::ShortRead);
768 let ctx = Secp256k1::without_caps();
770 let mut shutdown_scriptpubkey = None;
772 let len = byte_utils::slice_to_be16(&v[270..272]) as usize;
773 if v.len() < 272+len {
774 return Err(DecodeError::ShortRead);
776 shutdown_scriptpubkey = Some(Script::from(v[272..272+len].to_vec()));
779 let mut temporary_channel_id = [0; 32];
780 temporary_channel_id[..].copy_from_slice(&v[0..32]);
782 temporary_channel_id,
783 dust_limit_satoshis: byte_utils::slice_to_be64(&v[32..40]),
784 max_htlc_value_in_flight_msat: byte_utils::slice_to_be64(&v[40..48]),
785 channel_reserve_satoshis: byte_utils::slice_to_be64(&v[48..56]),
786 htlc_minimum_msat: byte_utils::slice_to_be64(&v[56..64]),
787 minimum_depth: byte_utils::slice_to_be32(&v[64..68]),
788 to_self_delay: byte_utils::slice_to_be16(&v[68..70]),
789 max_accepted_htlcs: byte_utils::slice_to_be16(&v[70..72]),
790 funding_pubkey: secp_pubkey!(&ctx, &v[72..105]),
791 revocation_basepoint: secp_pubkey!(&ctx, &v[105..138]),
792 payment_basepoint: secp_pubkey!(&ctx, &v[138..171]),
793 delayed_payment_basepoint: secp_pubkey!(&ctx, &v[171..204]),
794 htlc_basepoint: secp_pubkey!(&ctx, &v[204..237]),
795 first_per_commitment_point: secp_pubkey!(&ctx, &v[237..270]),
796 shutdown_scriptpubkey: shutdown_scriptpubkey
800 impl MsgEncodable for AcceptChannel {
801 fn encode(&self) -> Vec<u8> {
802 let mut res = match &self.shutdown_scriptpubkey {
803 &Some(ref script) => Vec::with_capacity(270 + 2 + script.len()),
804 &None => Vec::with_capacity(270),
806 res.extend_from_slice(&self.temporary_channel_id);
807 res.extend_from_slice(&byte_utils::be64_to_array(self.dust_limit_satoshis));
808 res.extend_from_slice(&byte_utils::be64_to_array(self.max_htlc_value_in_flight_msat));
809 res.extend_from_slice(&byte_utils::be64_to_array(self.channel_reserve_satoshis));
810 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_minimum_msat));
811 res.extend_from_slice(&byte_utils::be32_to_array(self.minimum_depth));
812 res.extend_from_slice(&byte_utils::be16_to_array(self.to_self_delay));
813 res.extend_from_slice(&byte_utils::be16_to_array(self.max_accepted_htlcs));
814 res.extend_from_slice(&self.funding_pubkey.serialize());
815 res.extend_from_slice(&self.revocation_basepoint.serialize());
816 res.extend_from_slice(&self.payment_basepoint.serialize());
817 res.extend_from_slice(&self.delayed_payment_basepoint.serialize());
818 res.extend_from_slice(&self.htlc_basepoint.serialize());
819 res.extend_from_slice(&self.first_per_commitment_point.serialize());
820 if let &Some(ref script) = &self.shutdown_scriptpubkey {
821 res.extend_from_slice(&byte_utils::be16_to_array(script.len() as u16));
822 res.extend_from_slice(&script[..]);
828 impl MsgDecodable for FundingCreated {
829 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
830 if v.len() < 32+32+2+64 {
831 return Err(DecodeError::ShortRead);
833 let ctx = Secp256k1::without_caps();
834 let mut temporary_channel_id = [0; 32];
835 temporary_channel_id[..].copy_from_slice(&v[0..32]);
837 temporary_channel_id,
838 funding_txid: deserialize(&v[32..64]).unwrap(),
839 funding_output_index: byte_utils::slice_to_be16(&v[64..66]),
840 signature: secp_signature!(&ctx, &v[66..130]),
844 impl MsgEncodable for FundingCreated {
845 fn encode(&self) -> Vec<u8> {
846 let mut res = Vec::with_capacity(32+32+2+64);
847 res.extend_from_slice(&self.temporary_channel_id);
848 res.extend_from_slice(&serialize(&self.funding_txid).unwrap()[..]);
849 res.extend_from_slice(&byte_utils::be16_to_array(self.funding_output_index));
850 let secp_ctx = Secp256k1::without_caps();
851 res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx));
856 impl MsgDecodable for FundingSigned {
857 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
859 return Err(DecodeError::ShortRead);
861 let ctx = Secp256k1::without_caps();
862 let mut channel_id = [0; 32];
863 channel_id[..].copy_from_slice(&v[0..32]);
866 signature: secp_signature!(&ctx, &v[32..96]),
870 impl MsgEncodable for FundingSigned {
871 fn encode(&self) -> Vec<u8> {
872 let mut res = Vec::with_capacity(32+64);
873 res.extend_from_slice(&self.channel_id);
874 res.extend_from_slice(&self.signature.serialize_compact(&Secp256k1::without_caps()));
879 impl MsgDecodable for FundingLocked {
880 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
882 return Err(DecodeError::ShortRead);
884 let ctx = Secp256k1::without_caps();
885 let mut channel_id = [0; 32];
886 channel_id[..].copy_from_slice(&v[0..32]);
889 next_per_commitment_point: secp_pubkey!(&ctx, &v[32..65]),
893 impl MsgEncodable for FundingLocked {
894 fn encode(&self) -> Vec<u8> {
895 let mut res = Vec::with_capacity(32+33);
896 res.extend_from_slice(&self.channel_id);
897 res.extend_from_slice(&self.next_per_commitment_point.serialize());
902 impl MsgDecodable for Shutdown {
903 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
904 if v.len() < 32 + 2 {
905 return Err(DecodeError::ShortRead);
907 let scriptlen = byte_utils::slice_to_be16(&v[32..34]) as usize;
908 if v.len() < 32 + 2 + scriptlen {
909 return Err(DecodeError::ShortRead);
911 let mut channel_id = [0; 32];
912 channel_id[..].copy_from_slice(&v[0..32]);
915 scriptpubkey: Script::from(v[34..34 + scriptlen].to_vec()),
919 impl MsgEncodable for Shutdown {
920 fn encode(&self) -> Vec<u8> {
921 let mut res = Vec::with_capacity(32 + 2 + self.scriptpubkey.len());
922 res.extend_from_slice(&self.channel_id);
923 res.extend_from_slice(&byte_utils::be16_to_array(self.scriptpubkey.len() as u16));
924 res.extend_from_slice(&self.scriptpubkey[..]);
929 impl MsgDecodable for ClosingSigned {
930 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
931 if v.len() < 32 + 8 + 64 {
932 return Err(DecodeError::ShortRead);
934 let secp_ctx = Secp256k1::without_caps();
935 let mut channel_id = [0; 32];
936 channel_id[..].copy_from_slice(&v[0..32]);
939 fee_satoshis: byte_utils::slice_to_be64(&v[32..40]),
940 signature: secp_signature!(&secp_ctx, &v[40..104]),
944 impl MsgEncodable for ClosingSigned {
945 fn encode(&self) -> Vec<u8> {
946 let mut res = Vec::with_capacity(32+8+64);
947 res.extend_from_slice(&self.channel_id);
948 res.extend_from_slice(&byte_utils::be64_to_array(self.fee_satoshis));
949 let secp_ctx = Secp256k1::without_caps();
950 res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx));
955 impl MsgDecodable for UpdateAddHTLC {
956 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
957 if v.len() < 32+8+8+32+4+1+33+20*65+32 {
958 return Err(DecodeError::ShortRead);
960 let mut channel_id = [0; 32];
961 channel_id[..].copy_from_slice(&v[0..32]);
962 let mut payment_hash = [0; 32];
963 payment_hash.copy_from_slice(&v[48..80]);
966 htlc_id: byte_utils::slice_to_be64(&v[32..40]),
967 amount_msat: byte_utils::slice_to_be64(&v[40..48]),
969 cltv_expiry: byte_utils::slice_to_be32(&v[80..84]),
970 onion_routing_packet: OnionPacket::decode(&v[84..84+1366])?,
974 impl MsgEncodable for UpdateAddHTLC {
975 fn encode(&self) -> Vec<u8> {
976 let mut res = Vec::with_capacity(32+8+8+32+4+1366);
977 res.extend_from_slice(&self.channel_id);
978 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
979 res.extend_from_slice(&byte_utils::be64_to_array(self.amount_msat));
980 res.extend_from_slice(&self.payment_hash);
981 res.extend_from_slice(&byte_utils::be32_to_array(self.cltv_expiry));
982 res.extend_from_slice(&self.onion_routing_packet.encode()[..]);
987 impl MsgDecodable for UpdateFulfillHTLC {
988 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
989 if v.len() < 32+8+32 {
990 return Err(DecodeError::ShortRead);
992 let mut channel_id = [0; 32];
993 channel_id[..].copy_from_slice(&v[0..32]);
994 let mut payment_preimage = [0; 32];
995 payment_preimage.copy_from_slice(&v[40..72]);
998 htlc_id: byte_utils::slice_to_be64(&v[32..40]),
1003 impl MsgEncodable for UpdateFulfillHTLC {
1004 fn encode(&self) -> Vec<u8> {
1005 let mut res = Vec::with_capacity(32+8+32);
1006 res.extend_from_slice(&self.channel_id);
1007 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
1008 res.extend_from_slice(&self.payment_preimage);
1013 impl MsgDecodable for UpdateFailHTLC {
1014 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1016 return Err(DecodeError::ShortRead);
1018 let mut channel_id = [0; 32];
1019 channel_id[..].copy_from_slice(&v[0..32]);
1022 htlc_id: byte_utils::slice_to_be64(&v[32..40]),
1023 reason: OnionErrorPacket::decode(&v[40..])?,
1027 impl MsgEncodable for UpdateFailHTLC {
1028 fn encode(&self) -> Vec<u8> {
1029 let reason = self.reason.encode();
1030 let mut res = Vec::with_capacity(32+8+reason.len());
1031 res.extend_from_slice(&self.channel_id);
1032 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
1033 res.extend_from_slice(&reason[..]);
1038 impl MsgDecodable for UpdateFailMalformedHTLC {
1039 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1040 if v.len() < 32+8+32+2 {
1041 return Err(DecodeError::ShortRead);
1043 let mut channel_id = [0; 32];
1044 channel_id[..].copy_from_slice(&v[0..32]);
1045 let mut sha256_of_onion = [0; 32];
1046 sha256_of_onion.copy_from_slice(&v[40..72]);
1049 htlc_id: byte_utils::slice_to_be64(&v[32..40]),
1051 failure_code: byte_utils::slice_to_be16(&v[72..74]),
1055 impl MsgEncodable for UpdateFailMalformedHTLC {
1056 fn encode(&self) -> Vec<u8> {
1057 let mut res = Vec::with_capacity(32+8+32+2);
1058 res.extend_from_slice(&self.channel_id);
1059 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
1060 res.extend_from_slice(&self.sha256_of_onion);
1061 res.extend_from_slice(&byte_utils::be16_to_array(self.failure_code));
1066 impl MsgDecodable for CommitmentSigned {
1067 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1068 if v.len() < 32+64+2 {
1069 return Err(DecodeError::ShortRead);
1071 let mut channel_id = [0; 32];
1072 channel_id[..].copy_from_slice(&v[0..32]);
1074 let htlcs = byte_utils::slice_to_be16(&v[96..98]) as usize;
1075 if v.len() < 32+64+2+htlcs*64 {
1076 return Err(DecodeError::ShortRead);
1078 let mut htlc_signatures = Vec::with_capacity(htlcs);
1079 let secp_ctx = Secp256k1::without_caps();
1081 htlc_signatures.push(secp_signature!(&secp_ctx, &v[98+i*64..98+(i+1)*64]));
1085 signature: secp_signature!(&secp_ctx, &v[32..96]),
1090 impl MsgEncodable for CommitmentSigned {
1091 fn encode(&self) -> Vec<u8> {
1092 let mut res = Vec::with_capacity(32+64+2+self.htlc_signatures.len()*64);
1093 res.extend_from_slice(&self.channel_id);
1094 let secp_ctx = Secp256k1::without_caps();
1095 res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx));
1096 res.extend_from_slice(&byte_utils::be16_to_array(self.htlc_signatures.len() as u16));
1097 for i in 0..self.htlc_signatures.len() {
1098 res.extend_from_slice(&self.htlc_signatures[i].serialize_compact(&secp_ctx));
1104 impl MsgDecodable for RevokeAndACK {
1105 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1106 if v.len() < 32+32+33 {
1107 return Err(DecodeError::ShortRead);
1109 let mut channel_id = [0; 32];
1110 channel_id[..].copy_from_slice(&v[0..32]);
1111 let mut per_commitment_secret = [0; 32];
1112 per_commitment_secret.copy_from_slice(&v[32..64]);
1113 let secp_ctx = Secp256k1::without_caps();
1116 per_commitment_secret,
1117 next_per_commitment_point: secp_pubkey!(&secp_ctx, &v[64..97]),
1121 impl MsgEncodable for RevokeAndACK {
1122 fn encode(&self) -> Vec<u8> {
1123 let mut res = Vec::with_capacity(32+32+33);
1124 res.extend_from_slice(&self.channel_id);
1125 res.extend_from_slice(&self.per_commitment_secret);
1126 res.extend_from_slice(&self.next_per_commitment_point.serialize());
1131 impl MsgDecodable for UpdateFee {
1132 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1134 return Err(DecodeError::ShortRead);
1136 let mut channel_id = [0; 32];
1137 channel_id[..].copy_from_slice(&v[0..32]);
1140 feerate_per_kw: byte_utils::slice_to_be32(&v[32..36]),
1144 impl MsgEncodable for UpdateFee {
1145 fn encode(&self) -> Vec<u8> {
1146 let mut res = Vec::with_capacity(32+4);
1147 res.extend_from_slice(&self.channel_id);
1148 res.extend_from_slice(&byte_utils::be32_to_array(self.feerate_per_kw));
1153 impl MsgDecodable for ChannelReestablish {
1154 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1155 if v.len() < 32+2*8 {
1156 return Err(DecodeError::ShortRead);
1159 let data_loss_protect = if v.len() > 32+2*8 {
1160 if v.len() < 32+2*8 + 33+32 {
1161 return Err(DecodeError::ShortRead);
1163 let mut inner_array = [0; 32];
1164 inner_array.copy_from_slice(&v[48..48+32]);
1165 Some(DataLossProtect {
1166 your_last_per_commitment_secret: inner_array,
1167 my_current_per_commitment_point: secp_pubkey!(&Secp256k1::without_caps(), &v[48+32..48+32+33]),
1172 channel_id: deserialize(&v[0..32]).unwrap(),
1173 next_local_commitment_number: byte_utils::slice_to_be64(&v[32..40]),
1174 next_remote_commitment_number: byte_utils::slice_to_be64(&v[40..48]),
1175 data_loss_protect: data_loss_protect,
1179 impl MsgEncodable for ChannelReestablish {
1180 fn encode(&self) -> Vec<u8> {
1181 let mut res = Vec::with_capacity(if self.data_loss_protect.is_some() { 32+2*8+33+32 } else { 32+2*8 });
1183 res.extend_from_slice(&serialize(&self.channel_id).unwrap()[..]);
1184 res.extend_from_slice(&byte_utils::be64_to_array(self.next_local_commitment_number));
1185 res.extend_from_slice(&byte_utils::be64_to_array(self.next_remote_commitment_number));
1187 if let &Some(ref data_loss_protect) = &self.data_loss_protect {
1188 res.extend_from_slice(&data_loss_protect.your_last_per_commitment_secret[..]);
1189 res.extend_from_slice(&data_loss_protect.my_current_per_commitment_point.serialize());
1195 impl MsgDecodable for AnnouncementSignatures {
1196 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1197 if v.len() < 32+8+64*2 {
1198 return Err(DecodeError::ShortRead);
1200 let secp_ctx = Secp256k1::without_caps();
1201 let mut channel_id = [0; 32];
1202 channel_id[..].copy_from_slice(&v[0..32]);
1205 short_channel_id: byte_utils::slice_to_be64(&v[32..40]),
1206 node_signature: secp_signature!(&secp_ctx, &v[40..104]),
1207 bitcoin_signature: secp_signature!(&secp_ctx, &v[104..168]),
1211 impl MsgEncodable for AnnouncementSignatures {
1212 fn encode(&self) -> Vec<u8> {
1213 let mut res = Vec::with_capacity(32+8+64*2);
1214 res.extend_from_slice(&self.channel_id);
1215 res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id));
1216 let secp_ctx = Secp256k1::without_caps();
1217 res.extend_from_slice(&self.node_signature.serialize_compact(&secp_ctx));
1218 res.extend_from_slice(&self.bitcoin_signature.serialize_compact(&secp_ctx));
1223 impl MsgDecodable for UnsignedNodeAnnouncement {
1224 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1225 let features = GlobalFeatures::decode(&v[..])?;
1226 if features.requires_unknown_bits() {
1227 return Err(DecodeError::UnknownRequiredFeature);
1230 if v.len() < features.encoded_len() + 4 + 33 + 3 + 32 + 2 {
1231 return Err(DecodeError::ShortRead);
1233 let start = features.encoded_len();
1235 let mut rgb = [0; 3];
1236 rgb.copy_from_slice(&v[start + 37..start + 40]);
1238 let mut alias = [0; 32];
1239 alias.copy_from_slice(&v[start + 40..start + 72]);
1241 let addrlen = byte_utils::slice_to_be16(&v[start + 72..start + 74]) as usize;
1242 if v.len() < start + 74 + addrlen {
1243 return Err(DecodeError::ShortRead);
1245 let addr_read_limit = start + 74 + addrlen;
1247 let mut addresses = Vec::with_capacity(4);
1248 let mut read_pos = start + 74;
1250 if addr_read_limit <= read_pos { break; }
1253 if addresses.len() > 0 {
1254 return Err(DecodeError::ExtraAddressesPerType);
1256 if addr_read_limit < read_pos + 1 + 6 {
1257 return Err(DecodeError::BadLengthDescriptor);
1259 let mut addr = [0; 4];
1260 addr.copy_from_slice(&v[read_pos + 1..read_pos + 5]);
1261 addresses.push(NetAddress::IPv4 {
1263 port: byte_utils::slice_to_be16(&v[read_pos + 5..read_pos + 7]),
1268 if addresses.len() > 1 || (addresses.len() == 1 && addresses[0].get_id() != 1) {
1269 return Err(DecodeError::ExtraAddressesPerType);
1271 if addr_read_limit < read_pos + 1 + 18 {
1272 return Err(DecodeError::BadLengthDescriptor);
1274 let mut addr = [0; 16];
1275 addr.copy_from_slice(&v[read_pos + 1..read_pos + 17]);
1276 addresses.push(NetAddress::IPv6 {
1278 port: byte_utils::slice_to_be16(&v[read_pos + 17..read_pos + 19]),
1283 if addresses.len() > 2 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 2) {
1284 return Err(DecodeError::ExtraAddressesPerType);
1286 if addr_read_limit < read_pos + 1 + 12 {
1287 return Err(DecodeError::BadLengthDescriptor);
1289 let mut addr = [0; 10];
1290 addr.copy_from_slice(&v[read_pos + 1..read_pos + 11]);
1291 addresses.push(NetAddress::OnionV2 {
1293 port: byte_utils::slice_to_be16(&v[read_pos + 11..read_pos + 13]),
1298 if addresses.len() > 3 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 3) {
1299 return Err(DecodeError::ExtraAddressesPerType);
1301 if addr_read_limit < read_pos + 1 + 37 {
1302 return Err(DecodeError::BadLengthDescriptor);
1304 let mut ed25519_pubkey = [0; 32];
1305 ed25519_pubkey.copy_from_slice(&v[read_pos + 1..read_pos + 33]);
1306 addresses.push(NetAddress::OnionV3 {
1308 checksum: byte_utils::slice_to_be16(&v[read_pos + 33..read_pos + 35]),
1309 version: v[read_pos + 35],
1310 port: byte_utils::slice_to_be16(&v[read_pos + 36..read_pos + 38]),
1314 _ => { break; } // We've read all we can, we dont understand anything higher (and they're sorted)
1318 let excess_address_data = if read_pos < addr_read_limit {
1319 let mut excess_address_data = Vec::with_capacity(addr_read_limit - read_pos);
1320 excess_address_data.extend_from_slice(&v[read_pos..addr_read_limit]);
1322 } else { Vec::new() };
1324 let mut excess_data = Vec::with_capacity(v.len() - addr_read_limit);
1325 excess_data.extend_from_slice(&v[addr_read_limit..]);
1327 let secp_ctx = Secp256k1::without_caps();
1330 timestamp: byte_utils::slice_to_be32(&v[start..start + 4]),
1331 node_id: secp_pubkey!(&secp_ctx, &v[start + 4..start + 37]),
1335 excess_address_data,
1340 impl MsgEncodable for UnsignedNodeAnnouncement {
1341 fn encode(&self) -> Vec<u8> {
1342 let features = self.features.encode();
1343 let mut res = Vec::with_capacity(74 + features.len() + self.addresses.len()*7 + self.excess_address_data.len() + self.excess_data.len());
1344 res.extend_from_slice(&features[..]);
1345 res.extend_from_slice(&byte_utils::be32_to_array(self.timestamp));
1346 res.extend_from_slice(&self.node_id.serialize());
1347 res.extend_from_slice(&self.rgb);
1348 res.extend_from_slice(&self.alias);
1349 let mut addr_slice = Vec::with_capacity(self.addresses.len() * 18);
1350 let mut addrs_to_encode = self.addresses.clone();
1351 addrs_to_encode.sort_unstable_by(|a, b| { a.get_id().cmp(&b.get_id()) });
1352 addrs_to_encode.dedup_by(|a, b| { a.get_id() == b.get_id() });
1353 for addr in addrs_to_encode.iter() {
1355 &NetAddress::IPv4{addr, port} => {
1357 addr_slice.extend_from_slice(&addr);
1358 addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
1360 &NetAddress::IPv6{addr, port} => {
1362 addr_slice.extend_from_slice(&addr);
1363 addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
1365 &NetAddress::OnionV2{addr, port} => {
1367 addr_slice.extend_from_slice(&addr);
1368 addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
1370 &NetAddress::OnionV3{ed25519_pubkey, checksum, version, port} => {
1372 addr_slice.extend_from_slice(&ed25519_pubkey);
1373 addr_slice.extend_from_slice(&byte_utils::be16_to_array(checksum));
1374 addr_slice.push(version);
1375 addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
1379 res.extend_from_slice(&byte_utils::be16_to_array((addr_slice.len() + self.excess_address_data.len()) as u16));
1380 res.extend_from_slice(&addr_slice[..]);
1381 res.extend_from_slice(&self.excess_address_data[..]);
1382 res.extend_from_slice(&self.excess_data[..]);
1387 impl MsgDecodable for NodeAnnouncement {
1388 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1390 return Err(DecodeError::ShortRead);
1392 let secp_ctx = Secp256k1::without_caps();
1394 signature: secp_signature!(&secp_ctx, &v[0..64]),
1395 contents: UnsignedNodeAnnouncement::decode(&v[64..])?,
1399 impl MsgEncodable for NodeAnnouncement {
1400 fn encode(&self) -> Vec<u8> {
1401 let contents = self.contents.encode();
1402 let mut res = Vec::with_capacity(64 + contents.len());
1403 let secp_ctx = Secp256k1::without_caps();
1404 res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx));
1405 res.extend_from_slice(&contents);
1410 impl MsgDecodable for UnsignedChannelAnnouncement {
1411 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1412 let features = GlobalFeatures::decode(&v[..])?;
1413 if features.requires_unknown_bits() {
1414 return Err(DecodeError::UnknownRequiredFeature);
1416 if v.len() < features.encoded_len() + 32 + 8 + 33*4 {
1417 return Err(DecodeError::ShortRead);
1419 let start = features.encoded_len();
1420 let secp_ctx = Secp256k1::without_caps();
1421 let mut excess_data = Vec::with_capacity(v.len() - start - 172);
1422 excess_data.extend_from_slice(&v[start + 172..]);
1425 chain_hash: deserialize(&v[start..start + 32]).unwrap(),
1426 short_channel_id: byte_utils::slice_to_be64(&v[start + 32..start + 40]),
1427 node_id_1: secp_pubkey!(&secp_ctx, &v[start + 40..start + 73]),
1428 node_id_2: secp_pubkey!(&secp_ctx, &v[start + 73..start + 106]),
1429 bitcoin_key_1: secp_pubkey!(&secp_ctx, &v[start + 106..start + 139]),
1430 bitcoin_key_2: secp_pubkey!(&secp_ctx, &v[start + 139..start + 172]),
1435 impl MsgEncodable for UnsignedChannelAnnouncement {
1436 fn encode(&self) -> Vec<u8> {
1437 let features = self.features.encode();
1438 let mut res = Vec::with_capacity(172 + features.len() + self.excess_data.len());
1439 res.extend_from_slice(&features[..]);
1440 res.extend_from_slice(&self.chain_hash[..]);
1441 res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id));
1442 res.extend_from_slice(&self.node_id_1.serialize());
1443 res.extend_from_slice(&self.node_id_2.serialize());
1444 res.extend_from_slice(&self.bitcoin_key_1.serialize());
1445 res.extend_from_slice(&self.bitcoin_key_2.serialize());
1446 res.extend_from_slice(&self.excess_data[..]);
1451 impl MsgDecodable for ChannelAnnouncement {
1452 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1454 return Err(DecodeError::ShortRead);
1456 let secp_ctx = Secp256k1::without_caps();
1458 node_signature_1: secp_signature!(&secp_ctx, &v[0..64]),
1459 node_signature_2: secp_signature!(&secp_ctx, &v[64..128]),
1460 bitcoin_signature_1: secp_signature!(&secp_ctx, &v[128..192]),
1461 bitcoin_signature_2: secp_signature!(&secp_ctx, &v[192..256]),
1462 contents: UnsignedChannelAnnouncement::decode(&v[256..])?,
1466 impl MsgEncodable for ChannelAnnouncement {
1467 fn encode(&self) -> Vec<u8> {
1468 let secp_ctx = Secp256k1::without_caps();
1469 let contents = self.contents.encode();
1470 let mut res = Vec::with_capacity(64 + contents.len());
1471 res.extend_from_slice(&self.node_signature_1.serialize_compact(&secp_ctx));
1472 res.extend_from_slice(&self.node_signature_2.serialize_compact(&secp_ctx));
1473 res.extend_from_slice(&self.bitcoin_signature_1.serialize_compact(&secp_ctx));
1474 res.extend_from_slice(&self.bitcoin_signature_2.serialize_compact(&secp_ctx));
1475 res.extend_from_slice(&contents);
1480 impl MsgDecodable for UnsignedChannelUpdate {
1481 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1482 if v.len() < 32+8+4+2+2+8+4+4 {
1483 return Err(DecodeError::ShortRead);
1485 let mut excess_data = Vec::with_capacity(v.len() - 64);
1486 excess_data.extend_from_slice(&v[64..]);
1488 chain_hash: deserialize(&v[0..32]).unwrap(),
1489 short_channel_id: byte_utils::slice_to_be64(&v[32..40]),
1490 timestamp: byte_utils::slice_to_be32(&v[40..44]),
1491 flags: byte_utils::slice_to_be16(&v[44..46]),
1492 cltv_expiry_delta: byte_utils::slice_to_be16(&v[46..48]),
1493 htlc_minimum_msat: byte_utils::slice_to_be64(&v[48..56]),
1494 fee_base_msat: byte_utils::slice_to_be32(&v[56..60]),
1495 fee_proportional_millionths: byte_utils::slice_to_be32(&v[60..64]),
1500 impl MsgEncodable for UnsignedChannelUpdate {
1501 fn encode(&self) -> Vec<u8> {
1502 let mut res = Vec::with_capacity(64 + self.excess_data.len());
1503 res.extend_from_slice(&self.chain_hash[..]);
1504 res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id));
1505 res.extend_from_slice(&byte_utils::be32_to_array(self.timestamp));
1506 res.extend_from_slice(&byte_utils::be16_to_array(self.flags));
1507 res.extend_from_slice(&byte_utils::be16_to_array(self.cltv_expiry_delta));
1508 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_minimum_msat));
1509 res.extend_from_slice(&byte_utils::be32_to_array(self.fee_base_msat));
1510 res.extend_from_slice(&byte_utils::be32_to_array(self.fee_proportional_millionths));
1511 res.extend_from_slice(&self.excess_data[..]);
1516 impl MsgDecodable for ChannelUpdate {
1517 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1519 return Err(DecodeError::ShortRead);
1521 let secp_ctx = Secp256k1::without_caps();
1523 signature: secp_signature!(&secp_ctx, &v[0..64]),
1524 contents: UnsignedChannelUpdate::decode(&v[64..])?,
1528 impl MsgEncodable for ChannelUpdate {
1529 fn encode(&self) -> Vec<u8> {
1530 let mut res = Vec::with_capacity(128);
1531 res.extend_from_slice(&self.signature.serialize_compact(&Secp256k1::without_caps())[..]);
1532 res.extend_from_slice(&self.contents.encode()[..]);
1537 impl MsgDecodable for OnionRealm0HopData {
1538 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1540 return Err(DecodeError::ShortRead);
1542 Ok(OnionRealm0HopData {
1543 short_channel_id: byte_utils::slice_to_be64(&v[0..8]),
1544 amt_to_forward: byte_utils::slice_to_be64(&v[8..16]),
1545 outgoing_cltv_value: byte_utils::slice_to_be32(&v[16..20]),
1549 impl MsgEncodable for OnionRealm0HopData {
1550 fn encode(&self) -> Vec<u8> {
1551 let mut res = Vec::with_capacity(32);
1552 res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id));
1553 res.extend_from_slice(&byte_utils::be64_to_array(self.amt_to_forward));
1554 res.extend_from_slice(&byte_utils::be32_to_array(self.outgoing_cltv_value));
1560 impl MsgDecodable for OnionHopData {
1561 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1563 return Err(DecodeError::ShortRead);
1567 return Err(DecodeError::UnknownRealmByte);
1569 let mut hmac = [0; 32];
1570 hmac[..].copy_from_slice(&v[33..65]);
1573 data: OnionRealm0HopData::decode(&v[1..33])?,
1578 impl MsgEncodable for OnionHopData {
1579 fn encode(&self) -> Vec<u8> {
1580 let mut res = Vec::with_capacity(65);
1581 res.push(self.realm);
1582 res.extend_from_slice(&self.data.encode()[..]);
1583 res.extend_from_slice(&self.hmac);
1588 impl MsgDecodable for OnionPacket {
1589 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1590 if v.len() < 1+33+20*65+32 {
1591 return Err(DecodeError::ShortRead);
1593 let mut hop_data = [0; 20*65];
1594 hop_data.copy_from_slice(&v[34..1334]);
1595 let mut hmac = [0; 32];
1596 hmac.copy_from_slice(&v[1334..1366]);
1597 let secp_ctx = Secp256k1::without_caps();
1600 public_key: PublicKey::from_slice(&secp_ctx, &v[1..34]),
1606 impl MsgEncodable for OnionPacket {
1607 fn encode(&self) -> Vec<u8> {
1608 let mut res = Vec::with_capacity(1 + 33 + 20*65 + 32);
1609 res.push(self.version);
1610 match self.public_key {
1611 Ok(pubkey) => res.extend_from_slice(&pubkey.serialize()),
1612 Err(_) => res.extend_from_slice(&[0; 33]),
1614 res.extend_from_slice(&self.hop_data);
1615 res.extend_from_slice(&self.hmac);
1620 impl MsgDecodable for DecodedOnionErrorPacket {
1621 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1622 if v.len() < 32 + 4 {
1623 return Err(DecodeError::ShortRead);
1625 let failuremsg_len = byte_utils::slice_to_be16(&v[32..34]) as usize;
1626 if v.len() < 32 + 4 + failuremsg_len {
1627 return Err(DecodeError::ShortRead);
1629 let padding_len = byte_utils::slice_to_be16(&v[34 + failuremsg_len..]) as usize;
1630 if v.len() < 32 + 4 + failuremsg_len + padding_len {
1631 return Err(DecodeError::ShortRead);
1634 let mut hmac = [0; 32];
1635 hmac.copy_from_slice(&v[0..32]);
1638 failuremsg: v[34..34 + failuremsg_len].to_vec(),
1639 pad: v[36 + failuremsg_len..36 + failuremsg_len + padding_len].to_vec(),
1643 impl MsgEncodable for DecodedOnionErrorPacket {
1644 fn encode(&self) -> Vec<u8> {
1645 let mut res = Vec::with_capacity(32 + 4 + self.failuremsg.len() + self.pad.len());
1646 res.extend_from_slice(&self.hmac);
1647 res.extend_from_slice(&[((self.failuremsg.len() >> 8) & 0xff) as u8, (self.failuremsg.len() & 0xff) as u8]);
1648 res.extend_from_slice(&self.failuremsg);
1649 res.extend_from_slice(&[((self.pad.len() >> 8) & 0xff) as u8, (self.pad.len() & 0xff) as u8]);
1650 res.extend_from_slice(&self.pad);
1655 impl MsgDecodable for OnionErrorPacket {
1656 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1658 return Err(DecodeError::ShortRead);
1660 let len = byte_utils::slice_to_be16(&v[0..2]) as usize;
1661 if v.len() < 2 + len {
1662 return Err(DecodeError::ShortRead);
1665 data: v[2..len+2].to_vec(),
1669 impl MsgEncodable for OnionErrorPacket {
1670 fn encode(&self) -> Vec<u8> {
1671 let mut res = Vec::with_capacity(2 + self.data.len());
1672 res.extend_from_slice(&byte_utils::be16_to_array(self.data.len() as u16));
1673 res.extend_from_slice(&self.data);
1678 impl MsgEncodable for ErrorMessage {
1679 fn encode(&self) -> Vec<u8> {
1680 let mut res = Vec::with_capacity(34 + self.data.len());
1681 res.extend_from_slice(&self.channel_id);
1682 res.extend_from_slice(&byte_utils::be16_to_array(self.data.len() as u16));
1683 res.extend_from_slice(&self.data.as_bytes());
1687 impl MsgDecodable for ErrorMessage {
1688 fn decode(v: &[u8]) -> Result<Self,DecodeError> {
1690 return Err(DecodeError::ShortRead);
1692 // Unlike most messages, BOLT 1 requires we truncate our read if the value is out of range
1693 let len = cmp::min(byte_utils::slice_to_be16(&v[32..34]) as usize, v.len() - 34);
1694 let data = match String::from_utf8(v[34..34 + len].to_vec()) {
1696 Err(_) => return Err(DecodeError::BadText),
1698 let mut channel_id = [0; 32];
1699 channel_id[..].copy_from_slice(&v[0..32]);
1707 impl_writeable!(AcceptChannel, {
1708 temporary_channel_id,
1709 dust_limit_satoshis,
1710 max_htlc_value_in_flight_msat,
1711 channel_reserve_satoshis,
1717 revocation_basepoint,
1719 delayed_payment_basepoint,
1721 first_per_commitment_point,
1722 shutdown_scriptpubkey
1725 impl_writeable!(AnnouncementSignatures, {
1732 impl<W: Writer> Writeable<W> for ChannelReestablish {
1733 fn write(&self, w: &mut W) -> Result<(), DecodeError> {
1734 self.channel_id.write(w)?;
1735 self.next_local_commitment_number.write(w)?;
1736 self.next_remote_commitment_number.write(w)?;
1737 if let Some(ref data_loss_protect) = self.data_loss_protect {
1738 data_loss_protect.your_last_per_commitment_secret.write(w)?;
1739 data_loss_protect.my_current_per_commitment_point.write(w)?;
1745 impl<R: Read> Readable<R> for ChannelReestablish{
1746 fn read(r: &mut R) -> Result<Self, DecodeError> {
1748 channel_id: Readable::read(r)?,
1749 next_local_commitment_number: Readable::read(r)?,
1750 next_remote_commitment_number: Readable::read(r)?,
1751 data_loss_protect: {
1752 match <[u8; 32] as Readable<R>>::read(r) {
1753 Ok(your_last_per_commitment_secret) =>
1754 Some(DataLossProtect {
1755 your_last_per_commitment_secret,
1756 my_current_per_commitment_point: Readable::read(r)?,
1758 Err(DecodeError::ShortRead) => None,
1759 Err(e) => return Err(e)
1766 impl_writeable!(ClosingSigned, {
1772 impl_writeable!(CommitmentSigned, {
1778 impl_writeable!(DecodedOnionErrorPacket, {
1784 impl_writeable!(FundingCreated, {
1785 temporary_channel_id,
1787 funding_output_index,
1791 impl_writeable!(FundingSigned, {
1796 impl_writeable!(FundingLocked, {
1798 next_per_commitment_point
1801 impl_writeable!(GlobalFeatures, {
1805 impl_writeable!(LocalFeatures, {
1809 impl_writeable!(Init, {
1814 impl_writeable!(OpenChannel, {
1816 temporary_channel_id,
1819 dust_limit_satoshis,
1820 max_htlc_value_in_flight_msat,
1821 channel_reserve_satoshis,
1827 revocation_basepoint,
1829 delayed_payment_basepoint,
1831 first_per_commitment_point,
1833 shutdown_scriptpubkey
1836 impl_writeable!(RevokeAndACK, {
1838 per_commitment_secret,
1839 next_per_commitment_point
1842 impl_writeable!(Shutdown, {
1847 impl_writeable!(UpdateFailHTLC, {
1853 impl_writeable!(UpdateFailMalformedHTLC, {
1860 impl_writeable!(UpdateFee, {
1865 impl_writeable!(UpdateFulfillHTLC, {
1871 impl_writeable!(OnionErrorPacket, {
1875 impl<W: Writer> Writeable<W> for OnionPacket {
1876 fn write(&self, w: &mut W) -> Result<(), DecodeError> {
1877 self.version.write(w)?;
1878 match self.public_key {
1879 Ok(pubkey) => pubkey.write(w)?,
1880 Err(_) => [0u8;33].write(w)?,
1882 w.write_all(&self.hop_data)?;
1883 self.hmac.write(w)?;
1888 impl<R: Read> Readable<R> for OnionPacket {
1889 fn read(r: &mut R) -> Result<Self, DecodeError> {
1891 version: Readable::read(r)?,
1893 let mut buf = [0u8;33];
1894 r.read_exact(&mut buf)?;
1895 PublicKey::from_slice(&Secp256k1::without_caps(), &buf)
1897 hop_data: Readable::read(r)?,
1898 hmac: Readable::read(r)?,
1903 impl_writeable!(UpdateAddHTLC, {
1909 onion_routing_packet
1912 impl<W: Writer> Writeable<W> for OnionRealm0HopData {
1913 fn write(&self, w: &mut W) -> Result<(), DecodeError> {
1914 self.short_channel_id.write(w)?;
1915 self.amt_to_forward.write(w)?;
1916 self.outgoing_cltv_value.write(w)?;
1917 w.write_all(&[0;12])?;
1922 impl<R: Read> Readable<R> for OnionRealm0HopData {
1923 fn read(r: &mut R) -> Result<Self, DecodeError> {
1924 Ok(OnionRealm0HopData {
1925 short_channel_id: Readable::read(r)?,
1926 amt_to_forward: Readable::read(r)?,
1927 outgoing_cltv_value: {
1928 let v: u32 = Readable::read(r)?;
1929 r.read_exact(&mut [0; 12])?;
1936 impl<W: Writer> Writeable<W> for OnionHopData {
1937 fn write(&self, w: &mut W) -> Result<(), DecodeError> {
1938 self.realm.write(w)?;
1939 self.data.write(w)?;
1940 self.hmac.write(w)?;
1945 impl<R: Read> Readable<R> for OnionHopData {
1946 fn read(r: &mut R) -> Result<Self, DecodeError> {
1949 let r: u8 = Readable::read(r)?;
1951 return Err(DecodeError::UnknownRealmByte);
1955 data: Readable::read(r)?,
1956 hmac: Readable::read(r)?,
1961 impl<W: Writer> Writeable<W> for Ping {
1962 fn write(&self, w: &mut W) -> Result<(), DecodeError> {
1963 self.ponglen.write(w)?;
1964 vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
1969 impl<R: Read> Readable<R> for Ping {
1970 fn read(r: &mut R) -> Result<Self, DecodeError> {
1972 ponglen: Readable::read(r)?,
1974 let byteslen = Readable::read(r)?;
1975 r.read_exact(&mut vec![0u8; byteslen as usize][..])?;
1982 impl<W: Writer> Writeable<W> for Pong {
1983 fn write(&self, w: &mut W) -> Result<(), DecodeError> {
1984 vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
1989 impl<R: Read> Readable<R> for Pong {
1990 fn read(r: &mut R) -> Result<Self, DecodeError> {
1993 let byteslen = Readable::read(r)?;
1994 r.read_exact(&mut vec![0u8; byteslen as usize][..])?;
2001 impl<W: Writer> Writeable<W> for UnsignedChannelAnnouncement {
2002 fn write(&self, w: &mut W) -> Result<(), DecodeError> {
2003 self.features.write(w)?;
2004 self.chain_hash.write(w)?;
2005 self.short_channel_id.write(w)?;
2006 self.node_id_1.write(w)?;
2007 self.node_id_2.write(w)?;
2008 self.bitcoin_key_1.write(w)?;
2009 self.bitcoin_key_2.write(w)?;
2010 w.write_all(&self.excess_data[..])?;
2015 impl<R: Read> Readable<R> for UnsignedChannelAnnouncement {
2016 fn read(r: &mut R) -> Result<Self, DecodeError> {
2019 let f: GlobalFeatures = Readable::read(r)?;
2020 if f.requires_unknown_bits() {
2021 return Err(DecodeError::UnknownRequiredFeature);
2025 chain_hash: Readable::read(r)?,
2026 short_channel_id: Readable::read(r)?,
2027 node_id_1: Readable::read(r)?,
2028 node_id_2: Readable::read(r)?,
2029 bitcoin_key_1: Readable::read(r)?,
2030 bitcoin_key_2: Readable::read(r)?,
2032 let mut excess_data = vec![];
2033 r.read_to_end(&mut excess_data)?;
2040 impl_writeable!(ChannelAnnouncement,{
2043 bitcoin_signature_1,
2044 bitcoin_signature_2,
2048 impl<W: Writer> Writeable<W> for UnsignedChannelUpdate {
2049 fn write(&self, w: &mut W) -> Result<(), DecodeError> {
2050 self.chain_hash.write(w)?;
2051 self.short_channel_id.write(w)?;
2052 self.timestamp.write(w)?;
2053 self.flags.write(w)?;
2054 self.cltv_expiry_delta.write(w)?;
2055 self.htlc_minimum_msat.write(w)?;
2056 self.fee_base_msat.write(w)?;
2057 self.fee_proportional_millionths.write(w)?;
2058 w.write_all(&self.excess_data[..])?;
2063 impl<R: Read> Readable<R> for UnsignedChannelUpdate {
2064 fn read(r: &mut R) -> Result<Self, DecodeError> {
2066 chain_hash: Readable::read(r)?,
2067 short_channel_id: Readable::read(r)?,
2068 timestamp: Readable::read(r)?,
2069 flags: Readable::read(r)?,
2070 cltv_expiry_delta: Readable::read(r)?,
2071 htlc_minimum_msat: Readable::read(r)?,
2072 fee_base_msat: Readable::read(r)?,
2073 fee_proportional_millionths: Readable::read(r)?,
2075 let mut excess_data = vec![];
2076 r.read_to_end(&mut excess_data)?;
2083 impl_writeable!(ChannelUpdate, {
2088 impl<W: Writer> Writeable<W> for ErrorMessage {
2089 fn write(&self, w: &mut W) -> Result<(), DecodeError> {
2090 self.channel_id.write(w)?;
2091 self.data.as_bytes().to_vec().write(w)?; // write with size prefix
2096 impl<R: Read> Readable<R> for ErrorMessage {
2097 fn read(r: &mut R) -> Result<Self, DecodeError> {
2099 channel_id: Readable::read(r)?,
2101 let mut sz: usize = <u16 as Readable<R>>::read(r)? as usize;
2102 let mut data = vec![];
2103 let data_len = r.read_to_end(&mut data)?;
2104 sz = cmp::min(data_len, sz);
2105 match String::from_utf8(data[..sz as usize].to_vec()) {
2107 Err(_) => return Err(DecodeError::BadText),
2114 impl<W: Writer> Writeable<W> for UnsignedNodeAnnouncement {
2115 fn write(&self, w: &mut W) -> Result<(), DecodeError> {
2116 self.features.write(w)?;
2117 self.timestamp.write(w)?;
2118 self.node_id.write(w)?;
2119 w.write_all(&self.rgb)?;
2120 self.alias.write(w)?;
2122 let mut addr_slice = Vec::with_capacity(self.addresses.len() * 18);
2123 let mut addrs_to_encode = self.addresses.clone();
2124 addrs_to_encode.sort_unstable_by(|a, b| { a.get_id().cmp(&b.get_id()) });
2125 addrs_to_encode.dedup_by(|a, b| { a.get_id() == b.get_id() });
2126 for addr in addrs_to_encode.iter() {
2128 &NetAddress::IPv4{addr, port} => {
2130 addr_slice.extend_from_slice(&addr);
2131 addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
2133 &NetAddress::IPv6{addr, port} => {
2135 addr_slice.extend_from_slice(&addr);
2136 addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
2138 &NetAddress::OnionV2{addr, port} => {
2140 addr_slice.extend_from_slice(&addr);
2141 addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
2143 &NetAddress::OnionV3{ed25519_pubkey, checksum, version, port} => {
2145 addr_slice.extend_from_slice(&ed25519_pubkey);
2146 addr_slice.extend_from_slice(&byte_utils::be16_to_array(checksum));
2147 addr_slice.push(version);
2148 addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
2152 ((addr_slice.len() + self.excess_address_data.len()) as u16).write(w)?;
2153 w.write_all(&addr_slice[..])?;
2154 w.write_all(&self.excess_address_data[..])?;
2155 w.write_all(&self.excess_data[..])?;
2160 impl<R: Read> Readable<R> for UnsignedNodeAnnouncement {
2161 fn read(r: &mut R) -> Result<Self, DecodeError> {
2162 let features: GlobalFeatures = Readable::read(r)?;
2163 if features.requires_unknown_bits() {
2164 return Err(DecodeError::UnknownRequiredFeature);
2166 let timestamp: u32 = Readable::read(r)?;
2167 let node_id: PublicKey = Readable::read(r)?;
2168 let mut rgb = [0; 3];
2169 r.read_exact(&mut rgb)?;
2170 let alias: [u8; 32] = Readable::read(r)?;
2172 let addrlen: u16 = Readable::read(r)?;
2173 let mut addr_readpos = 0;
2174 let mut addresses = Vec::with_capacity(4);
2178 if addrlen <= addr_readpos { break; }
2179 f = Readable::read(r)?;
2182 if addresses.len() > 0 {
2183 return Err(DecodeError::ExtraAddressesPerType);
2185 if addrlen < addr_readpos + 1 + 6 {
2186 return Err(DecodeError::BadLengthDescriptor);
2188 addresses.push(NetAddress::IPv4 {
2190 let mut addr = [0; 4];
2191 r.read_exact(&mut addr)?;
2194 port: Readable::read(r)?,
2196 addr_readpos += 1 + 6
2199 if addresses.len() > 1 || (addresses.len() == 1 && addresses[0].get_id() != 1) {
2200 return Err(DecodeError::ExtraAddressesPerType);
2202 if addrlen < addr_readpos + 1 + 18 {
2203 return Err(DecodeError::BadLengthDescriptor);
2205 addresses.push(NetAddress::IPv6 {
2207 let mut addr = [0; 16];
2208 r.read_exact(&mut addr)?;
2211 port: Readable::read(r)?,
2213 addr_readpos += 1 + 18
2216 if addresses.len() > 2 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 2) {
2217 return Err(DecodeError::ExtraAddressesPerType);
2219 if addrlen < addr_readpos + 1 + 12 {
2220 return Err(DecodeError::BadLengthDescriptor);
2222 addresses.push(NetAddress::OnionV2 {
2224 let mut addr = [0; 10];
2225 r.read_exact(&mut addr)?;
2228 port: Readable::read(r)?,
2230 addr_readpos += 1 + 12
2233 if addresses.len() > 3 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 3) {
2234 return Err(DecodeError::ExtraAddressesPerType);
2236 if addrlen < addr_readpos + 1 + 37 {
2237 return Err(DecodeError::BadLengthDescriptor);
2239 addresses.push(NetAddress::OnionV3 {
2240 ed25519_pubkey: Readable::read(r)?,
2241 checksum: Readable::read(r)?,
2242 version: Readable::read(r)?,
2243 port: Readable::read(r)?,
2245 addr_readpos += 1 + 37
2247 _ => { excess = 1; break; }
2251 let mut excess_data = vec![];
2252 let excess_address_data = if addr_readpos < addrlen {
2253 let mut excess_address_data = vec![0; (addrlen - addr_readpos) as usize];
2254 r.read_exact(&mut excess_address_data[excess..])?;
2256 excess_address_data[0] = f;
2261 excess_data.push(f);
2266 Ok(UnsignedNodeAnnouncement {
2268 timestamp: timestamp,
2272 addresses: addresses,
2273 excess_address_data: excess_address_data,
2275 r.read_to_end(&mut excess_data)?;
2282 impl_writeable!(NodeAnnouncement, {
2290 use ln::msgs::MsgEncodable;
2292 use secp256k1::key::{PublicKey,SecretKey};
2293 use secp256k1::Secp256k1;
2296 fn encoding_channel_reestablish_no_secret() {
2297 let cr = msgs::ChannelReestablish {
2298 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],
2299 next_local_commitment_number: 3,
2300 next_remote_commitment_number: 4,
2301 data_loss_protect: None,
2304 let encoded_value = cr.encode();
2307 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]
2312 fn encoding_channel_reestablish_with_secret() {
2314 let secp_ctx = Secp256k1::new();
2315 PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap())
2318 let cr = msgs::ChannelReestablish {
2319 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],
2320 next_local_commitment_number: 3,
2321 next_remote_commitment_number: 4,
2322 data_loss_protect: Some(msgs::DataLossProtect { your_last_per_commitment_secret: [9;32], my_current_per_commitment_point: public_key}),
2325 let encoded_value = cr.encode();
2328 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]