1 use secp256k1::key::PublicKey;
2 use secp256k1::{Secp256k1, Signature};
3 use bitcoin::util::uint::Uint256;
4 use bitcoin::util::hash::Sha256dHash;
5 use bitcoin::network::serialize::{deserialize,serialize};
6 use bitcoin::blockdata::script::Script;
10 use std::result::Result;
12 use util::{byte_utils, internal_traits, events};
14 pub trait MsgEncodable {
15 fn encode(&self) -> Vec<u8>;
17 fn encoded_len(&self) -> usize { self.encode().len() }
19 fn encode_with_len(&self) -> Vec<u8> {
20 let enc = self.encode();
21 let mut res = Vec::with_capacity(enc.len() + 2);
22 res.extend_from_slice(&byte_utils::be16_to_array(enc.len() as u16));
23 res.extend_from_slice(&enc);
28 pub enum DecodeError {
29 /// Unknown realm byte in an OnionHopData packet
31 /// Failed to decode a public key (ie it's invalid)
33 /// Failed to decode a signature (ie it's invalid)
35 /// Buffer not of right length (either too short or too long)
37 /// node_announcement included more than one address of a given type!
38 ExtraAddressesPerType,
40 pub trait MsgDecodable: Sized {
41 fn decode(v: &[u8]) -> Result<Self, DecodeError>;
44 /// Tracks localfeatures which are only in init messages
45 #[derive(Clone, PartialEq)]
46 pub struct LocalFeatures {
51 pub fn new() -> LocalFeatures {
57 pub fn supports_data_loss_protect(&self) -> bool {
58 self.flags.len() > 0 && (self.flags[0] & 3) != 0
60 pub fn requires_data_loss_protect(&self) -> bool {
61 self.flags.len() > 0 && (self.flags[0] & 1) != 0
64 pub fn supports_initial_routing_sync(&self) -> bool {
65 self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0
68 pub fn supports_upfront_shutdown_script(&self) -> bool {
69 self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0
71 pub fn requires_upfront_shutdown_script(&self) -> bool {
72 self.flags.len() > 0 && (self.flags[0] & (1 << 4)) != 0
75 pub fn requires_unknown_bits(&self) -> bool {
76 for (idx, &byte) in self.flags.iter().enumerate() {
77 if idx != 0 && (byte & 0x55) != 0 {
79 } else if idx == 0 && (byte & 0x14) != 0 {
86 pub fn supports_unknown_bits(&self) -> bool {
87 for (idx, &byte) in self.flags.iter().enumerate() {
88 if idx != 0 && byte != 0 {
90 } else if idx == 0 && (byte & 0xc4) != 0 {
98 /// Tracks globalfeatures which are in init messages and routing announcements
99 #[derive(Clone, PartialEq)]
100 pub struct GlobalFeatures {
104 impl GlobalFeatures {
105 pub fn new() -> GlobalFeatures {
111 pub fn requires_unknown_bits(&self) -> bool {
112 for &byte in self.flags.iter() {
113 if (byte & 0x55) != 0 {
120 pub fn supports_unknown_bits(&self) -> bool {
121 for &byte in self.flags.iter() {
131 pub global_features: GlobalFeatures,
132 pub local_features: LocalFeatures,
135 pub struct OpenChannel {
136 pub chain_hash: Sha256dHash,
137 pub temporary_channel_id: Uint256,
138 pub funding_satoshis: u64,
140 pub dust_limit_satoshis: u64,
141 pub max_htlc_value_in_flight_msat: u64,
142 pub channel_reserve_satoshis: u64,
143 pub htlc_minimum_msat: u64,
144 pub feerate_per_kw: u32,
145 pub to_self_delay: u16,
146 pub max_accepted_htlcs: u16,
147 pub funding_pubkey: PublicKey,
148 pub revocation_basepoint: PublicKey,
149 pub payment_basepoint: PublicKey,
150 pub delayed_payment_basepoint: PublicKey,
151 pub htlc_basepoint: PublicKey,
152 pub first_per_commitment_point: PublicKey,
153 pub channel_flags: u8,
154 pub shutdown_scriptpubkey: Option<Script>,
157 pub struct AcceptChannel {
158 pub temporary_channel_id: Uint256,
159 pub dust_limit_satoshis: u64,
160 pub max_htlc_value_in_flight_msat: u64,
161 pub channel_reserve_satoshis: u64,
162 pub htlc_minimum_msat: u64,
163 pub minimum_depth: u32,
164 pub to_self_delay: u16,
165 pub max_accepted_htlcs: u16,
166 pub funding_pubkey: PublicKey,
167 pub revocation_basepoint: PublicKey,
168 pub payment_basepoint: PublicKey,
169 pub delayed_payment_basepoint: PublicKey,
170 pub htlc_basepoint: PublicKey,
171 pub first_per_commitment_point: PublicKey,
172 pub shutdown_scriptpubkey: Option<Script>,
175 pub struct FundingCreated {
176 pub temporary_channel_id: Uint256,
177 pub funding_txid: Sha256dHash,
178 pub funding_output_index: u16,
179 pub signature: Signature,
182 pub struct FundingSigned {
183 pub channel_id: Uint256,
184 pub signature: Signature,
187 pub struct FundingLocked {
188 pub channel_id: Uint256,
189 pub next_per_commitment_point: PublicKey,
192 pub struct Shutdown {
193 pub channel_id: Uint256,
194 pub scriptpubkey: Script,
197 pub struct ClosingSigned {
198 pub channel_id: Uint256,
199 pub fee_satoshis: u64,
200 pub signature: Signature,
204 pub struct UpdateAddHTLC {
205 pub channel_id: Uint256,
207 pub amount_msat: u64,
208 pub payment_hash: [u8; 32],
209 pub cltv_expiry: u32,
210 pub onion_routing_packet: OnionPacket,
214 pub struct UpdateFulfillHTLC {
215 pub channel_id: Uint256,
217 pub payment_preimage: [u8; 32],
221 pub struct UpdateFailHTLC {
222 pub channel_id: Uint256,
224 pub reason: OnionErrorPacket,
228 pub struct UpdateFailMalformedHTLC {
229 pub channel_id: Uint256,
231 pub sha256_of_onion: [u8; 32],
232 pub failure_code: u16,
236 pub struct CommitmentSigned {
237 pub channel_id: Uint256,
238 pub signature: Signature,
239 pub htlc_signatures: Vec<Signature>,
242 pub struct RevokeAndACK {
243 pub channel_id: Uint256,
244 pub per_commitment_secret: [u8; 32],
245 pub next_per_commitment_point: PublicKey,
248 pub struct UpdateFee {
249 pub channel_id: Uint256,
250 pub feerate_per_kw: u32,
253 pub struct ChannelReestablish {
254 pub channel_id: Uint256,
255 pub next_local_commitment_number: u64,
256 pub next_remote_commitment_number: u64,
257 pub your_last_per_commitment_secret: Option<[u8; 32]>,
258 pub my_current_per_commitment_point: PublicKey,
262 pub struct AnnouncementSignatures {
263 pub channel_id: Uint256,
264 pub short_channel_id: u64,
265 pub node_signature: Signature,
266 pub bitcoin_signature: Signature,
270 pub enum NetAddress {
284 ed25519_pubkey: [u8; 32],
291 fn get_id(&self) -> u8 {
293 &NetAddress::IPv4 {..} => { 1 },
294 &NetAddress::IPv6 {..} => { 2 },
295 &NetAddress::OnionV2 {..} => { 3 },
296 &NetAddress::OnionV3 {..} => { 4 },
301 pub struct UnsignedNodeAnnouncement {
302 pub features: GlobalFeatures,
304 pub node_id: PublicKey,
307 /// List of addresses on which this node is reachable. Note that you may only have up to one
308 /// address of each type, if you have more, they may be silently discarded or we may panic!
309 pub addresses: Vec<NetAddress>,
311 pub struct NodeAnnouncement {
312 pub signature: Signature,
313 pub contents: UnsignedNodeAnnouncement,
316 #[derive(PartialEq, Clone)]
317 pub struct UnsignedChannelAnnouncement {
318 pub features: GlobalFeatures,
319 pub chain_hash: Sha256dHash,
320 pub short_channel_id: u64,
321 pub node_id_1: PublicKey,
322 pub node_id_2: PublicKey,
323 pub bitcoin_key_1: PublicKey,
324 pub bitcoin_key_2: PublicKey,
326 #[derive(PartialEq, Clone)]
327 pub struct ChannelAnnouncement {
328 pub node_signature_1: Signature,
329 pub node_signature_2: Signature,
330 pub bitcoin_signature_1: Signature,
331 pub bitcoin_signature_2: Signature,
332 pub contents: UnsignedChannelAnnouncement,
335 #[derive(PartialEq, Clone)]
336 pub struct UnsignedChannelUpdate {
337 pub chain_hash: Sha256dHash,
338 pub short_channel_id: u64,
341 pub cltv_expiry_delta: u16,
342 pub htlc_minimum_msat: u64,
343 pub fee_base_msat: u32,
344 pub fee_proportional_millionths: u32,
346 #[derive(PartialEq, Clone)]
347 pub struct ChannelUpdate {
348 pub signature: Signature,
349 pub contents: UnsignedChannelUpdate,
352 /// Used to put an error message in a HandleError
353 pub enum ErrorAction {
354 /// Indicates an inbound HTLC add resulted in a failure, and the UpdateFailHTLC provided in msg
355 /// should be sent back to the sender.
359 /// The peer took some action which made us think they were useless. Disconnect them.
361 /// The peer did something harmless that we weren't able to process, just log and ignore
365 pub struct HandleError { //TODO: rename me
366 pub err: &'static str,
367 pub msg: Option<ErrorAction>, //TODO: Make this required and rename it
370 /// Struct used to return values from revoke_and_ack messages, containing a bunch of commitment
371 /// transaction updates if they were pending.
372 pub struct CommitmentUpdate {
373 pub update_add_htlcs: Vec<UpdateAddHTLC>,
374 pub update_fulfill_htlcs: Vec<UpdateFulfillHTLC>,
375 pub update_fail_htlcs: Vec<UpdateFailHTLC>,
376 pub commitment_signed: CommitmentSigned,
379 pub enum HTLCFailChannelUpdate {
380 ChannelUpdateMessage {
384 short_channel_id: u64,
388 /// A trait to describe an object which can receive channel messages. Messages MAY be called in
389 /// paralell when they originate from different their_node_ids, however they MUST NOT be called in
390 /// paralell when the two calls have the same their_node_id.
391 pub trait ChannelMessageHandler : events::EventsProvider + Send + Sync {
393 fn handle_open_channel(&self, their_node_id: &PublicKey, msg: &OpenChannel) -> Result<AcceptChannel, HandleError>;
394 fn handle_accept_channel(&self, their_node_id: &PublicKey, msg: &AcceptChannel) -> Result<(), HandleError>;
395 fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &FundingCreated) -> Result<FundingSigned, HandleError>;
396 fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &FundingSigned) -> Result<(), HandleError>;
397 fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &FundingLocked) -> Result<Option<AnnouncementSignatures>, HandleError>;
400 fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &Shutdown) -> Result<(Option<Shutdown>, Option<ClosingSigned>), HandleError>;
401 fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned) -> Result<Option<ClosingSigned>, HandleError>;
404 fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &UpdateAddHTLC) -> Result<(), HandleError>;
405 fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFulfillHTLC) -> Result<(), HandleError>;
406 fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC) -> Result<Option<HTLCFailChannelUpdate>, HandleError>;
407 fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailMalformedHTLC) -> Result<(), HandleError>;
408 fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned) -> Result<(RevokeAndACK, Option<CommitmentSigned>), HandleError>;
409 fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &RevokeAndACK) -> Result<Option<CommitmentUpdate>, HandleError>;
411 fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &UpdateFee) -> Result<(), HandleError>;
413 // Channel-to-announce:
414 fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures) -> Result<(), HandleError>;
417 /// Indicates a connection to the peer failed/an existing connection was lost. If no connection
418 /// is believed to be possible in the future (eg they're sending us messages we don't
419 /// understand or indicate they require unknown feature bits), no_connection_possible is set
420 /// and any outstanding channels should be failed.
421 fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
424 pub trait RoutingMessageHandler : Send + Sync {
425 fn handle_node_announcement(&self, msg: &NodeAnnouncement) -> Result<(), HandleError>;
426 /// Handle a channel_announcement message, returning true if it should be forwarded on, false
427 /// or returning an Err otherwise.
428 fn handle_channel_announcement(&self, msg: &ChannelAnnouncement) -> Result<bool, HandleError>;
429 fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result<(), HandleError>;
430 fn handle_htlc_fail_channel_update(&self, update: &HTLCFailChannelUpdate);
433 pub struct OnionRealm0HopData {
434 pub short_channel_id: u64,
435 pub amt_to_forward: u64,
436 pub outgoing_cltv_value: u32,
437 // 12 bytes of 0-padding
440 pub struct OnionHopData {
442 pub data: OnionRealm0HopData,
445 unsafe impl internal_traits::NoDealloc for OnionHopData{}
448 pub struct OnionPacket {
450 pub public_key: PublicKey,
451 pub hop_data: [u8; 20*65],
455 pub struct DecodedOnionErrorPacket {
457 pub failuremsg: Vec<u8>,
462 pub struct OnionErrorPacket {
463 // This really should be a constant size slice, but the spec lets these things be up to 128KB?
464 // (TODO) We limit it in decode to much lower...
468 impl Error for DecodeError {
469 fn description(&self) -> &str {
471 DecodeError::UnknownRealmByte => "Unknown realm byte in Onion packet",
472 DecodeError::BadPublicKey => "Invalid public key in packet",
473 DecodeError::BadSignature => "Invalid signature in packet",
474 DecodeError::WrongLength => "Data was wrong length for packet",
475 DecodeError::ExtraAddressesPerType => "More than one address of a single type",
479 impl fmt::Display for DecodeError {
480 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
481 f.write_str(self.description())
485 impl fmt::Debug for HandleError {
486 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
487 f.write_str(self.err)
491 macro_rules! secp_pubkey {
492 ( $ctx: expr, $slice: expr ) => {
493 match PublicKey::from_slice($ctx, $slice) {
495 Err(_) => return Err(DecodeError::BadPublicKey)
500 macro_rules! secp_signature {
501 ( $ctx: expr, $slice: expr ) => {
502 match Signature::from_compact($ctx, $slice) {
504 Err(_) => return Err(DecodeError::BadSignature)
509 impl MsgDecodable for LocalFeatures {
510 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
511 if v.len() < 2 { return Err(DecodeError::WrongLength); }
512 let len = byte_utils::slice_to_be16(&v[0..2]) as usize;
513 if v.len() < len + 2 { return Err(DecodeError::WrongLength); }
514 let mut flags = Vec::with_capacity(len);
515 flags.extend_from_slice(&v[2..2 + len]);
521 impl MsgEncodable for LocalFeatures {
522 fn encode(&self) -> Vec<u8> {
523 let mut res = Vec::with_capacity(self.flags.len() + 2);
524 res.extend_from_slice(&byte_utils::be16_to_array(self.flags.len() as u16));
525 res.extend_from_slice(&self.flags[..]);
528 fn encoded_len(&self) -> usize { self.flags.len() + 2 }
531 impl MsgDecodable for GlobalFeatures {
532 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
533 if v.len() < 2 { return Err(DecodeError::WrongLength); }
534 let len = byte_utils::slice_to_be16(&v[0..2]) as usize;
535 if v.len() < len + 2 { return Err(DecodeError::WrongLength); }
536 let mut flags = Vec::with_capacity(len);
537 flags.extend_from_slice(&v[2..2 + len]);
543 impl MsgEncodable for GlobalFeatures {
544 fn encode(&self) -> Vec<u8> {
545 let mut res = Vec::with_capacity(self.flags.len() + 2);
546 res.extend_from_slice(&byte_utils::be16_to_array(self.flags.len() as u16));
547 res.extend_from_slice(&self.flags[..]);
550 fn encoded_len(&self) -> usize { self.flags.len() + 2 }
553 impl MsgDecodable for Init {
554 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
555 let global_features = GlobalFeatures::decode(v)?;
556 if v.len() < global_features.flags.len() + 4 {
557 return Err(DecodeError::WrongLength);
559 let local_features = LocalFeatures::decode(&v[global_features.flags.len() + 2..])?;
561 global_features: global_features,
562 local_features: local_features,
566 impl MsgEncodable for Init {
567 fn encode(&self) -> Vec<u8> {
568 let mut res = Vec::with_capacity(self.global_features.flags.len() + self.local_features.flags.len());
569 res.extend_from_slice(&self.global_features.encode()[..]);
570 res.extend_from_slice(&self.local_features.encode()[..]);
575 impl MsgDecodable for OpenChannel {
576 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
577 if v.len() < 2*32+6*8+4+2*2+6*33+1 {
578 return Err(DecodeError::WrongLength);
580 let ctx = Secp256k1::without_caps();
582 let mut shutdown_scriptpubkey = None;
584 let len = byte_utils::slice_to_be16(&v[319..321]) as usize;
585 if v.len() < 321+len {
586 return Err(DecodeError::WrongLength);
588 shutdown_scriptpubkey = Some(Script::from(v[321..321+len].to_vec()));
589 } else if v.len() != 2*32+6*8+4+2*2+6*33+1 { // Message cant have 1 extra byte
590 return Err(DecodeError::WrongLength);
594 chain_hash: deserialize(&v[0..32]).unwrap(),
595 temporary_channel_id: deserialize(&v[32..64]).unwrap(),
596 funding_satoshis: byte_utils::slice_to_be64(&v[64..72]),
597 push_msat: byte_utils::slice_to_be64(&v[72..80]),
598 dust_limit_satoshis: byte_utils::slice_to_be64(&v[80..88]),
599 max_htlc_value_in_flight_msat: byte_utils::slice_to_be64(&v[88..96]),
600 channel_reserve_satoshis: byte_utils::slice_to_be64(&v[96..104]),
601 htlc_minimum_msat: byte_utils::slice_to_be64(&v[104..112]),
602 feerate_per_kw: byte_utils::slice_to_be32(&v[112..116]),
603 to_self_delay: byte_utils::slice_to_be16(&v[116..118]),
604 max_accepted_htlcs: byte_utils::slice_to_be16(&v[118..120]),
605 funding_pubkey: secp_pubkey!(&ctx, &v[120..153]),
606 revocation_basepoint: secp_pubkey!(&ctx, &v[153..186]),
607 payment_basepoint: secp_pubkey!(&ctx, &v[186..219]),
608 delayed_payment_basepoint: secp_pubkey!(&ctx, &v[219..252]),
609 htlc_basepoint: secp_pubkey!(&ctx, &v[252..285]),
610 first_per_commitment_point: secp_pubkey!(&ctx, &v[285..318]),
611 channel_flags: v[318],
612 shutdown_scriptpubkey: shutdown_scriptpubkey
616 impl MsgEncodable for OpenChannel {
617 fn encode(&self) -> Vec<u8> {
618 let mut res = match &self.shutdown_scriptpubkey {
619 &Some(ref script) => Vec::with_capacity(319 + 2 + script.len()),
620 &None => Vec::with_capacity(319),
622 res.extend_from_slice(&serialize(&self.chain_hash).unwrap());
623 res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap());
624 res.extend_from_slice(&byte_utils::be64_to_array(self.funding_satoshis));
625 res.extend_from_slice(&byte_utils::be64_to_array(self.push_msat));
626 res.extend_from_slice(&byte_utils::be64_to_array(self.dust_limit_satoshis));
627 res.extend_from_slice(&byte_utils::be64_to_array(self.max_htlc_value_in_flight_msat));
628 res.extend_from_slice(&byte_utils::be64_to_array(self.channel_reserve_satoshis));
629 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_minimum_msat));
630 res.extend_from_slice(&byte_utils::be32_to_array(self.feerate_per_kw));
631 res.extend_from_slice(&byte_utils::be16_to_array(self.to_self_delay));
632 res.extend_from_slice(&byte_utils::be16_to_array(self.max_accepted_htlcs));
633 res.extend_from_slice(&self.funding_pubkey.serialize());
634 res.extend_from_slice(&self.revocation_basepoint.serialize());
635 res.extend_from_slice(&self.payment_basepoint.serialize());
636 res.extend_from_slice(&self.delayed_payment_basepoint.serialize());
637 res.extend_from_slice(&self.htlc_basepoint.serialize());
638 res.extend_from_slice(&self.first_per_commitment_point.serialize());
639 res.push(self.channel_flags);
640 if let &Some(ref script) = &self.shutdown_scriptpubkey {
641 res.extend_from_slice(&byte_utils::be16_to_array(script.len() as u16));
642 res.extend_from_slice(&script[..]);
648 impl MsgDecodable for AcceptChannel {
649 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
650 if v.len() < 32+4*8+4+2*2+6*33 {
651 return Err(DecodeError::WrongLength);
653 let ctx = Secp256k1::without_caps();
655 let mut shutdown_scriptpubkey = None;
657 let len = byte_utils::slice_to_be16(&v[270..272]) as usize;
658 if v.len() < 272+len {
659 return Err(DecodeError::WrongLength);
661 shutdown_scriptpubkey = Some(Script::from(v[272..272+len].to_vec()));
662 } else if v.len() != 32+4*8+4+2*2+6*33 { // Message cant have 1 extra byte
663 return Err(DecodeError::WrongLength);
667 temporary_channel_id: deserialize(&v[0..32]).unwrap(),
668 dust_limit_satoshis: byte_utils::slice_to_be64(&v[32..40]),
669 max_htlc_value_in_flight_msat: byte_utils::slice_to_be64(&v[40..48]),
670 channel_reserve_satoshis: byte_utils::slice_to_be64(&v[48..56]),
671 htlc_minimum_msat: byte_utils::slice_to_be64(&v[56..64]),
672 minimum_depth: byte_utils::slice_to_be32(&v[64..68]),
673 to_self_delay: byte_utils::slice_to_be16(&v[68..70]),
674 max_accepted_htlcs: byte_utils::slice_to_be16(&v[70..72]),
675 funding_pubkey: secp_pubkey!(&ctx, &v[72..105]),
676 revocation_basepoint: secp_pubkey!(&ctx, &v[105..138]),
677 payment_basepoint: secp_pubkey!(&ctx, &v[138..171]),
678 delayed_payment_basepoint: secp_pubkey!(&ctx, &v[171..204]),
679 htlc_basepoint: secp_pubkey!(&ctx, &v[204..237]),
680 first_per_commitment_point: secp_pubkey!(&ctx, &v[237..270]),
681 shutdown_scriptpubkey: shutdown_scriptpubkey
685 impl MsgEncodable for AcceptChannel {
686 fn encode(&self) -> Vec<u8> {
687 let mut res = match &self.shutdown_scriptpubkey {
688 &Some(ref script) => Vec::with_capacity(270 + 2 + script.len()),
689 &None => Vec::with_capacity(270),
691 res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap()[..]);
692 res.extend_from_slice(&byte_utils::be64_to_array(self.dust_limit_satoshis));
693 res.extend_from_slice(&byte_utils::be64_to_array(self.max_htlc_value_in_flight_msat));
694 res.extend_from_slice(&byte_utils::be64_to_array(self.channel_reserve_satoshis));
695 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_minimum_msat));
696 res.extend_from_slice(&byte_utils::be32_to_array(self.minimum_depth));
697 res.extend_from_slice(&byte_utils::be16_to_array(self.to_self_delay));
698 res.extend_from_slice(&byte_utils::be16_to_array(self.max_accepted_htlcs));
699 res.extend_from_slice(&self.funding_pubkey.serialize());
700 res.extend_from_slice(&self.revocation_basepoint.serialize());
701 res.extend_from_slice(&self.payment_basepoint.serialize());
702 res.extend_from_slice(&self.delayed_payment_basepoint.serialize());
703 res.extend_from_slice(&self.htlc_basepoint.serialize());
704 res.extend_from_slice(&self.first_per_commitment_point.serialize());
705 if let &Some(ref script) = &self.shutdown_scriptpubkey {
706 res.extend_from_slice(&byte_utils::be16_to_array(script.len() as u16));
707 res.extend_from_slice(&script[..]);
713 impl MsgDecodable for FundingCreated {
714 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
715 if v.len() < 32+32+2+64 {
716 return Err(DecodeError::WrongLength);
718 let ctx = Secp256k1::without_caps();
720 temporary_channel_id: deserialize(&v[0..32]).unwrap(),
721 funding_txid: deserialize(&v[32..64]).unwrap(),
722 funding_output_index: byte_utils::slice_to_be16(&v[64..66]),
723 signature: secp_signature!(&ctx, &v[66..130]),
727 impl MsgEncodable for FundingCreated {
728 fn encode(&self) -> Vec<u8> {
729 let mut res = Vec::with_capacity(32+32+2+64);
730 res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap()[..]);
731 res.extend_from_slice(&serialize(&self.funding_txid).unwrap()[..]);
732 res.extend_from_slice(&byte_utils::be16_to_array(self.funding_output_index));
733 let secp_ctx = Secp256k1::without_caps();
734 res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx));
739 impl MsgDecodable for FundingSigned {
740 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
742 return Err(DecodeError::WrongLength);
744 let ctx = Secp256k1::without_caps();
746 channel_id: deserialize(&v[0..32]).unwrap(),
747 signature: secp_signature!(&ctx, &v[32..96]),
751 impl MsgEncodable for FundingSigned {
752 fn encode(&self) -> Vec<u8> {
753 let mut res = Vec::with_capacity(32+64);
754 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
755 res.extend_from_slice(&self.signature.serialize_compact(&Secp256k1::without_caps()));
760 impl MsgDecodable for FundingLocked {
761 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
763 return Err(DecodeError::WrongLength);
765 let ctx = Secp256k1::without_caps();
767 channel_id: deserialize(&v[0..32]).unwrap(),
768 next_per_commitment_point: secp_pubkey!(&ctx, &v[32..65]),
772 impl MsgEncodable for FundingLocked {
773 fn encode(&self) -> Vec<u8> {
774 let mut res = Vec::with_capacity(32+33);
775 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
776 res.extend_from_slice(&self.next_per_commitment_point.serialize());
781 impl MsgDecodable for Shutdown {
782 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
783 if v.len() < 32 + 2 {
784 return Err(DecodeError::WrongLength);
786 let scriptlen = byte_utils::slice_to_be16(&v[32..34]) as usize;
787 if v.len() < 32 + 2 + scriptlen {
788 return Err(DecodeError::WrongLength);
791 channel_id: deserialize(&v[0..32]).unwrap(),
792 scriptpubkey: Script::from(v[34..34 + scriptlen].to_vec()),
796 impl MsgEncodable for Shutdown {
797 fn encode(&self) -> Vec<u8> {
798 let mut res = Vec::with_capacity(32 + 2 + self.scriptpubkey.len());
799 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
800 res.extend_from_slice(&byte_utils::be16_to_array(self.scriptpubkey.len() as u16));
801 res.extend_from_slice(&self.scriptpubkey[..]);
806 impl MsgDecodable for ClosingSigned {
807 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
808 if v.len() < 32 + 8 + 64 {
809 return Err(DecodeError::WrongLength);
811 let secp_ctx = Secp256k1::without_caps();
813 channel_id: deserialize(&v[0..32]).unwrap(),
814 fee_satoshis: byte_utils::slice_to_be64(&v[32..40]),
815 signature: secp_signature!(&secp_ctx, &v[40..104]),
819 impl MsgEncodable for ClosingSigned {
820 fn encode(&self) -> Vec<u8> {
821 let mut res = Vec::with_capacity(32+8+64);
822 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
823 res.extend_from_slice(&byte_utils::be64_to_array(self.fee_satoshis));
824 let secp_ctx = Secp256k1::without_caps();
825 res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx));
830 impl MsgDecodable for UpdateAddHTLC {
831 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
832 if v.len() < 32+8+8+32+4+1+33+20*65+32 {
833 return Err(DecodeError::WrongLength);
835 let mut payment_hash = [0; 32];
836 payment_hash.copy_from_slice(&v[48..80]);
838 channel_id: deserialize(&v[0..32]).unwrap(),
839 htlc_id: byte_utils::slice_to_be64(&v[32..40]),
840 amount_msat: byte_utils::slice_to_be64(&v[40..48]),
842 cltv_expiry: byte_utils::slice_to_be32(&v[80..84]),
843 onion_routing_packet: OnionPacket::decode(&v[84..84+1366])?,
847 impl MsgEncodable for UpdateAddHTLC {
848 fn encode(&self) -> Vec<u8> {
849 let mut res = Vec::with_capacity(32+8+8+32+4+1+1366);
850 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
851 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
852 res.extend_from_slice(&byte_utils::be64_to_array(self.amount_msat));
853 res.extend_from_slice(&self.payment_hash);
854 res.extend_from_slice(&byte_utils::be32_to_array(self.cltv_expiry));
855 res.extend_from_slice(&self.onion_routing_packet.encode()[..]);
860 impl MsgDecodable for UpdateFulfillHTLC {
861 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
862 if v.len() < 32+8+32 {
863 return Err(DecodeError::WrongLength);
865 let mut payment_preimage = [0; 32];
866 payment_preimage.copy_from_slice(&v[40..72]);
868 channel_id: deserialize(&v[0..32]).unwrap(),
869 htlc_id: byte_utils::slice_to_be64(&v[32..40]),
874 impl MsgEncodable for UpdateFulfillHTLC {
875 fn encode(&self) -> Vec<u8> {
876 let mut res = Vec::with_capacity(32+8+32);
877 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
878 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
879 res.extend_from_slice(&self.payment_preimage);
884 impl MsgDecodable for UpdateFailHTLC {
885 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
887 return Err(DecodeError::WrongLength);
890 channel_id: deserialize(&v[0..32]).unwrap(),
891 htlc_id: byte_utils::slice_to_be64(&v[32..40]),
892 reason: OnionErrorPacket::decode(&v[40..])?,
896 impl MsgEncodable for UpdateFailHTLC {
897 fn encode(&self) -> Vec<u8> {
898 let reason = self.reason.encode();
899 let mut res = Vec::with_capacity(32+8+reason.len());
900 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
901 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
902 res.extend_from_slice(&reason[..]);
907 impl MsgDecodable for UpdateFailMalformedHTLC {
908 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
909 if v.len() < 32+8+32+2 {
910 return Err(DecodeError::WrongLength);
912 let mut sha256_of_onion = [0; 32];
913 sha256_of_onion.copy_from_slice(&v[40..72]);
915 channel_id: deserialize(&v[0..32]).unwrap(),
916 htlc_id: byte_utils::slice_to_be64(&v[32..40]),
918 failure_code: byte_utils::slice_to_be16(&v[72..74]),
922 impl MsgEncodable for UpdateFailMalformedHTLC {
923 fn encode(&self) -> Vec<u8> {
924 let mut res = Vec::with_capacity(32+8+32+2);
925 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
926 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
927 res.extend_from_slice(&self.sha256_of_onion);
928 res.extend_from_slice(&byte_utils::be16_to_array(self.failure_code));
933 impl MsgDecodable for CommitmentSigned {
934 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
935 if v.len() < 32+64+2 {
936 return Err(DecodeError::WrongLength);
938 let htlcs = byte_utils::slice_to_be16(&v[96..98]) as usize;
939 if v.len() < 32+64+2+htlcs*64 {
940 return Err(DecodeError::WrongLength);
942 let mut htlc_signatures = Vec::with_capacity(htlcs);
943 let secp_ctx = Secp256k1::without_caps();
945 htlc_signatures.push(secp_signature!(&secp_ctx, &v[98+i*64..98+(i+1)*64]));
948 channel_id: deserialize(&v[0..32]).unwrap(),
949 signature: secp_signature!(&secp_ctx, &v[32..96]),
954 impl MsgEncodable for CommitmentSigned {
955 fn encode(&self) -> Vec<u8> {
956 let mut res = Vec::with_capacity(32+64+2+self.htlc_signatures.len()*64);
957 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
958 let secp_ctx = Secp256k1::without_caps();
959 res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx));
960 res.extend_from_slice(&byte_utils::be16_to_array(self.htlc_signatures.len() as u16));
961 for i in 0..self.htlc_signatures.len() {
962 res.extend_from_slice(&self.htlc_signatures[i].serialize_compact(&secp_ctx));
968 impl MsgDecodable for RevokeAndACK {
969 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
970 if v.len() < 32+32+33 {
971 return Err(DecodeError::WrongLength);
973 let mut per_commitment_secret = [0; 32];
974 per_commitment_secret.copy_from_slice(&v[32..64]);
975 let secp_ctx = Secp256k1::without_caps();
977 channel_id: deserialize(&v[0..32]).unwrap(),
978 per_commitment_secret,
979 next_per_commitment_point: secp_pubkey!(&secp_ctx, &v[64..97]),
983 impl MsgEncodable for RevokeAndACK {
984 fn encode(&self) -> Vec<u8> {
985 let mut res = Vec::with_capacity(32+32+33);
986 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
987 res.extend_from_slice(&self.per_commitment_secret);
988 res.extend_from_slice(&self.next_per_commitment_point.serialize());
993 impl MsgDecodable for UpdateFee {
994 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
996 return Err(DecodeError::WrongLength);
999 channel_id: deserialize(&v[0..32]).unwrap(),
1000 feerate_per_kw: byte_utils::slice_to_be32(&v[32..36]),
1004 impl MsgEncodable for UpdateFee {
1005 fn encode(&self) -> Vec<u8> {
1006 let mut res = Vec::with_capacity(32+4);
1007 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
1008 res.extend_from_slice(&byte_utils::be32_to_array(self.feerate_per_kw));
1013 impl MsgDecodable for ChannelReestablish {
1014 fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
1018 impl MsgEncodable for ChannelReestablish {
1019 fn encode(&self) -> Vec<u8> {
1024 impl MsgDecodable for AnnouncementSignatures {
1025 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1026 if v.len() < 32+8+64*2 {
1027 return Err(DecodeError::WrongLength);
1029 let secp_ctx = Secp256k1::without_caps();
1031 channel_id: deserialize(&v[0..32]).unwrap(),
1032 short_channel_id: byte_utils::slice_to_be64(&v[32..40]),
1033 node_signature: secp_signature!(&secp_ctx, &v[40..104]),
1034 bitcoin_signature: secp_signature!(&secp_ctx, &v[104..168]),
1038 impl MsgEncodable for AnnouncementSignatures {
1039 fn encode(&self) -> Vec<u8> {
1040 let mut res = Vec::with_capacity(32+8+64*2);
1041 res.extend_from_slice(&serialize(&self.channel_id).unwrap());
1042 res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id));
1043 let secp_ctx = Secp256k1::without_caps();
1044 res.extend_from_slice(&self.node_signature.serialize_compact(&secp_ctx));
1045 res.extend_from_slice(&self.bitcoin_signature.serialize_compact(&secp_ctx));
1050 impl MsgDecodable for UnsignedNodeAnnouncement {
1051 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1052 let features = GlobalFeatures::decode(&v[..])?;
1053 if v.len() < features.encoded_len() + 4 + 33 + 3 + 32 + 2 {
1054 return Err(DecodeError::WrongLength);
1056 let start = features.encoded_len();
1058 let mut rgb = [0; 3];
1059 rgb.copy_from_slice(&v[start + 37..start + 40]);
1061 let mut alias = [0; 32];
1062 alias.copy_from_slice(&v[start + 40..start + 72]);
1064 let addrlen = byte_utils::slice_to_be16(&v[start + 72..start + 74]) as usize;
1065 if v.len() < start + 74 + addrlen {
1066 return Err(DecodeError::WrongLength);
1069 let mut addresses = Vec::with_capacity(4);
1070 let mut read_pos = start + 74;
1072 if v.len() <= read_pos { break; }
1074 0 => { read_pos += 1; },
1076 if v.len() < read_pos + 1 + 6 {
1077 return Err(DecodeError::WrongLength);
1079 if addresses.len() > 0 {
1080 return Err(DecodeError::ExtraAddressesPerType);
1082 let mut addr = [0; 4];
1083 addr.copy_from_slice(&v[read_pos + 1..read_pos + 5]);
1084 addresses.push(NetAddress::IPv4 {
1086 port: byte_utils::slice_to_be16(&v[read_pos + 5..read_pos + 7]),
1091 if v.len() < read_pos + 1 + 18 {
1092 return Err(DecodeError::WrongLength);
1094 if addresses.len() > 1 || (addresses.len() == 1 && addresses[0].get_id() != 1) {
1095 return Err(DecodeError::ExtraAddressesPerType);
1097 let mut addr = [0; 16];
1098 addr.copy_from_slice(&v[read_pos + 1..read_pos + 17]);
1099 addresses.push(NetAddress::IPv6 {
1101 port: byte_utils::slice_to_be16(&v[read_pos + 17..read_pos + 19]),
1106 if v.len() < read_pos + 1 + 12 {
1107 return Err(DecodeError::WrongLength);
1109 if addresses.len() > 2 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 2) {
1110 return Err(DecodeError::ExtraAddressesPerType);
1112 let mut addr = [0; 10];
1113 addr.copy_from_slice(&v[read_pos + 1..read_pos + 11]);
1114 addresses.push(NetAddress::OnionV2 {
1116 port: byte_utils::slice_to_be16(&v[read_pos + 11..read_pos + 13]),
1121 if v.len() < read_pos + 1 + 37 {
1122 return Err(DecodeError::WrongLength);
1124 if addresses.len() > 3 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 3) {
1125 return Err(DecodeError::ExtraAddressesPerType);
1127 let mut ed25519_pubkey = [0; 32];
1128 ed25519_pubkey.copy_from_slice(&v[read_pos + 1..read_pos + 33]);
1129 addresses.push(NetAddress::OnionV3 {
1131 checksum: byte_utils::slice_to_be16(&v[read_pos + 33..read_pos + 35]),
1132 version: v[read_pos + 35],
1133 port: byte_utils::slice_to_be16(&v[read_pos + 36..read_pos + 38]),
1137 _ => { break; } // We've read all we can, we dont understand anything higher (and they're sorted)
1141 let secp_ctx = Secp256k1::without_caps();
1144 timestamp: byte_utils::slice_to_be32(&v[start..start + 4]),
1145 node_id: secp_pubkey!(&secp_ctx, &v[start + 4..start + 37]),
1152 impl MsgEncodable for UnsignedNodeAnnouncement {
1153 fn encode(&self) -> Vec<u8> {
1154 let features = self.features.encode();
1155 let mut res = Vec::with_capacity(74 + features.len() + self.addresses.len());
1156 res.extend_from_slice(&features[..]);
1157 res.extend_from_slice(&byte_utils::be32_to_array(self.timestamp));
1158 res.extend_from_slice(&self.node_id.serialize());
1159 res.extend_from_slice(&self.rgb);
1160 res.extend_from_slice(&self.alias);
1161 let mut addr_slice = Vec::with_capacity(self.addresses.len() * 18);
1162 let mut addrs_to_encode = self.addresses.clone();
1163 addrs_to_encode.sort_unstable_by(|a, b| { a.get_id().cmp(&b.get_id()) });
1164 addrs_to_encode.dedup_by(|a, b| { a.get_id() == b.get_id() });
1165 for addr in addrs_to_encode.iter() {
1167 &NetAddress::IPv4{addr, port} => {
1169 addr_slice.extend_from_slice(&addr);
1170 addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
1172 &NetAddress::IPv6{addr, port} => {
1174 addr_slice.extend_from_slice(&addr);
1175 addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
1177 &NetAddress::OnionV2{addr, port} => {
1179 addr_slice.extend_from_slice(&addr);
1180 addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
1182 &NetAddress::OnionV3{ed25519_pubkey, checksum, version, port} => {
1184 addr_slice.extend_from_slice(&ed25519_pubkey);
1185 addr_slice.extend_from_slice(&byte_utils::be16_to_array(checksum));
1186 addr_slice.push(version);
1187 addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
1191 res.extend_from_slice(&byte_utils::be16_to_array(addr_slice.len() as u16));
1192 res.extend_from_slice(&addr_slice[..]);
1197 impl MsgDecodable for NodeAnnouncement {
1198 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1200 return Err(DecodeError::WrongLength);
1202 let secp_ctx = Secp256k1::without_caps();
1204 signature: secp_signature!(&secp_ctx, &v[0..64]),
1205 contents: UnsignedNodeAnnouncement::decode(&v[64..])?,
1209 impl MsgEncodable for NodeAnnouncement {
1210 fn encode(&self) -> Vec<u8> {
1211 let contents = self.contents.encode();
1212 let mut res = Vec::with_capacity(64 + contents.len());
1213 let secp_ctx = Secp256k1::without_caps();
1214 res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx));
1215 res.extend_from_slice(&contents);
1220 impl MsgDecodable for UnsignedChannelAnnouncement {
1221 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1222 let features = GlobalFeatures::decode(&v[..])?;
1223 if v.len() < features.encoded_len() + 32 + 8 + 33*4 {
1224 return Err(DecodeError::WrongLength);
1226 let start = features.encoded_len();
1227 let secp_ctx = Secp256k1::without_caps();
1230 chain_hash: deserialize(&v[start..start + 32]).unwrap(),
1231 short_channel_id: byte_utils::slice_to_be64(&v[start + 32..start + 40]),
1232 node_id_1: secp_pubkey!(&secp_ctx, &v[start + 40..start + 73]),
1233 node_id_2: secp_pubkey!(&secp_ctx, &v[start + 73..start + 106]),
1234 bitcoin_key_1: secp_pubkey!(&secp_ctx, &v[start + 106..start + 139]),
1235 bitcoin_key_2: secp_pubkey!(&secp_ctx, &v[start + 139..start + 172]),
1239 impl MsgEncodable for UnsignedChannelAnnouncement {
1240 fn encode(&self) -> Vec<u8> {
1241 let features = self.features.encode();
1242 let mut res = Vec::with_capacity(172 + features.len());
1243 res.extend_from_slice(&features[..]);
1244 res.extend_from_slice(&self.chain_hash[..]);
1245 res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id));
1246 res.extend_from_slice(&self.node_id_1.serialize());
1247 res.extend_from_slice(&self.node_id_2.serialize());
1248 res.extend_from_slice(&self.bitcoin_key_1.serialize());
1249 res.extend_from_slice(&self.bitcoin_key_2.serialize());
1254 impl MsgDecodable for ChannelAnnouncement {
1255 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1257 return Err(DecodeError::WrongLength);
1259 let secp_ctx = Secp256k1::without_caps();
1261 node_signature_1: secp_signature!(&secp_ctx, &v[0..64]),
1262 node_signature_2: secp_signature!(&secp_ctx, &v[64..128]),
1263 bitcoin_signature_1: secp_signature!(&secp_ctx, &v[128..192]),
1264 bitcoin_signature_2: secp_signature!(&secp_ctx, &v[192..256]),
1265 contents: UnsignedChannelAnnouncement::decode(&v[256..])?,
1269 impl MsgEncodable for ChannelAnnouncement {
1270 fn encode(&self) -> Vec<u8> {
1271 let secp_ctx = Secp256k1::without_caps();
1272 let contents = self.contents.encode();
1273 let mut res = Vec::with_capacity(64 + contents.len());
1274 res.extend_from_slice(&self.node_signature_1.serialize_compact(&secp_ctx));
1275 res.extend_from_slice(&self.node_signature_2.serialize_compact(&secp_ctx));
1276 res.extend_from_slice(&self.bitcoin_signature_1.serialize_compact(&secp_ctx));
1277 res.extend_from_slice(&self.bitcoin_signature_2.serialize_compact(&secp_ctx));
1278 res.extend_from_slice(&contents);
1283 impl MsgDecodable for UnsignedChannelUpdate {
1284 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1285 if v.len() < 32+8+4+2+2+8+4+4 {
1286 return Err(DecodeError::WrongLength);
1289 chain_hash: deserialize(&v[0..32]).unwrap(),
1290 short_channel_id: byte_utils::slice_to_be64(&v[32..40]),
1291 timestamp: byte_utils::slice_to_be32(&v[40..44]),
1292 flags: byte_utils::slice_to_be16(&v[44..46]),
1293 cltv_expiry_delta: byte_utils::slice_to_be16(&v[46..48]),
1294 htlc_minimum_msat: byte_utils::slice_to_be64(&v[48..56]),
1295 fee_base_msat: byte_utils::slice_to_be32(&v[56..60]),
1296 fee_proportional_millionths: byte_utils::slice_to_be32(&v[60..64]),
1300 impl MsgEncodable for UnsignedChannelUpdate {
1301 fn encode(&self) -> Vec<u8> {
1302 let mut res = Vec::with_capacity(64);
1303 res.extend_from_slice(&self.chain_hash[..]);
1304 res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id));
1305 res.extend_from_slice(&byte_utils::be32_to_array(self.timestamp));
1306 res.extend_from_slice(&byte_utils::be16_to_array(self.flags));
1307 res.extend_from_slice(&byte_utils::be16_to_array(self.cltv_expiry_delta));
1308 res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_minimum_msat));
1309 res.extend_from_slice(&byte_utils::be32_to_array(self.fee_base_msat));
1310 res.extend_from_slice(&byte_utils::be32_to_array(self.fee_proportional_millionths));
1315 impl MsgDecodable for ChannelUpdate {
1316 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1318 return Err(DecodeError::WrongLength);
1320 let secp_ctx = Secp256k1::without_caps();
1322 signature: secp_signature!(&secp_ctx, &v[0..64]),
1323 contents: UnsignedChannelUpdate::decode(&v[64..])?,
1327 impl MsgEncodable for ChannelUpdate {
1328 fn encode(&self) -> Vec<u8> {
1329 let mut res = Vec::with_capacity(128);
1330 res.extend_from_slice(&self.signature.serialize_compact(&Secp256k1::without_caps())[..]);
1331 res.extend_from_slice(&self.contents.encode()[..]);
1336 impl MsgDecodable for OnionRealm0HopData {
1337 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1339 return Err(DecodeError::WrongLength);
1341 Ok(OnionRealm0HopData {
1342 short_channel_id: byte_utils::slice_to_be64(&v[0..8]),
1343 amt_to_forward: byte_utils::slice_to_be64(&v[8..16]),
1344 outgoing_cltv_value: byte_utils::slice_to_be32(&v[16..20]),
1348 impl MsgEncodable for OnionRealm0HopData {
1349 fn encode(&self) -> Vec<u8> {
1350 let mut res = Vec::with_capacity(32);
1351 res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id));
1352 res.extend_from_slice(&byte_utils::be64_to_array(self.amt_to_forward));
1353 res.extend_from_slice(&byte_utils::be32_to_array(self.outgoing_cltv_value));
1359 impl MsgDecodable for OnionHopData {
1360 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1362 return Err(DecodeError::WrongLength);
1366 return Err(DecodeError::UnknownRealmByte);
1368 let mut hmac = [0; 32];
1369 hmac[..].copy_from_slice(&v[33..65]);
1372 data: OnionRealm0HopData::decode(&v[1..33])?,
1377 impl MsgEncodable for OnionHopData {
1378 fn encode(&self) -> Vec<u8> {
1379 let mut res = Vec::with_capacity(65);
1380 res.push(self.realm);
1381 res.extend_from_slice(&self.data.encode()[..]);
1382 res.extend_from_slice(&self.hmac);
1387 impl MsgDecodable for OnionPacket {
1388 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1389 if v.len() < 1+33+20*65+32 {
1390 return Err(DecodeError::WrongLength);
1392 let mut hop_data = [0; 20*65];
1393 hop_data.copy_from_slice(&v[34..1334]);
1394 let mut hmac = [0; 32];
1395 hmac.copy_from_slice(&v[1334..1366]);
1396 let secp_ctx = Secp256k1::without_caps();
1399 public_key: secp_pubkey!(&secp_ctx, &v[1..34]),
1405 impl MsgEncodable for OnionPacket {
1406 fn encode(&self) -> Vec<u8> {
1407 let mut res = Vec::with_capacity(1 + 33 + 20*65 + 32);
1408 res.push(self.version);
1409 res.extend_from_slice(&self.public_key.serialize());
1410 res.extend_from_slice(&self.hop_data);
1411 res.extend_from_slice(&self.hmac);
1416 impl MsgDecodable for DecodedOnionErrorPacket {
1417 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1418 if v.len() < 32 + 4 {
1419 return Err(DecodeError::WrongLength);
1421 let failuremsg_len = byte_utils::slice_to_be16(&v[32..34]) as usize;
1422 if v.len() < 32 + 4 + failuremsg_len {
1423 return Err(DecodeError::WrongLength);
1425 let padding_len = byte_utils::slice_to_be16(&v[34 + failuremsg_len..]) as usize;
1426 if v.len() < 32 + 4 + failuremsg_len + padding_len {
1427 return Err(DecodeError::WrongLength);
1430 let mut hmac = [0; 32];
1431 hmac.copy_from_slice(&v[0..32]);
1434 failuremsg: v[34..34 + failuremsg_len].to_vec(),
1435 pad: v[36 + failuremsg_len..36 + failuremsg_len + padding_len].to_vec(),
1439 impl MsgEncodable for DecodedOnionErrorPacket {
1440 fn encode(&self) -> Vec<u8> {
1441 let mut res = Vec::with_capacity(32 + 4 + self.failuremsg.len() + self.pad.len());
1442 res.extend_from_slice(&self.hmac);
1443 res.extend_from_slice(&[((self.failuremsg.len() >> 8) & 0xff) as u8, (self.failuremsg.len() & 0xff) as u8]);
1444 res.extend_from_slice(&self.failuremsg);
1445 res.extend_from_slice(&[((self.pad.len() >> 8) & 0xff) as u8, (self.pad.len() & 0xff) as u8]);
1446 res.extend_from_slice(&self.pad);
1451 impl MsgDecodable for OnionErrorPacket {
1452 fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1454 return Err(DecodeError::WrongLength);
1456 let len = byte_utils::slice_to_be16(&v[0..2]) as usize;
1457 if v.len() < 2 + len {
1458 return Err(DecodeError::WrongLength);
1461 data: v[2..len+2].to_vec(),
1465 impl MsgEncodable for OnionErrorPacket {
1466 fn encode(&self) -> Vec<u8> {
1467 let mut res = Vec::with_capacity(2 + self.data.len());
1468 res.extend_from_slice(&byte_utils::be16_to_array(self.data.len() as u16));
1469 res.extend_from_slice(&self.data);