use secp256k1::key::PublicKey; use secp256k1::{Secp256k1, Signature}; use bitcoin::util::uint::Uint256; use bitcoin::util::hash::Sha256dHash; use bitcoin::network::serialize::{deserialize,serialize}; use bitcoin::blockdata::script::Script; use std::error::Error; use std::fmt; use std::result::Result; use util::{byte_utils, internal_traits, events}; pub trait MsgEncodable { fn encode(&self) -> Vec; #[inline] fn encoded_len(&self) -> usize { self.encode().len() } } #[derive(Debug)] pub enum DecodeError { /// Unknown realm byte in an OnionHopData packet UnknownRealmByte, /// Failed to decode a public key (ie it's invalid) BadPublicKey, /// Failed to decode a signature (ie it's invalid) BadSignature, /// Buffer not of right length (either too short or too long) WrongLength, /// node_announcement included more than one address of a given type! ExtraAddressesPerType, } pub trait MsgDecodable: Sized { fn decode(v: &[u8]) -> Result; } /// Tracks localfeatures which are only in init messages #[derive(Clone, PartialEq)] pub struct LocalFeatures { flags: Vec, } impl LocalFeatures { pub fn new() -> LocalFeatures { LocalFeatures { flags: Vec::new(), } } pub fn supports_data_loss_protect(&self) -> bool { self.flags.len() > 0 && (self.flags[0] & 3) != 0 } pub fn requires_data_loss_protect(&self) -> bool { self.flags.len() > 0 && (self.flags[0] & 1) != 0 } pub fn supports_initial_routing_sync(&self) -> bool { self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0 } pub fn supports_upfront_shutdown_script(&self) -> bool { self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0 } pub fn requires_upfront_shutdown_script(&self) -> bool { self.flags.len() > 0 && (self.flags[0] & (1 << 4)) != 0 } pub fn requires_unknown_bits(&self) -> bool { for (idx, &byte) in self.flags.iter().enumerate() { if idx != 0 && (byte & 0x55) != 0 { return true; } else if idx == 0 && (byte & 0x14) != 0 { return true; } } return false; } pub fn supports_unknown_bits(&self) -> bool { for (idx, &byte) in self.flags.iter().enumerate() { if idx != 0 && byte != 0 { return true; } else if idx == 0 && (byte & 0xc4) != 0 { return true; } } return false; } } /// Tracks globalfeatures which are in init messages and routing announcements #[derive(Clone, PartialEq)] pub struct GlobalFeatures { flags: Vec, } impl GlobalFeatures { pub fn new() -> GlobalFeatures { GlobalFeatures { flags: Vec::new(), } } pub fn requires_unknown_bits(&self) -> bool { for &byte in self.flags.iter() { if (byte & 0x55) != 0 { return true; } } return false; } pub fn supports_unknown_bits(&self) -> bool { for &byte in self.flags.iter() { if byte != 0 { return true; } } return false; } } pub struct Init { pub global_features: GlobalFeatures, pub local_features: LocalFeatures, } pub struct OpenChannel { pub chain_hash: Sha256dHash, pub temporary_channel_id: Uint256, pub funding_satoshis: u64, pub push_msat: u64, pub dust_limit_satoshis: u64, pub max_htlc_value_in_flight_msat: u64, pub channel_reserve_satoshis: u64, pub htlc_minimum_msat: u64, pub feerate_per_kw: u32, pub to_self_delay: u16, pub max_accepted_htlcs: u16, pub funding_pubkey: PublicKey, pub revocation_basepoint: PublicKey, pub payment_basepoint: PublicKey, pub delayed_payment_basepoint: PublicKey, pub htlc_basepoint: PublicKey, pub first_per_commitment_point: PublicKey, pub channel_flags: u8, pub shutdown_scriptpubkey: Option