// This file is Copyright its original authors, visible in version control // history. // // This file is licensed under the Apache License, Version 2.0 or the MIT license // , at your option. // You may not use this file except in accordance with one or both of these // licenses. //! Wire messages, traits representing wire message handlers, and a few error types live here. //! //! For a normal node you probably don't need to use anything here, however, if you wish to split a //! node into an internet-facing route/message socket handling daemon and a separate daemon (or //! server entirely) which handles only channel-related messages you may wish to implement //! ChannelMessageHandler yourself and use it to re-serialize messages and pass them across //! daemons/servers. //! //! Note that if you go with such an architecture (instead of passing raw socket events to a //! non-internet-facing system) you trust the frontend internet-facing system to not lie about the //! source node_id of the message, however this does allow you to significantly reduce bandwidth //! between the systems as routing messages can represent a significant chunk of bandwidth usage //! (especially for non-channel-publicly-announcing nodes). As an alternate design which avoids //! this issue, if you have sufficient bidirectional bandwidth between your systems, you may send //! raw socket events into your non-internet-facing system and then send routing events back to //! track the network on the less-secure system. use bitcoin::secp256k1::key::PublicKey; use bitcoin::secp256k1::Signature; use bitcoin::secp256k1; use bitcoin::blockdata::script::Script; use bitcoin::hash_types::{Txid, BlockHash}; use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures}; use std::{cmp, fmt}; use std::fmt::Debug; use std::io::Read; use util::events::MessageSendEventsProvider; use util::ser::{Readable, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedVarInt}; use ln::{PaymentPreimage, PaymentHash, PaymentSecret}; /// 21 million * 10^8 * 1000 pub(crate) const MAX_VALUE_MSAT: u64 = 21_000_000_0000_0000_000; /// An error in decoding a message or struct. #[derive(Clone, Debug)] pub enum DecodeError { /// A version byte specified something we don't know how to handle. /// Includes unknown realm byte in an OnionHopData packet UnknownVersion, /// Unknown feature mandating we fail to parse message (eg TLV with an even, unknown type) UnknownRequiredFeature, /// Value was invalid, eg a byte which was supposed to be a bool was something other than a 0 /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, TLV was /// syntactically incorrect, etc InvalidValue, /// Buffer too short ShortRead, /// A length descriptor in the packet didn't describe the later data correctly BadLengthDescriptor, /// Error from std::io Io(/// (C-not exported) as ErrorKind doesn't have a reasonable mapping ::std::io::ErrorKind), /// The message included zlib-compressed values, which we don't support. UnsupportedCompression, } /// An init message to be sent or received from a peer #[derive(Clone, Debug, PartialEq)] pub struct Init { /// The relevant features which the sender supports pub features: InitFeatures, } /// An error message to be sent or received from a peer #[derive(Clone, Debug, PartialEq)] pub struct ErrorMessage { /// The channel ID involved in the error pub channel_id: [u8; 32], /// A possibly human-readable error description. /// The string should be sanitized before it is used (e.g. emitted to logs /// or printed to stdout). Otherwise, a well crafted error message may trigger a security /// vulnerability in the terminal emulator or the logging subsystem. pub data: String, } /// A ping message to be sent or received from a peer #[derive(Clone, Debug, PartialEq)] pub struct Ping { /// The desired response length pub ponglen: u16, /// The ping packet size. /// This field is not sent on the wire. byteslen zeros are sent. pub byteslen: u16, } /// A pong message to be sent or received from a peer #[derive(Clone, Debug, PartialEq)] pub struct Pong { /// The pong packet size. /// This field is not sent on the wire. byteslen zeros are sent. pub byteslen: u16, } /// An open_channel message to be sent or received from a peer #[derive(Clone, Debug, PartialEq)] pub struct OpenChannel { /// The genesis hash of the blockchain where the channel is to be opened pub chain_hash: BlockHash, /// A temporary channel ID, until the funding outpoint is announced pub temporary_channel_id: [u8; 32], /// The channel value pub funding_satoshis: u64, /// The amount to push to the counterparty as part of the open, in milli-satoshi pub push_msat: u64, /// The threshold below which outputs on transactions broadcast by sender will be omitted pub dust_limit_satoshis: u64, /// The maximum inbound HTLC value in flight towards sender, in milli-satoshi pub max_htlc_value_in_flight_msat: u64, /// The minimum value unencumbered by HTLCs for the counterparty to keep in the channel pub channel_reserve_satoshis: u64, /// The minimum HTLC size incoming to sender, in milli-satoshi pub htlc_minimum_msat: u64, /// The feerate per 1000-weight of sender generated transactions, until updated by update_fee pub feerate_per_kw: u32, /// The number of blocks which the counterparty will have to wait to claim on-chain funds if they broadcast a commitment transaction pub to_self_delay: u16, /// The maximum number of inbound HTLCs towards sender pub max_accepted_htlcs: u16, /// The sender's key controlling the funding transaction pub funding_pubkey: PublicKey, /// Used to derive a revocation key for transactions broadcast by counterparty pub revocation_basepoint: PublicKey, /// A payment key to sender for transactions broadcast by counterparty pub payment_point: PublicKey, /// Used to derive a payment key to sender for transactions broadcast by sender pub delayed_payment_basepoint: PublicKey, /// Used to derive an HTLC payment key to sender pub htlc_basepoint: PublicKey, /// The first to-be-broadcast-by-sender transaction's per commitment point pub first_per_commitment_point: PublicKey, /// Channel flags pub channel_flags: u8, /// Optionally, a request to pre-set the to-sender output's scriptPubkey for when we collaboratively close pub shutdown_scriptpubkey: OptionalField