1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! Wire messages, traits representing wire message handlers, and a few error types live here.
12 //! For a normal node you probably don't need to use anything here, however, if you wish to split a
13 //! node into an internet-facing route/message socket handling daemon and a separate daemon (or
14 //! server entirely) which handles only channel-related messages you may wish to implement
15 //! [`ChannelMessageHandler`] yourself and use it to re-serialize messages and pass them across
18 //! Note that if you go with such an architecture (instead of passing raw socket events to a
19 //! non-internet-facing system) you trust the frontend internet-facing system to not lie about the
20 //! source `node_id` of the message, however this does allow you to significantly reduce bandwidth
21 //! between the systems as routing messages can represent a significant chunk of bandwidth usage
22 //! (especially for non-channel-publicly-announcing nodes). As an alternate design which avoids
23 //! this issue, if you have sufficient bidirectional bandwidth between your systems, you may send
24 //! raw socket events into your non-internet-facing system and then send routing events back to
25 //! track the network on the less-secure system.
27 use bitcoin::blockdata::constants::ChainHash;
28 use bitcoin::secp256k1::PublicKey;
29 use bitcoin::secp256k1::ecdsa::Signature;
30 use bitcoin::{secp256k1, Witness};
31 use bitcoin::blockdata::script::ScriptBuf;
32 use bitcoin::hash_types::Txid;
34 use crate::blinded_path::payment::{BlindedPaymentTlvs, ForwardTlvs, ReceiveTlvs};
35 use crate::ln::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret};
36 use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
37 use crate::ln::onion_utils;
38 use crate::onion_message;
39 use crate::sign::{NodeSigner, Recipient};
41 use crate::prelude::*;
42 #[cfg(feature = "std")]
43 use core::convert::TryFrom;
47 #[cfg(feature = "std")]
48 use core::str::FromStr;
49 #[cfg(feature = "std")]
50 use std::net::SocketAddr;
51 use core::fmt::Display;
52 use crate::io::{self, Cursor, Read};
53 use crate::io_extras::read_to_end;
55 use crate::events::{EventsProvider, MessageSendEventsProvider};
56 use crate::crypto::streams::ChaChaPolyReadAdapter;
57 use crate::util::logger;
58 use crate::util::ser::{LengthReadable, LengthReadableArgs, Readable, ReadableArgs, Writeable, Writer, WithoutLength, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname, TransactionU16LenLimited, BigSize};
59 use crate::util::base32;
61 use crate::routing::gossip::{NodeAlias, NodeId};
63 /// 21 million * 10^8 * 1000
64 pub(crate) const MAX_VALUE_MSAT: u64 = 21_000_000_0000_0000_000;
67 /// A partial signature that also contains the Musig2 nonce its signer used
68 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
69 pub struct PartialSignatureWithNonce(pub musig2::types::PartialSignature, pub musig2::types::PublicNonce);
71 /// An error in decoding a message or struct.
72 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
73 pub enum DecodeError {
74 /// A version byte specified something we don't know how to handle.
76 /// Includes unknown realm byte in an onion hop data packet.
78 /// Unknown feature mandating we fail to parse message (e.g., TLV with an even, unknown type)
79 UnknownRequiredFeature,
80 /// Value was invalid.
82 /// For example, a byte which was supposed to be a bool was something other than a 0
83 /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, TLV was
84 /// syntactically incorrect, etc.
86 /// The buffer to be read was too short.
88 /// A length descriptor in the packet didn't describe the later data correctly.
90 /// Error from [`std::io`].
92 /// The message included zlib-compressed values, which we don't support.
93 UnsupportedCompression,
96 /// An [`init`] message to be sent to or received from a peer.
98 /// [`init`]: https://github.com/lightning/bolts/blob/master/01-messaging.md#the-init-message
99 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
101 /// The relevant features which the sender supports.
102 pub features: InitFeatures,
103 /// Indicates chains the sender is interested in.
105 /// If there are no common chains, the connection will be closed.
106 pub networks: Option<Vec<ChainHash>>,
107 /// The receipient's network address.
109 /// This adds the option to report a remote IP address back to a connecting peer using the init
110 /// message. A node can decide to use that information to discover a potential update to its
111 /// public IPv4 address (NAT) and use that for a [`NodeAnnouncement`] update message containing
113 pub remote_network_address: Option<SocketAddress>,
116 /// An [`error`] message to be sent to or received from a peer.
118 /// [`error`]: https://github.com/lightning/bolts/blob/master/01-messaging.md#the-error-and-warning-messages
119 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
120 pub struct ErrorMessage {
121 /// The channel ID involved in the error.
123 /// All-0s indicates a general error unrelated to a specific channel, after which all channels
124 /// with the sending peer should be closed.
125 pub channel_id: ChannelId,
126 /// A possibly human-readable error description.
128 /// The string should be sanitized before it is used (e.g., emitted to logs or printed to
129 /// `stdout`). Otherwise, a well crafted error message may trigger a security vulnerability in
130 /// the terminal emulator or the logging subsystem.
134 /// A [`warning`] message to be sent to or received from a peer.
136 /// [`warning`]: https://github.com/lightning/bolts/blob/master/01-messaging.md#the-error-and-warning-messages
137 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
138 pub struct WarningMessage {
139 /// The channel ID involved in the warning.
141 /// All-0s indicates a warning unrelated to a specific channel.
142 pub channel_id: ChannelId,
143 /// A possibly human-readable warning description.
145 /// The string should be sanitized before it is used (e.g. emitted to logs or printed to
146 /// stdout). Otherwise, a well crafted error message may trigger a security vulnerability in
147 /// the terminal emulator or the logging subsystem.
151 /// A [`ping`] message to be sent to or received from a peer.
153 /// [`ping`]: https://github.com/lightning/bolts/blob/master/01-messaging.md#the-ping-and-pong-messages
154 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
156 /// The desired response length.
158 /// The ping packet size.
160 /// This field is not sent on the wire. byteslen zeros are sent.
164 /// A [`pong`] message to be sent to or received from a peer.
166 /// [`pong`]: https://github.com/lightning/bolts/blob/master/01-messaging.md#the-ping-and-pong-messages
167 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
169 /// The pong packet size.
171 /// This field is not sent on the wire. byteslen zeros are sent.
175 /// Contains fields that are both common to [`open_channel`] and `open_channel2` messages.
177 /// [`open_channel`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#the-open_channel-message
178 // TODO(dual_funding): Add spec link for `open_channel2`.
179 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
180 pub struct CommonOpenChannelFields {
181 /// The genesis hash of the blockchain where the channel is to be opened
182 pub chain_hash: ChainHash,
183 /// A temporary channel ID
184 /// For V2 channels: derived using a zeroed out value for the channel acceptor's revocation basepoint
185 /// For V1 channels: a temporary channel ID, until the funding outpoint is announced
186 pub temporary_channel_id: ChannelId,
187 /// For V1 channels: The channel value
188 /// For V2 channels: Part of the channel value contributed by the channel initiator
189 pub funding_satoshis: u64,
190 /// The threshold below which outputs on transactions broadcast by the channel initiator will be
192 pub dust_limit_satoshis: u64,
193 /// The maximum inbound HTLC value in flight towards channel initiator, in milli-satoshi
194 pub max_htlc_value_in_flight_msat: u64,
195 /// The minimum HTLC size incoming to channel initiator, in milli-satoshi
196 pub htlc_minimum_msat: u64,
197 /// The feerate for the commitment transaction set by the channel initiator until updated by
199 pub commitment_feerate_sat_per_1000_weight: u32,
200 /// The number of blocks which the counterparty will have to wait to claim on-chain funds if they
201 /// broadcast a commitment transaction
202 pub to_self_delay: u16,
203 /// The maximum number of inbound HTLCs towards channel initiator
204 pub max_accepted_htlcs: u16,
205 /// The channel initiator's key controlling the funding transaction
206 pub funding_pubkey: PublicKey,
207 /// Used to derive a revocation key for transactions broadcast by counterparty
208 pub revocation_basepoint: PublicKey,
209 /// A payment key to channel initiator for transactions broadcast by counterparty
210 pub payment_basepoint: PublicKey,
211 /// Used to derive a payment key to channel initiator for transactions broadcast by channel
213 pub delayed_payment_basepoint: PublicKey,
214 /// Used to derive an HTLC payment key to channel initiator
215 pub htlc_basepoint: PublicKey,
216 /// The first to-be-broadcast-by-channel-initiator transaction's per commitment point
217 pub first_per_commitment_point: PublicKey,
218 /// The channel flags to be used
219 pub channel_flags: u8,
220 /// Optionally, a request to pre-set the to-channel-initiator output's scriptPubkey for when we
221 /// collaboratively close
222 pub shutdown_scriptpubkey: Option<ScriptBuf>,
223 /// The channel type that this channel will represent
225 /// If this is `None`, we derive the channel type from the intersection of our
226 /// feature bits with our counterparty's feature bits from the [`Init`] message.
227 pub channel_type: Option<ChannelTypeFeatures>,
230 /// An [`open_channel`] message to be sent to or received from a peer.
232 /// Used in V1 channel establishment
234 /// [`open_channel`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#the-open_channel-message
235 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
236 pub struct OpenChannel {
237 /// Common fields of `open_channel(2)`-like messages
238 pub common_fields: CommonOpenChannelFields,
239 /// The amount to push to the counterparty as part of the open, in milli-satoshi
241 /// The minimum value unencumbered by HTLCs for the counterparty to keep in the channel
242 pub channel_reserve_satoshis: u64,
245 /// An open_channel2 message to be sent by or received from the channel initiator.
247 /// Used in V2 channel establishment
249 // TODO(dual_funding): Add spec link for `open_channel2`.
250 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
251 pub struct OpenChannelV2 {
252 /// Common fields of `open_channel(2)`-like messages
253 pub common_fields: CommonOpenChannelFields,
254 /// The feerate for the funding transaction set by the channel initiator
255 pub funding_feerate_sat_per_1000_weight: u32,
256 /// The locktime for the funding transaction
258 /// The second to-be-broadcast-by-channel-initiator transaction's per commitment point
259 pub second_per_commitment_point: PublicKey,
260 /// Optionally, a requirement that only confirmed inputs can be added
261 pub require_confirmed_inputs: Option<()>,
264 /// An [`accept_channel`] message to be sent to or received from a peer.
266 /// Used in V1 channel establishment
268 /// [`accept_channel`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#the-accept_channel-message
269 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
270 pub struct AcceptChannel {
271 /// A temporary channel ID, until the funding outpoint is announced
272 pub temporary_channel_id: ChannelId,
273 /// The threshold below which outputs on transactions broadcast by sender will be omitted
274 pub dust_limit_satoshis: u64,
275 /// The maximum inbound HTLC value in flight towards sender, in milli-satoshi
276 pub max_htlc_value_in_flight_msat: u64,
277 /// The minimum value unencumbered by HTLCs for the counterparty to keep in the channel
278 pub channel_reserve_satoshis: u64,
279 /// The minimum HTLC size incoming to sender, in milli-satoshi
280 pub htlc_minimum_msat: u64,
281 /// Minimum depth of the funding transaction before the channel is considered open
282 pub minimum_depth: u32,
283 /// The number of blocks which the counterparty will have to wait to claim on-chain funds if they broadcast a commitment transaction
284 pub to_self_delay: u16,
285 /// The maximum number of inbound HTLCs towards sender
286 pub max_accepted_htlcs: u16,
287 /// The sender's key controlling the funding transaction
288 pub funding_pubkey: PublicKey,
289 /// Used to derive a revocation key for transactions broadcast by counterparty
290 pub revocation_basepoint: PublicKey,
291 /// A payment key to sender for transactions broadcast by counterparty
292 pub payment_point: PublicKey,
293 /// Used to derive a payment key to sender for transactions broadcast by sender
294 pub delayed_payment_basepoint: PublicKey,
295 /// Used to derive an HTLC payment key to sender for transactions broadcast by counterparty
296 pub htlc_basepoint: PublicKey,
297 /// The first to-be-broadcast-by-sender transaction's per commitment point
298 pub first_per_commitment_point: PublicKey,
299 /// A request to pre-set the to-sender output's scriptPubkey for when we collaboratively close
300 pub shutdown_scriptpubkey: Option<ScriptBuf>,
301 /// The channel type that this channel will represent.
303 /// If this is `None`, we derive the channel type from the intersection of
304 /// our feature bits with our counterparty's feature bits from the [`Init`] message.
305 /// This is required to match the equivalent field in [`OpenChannel`]'s [`CommonOpenChannelFields::channel_type`].
306 pub channel_type: Option<ChannelTypeFeatures>,
308 /// Next nonce the channel initiator should use to create a funding output signature against
309 pub next_local_nonce: Option<musig2::types::PublicNonce>,
312 /// An accept_channel2 message to be sent by or received from the channel accepter.
314 /// Used in V2 channel establishment
316 // TODO(dual_funding): Add spec link for `accept_channel2`.
317 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
318 pub struct AcceptChannelV2 {
319 /// The same `temporary_channel_id` received from the initiator's `open_channel2` message.
320 pub temporary_channel_id: ChannelId,
321 /// Part of the channel value contributed by the channel acceptor
322 pub funding_satoshis: u64,
323 /// The threshold below which outputs on transactions broadcast by the channel acceptor will be
325 pub dust_limit_satoshis: u64,
326 /// The maximum inbound HTLC value in flight towards channel acceptor, in milli-satoshi
327 pub max_htlc_value_in_flight_msat: u64,
328 /// The minimum HTLC size incoming to channel acceptor, in milli-satoshi
329 pub htlc_minimum_msat: u64,
330 /// Minimum depth of the funding transaction before the channel is considered open
331 pub minimum_depth: u32,
332 /// The number of blocks which the counterparty will have to wait to claim on-chain funds if they
333 /// broadcast a commitment transaction
334 pub to_self_delay: u16,
335 /// The maximum number of inbound HTLCs towards channel acceptor
336 pub max_accepted_htlcs: u16,
337 /// The channel acceptor's key controlling the funding transaction
338 pub funding_pubkey: PublicKey,
339 /// Used to derive a revocation key for transactions broadcast by counterparty
340 pub revocation_basepoint: PublicKey,
341 /// A payment key to channel acceptor for transactions broadcast by counterparty
342 pub payment_basepoint: PublicKey,
343 /// Used to derive a payment key to channel acceptor for transactions broadcast by channel
345 pub delayed_payment_basepoint: PublicKey,
346 /// Used to derive an HTLC payment key to channel acceptor for transactions broadcast by counterparty
347 pub htlc_basepoint: PublicKey,
348 /// The first to-be-broadcast-by-channel-acceptor transaction's per commitment point
349 pub first_per_commitment_point: PublicKey,
350 /// The second to-be-broadcast-by-channel-acceptor transaction's per commitment point
351 pub second_per_commitment_point: PublicKey,
352 /// Optionally, a request to pre-set the to-channel-acceptor output's scriptPubkey for when we
353 /// collaboratively close
354 pub shutdown_scriptpubkey: Option<ScriptBuf>,
355 /// The channel type that this channel will represent. If none is set, we derive the channel
356 /// type from the intersection of our feature bits with our counterparty's feature bits from
357 /// the Init message.
359 /// This is required to match the equivalent field in [`OpenChannelV2`]'s [`CommonOpenChannelFields::channel_type`].
360 pub channel_type: Option<ChannelTypeFeatures>,
361 /// Optionally, a requirement that only confirmed inputs can be added
362 pub require_confirmed_inputs: Option<()>,
365 /// A [`funding_created`] message to be sent to or received from a peer.
367 /// Used in V1 channel establishment
369 /// [`funding_created`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#the-funding_created-message
370 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
371 pub struct FundingCreated {
372 /// A temporary channel ID, until the funding is established
373 pub temporary_channel_id: ChannelId,
374 /// The funding transaction ID
375 pub funding_txid: Txid,
376 /// The specific output index funding this channel
377 pub funding_output_index: u16,
378 /// The signature of the channel initiator (funder) on the initial commitment transaction
379 pub signature: Signature,
381 /// The partial signature of the channel initiator (funder)
382 pub partial_signature_with_nonce: Option<PartialSignatureWithNonce>,
384 /// Next nonce the channel acceptor should use to finalize the funding output signature
385 pub next_local_nonce: Option<musig2::types::PublicNonce>
388 /// A [`funding_signed`] message to be sent to or received from a peer.
390 /// Used in V1 channel establishment
392 /// [`funding_signed`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#the-funding_signed-message
393 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
394 pub struct FundingSigned {
396 pub channel_id: ChannelId,
397 /// The signature of the channel acceptor (fundee) on the initial commitment transaction
398 pub signature: Signature,
400 /// The partial signature of the channel acceptor (fundee)
401 pub partial_signature_with_nonce: Option<PartialSignatureWithNonce>,
404 /// A [`channel_ready`] message to be sent to or received from a peer.
406 /// [`channel_ready`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#the-channel_ready-message
407 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
408 pub struct ChannelReady {
410 pub channel_id: ChannelId,
411 /// The per-commitment point of the second commitment transaction
412 pub next_per_commitment_point: PublicKey,
413 /// If set, provides a `short_channel_id` alias for this channel.
415 /// The sender will accept payments to be forwarded over this SCID and forward them to this
416 /// messages' recipient.
417 pub short_channel_id_alias: Option<u64>,
420 /// An stfu (quiescence) message to be sent by or received from the stfu initiator.
421 // TODO(splicing): Add spec link for `stfu`; still in draft, using from https://github.com/lightning/bolts/pull/863
422 #[derive(Clone, Debug, PartialEq, Eq)]
424 /// The channel ID where quiescence is intended
425 pub channel_id: ChannelId,
426 /// Initiator flag, 1 if initiating, 0 if replying to an stfu.
430 /// A splice message to be sent by or received from the stfu initiator (splice initiator).
431 // TODO(splicing): Add spec link for `splice`; still in draft, using from https://github.com/lightning/bolts/pull/863
432 #[derive(Clone, Debug, PartialEq, Eq)]
434 /// The channel ID where splicing is intended
435 pub channel_id: ChannelId,
436 /// The genesis hash of the blockchain where the channel is intended to be spliced
437 pub chain_hash: ChainHash,
438 /// The intended change in channel capacity: the amount to be added (positive value)
439 /// or removed (negative value) by the sender (splice initiator) by splicing into/from the channel.
440 pub relative_satoshis: i64,
441 /// The feerate for the new funding transaction, set by the splice initiator
442 pub funding_feerate_perkw: u32,
443 /// The locktime for the new funding transaction
445 /// The key of the sender (splice initiator) controlling the new funding transaction
446 pub funding_pubkey: PublicKey,
449 /// A splice_ack message to be received by or sent to the splice initiator.
451 // TODO(splicing): Add spec link for `splice_ack`; still in draft, using from https://github.com/lightning/bolts/pull/863
452 #[derive(Clone, Debug, PartialEq, Eq)]
453 pub struct SpliceAck {
454 /// The channel ID where splicing is intended
455 pub channel_id: ChannelId,
456 /// The genesis hash of the blockchain where the channel is intended to be spliced
457 pub chain_hash: ChainHash,
458 /// The intended change in channel capacity: the amount to be added (positive value)
459 /// or removed (negative value) by the sender (splice acceptor) by splicing into/from the channel.
460 pub relative_satoshis: i64,
461 /// The key of the sender (splice acceptor) controlling the new funding transaction
462 pub funding_pubkey: PublicKey,
465 /// A splice_locked message to be sent to or received from a peer.
467 // TODO(splicing): Add spec link for `splice_locked`; still in draft, using from https://github.com/lightning/bolts/pull/863
468 #[derive(Clone, Debug, PartialEq, Eq)]
469 pub struct SpliceLocked {
471 pub channel_id: ChannelId,
474 /// A tx_add_input message for adding an input during interactive transaction construction
476 // TODO(dual_funding): Add spec link for `tx_add_input`.
477 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
478 pub struct TxAddInput {
480 pub channel_id: ChannelId,
481 /// A randomly chosen unique identifier for this input, which is even for initiators and odd for
484 /// Serialized transaction that contains the output this input spends to verify that it is non
486 pub prevtx: TransactionU16LenLimited,
487 /// The index of the output being spent
489 /// The sequence number of this input
493 /// A tx_add_output message for adding an output during interactive transaction construction.
495 // TODO(dual_funding): Add spec link for `tx_add_output`.
496 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
497 pub struct TxAddOutput {
499 pub channel_id: ChannelId,
500 /// A randomly chosen unique identifier for this output, which is even for initiators and odd for
503 /// The satoshi value of the output
505 /// The scriptPubKey for the output
506 pub script: ScriptBuf,
509 /// A tx_remove_input message for removing an input during interactive transaction construction.
511 // TODO(dual_funding): Add spec link for `tx_remove_input`.
512 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
513 pub struct TxRemoveInput {
515 pub channel_id: ChannelId,
516 /// The serial ID of the input to be removed
520 /// A tx_remove_output message for removing an output during interactive transaction construction.
522 // TODO(dual_funding): Add spec link for `tx_remove_output`.
523 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
524 pub struct TxRemoveOutput {
526 pub channel_id: ChannelId,
527 /// The serial ID of the output to be removed
531 /// A tx_complete message signalling the conclusion of a peer's transaction contributions during
532 /// interactive transaction construction.
534 // TODO(dual_funding): Add spec link for `tx_complete`.
535 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
536 pub struct TxComplete {
538 pub channel_id: ChannelId,
541 /// A tx_signatures message containing the sender's signatures for a transaction constructed with
542 /// interactive transaction construction.
544 // TODO(dual_funding): Add spec link for `tx_signatures`.
545 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
546 pub struct TxSignatures {
548 pub channel_id: ChannelId,
551 /// The list of witnesses
552 pub witnesses: Vec<Witness>,
555 /// A tx_init_rbf message which initiates a replacement of the transaction after it's been
558 // TODO(dual_funding): Add spec link for `tx_init_rbf`.
559 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
560 pub struct TxInitRbf {
562 pub channel_id: ChannelId,
563 /// The locktime of the transaction
565 /// The feerate of the transaction
566 pub feerate_sat_per_1000_weight: u32,
567 /// The number of satoshis the sender will contribute to or, if negative, remove from
568 /// (e.g. splice-out) the funding output of the transaction
569 pub funding_output_contribution: Option<i64>,
572 /// A tx_ack_rbf message which acknowledges replacement of the transaction after it's been
575 // TODO(dual_funding): Add spec link for `tx_ack_rbf`.
576 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
577 pub struct TxAckRbf {
579 pub channel_id: ChannelId,
580 /// The number of satoshis the sender will contribute to or, if negative, remove from
581 /// (e.g. splice-out) the funding output of the transaction
582 pub funding_output_contribution: Option<i64>,
585 /// A tx_abort message which signals the cancellation of an in-progress transaction negotiation.
587 // TODO(dual_funding): Add spec link for `tx_abort`.
588 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
591 pub channel_id: ChannelId,
596 /// A [`shutdown`] message to be sent to or received from a peer.
598 /// [`shutdown`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#closing-initiation-shutdown
599 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
600 pub struct Shutdown {
602 pub channel_id: ChannelId,
603 /// The destination of this peer's funds on closing.
605 /// Must be in one of these forms: P2PKH, P2SH, P2WPKH, P2WSH, P2TR.
606 pub scriptpubkey: ScriptBuf,
609 /// The minimum and maximum fees which the sender is willing to place on the closing transaction.
611 /// This is provided in [`ClosingSigned`] by both sides to indicate the fee range they are willing
613 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
614 pub struct ClosingSignedFeeRange {
615 /// The minimum absolute fee, in satoshis, which the sender is willing to place on the closing
617 pub min_fee_satoshis: u64,
618 /// The maximum absolute fee, in satoshis, which the sender is willing to place on the closing
620 pub max_fee_satoshis: u64,
623 /// A [`closing_signed`] message to be sent to or received from a peer.
625 /// [`closing_signed`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#closing-negotiation-closing_signed
626 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
627 pub struct ClosingSigned {
629 pub channel_id: ChannelId,
630 /// The proposed total fee for the closing transaction
631 pub fee_satoshis: u64,
632 /// A signature on the closing transaction
633 pub signature: Signature,
634 /// The minimum and maximum fees which the sender is willing to accept, provided only by new
636 pub fee_range: Option<ClosingSignedFeeRange>,
639 /// An [`update_add_htlc`] message to be sent to or received from a peer.
641 /// [`update_add_htlc`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#adding-an-htlc-update_add_htlc
642 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
643 pub struct UpdateAddHTLC {
645 pub channel_id: ChannelId,
648 /// The HTLC value in milli-satoshi
649 pub amount_msat: u64,
650 /// The payment hash, the pre-image of which controls HTLC redemption
651 pub payment_hash: PaymentHash,
652 /// The expiry height of the HTLC
653 pub cltv_expiry: u32,
654 /// The extra fee skimmed by the sender of this message. See
655 /// [`ChannelConfig::accept_underpaying_htlcs`].
657 /// [`ChannelConfig::accept_underpaying_htlcs`]: crate::util::config::ChannelConfig::accept_underpaying_htlcs
658 pub skimmed_fee_msat: Option<u64>,
659 /// The onion routing packet with encrypted data for the next hop.
660 pub onion_routing_packet: OnionPacket,
661 /// Provided if we are relaying or receiving a payment within a blinded path, to decrypt the onion
662 /// routing packet and the recipient-provided encrypted payload within.
663 pub blinding_point: Option<PublicKey>,
666 /// An onion message to be sent to or received from a peer.
668 // TODO: update with link to OM when they are merged into the BOLTs
669 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
670 pub struct OnionMessage {
671 /// Used in decrypting the onion packet's payload.
672 pub blinding_point: PublicKey,
673 /// The full onion packet including hop data, pubkey, and hmac
674 pub onion_routing_packet: onion_message::packet::Packet,
677 /// An [`update_fulfill_htlc`] message to be sent to or received from a peer.
679 /// [`update_fulfill_htlc`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#removing-an-htlc-update_fulfill_htlc-update_fail_htlc-and-update_fail_malformed_htlc
680 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
681 pub struct UpdateFulfillHTLC {
683 pub channel_id: ChannelId,
686 /// The pre-image of the payment hash, allowing HTLC redemption
687 pub payment_preimage: PaymentPreimage,
690 /// An [`update_fail_htlc`] message to be sent to or received from a peer.
692 /// [`update_fail_htlc`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#removing-an-htlc-update_fulfill_htlc-update_fail_htlc-and-update_fail_malformed_htlc
693 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
694 pub struct UpdateFailHTLC {
696 pub channel_id: ChannelId,
699 pub(crate) reason: OnionErrorPacket,
702 /// An [`update_fail_malformed_htlc`] message to be sent to or received from a peer.
704 /// [`update_fail_malformed_htlc`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#removing-an-htlc-update_fulfill_htlc-update_fail_htlc-and-update_fail_malformed_htlc
705 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
706 pub struct UpdateFailMalformedHTLC {
708 pub channel_id: ChannelId,
711 pub(crate) sha256_of_onion: [u8; 32],
713 pub failure_code: u16,
716 /// A [`commitment_signed`] message to be sent to or received from a peer.
718 /// [`commitment_signed`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#committing-updates-so-far-commitment_signed
719 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
720 pub struct CommitmentSigned {
722 pub channel_id: ChannelId,
723 /// A signature on the commitment transaction
724 pub signature: Signature,
725 /// Signatures on the HTLC transactions
726 pub htlc_signatures: Vec<Signature>,
728 /// The partial Taproot signature on the commitment transaction
729 pub partial_signature_with_nonce: Option<PartialSignatureWithNonce>,
732 /// A [`revoke_and_ack`] message to be sent to or received from a peer.
734 /// [`revoke_and_ack`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#completing-the-transition-to-the-updated-state-revoke_and_ack
735 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
736 pub struct RevokeAndACK {
738 pub channel_id: ChannelId,
739 /// The secret corresponding to the per-commitment point
740 pub per_commitment_secret: [u8; 32],
741 /// The next sender-broadcast commitment transaction's per-commitment point
742 pub next_per_commitment_point: PublicKey,
744 /// Musig nonce the recipient should use in their next commitment signature message
745 pub next_local_nonce: Option<musig2::types::PublicNonce>
748 /// An [`update_fee`] message to be sent to or received from a peer
750 /// [`update_fee`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#updating-fees-update_fee
751 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
752 pub struct UpdateFee {
754 pub channel_id: ChannelId,
755 /// Fee rate per 1000-weight of the transaction
756 pub feerate_per_kw: u32,
759 /// A [`channel_reestablish`] message to be sent to or received from a peer.
761 /// [`channel_reestablish`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#message-retransmission
762 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
763 pub struct ChannelReestablish {
765 pub channel_id: ChannelId,
766 /// The next commitment number for the sender
767 pub next_local_commitment_number: u64,
768 /// The next commitment number for the recipient
769 pub next_remote_commitment_number: u64,
770 /// Proof that the sender knows the per-commitment secret of a specific commitment transaction
771 /// belonging to the recipient
772 pub your_last_per_commitment_secret: [u8; 32],
773 /// The sender's per-commitment point for their current commitment transaction
774 pub my_current_per_commitment_point: PublicKey,
775 /// The next funding transaction ID
776 pub next_funding_txid: Option<Txid>,
779 /// An [`announcement_signatures`] message to be sent to or received from a peer.
781 /// [`announcement_signatures`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#the-announcement_signatures-message
782 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
783 pub struct AnnouncementSignatures {
785 pub channel_id: ChannelId,
786 /// The short channel ID
787 pub short_channel_id: u64,
788 /// A signature by the node key
789 pub node_signature: Signature,
790 /// A signature by the funding key
791 pub bitcoin_signature: Signature,
794 /// An address which can be used to connect to a remote peer.
795 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
796 pub enum SocketAddress {
797 /// An IPv4 address and port on which the peer is listening.
799 /// The 4-byte IPv4 address
801 /// The port on which the node is listening
804 /// An IPv6 address and port on which the peer is listening.
806 /// The 16-byte IPv6 address
808 /// The port on which the node is listening
811 /// An old-style Tor onion address/port on which the peer is listening.
813 /// This field is deprecated and the Tor network generally no longer supports V2 Onion
814 /// addresses. Thus, the details are not parsed here.
816 /// A new-style Tor onion address/port on which the peer is listening.
818 /// To create the human-readable "hostname", concatenate the ED25519 pubkey, checksum, and version,
819 /// wrap as base32 and append ".onion".
821 /// The ed25519 long-term public key of the peer
822 ed25519_pubkey: [u8; 32],
823 /// The checksum of the pubkey and version, as included in the onion address
825 /// The version byte, as defined by the Tor Onion v3 spec.
827 /// The port on which the node is listening
830 /// A hostname/port on which the peer is listening.
832 /// The hostname on which the node is listening.
834 /// The port on which the node is listening.
839 /// Gets the ID of this address type. Addresses in [`NodeAnnouncement`] messages should be sorted
841 pub(crate) fn get_id(&self) -> u8 {
843 &SocketAddress::TcpIpV4 {..} => { 1 },
844 &SocketAddress::TcpIpV6 {..} => { 2 },
845 &SocketAddress::OnionV2(_) => { 3 },
846 &SocketAddress::OnionV3 {..} => { 4 },
847 &SocketAddress::Hostname {..} => { 5 },
851 /// Strict byte-length of address descriptor, 1-byte type not recorded
852 fn len(&self) -> u16 {
854 &SocketAddress::TcpIpV4 { .. } => { 6 },
855 &SocketAddress::TcpIpV6 { .. } => { 18 },
856 &SocketAddress::OnionV2(_) => { 12 },
857 &SocketAddress::OnionV3 { .. } => { 37 },
858 // Consists of 1-byte hostname length, hostname bytes, and 2-byte port.
859 &SocketAddress::Hostname { ref hostname, .. } => { u16::from(hostname.len()) + 3 },
863 /// The maximum length of any address descriptor, not including the 1-byte type.
864 /// This maximum length is reached by a hostname address descriptor:
865 /// a hostname with a maximum length of 255, its 1-byte length and a 2-byte port.
866 pub(crate) const MAX_LEN: u16 = 258;
869 impl Writeable for SocketAddress {
870 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
872 &SocketAddress::TcpIpV4 { ref addr, ref port } => {
877 &SocketAddress::TcpIpV6 { ref addr, ref port } => {
882 &SocketAddress::OnionV2(bytes) => {
884 bytes.write(writer)?;
886 &SocketAddress::OnionV3 { ref ed25519_pubkey, ref checksum, ref version, ref port } => {
888 ed25519_pubkey.write(writer)?;
889 checksum.write(writer)?;
890 version.write(writer)?;
893 &SocketAddress::Hostname { ref hostname, ref port } => {
895 hostname.write(writer)?;
903 impl Readable for Result<SocketAddress, u8> {
904 fn read<R: Read>(reader: &mut R) -> Result<Result<SocketAddress, u8>, DecodeError> {
905 let byte = <u8 as Readable>::read(reader)?;
908 Ok(Ok(SocketAddress::TcpIpV4 {
909 addr: Readable::read(reader)?,
910 port: Readable::read(reader)?,
914 Ok(Ok(SocketAddress::TcpIpV6 {
915 addr: Readable::read(reader)?,
916 port: Readable::read(reader)?,
919 3 => Ok(Ok(SocketAddress::OnionV2(Readable::read(reader)?))),
921 Ok(Ok(SocketAddress::OnionV3 {
922 ed25519_pubkey: Readable::read(reader)?,
923 checksum: Readable::read(reader)?,
924 version: Readable::read(reader)?,
925 port: Readable::read(reader)?,
929 Ok(Ok(SocketAddress::Hostname {
930 hostname: Readable::read(reader)?,
931 port: Readable::read(reader)?,
934 _ => return Ok(Err(byte)),
939 impl Readable for SocketAddress {
940 fn read<R: Read>(reader: &mut R) -> Result<SocketAddress, DecodeError> {
941 match Readable::read(reader) {
942 Ok(Ok(res)) => Ok(res),
943 Ok(Err(_)) => Err(DecodeError::UnknownVersion),
949 /// [`SocketAddress`] error variants
950 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
951 pub enum SocketAddressParseError {
952 /// Socket address (IPv4/IPv6) parsing error
954 /// Invalid input format
958 /// Invalid onion v3 address
962 impl fmt::Display for SocketAddressParseError {
963 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
965 SocketAddressParseError::SocketAddrParse => write!(f, "Socket address (IPv4/IPv6) parsing error"),
966 SocketAddressParseError::InvalidInput => write!(f, "Invalid input format. \
967 Expected: \"<ipv4>:<port>\", \"[<ipv6>]:<port>\", \"<onion address>.onion:<port>\" or \"<hostname>:<port>\""),
968 SocketAddressParseError::InvalidPort => write!(f, "Invalid port"),
969 SocketAddressParseError::InvalidOnionV3 => write!(f, "Invalid onion v3 address"),
974 #[cfg(feature = "std")]
975 impl From<std::net::SocketAddrV4> for SocketAddress {
976 fn from(addr: std::net::SocketAddrV4) -> Self {
977 SocketAddress::TcpIpV4 { addr: addr.ip().octets(), port: addr.port() }
981 #[cfg(feature = "std")]
982 impl From<std::net::SocketAddrV6> for SocketAddress {
983 fn from(addr: std::net::SocketAddrV6) -> Self {
984 SocketAddress::TcpIpV6 { addr: addr.ip().octets(), port: addr.port() }
988 #[cfg(feature = "std")]
989 impl From<std::net::SocketAddr> for SocketAddress {
990 fn from(addr: std::net::SocketAddr) -> Self {
992 std::net::SocketAddr::V4(addr) => addr.into(),
993 std::net::SocketAddr::V6(addr) => addr.into(),
998 #[cfg(feature = "std")]
999 impl std::net::ToSocketAddrs for SocketAddress {
1000 type Iter = std::vec::IntoIter<std::net::SocketAddr>;
1002 fn to_socket_addrs(&self) -> std::io::Result<Self::Iter> {
1004 SocketAddress::TcpIpV4 { addr, port } => {
1005 let ip_addr = std::net::Ipv4Addr::from(*addr);
1006 let socket_addr = SocketAddr::new(ip_addr.into(), *port);
1007 Ok(vec![socket_addr].into_iter())
1009 SocketAddress::TcpIpV6 { addr, port } => {
1010 let ip_addr = std::net::Ipv6Addr::from(*addr);
1011 let socket_addr = SocketAddr::new(ip_addr.into(), *port);
1012 Ok(vec![socket_addr].into_iter())
1014 SocketAddress::Hostname { ref hostname, port } => {
1015 (hostname.as_str(), *port).to_socket_addrs()
1017 SocketAddress::OnionV2(..) => {
1018 Err(std::io::Error::new(std::io::ErrorKind::Other, "Resolution of OnionV2 \
1019 addresses is currently unsupported."))
1021 SocketAddress::OnionV3 { .. } => {
1022 Err(std::io::Error::new(std::io::ErrorKind::Other, "Resolution of OnionV3 \
1023 addresses is currently unsupported."))
1029 /// Parses an OnionV3 host and port into a [`SocketAddress::OnionV3`].
1031 /// The host part must end with ".onion".
1032 pub fn parse_onion_address(host: &str, port: u16) -> Result<SocketAddress, SocketAddressParseError> {
1033 if host.ends_with(".onion") {
1034 let domain = &host[..host.len() - ".onion".len()];
1035 if domain.len() != 56 {
1036 return Err(SocketAddressParseError::InvalidOnionV3);
1038 let onion = base32::Alphabet::RFC4648 { padding: false }.decode(&domain).map_err(|_| SocketAddressParseError::InvalidOnionV3)?;
1039 if onion.len() != 35 {
1040 return Err(SocketAddressParseError::InvalidOnionV3);
1042 let version = onion[0];
1043 let first_checksum_flag = onion[1];
1044 let second_checksum_flag = onion[2];
1045 let mut ed25519_pubkey = [0; 32];
1046 ed25519_pubkey.copy_from_slice(&onion[3..35]);
1047 let checksum = u16::from_be_bytes([first_checksum_flag, second_checksum_flag]);
1048 return Ok(SocketAddress::OnionV3 { ed25519_pubkey, checksum, version, port });
1051 return Err(SocketAddressParseError::InvalidInput);
1055 impl Display for SocketAddress {
1056 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1058 SocketAddress::TcpIpV4{addr, port} => write!(
1059 f, "{}.{}.{}.{}:{}", addr[0], addr[1], addr[2], addr[3], port)?,
1060 SocketAddress::TcpIpV6{addr, port} => write!(
1062 "[{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}]:{}",
1063 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7], addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15], port
1065 SocketAddress::OnionV2(bytes) => write!(f, "OnionV2({:?})", bytes)?,
1066 SocketAddress::OnionV3 {
1072 let [first_checksum_flag, second_checksum_flag] = checksum.to_be_bytes();
1073 let mut addr = vec![*version, first_checksum_flag, second_checksum_flag];
1074 addr.extend_from_slice(ed25519_pubkey);
1075 let onion = base32::Alphabet::RFC4648 { padding: false }.encode(&addr);
1076 write!(f, "{}.onion:{}", onion, port)?
1078 SocketAddress::Hostname { hostname, port } => write!(f, "{}:{}", hostname, port)?,
1084 #[cfg(feature = "std")]
1085 impl FromStr for SocketAddress {
1086 type Err = SocketAddressParseError;
1088 fn from_str(s: &str) -> Result<Self, Self::Err> {
1089 match std::net::SocketAddr::from_str(s) {
1090 Ok(addr) => Ok(addr.into()),
1092 let trimmed_input = match s.rfind(":") {
1094 None => return Err(SocketAddressParseError::InvalidInput),
1096 let host = &s[..trimmed_input];
1097 let port: u16 = s[trimmed_input + 1..].parse().map_err(|_| SocketAddressParseError::InvalidPort)?;
1098 if host.ends_with(".onion") {
1099 return parse_onion_address(host, port);
1101 if let Ok(hostname) = Hostname::try_from(s[..trimmed_input].to_string()) {
1102 return Ok(SocketAddress::Hostname { hostname, port });
1104 return Err(SocketAddressParseError::SocketAddrParse)
1110 /// Represents the set of gossip messages that require a signature from a node's identity key.
1111 pub enum UnsignedGossipMessage<'a> {
1112 /// An unsigned channel announcement.
1113 ChannelAnnouncement(&'a UnsignedChannelAnnouncement),
1114 /// An unsigned channel update.
1115 ChannelUpdate(&'a UnsignedChannelUpdate),
1116 /// An unsigned node announcement.
1117 NodeAnnouncement(&'a UnsignedNodeAnnouncement)
1120 impl<'a> Writeable for UnsignedGossipMessage<'a> {
1121 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
1123 UnsignedGossipMessage::ChannelAnnouncement(ref msg) => msg.write(writer),
1124 UnsignedGossipMessage::ChannelUpdate(ref msg) => msg.write(writer),
1125 UnsignedGossipMessage::NodeAnnouncement(ref msg) => msg.write(writer),
1130 /// The unsigned part of a [`node_announcement`] message.
1132 /// [`node_announcement`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#the-node_announcement-message
1133 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
1134 pub struct UnsignedNodeAnnouncement {
1135 /// The advertised features
1136 pub features: NodeFeatures,
1137 /// A strictly monotonic announcement counter, with gaps allowed
1139 /// The `node_id` this announcement originated from (don't rebroadcast the `node_announcement` back
1141 pub node_id: NodeId,
1142 /// An RGB color for UI purposes
1144 /// An alias, for UI purposes.
1146 /// This should be sanitized before use. There is no guarantee of uniqueness.
1147 pub alias: NodeAlias,
1148 /// List of addresses on which this node is reachable
1149 pub addresses: Vec<SocketAddress>,
1150 pub(crate) excess_address_data: Vec<u8>,
1151 pub(crate) excess_data: Vec<u8>,
1153 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
1154 /// A [`node_announcement`] message to be sent to or received from a peer.
1156 /// [`node_announcement`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#the-node_announcement-message
1157 pub struct NodeAnnouncement {
1158 /// The signature by the node key
1159 pub signature: Signature,
1160 /// The actual content of the announcement
1161 pub contents: UnsignedNodeAnnouncement,
1164 /// The unsigned part of a [`channel_announcement`] message.
1166 /// [`channel_announcement`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#the-channel_announcement-message
1167 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
1168 pub struct UnsignedChannelAnnouncement {
1169 /// The advertised channel features
1170 pub features: ChannelFeatures,
1171 /// The genesis hash of the blockchain where the channel is to be opened
1172 pub chain_hash: ChainHash,
1173 /// The short channel ID
1174 pub short_channel_id: u64,
1175 /// One of the two `node_id`s which are endpoints of this channel
1176 pub node_id_1: NodeId,
1177 /// The other of the two `node_id`s which are endpoints of this channel
1178 pub node_id_2: NodeId,
1179 /// The funding key for the first node
1180 pub bitcoin_key_1: NodeId,
1181 /// The funding key for the second node
1182 pub bitcoin_key_2: NodeId,
1183 /// Excess data which was signed as a part of the message which we do not (yet) understand how
1186 /// This is stored to ensure forward-compatibility as new fields are added to the lightning gossip protocol.
1187 pub excess_data: Vec<u8>,
1189 /// A [`channel_announcement`] message to be sent to or received from a peer.
1191 /// [`channel_announcement`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#the-channel_announcement-message
1192 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
1193 pub struct ChannelAnnouncement {
1194 /// Authentication of the announcement by the first public node
1195 pub node_signature_1: Signature,
1196 /// Authentication of the announcement by the second public node
1197 pub node_signature_2: Signature,
1198 /// Proof of funding UTXO ownership by the first public node
1199 pub bitcoin_signature_1: Signature,
1200 /// Proof of funding UTXO ownership by the second public node
1201 pub bitcoin_signature_2: Signature,
1202 /// The actual announcement
1203 pub contents: UnsignedChannelAnnouncement,
1206 /// The unsigned part of a [`channel_update`] message.
1208 /// [`channel_update`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#the-channel_update-message
1209 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
1210 pub struct UnsignedChannelUpdate {
1211 /// The genesis hash of the blockchain where the channel is to be opened
1212 pub chain_hash: ChainHash,
1213 /// The short channel ID
1214 pub short_channel_id: u64,
1215 /// A strictly monotonic announcement counter, with gaps allowed, specific to this channel
1219 /// The number of blocks such that if:
1220 /// `incoming_htlc.cltv_expiry < outgoing_htlc.cltv_expiry + cltv_expiry_delta`
1221 /// then we need to fail the HTLC backwards. When forwarding an HTLC, `cltv_expiry_delta` determines
1222 /// the outgoing HTLC's minimum `cltv_expiry` value -- so, if an incoming HTLC comes in with a
1223 /// `cltv_expiry` of 100000, and the node we're forwarding to has a `cltv_expiry_delta` value of 10,
1224 /// then we'll check that the outgoing HTLC's `cltv_expiry` value is at least 100010 before
1225 /// forwarding. Note that the HTLC sender is the one who originally sets this value when
1226 /// constructing the route.
1227 pub cltv_expiry_delta: u16,
1228 /// The minimum HTLC size incoming to sender, in milli-satoshi
1229 pub htlc_minimum_msat: u64,
1230 /// The maximum HTLC value incoming to sender, in milli-satoshi.
1232 /// This used to be optional.
1233 pub htlc_maximum_msat: u64,
1234 /// The base HTLC fee charged by sender, in milli-satoshi
1235 pub fee_base_msat: u32,
1236 /// The amount to fee multiplier, in micro-satoshi
1237 pub fee_proportional_millionths: u32,
1238 /// Excess data which was signed as a part of the message which we do not (yet) understand how
1241 /// This is stored to ensure forward-compatibility as new fields are added to the lightning gossip protocol.
1242 pub excess_data: Vec<u8>,
1244 /// A [`channel_update`] message to be sent to or received from a peer.
1246 /// [`channel_update`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#the-channel_update-message
1247 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
1248 pub struct ChannelUpdate {
1249 /// A signature of the channel update
1250 pub signature: Signature,
1251 /// The actual channel update
1252 pub contents: UnsignedChannelUpdate,
1255 /// A [`query_channel_range`] message is used to query a peer for channel
1256 /// UTXOs in a range of blocks. The recipient of a query makes a best
1257 /// effort to reply to the query using one or more [`ReplyChannelRange`]
1260 /// [`query_channel_range`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#the-query_channel_range-and-reply_channel_range-messages
1261 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
1262 pub struct QueryChannelRange {
1263 /// The genesis hash of the blockchain being queried
1264 pub chain_hash: ChainHash,
1265 /// The height of the first block for the channel UTXOs being queried
1266 pub first_blocknum: u32,
1267 /// The number of blocks to include in the query results
1268 pub number_of_blocks: u32,
1271 /// A [`reply_channel_range`] message is a reply to a [`QueryChannelRange`]
1274 /// Multiple `reply_channel_range` messages can be sent in reply
1275 /// to a single [`QueryChannelRange`] message. The query recipient makes a
1276 /// best effort to respond based on their local network view which may
1277 /// not be a perfect view of the network. The `short_channel_id`s in the
1278 /// reply are encoded. We only support `encoding_type=0` uncompressed
1279 /// serialization and do not support `encoding_type=1` zlib serialization.
1281 /// [`reply_channel_range`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#the-query_channel_range-and-reply_channel_range-messages
1282 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
1283 pub struct ReplyChannelRange {
1284 /// The genesis hash of the blockchain being queried
1285 pub chain_hash: ChainHash,
1286 /// The height of the first block in the range of the reply
1287 pub first_blocknum: u32,
1288 /// The number of blocks included in the range of the reply
1289 pub number_of_blocks: u32,
1290 /// True when this is the final reply for a query
1291 pub sync_complete: bool,
1292 /// The `short_channel_id`s in the channel range
1293 pub short_channel_ids: Vec<u64>,
1296 /// A [`query_short_channel_ids`] message is used to query a peer for
1297 /// routing gossip messages related to one or more `short_channel_id`s.
1299 /// The query recipient will reply with the latest, if available,
1300 /// [`ChannelAnnouncement`], [`ChannelUpdate`] and [`NodeAnnouncement`] messages
1301 /// it maintains for the requested `short_channel_id`s followed by a
1302 /// [`ReplyShortChannelIdsEnd`] message. The `short_channel_id`s sent in
1303 /// this query are encoded. We only support `encoding_type=0` uncompressed
1304 /// serialization and do not support `encoding_type=1` zlib serialization.
1306 /// [`query_short_channel_ids`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#the-query_short_channel_idsreply_short_channel_ids_end-messages
1307 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
1308 pub struct QueryShortChannelIds {
1309 /// The genesis hash of the blockchain being queried
1310 pub chain_hash: ChainHash,
1311 /// The short_channel_ids that are being queried
1312 pub short_channel_ids: Vec<u64>,
1315 /// A [`reply_short_channel_ids_end`] message is sent as a reply to a
1316 /// message. The query recipient makes a best
1317 /// effort to respond based on their local network view which may not be
1318 /// a perfect view of the network.
1320 /// [`reply_short_channel_ids_end`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#the-query_short_channel_idsreply_short_channel_ids_end-messages
1321 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
1322 pub struct ReplyShortChannelIdsEnd {
1323 /// The genesis hash of the blockchain that was queried
1324 pub chain_hash: ChainHash,
1325 /// Indicates if the query recipient maintains up-to-date channel
1326 /// information for the `chain_hash`
1327 pub full_information: bool,
1330 /// A [`gossip_timestamp_filter`] message is used by a node to request
1331 /// gossip relay for messages in the requested time range when the
1332 /// `gossip_queries` feature has been negotiated.
1334 /// [`gossip_timestamp_filter`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#the-gossip_timestamp_filter-message
1335 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
1336 pub struct GossipTimestampFilter {
1337 /// The genesis hash of the blockchain for channel and node information
1338 pub chain_hash: ChainHash,
1339 /// The starting unix timestamp
1340 pub first_timestamp: u32,
1341 /// The range of information in seconds
1342 pub timestamp_range: u32,
1345 /// Encoding type for data compression of collections in gossip queries.
1347 /// We do not support `encoding_type=1` zlib serialization [defined in BOLT
1348 /// #7](https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#query-messages).
1350 Uncompressed = 0x00,
1353 /// Used to put an error message in a [`LightningError`].
1354 #[derive(Clone, Debug, Hash, PartialEq)]
1355 pub enum ErrorAction {
1356 /// The peer took some action which made us think they were useless. Disconnect them.
1358 /// An error message which we should make an effort to send before we disconnect.
1359 msg: Option<ErrorMessage>
1361 /// The peer did something incorrect. Tell them without closing any channels and disconnect them.
1362 DisconnectPeerWithWarning {
1363 /// A warning message which we should make an effort to send before we disconnect.
1364 msg: WarningMessage,
1366 /// The peer did something harmless that we weren't able to process, just log and ignore
1367 // New code should *not* use this. New code must use IgnoreAndLog, below!
1369 /// The peer did something harmless that we weren't able to meaningfully process.
1370 /// If the error is logged, log it at the given level.
1371 IgnoreAndLog(logger::Level),
1372 /// The peer provided us with a gossip message which we'd already seen. In most cases this
1373 /// should be ignored, but it may result in the message being forwarded if it is a duplicate of
1374 /// our own channel announcements.
1375 IgnoreDuplicateGossip,
1376 /// The peer did something incorrect. Tell them.
1378 /// The message to send.
1381 /// The peer did something incorrect. Tell them without closing any channels.
1382 SendWarningMessage {
1383 /// The message to send.
1384 msg: WarningMessage,
1385 /// The peer may have done something harmless that we weren't able to meaningfully process,
1386 /// though we should still tell them about it.
1387 /// If this event is logged, log it at the given level.
1388 log_level: logger::Level,
1392 /// An Err type for failure to process messages.
1393 #[derive(Clone, Debug)]
1394 pub struct LightningError {
1395 /// A human-readable message describing the error
1397 /// The action which should be taken against the offending peer.
1398 pub action: ErrorAction,
1401 /// Struct used to return values from [`RevokeAndACK`] messages, containing a bunch of commitment
1402 /// transaction updates if they were pending.
1403 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
1404 pub struct CommitmentUpdate {
1405 /// `update_add_htlc` messages which should be sent
1406 pub update_add_htlcs: Vec<UpdateAddHTLC>,
1407 /// `update_fulfill_htlc` messages which should be sent
1408 pub update_fulfill_htlcs: Vec<UpdateFulfillHTLC>,
1409 /// `update_fail_htlc` messages which should be sent
1410 pub update_fail_htlcs: Vec<UpdateFailHTLC>,
1411 /// `update_fail_malformed_htlc` messages which should be sent
1412 pub update_fail_malformed_htlcs: Vec<UpdateFailMalformedHTLC>,
1413 /// An `update_fee` message which should be sent
1414 pub update_fee: Option<UpdateFee>,
1415 /// A `commitment_signed` message which should be sent
1416 pub commitment_signed: CommitmentSigned,
1419 /// A trait to describe an object which can receive channel messages.
1421 /// Messages MAY be called in parallel when they originate from different `their_node_ids`, however
1422 /// they MUST NOT be called in parallel when the two calls have the same `their_node_id`.
1423 pub trait ChannelMessageHandler : MessageSendEventsProvider {
1425 /// Handle an incoming `open_channel` message from the given peer.
1426 fn handle_open_channel(&self, their_node_id: &PublicKey, msg: &OpenChannel);
1427 /// Handle an incoming `open_channel2` message from the given peer.
1428 fn handle_open_channel_v2(&self, their_node_id: &PublicKey, msg: &OpenChannelV2);
1429 /// Handle an incoming `accept_channel` message from the given peer.
1430 fn handle_accept_channel(&self, their_node_id: &PublicKey, msg: &AcceptChannel);
1431 /// Handle an incoming `accept_channel2` message from the given peer.
1432 fn handle_accept_channel_v2(&self, their_node_id: &PublicKey, msg: &AcceptChannelV2);
1433 /// Handle an incoming `funding_created` message from the given peer.
1434 fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &FundingCreated);
1435 /// Handle an incoming `funding_signed` message from the given peer.
1436 fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &FundingSigned);
1437 /// Handle an incoming `channel_ready` message from the given peer.
1438 fn handle_channel_ready(&self, their_node_id: &PublicKey, msg: &ChannelReady);
1441 /// Handle an incoming `shutdown` message from the given peer.
1442 fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &Shutdown);
1443 /// Handle an incoming `closing_signed` message from the given peer.
1444 fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned);
1447 /// Handle an incoming `stfu` message from the given peer.
1448 fn handle_stfu(&self, their_node_id: &PublicKey, msg: &Stfu);
1451 /// Handle an incoming `splice` message from the given peer.
1452 fn handle_splice(&self, their_node_id: &PublicKey, msg: &Splice);
1453 /// Handle an incoming `splice_ack` message from the given peer.
1454 fn handle_splice_ack(&self, their_node_id: &PublicKey, msg: &SpliceAck);
1455 /// Handle an incoming `splice_locked` message from the given peer.
1456 fn handle_splice_locked(&self, their_node_id: &PublicKey, msg: &SpliceLocked);
1458 // Interactive channel construction
1459 /// Handle an incoming `tx_add_input message` from the given peer.
1460 fn handle_tx_add_input(&self, their_node_id: &PublicKey, msg: &TxAddInput);
1461 /// Handle an incoming `tx_add_output` message from the given peer.
1462 fn handle_tx_add_output(&self, their_node_id: &PublicKey, msg: &TxAddOutput);
1463 /// Handle an incoming `tx_remove_input` message from the given peer.
1464 fn handle_tx_remove_input(&self, their_node_id: &PublicKey, msg: &TxRemoveInput);
1465 /// Handle an incoming `tx_remove_output` message from the given peer.
1466 fn handle_tx_remove_output(&self, their_node_id: &PublicKey, msg: &TxRemoveOutput);
1467 /// Handle an incoming `tx_complete message` from the given peer.
1468 fn handle_tx_complete(&self, their_node_id: &PublicKey, msg: &TxComplete);
1469 /// Handle an incoming `tx_signatures` message from the given peer.
1470 fn handle_tx_signatures(&self, their_node_id: &PublicKey, msg: &TxSignatures);
1471 /// Handle an incoming `tx_init_rbf` message from the given peer.
1472 fn handle_tx_init_rbf(&self, their_node_id: &PublicKey, msg: &TxInitRbf);
1473 /// Handle an incoming `tx_ack_rbf` message from the given peer.
1474 fn handle_tx_ack_rbf(&self, their_node_id: &PublicKey, msg: &TxAckRbf);
1475 /// Handle an incoming `tx_abort message` from the given peer.
1476 fn handle_tx_abort(&self, their_node_id: &PublicKey, msg: &TxAbort);
1479 /// Handle an incoming `update_add_htlc` message from the given peer.
1480 fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &UpdateAddHTLC);
1481 /// Handle an incoming `update_fulfill_htlc` message from the given peer.
1482 fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFulfillHTLC);
1483 /// Handle an incoming `update_fail_htlc` message from the given peer.
1484 fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC);
1485 /// Handle an incoming `update_fail_malformed_htlc` message from the given peer.
1486 fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailMalformedHTLC);
1487 /// Handle an incoming `commitment_signed` message from the given peer.
1488 fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned);
1489 /// Handle an incoming `revoke_and_ack` message from the given peer.
1490 fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &RevokeAndACK);
1492 /// Handle an incoming `update_fee` message from the given peer.
1493 fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &UpdateFee);
1495 // Channel-to-announce:
1496 /// Handle an incoming `announcement_signatures` message from the given peer.
1497 fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &AnnouncementSignatures);
1499 // Connection loss/reestablish:
1500 /// Indicates a connection to the peer failed/an existing connection was lost.
1501 fn peer_disconnected(&self, their_node_id: &PublicKey);
1503 /// Handle a peer reconnecting, possibly generating `channel_reestablish` message(s).
1505 /// May return an `Err(())` if the features the peer supports are not sufficient to communicate
1506 /// with us. Implementors should be somewhat conservative about doing so, however, as other
1507 /// message handlers may still wish to communicate with this peer.
1508 fn peer_connected(&self, their_node_id: &PublicKey, msg: &Init, inbound: bool) -> Result<(), ()>;
1509 /// Handle an incoming `channel_reestablish` message from the given peer.
1510 fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish);
1512 /// Handle an incoming `channel_update` message from the given peer.
1513 fn handle_channel_update(&self, their_node_id: &PublicKey, msg: &ChannelUpdate);
1516 /// Handle an incoming `error` message from the given peer.
1517 fn handle_error(&self, their_node_id: &PublicKey, msg: &ErrorMessage);
1519 // Handler information:
1520 /// Gets the node feature flags which this handler itself supports. All available handlers are
1521 /// queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
1522 /// which are broadcasted in our [`NodeAnnouncement`] message.
1523 fn provided_node_features(&self) -> NodeFeatures;
1525 /// Gets the init feature flags which should be sent to the given peer. All available handlers
1526 /// are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
1527 /// which are sent in our [`Init`] message.
1529 /// Note that this method is called before [`Self::peer_connected`].
1530 fn provided_init_features(&self, their_node_id: &PublicKey) -> InitFeatures;
1532 /// Gets the chain hashes for this `ChannelMessageHandler` indicating which chains it supports.
1534 /// If it's `None`, then no particular network chain hash compatibility will be enforced when
1535 /// connecting to peers.
1536 fn get_chain_hashes(&self) -> Option<Vec<ChainHash>>;
1539 /// A trait to describe an object which can receive routing messages.
1541 /// # Implementor DoS Warnings
1543 /// For messages enabled with the `gossip_queries` feature there are potential DoS vectors when
1544 /// handling inbound queries. Implementors using an on-disk network graph should be aware of
1545 /// repeated disk I/O for queries accessing different parts of the network graph.
1546 pub trait RoutingMessageHandler : MessageSendEventsProvider {
1547 /// Handle an incoming `node_announcement` message, returning `true` if it should be forwarded on,
1548 /// `false` or returning an `Err` otherwise.
1549 fn handle_node_announcement(&self, msg: &NodeAnnouncement) -> Result<bool, LightningError>;
1550 /// Handle a `channel_announcement` message, returning `true` if it should be forwarded on, `false`
1551 /// or returning an `Err` otherwise.
1552 fn handle_channel_announcement(&self, msg: &ChannelAnnouncement) -> Result<bool, LightningError>;
1553 /// Handle an incoming `channel_update` message, returning true if it should be forwarded on,
1554 /// `false` or returning an `Err` otherwise.
1555 fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result<bool, LightningError>;
1556 /// Gets channel announcements and updates required to dump our routing table to a remote node,
1557 /// starting at the `short_channel_id` indicated by `starting_point` and including announcements
1558 /// for a single channel.
1559 fn get_next_channel_announcement(&self, starting_point: u64) -> Option<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)>;
1560 /// Gets a node announcement required to dump our routing table to a remote node, starting at
1561 /// the node *after* the provided pubkey and including up to one announcement immediately
1562 /// higher (as defined by `<PublicKey as Ord>::cmp`) than `starting_point`.
1563 /// If `None` is provided for `starting_point`, we start at the first node.
1564 fn get_next_node_announcement(&self, starting_point: Option<&NodeId>) -> Option<NodeAnnouncement>;
1565 /// Called when a connection is established with a peer. This can be used to
1566 /// perform routing table synchronization using a strategy defined by the
1569 /// May return an `Err(())` if the features the peer supports are not sufficient to communicate
1570 /// with us. Implementors should be somewhat conservative about doing so, however, as other
1571 /// message handlers may still wish to communicate with this peer.
1572 fn peer_connected(&self, their_node_id: &PublicKey, init: &Init, inbound: bool) -> Result<(), ()>;
1573 /// Handles the reply of a query we initiated to learn about channels
1574 /// for a given range of blocks. We can expect to receive one or more
1575 /// replies to a single query.
1576 fn handle_reply_channel_range(&self, their_node_id: &PublicKey, msg: ReplyChannelRange) -> Result<(), LightningError>;
1577 /// Handles the reply of a query we initiated asking for routing gossip
1578 /// messages for a list of channels. We should receive this message when
1579 /// a node has completed its best effort to send us the pertaining routing
1580 /// gossip messages.
1581 fn handle_reply_short_channel_ids_end(&self, their_node_id: &PublicKey, msg: ReplyShortChannelIdsEnd) -> Result<(), LightningError>;
1582 /// Handles when a peer asks us to send a list of `short_channel_id`s
1583 /// for the requested range of blocks.
1584 fn handle_query_channel_range(&self, their_node_id: &PublicKey, msg: QueryChannelRange) -> Result<(), LightningError>;
1585 /// Handles when a peer asks us to send routing gossip messages for a
1586 /// list of `short_channel_id`s.
1587 fn handle_query_short_channel_ids(&self, their_node_id: &PublicKey, msg: QueryShortChannelIds) -> Result<(), LightningError>;
1589 // Handler queueing status:
1590 /// Indicates that there are a large number of [`ChannelAnnouncement`] (or other) messages
1591 /// pending some async action. While there is no guarantee of the rate of future messages, the
1592 /// caller should seek to reduce the rate of new gossip messages handled, especially
1593 /// [`ChannelAnnouncement`]s.
1594 fn processing_queue_high(&self) -> bool;
1596 // Handler information:
1597 /// Gets the node feature flags which this handler itself supports. All available handlers are
1598 /// queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
1599 /// which are broadcasted in our [`NodeAnnouncement`] message.
1600 fn provided_node_features(&self) -> NodeFeatures;
1601 /// Gets the init feature flags which should be sent to the given peer. All available handlers
1602 /// are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
1603 /// which are sent in our [`Init`] message.
1605 /// Note that this method is called before [`Self::peer_connected`].
1606 fn provided_init_features(&self, their_node_id: &PublicKey) -> InitFeatures;
1609 /// A handler for received [`OnionMessage`]s and for providing generated ones to send.
1610 pub trait OnionMessageHandler: EventsProvider {
1611 /// Handle an incoming `onion_message` message from the given peer.
1612 fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &OnionMessage);
1614 /// Returns the next pending onion message for the peer with the given node id.
1615 fn next_onion_message_for_peer(&self, peer_node_id: PublicKey) -> Option<OnionMessage>;
1617 /// Called when a connection is established with a peer. Can be used to track which peers
1618 /// advertise onion message support and are online.
1620 /// May return an `Err(())` if the features the peer supports are not sufficient to communicate
1621 /// with us. Implementors should be somewhat conservative about doing so, however, as other
1622 /// message handlers may still wish to communicate with this peer.
1623 fn peer_connected(&self, their_node_id: &PublicKey, init: &Init, inbound: bool) -> Result<(), ()>;
1625 /// Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
1626 /// drop and refuse to forward onion messages to this peer.
1627 fn peer_disconnected(&self, their_node_id: &PublicKey);
1629 /// Performs actions that should happen roughly every ten seconds after startup. Allows handlers
1630 /// to drop any buffered onion messages intended for prospective peers.
1631 fn timer_tick_occurred(&self);
1633 // Handler information:
1634 /// Gets the node feature flags which this handler itself supports. All available handlers are
1635 /// queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
1636 /// which are broadcasted in our [`NodeAnnouncement`] message.
1637 fn provided_node_features(&self) -> NodeFeatures;
1639 /// Gets the init feature flags which should be sent to the given peer. All available handlers
1640 /// are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
1641 /// which are sent in our [`Init`] message.
1643 /// Note that this method is called before [`Self::peer_connected`].
1644 fn provided_init_features(&self, their_node_id: &PublicKey) -> InitFeatures;
1648 #[cfg_attr(test, derive(Debug, PartialEq))]
1649 /// Information communicated in the onion to the recipient for multi-part tracking and proof that
1650 /// the payment is associated with an invoice.
1651 pub struct FinalOnionHopData {
1652 /// When sending a multi-part payment, this secret is used to identify a payment across HTLCs.
1653 /// Because it is generated by the recipient and included in the invoice, it also provides
1654 /// proof to the recipient that the payment was sent by someone with the generated invoice.
1655 pub payment_secret: PaymentSecret,
1656 /// The intended total amount that this payment is for.
1658 /// Message serialization may panic if this value is more than 21 million Bitcoin.
1659 pub total_msat: u64,
1662 mod fuzzy_internal_msgs {
1663 use bitcoin::secp256k1::PublicKey;
1664 use crate::blinded_path::payment::{PaymentConstraints, PaymentRelay};
1665 use crate::prelude::*;
1666 use crate::ln::{PaymentPreimage, PaymentSecret};
1667 use crate::ln::features::BlindedHopFeatures;
1668 use super::FinalOnionHopData;
1670 // These types aren't intended to be pub, but are exposed for direct fuzzing (as we deserialize
1671 // them from untrusted input):
1673 pub enum InboundOnionPayload {
1675 short_channel_id: u64,
1676 /// The value, in msat, of the payment after this hop's fee is deducted.
1677 amt_to_forward: u64,
1678 outgoing_cltv_value: u32,
1681 payment_data: Option<FinalOnionHopData>,
1682 payment_metadata: Option<Vec<u8>>,
1683 keysend_preimage: Option<PaymentPreimage>,
1684 custom_tlvs: Vec<(u64, Vec<u8>)>,
1685 sender_intended_htlc_amt_msat: u64,
1686 cltv_expiry_height: u32,
1689 short_channel_id: u64,
1690 payment_relay: PaymentRelay,
1691 payment_constraints: PaymentConstraints,
1692 features: BlindedHopFeatures,
1693 intro_node_blinding_point: Option<PublicKey>,
1696 sender_intended_htlc_amt_msat: u64,
1698 cltv_expiry_height: u32,
1699 payment_secret: PaymentSecret,
1700 payment_constraints: PaymentConstraints,
1701 intro_node_blinding_point: Option<PublicKey>,
1705 pub(crate) enum OutboundOnionPayload {
1707 short_channel_id: u64,
1708 /// The value, in msat, of the payment after this hop's fee is deducted.
1709 amt_to_forward: u64,
1710 outgoing_cltv_value: u32,
1713 payment_data: Option<FinalOnionHopData>,
1714 payment_metadata: Option<Vec<u8>>,
1715 keysend_preimage: Option<PaymentPreimage>,
1716 custom_tlvs: Vec<(u64, Vec<u8>)>,
1717 sender_intended_htlc_amt_msat: u64,
1718 cltv_expiry_height: u32,
1721 encrypted_tlvs: Vec<u8>,
1722 intro_node_blinding_point: Option<PublicKey>,
1725 sender_intended_htlc_amt_msat: u64,
1727 cltv_expiry_height: u32,
1728 encrypted_tlvs: Vec<u8>,
1729 intro_node_blinding_point: Option<PublicKey>, // Set if the introduction node of the blinded path is the final node
1733 pub struct DecodedOnionErrorPacket {
1734 pub(crate) hmac: [u8; 32],
1735 pub(crate) failuremsg: Vec<u8>,
1736 pub(crate) pad: Vec<u8>,
1740 pub use self::fuzzy_internal_msgs::*;
1741 #[cfg(not(fuzzing))]
1742 pub(crate) use self::fuzzy_internal_msgs::*;
1744 /// BOLT 4 onion packet including hop data for the next peer.
1745 #[derive(Clone, Hash, PartialEq, Eq)]
1746 pub struct OnionPacket {
1747 /// BOLT 4 version number.
1749 /// In order to ensure we always return an error on onion decode in compliance with [BOLT
1750 /// #4](https://github.com/lightning/bolts/blob/master/04-onion-routing.md), we have to
1751 /// deserialize `OnionPacket`s contained in [`UpdateAddHTLC`] messages even if the ephemeral
1752 /// public key (here) is bogus, so we hold a [`Result`] instead of a [`PublicKey`] as we'd
1754 pub public_key: Result<PublicKey, secp256k1::Error>,
1755 /// 1300 bytes encrypted payload for the next hop.
1756 pub hop_data: [u8; 20*65],
1757 /// HMAC to verify the integrity of hop_data.
1761 impl onion_utils::Packet for OnionPacket {
1762 type Data = onion_utils::FixedSizeOnionPacket;
1763 fn new(pubkey: PublicKey, hop_data: onion_utils::FixedSizeOnionPacket, hmac: [u8; 32]) -> Self {
1766 public_key: Ok(pubkey),
1767 hop_data: hop_data.0,
1773 impl fmt::Debug for OnionPacket {
1774 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1775 f.write_fmt(format_args!("OnionPacket version {} with hmac {:?}", self.version, &self.hmac[..]))
1779 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
1780 pub(crate) struct OnionErrorPacket {
1781 // This really should be a constant size slice, but the spec lets these things be up to 128KB?
1782 // (TODO) We limit it in decode to much lower...
1783 pub(crate) data: Vec<u8>,
1786 impl fmt::Display for DecodeError {
1787 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1789 DecodeError::UnknownVersion => f.write_str("Unknown realm byte in Onion packet"),
1790 DecodeError::UnknownRequiredFeature => f.write_str("Unknown required feature preventing decode"),
1791 DecodeError::InvalidValue => f.write_str("Nonsense bytes didn't map to the type they were interpreted as"),
1792 DecodeError::ShortRead => f.write_str("Packet extended beyond the provided bytes"),
1793 DecodeError::BadLengthDescriptor => f.write_str("A length descriptor in the packet didn't describe the later data correctly"),
1794 DecodeError::Io(ref e) => fmt::Debug::fmt(e, f),
1795 DecodeError::UnsupportedCompression => f.write_str("We don't support receiving messages with zlib-compressed fields"),
1800 impl From<io::Error> for DecodeError {
1801 fn from(e: io::Error) -> Self {
1802 if e.kind() == io::ErrorKind::UnexpectedEof {
1803 DecodeError::ShortRead
1805 DecodeError::Io(e.kind())
1810 #[cfg(not(taproot))]
1811 impl_writeable_msg!(AcceptChannel, {
1812 temporary_channel_id,
1813 dust_limit_satoshis,
1814 max_htlc_value_in_flight_msat,
1815 channel_reserve_satoshis,
1821 revocation_basepoint,
1823 delayed_payment_basepoint,
1825 first_per_commitment_point,
1827 (0, shutdown_scriptpubkey, (option, encoding: (ScriptBuf, WithoutLength))), // Don't encode length twice.
1828 (1, channel_type, option),
1832 impl_writeable_msg!(AcceptChannel, {
1833 temporary_channel_id,
1834 dust_limit_satoshis,
1835 max_htlc_value_in_flight_msat,
1836 channel_reserve_satoshis,
1842 revocation_basepoint,
1844 delayed_payment_basepoint,
1846 first_per_commitment_point,
1848 (0, shutdown_scriptpubkey, (option, encoding: (ScriptBuf, WithoutLength))), // Don't encode length twice.
1849 (1, channel_type, option),
1850 (4, next_local_nonce, option),
1853 impl_writeable_msg!(AcceptChannelV2, {
1854 temporary_channel_id,
1856 dust_limit_satoshis,
1857 max_htlc_value_in_flight_msat,
1863 revocation_basepoint,
1865 delayed_payment_basepoint,
1867 first_per_commitment_point,
1868 second_per_commitment_point,
1870 (0, shutdown_scriptpubkey, option),
1871 (1, channel_type, option),
1872 (2, require_confirmed_inputs, option),
1875 impl_writeable_msg!(Stfu, {
1880 impl_writeable_msg!(Splice, {
1884 funding_feerate_perkw,
1889 impl_writeable_msg!(SpliceAck, {
1896 impl_writeable_msg!(SpliceLocked, {
1900 impl_writeable_msg!(TxAddInput, {
1908 impl_writeable_msg!(TxAddOutput, {
1915 impl_writeable_msg!(TxRemoveInput, {
1920 impl_writeable_msg!(TxRemoveOutput, {
1925 impl_writeable_msg!(TxComplete, {
1929 impl_writeable_msg!(TxSignatures, {
1935 impl_writeable_msg!(TxInitRbf, {
1938 feerate_sat_per_1000_weight,
1940 (0, funding_output_contribution, option),
1943 impl_writeable_msg!(TxAckRbf, {
1946 (0, funding_output_contribution, option),
1949 impl_writeable_msg!(TxAbort, {
1954 impl_writeable_msg!(AnnouncementSignatures, {
1961 impl_writeable_msg!(ChannelReestablish, {
1963 next_local_commitment_number,
1964 next_remote_commitment_number,
1965 your_last_per_commitment_secret,
1966 my_current_per_commitment_point,
1968 (0, next_funding_txid, option),
1971 impl_writeable_msg!(ClosingSigned,
1972 { channel_id, fee_satoshis, signature },
1973 { (1, fee_range, option) }
1976 impl_writeable!(ClosingSignedFeeRange, {
1981 #[cfg(not(taproot))]
1982 impl_writeable_msg!(CommitmentSigned, {
1989 impl_writeable_msg!(CommitmentSigned, {
1994 (2, partial_signature_with_nonce, option)
1997 impl_writeable!(DecodedOnionErrorPacket, {
2003 #[cfg(not(taproot))]
2004 impl_writeable_msg!(FundingCreated, {
2005 temporary_channel_id,
2007 funding_output_index,
2011 impl_writeable_msg!(FundingCreated, {
2012 temporary_channel_id,
2014 funding_output_index,
2017 (2, partial_signature_with_nonce, option),
2018 (4, next_local_nonce, option)
2021 #[cfg(not(taproot))]
2022 impl_writeable_msg!(FundingSigned, {
2028 impl_writeable_msg!(FundingSigned, {
2032 (2, partial_signature_with_nonce, option)
2035 impl_writeable_msg!(ChannelReady, {
2037 next_per_commitment_point,
2039 (1, short_channel_id_alias, option),
2042 impl Writeable for Init {
2043 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2044 // global_features gets the bottom 13 bits of our features, and local_features gets all of
2045 // our relevant feature bits. This keeps us compatible with old nodes.
2046 self.features.write_up_to_13(w)?;
2047 self.features.write(w)?;
2048 encode_tlv_stream!(w, {
2049 (1, self.networks.as_ref().map(|n| WithoutLength(n)), option),
2050 (3, self.remote_network_address, option),
2056 impl Readable for Init {
2057 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
2058 let global_features: InitFeatures = Readable::read(r)?;
2059 let features: InitFeatures = Readable::read(r)?;
2060 let mut remote_network_address: Option<SocketAddress> = None;
2061 let mut networks: Option<WithoutLength<Vec<ChainHash>>> = None;
2062 decode_tlv_stream!(r, {
2063 (1, networks, option),
2064 (3, remote_network_address, option)
2067 features: features | global_features,
2068 networks: networks.map(|n| n.0),
2069 remote_network_address,
2074 impl Writeable for OpenChannel {
2075 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2076 self.common_fields.chain_hash.write(w)?;
2077 self.common_fields.temporary_channel_id.write(w)?;
2078 self.common_fields.funding_satoshis.write(w)?;
2079 self.push_msat.write(w)?;
2080 self.common_fields.dust_limit_satoshis.write(w)?;
2081 self.common_fields.max_htlc_value_in_flight_msat.write(w)?;
2082 self.channel_reserve_satoshis.write(w)?;
2083 self.common_fields.htlc_minimum_msat.write(w)?;
2084 self.common_fields.commitment_feerate_sat_per_1000_weight.write(w)?;
2085 self.common_fields.to_self_delay.write(w)?;
2086 self.common_fields.max_accepted_htlcs.write(w)?;
2087 self.common_fields.funding_pubkey.write(w)?;
2088 self.common_fields.revocation_basepoint.write(w)?;
2089 self.common_fields.payment_basepoint.write(w)?;
2090 self.common_fields.delayed_payment_basepoint.write(w)?;
2091 self.common_fields.htlc_basepoint.write(w)?;
2092 self.common_fields.first_per_commitment_point.write(w)?;
2093 self.common_fields.channel_flags.write(w)?;
2094 encode_tlv_stream!(w, {
2095 (0, self.common_fields.shutdown_scriptpubkey.as_ref().map(|s| WithoutLength(s)), option), // Don't encode length twice.
2096 (1, self.common_fields.channel_type, option),
2102 impl Readable for OpenChannel {
2103 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
2104 let chain_hash: ChainHash = Readable::read(r)?;
2105 let temporary_channel_id: ChannelId = Readable::read(r)?;
2106 let funding_satoshis: u64 = Readable::read(r)?;
2107 let push_msat: u64 = Readable::read(r)?;
2108 let dust_limit_satoshis: u64 = Readable::read(r)?;
2109 let max_htlc_value_in_flight_msat: u64 = Readable::read(r)?;
2110 let channel_reserve_satoshis: u64 = Readable::read(r)?;
2111 let htlc_minimum_msat: u64 = Readable::read(r)?;
2112 let commitment_feerate_sat_per_1000_weight: u32 = Readable::read(r)?;
2113 let to_self_delay: u16 = Readable::read(r)?;
2114 let max_accepted_htlcs: u16 = Readable::read(r)?;
2115 let funding_pubkey: PublicKey = Readable::read(r)?;
2116 let revocation_basepoint: PublicKey = Readable::read(r)?;
2117 let payment_basepoint: PublicKey = Readable::read(r)?;
2118 let delayed_payment_basepoint: PublicKey = Readable::read(r)?;
2119 let htlc_basepoint: PublicKey = Readable::read(r)?;
2120 let first_per_commitment_point: PublicKey = Readable::read(r)?;
2121 let channel_flags: u8 = Readable::read(r)?;
2123 let mut shutdown_scriptpubkey: Option<ScriptBuf> = None;
2124 let mut channel_type: Option<ChannelTypeFeatures> = None;
2125 decode_tlv_stream!(r, {
2126 (0, shutdown_scriptpubkey, (option, encoding: (ScriptBuf, WithoutLength))),
2127 (1, channel_type, option),
2130 common_fields: CommonOpenChannelFields {
2132 temporary_channel_id,
2134 dust_limit_satoshis,
2135 max_htlc_value_in_flight_msat,
2137 commitment_feerate_sat_per_1000_weight,
2141 revocation_basepoint,
2143 delayed_payment_basepoint,
2145 first_per_commitment_point,
2147 shutdown_scriptpubkey,
2151 channel_reserve_satoshis,
2156 impl Writeable for OpenChannelV2 {
2157 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2158 self.common_fields.chain_hash.write(w)?;
2159 self.common_fields.temporary_channel_id.write(w)?;
2160 self.funding_feerate_sat_per_1000_weight.write(w)?;
2161 self.common_fields.commitment_feerate_sat_per_1000_weight.write(w)?;
2162 self.common_fields.funding_satoshis.write(w)?;
2163 self.common_fields.dust_limit_satoshis.write(w)?;
2164 self.common_fields.max_htlc_value_in_flight_msat.write(w)?;
2165 self.common_fields.htlc_minimum_msat.write(w)?;
2166 self.common_fields.to_self_delay.write(w)?;
2167 self.common_fields.max_accepted_htlcs.write(w)?;
2168 self.locktime.write(w)?;
2169 self.common_fields.funding_pubkey.write(w)?;
2170 self.common_fields.revocation_basepoint.write(w)?;
2171 self.common_fields.payment_basepoint.write(w)?;
2172 self.common_fields.delayed_payment_basepoint.write(w)?;
2173 self.common_fields.htlc_basepoint.write(w)?;
2174 self.common_fields.first_per_commitment_point.write(w)?;
2175 self.second_per_commitment_point.write(w)?;
2176 self.common_fields.channel_flags.write(w)?;
2177 encode_tlv_stream!(w, {
2178 (0, self.common_fields.shutdown_scriptpubkey.as_ref().map(|s| WithoutLength(s)), option), // Don't encode length twice.
2179 (1, self.common_fields.channel_type, option),
2180 (2, self.require_confirmed_inputs, option),
2186 impl Readable for OpenChannelV2 {
2187 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
2188 let chain_hash: ChainHash = Readable::read(r)?;
2189 let temporary_channel_id: ChannelId = Readable::read(r)?;
2190 let funding_feerate_sat_per_1000_weight: u32 = Readable::read(r)?;
2191 let commitment_feerate_sat_per_1000_weight: u32 = Readable::read(r)?;
2192 let funding_satoshis: u64 = Readable::read(r)?;
2193 let dust_limit_satoshis: u64 = Readable::read(r)?;
2194 let max_htlc_value_in_flight_msat: u64 = Readable::read(r)?;
2195 let htlc_minimum_msat: u64 = Readable::read(r)?;
2196 let to_self_delay: u16 = Readable::read(r)?;
2197 let max_accepted_htlcs: u16 = Readable::read(r)?;
2198 let locktime: u32 = Readable::read(r)?;
2199 let funding_pubkey: PublicKey = Readable::read(r)?;
2200 let revocation_basepoint: PublicKey = Readable::read(r)?;
2201 let payment_basepoint: PublicKey = Readable::read(r)?;
2202 let delayed_payment_basepoint: PublicKey = Readable::read(r)?;
2203 let htlc_basepoint: PublicKey = Readable::read(r)?;
2204 let first_per_commitment_point: PublicKey = Readable::read(r)?;
2205 let second_per_commitment_point: PublicKey = Readable::read(r)?;
2206 let channel_flags: u8 = Readable::read(r)?;
2208 let mut shutdown_scriptpubkey: Option<ScriptBuf> = None;
2209 let mut channel_type: Option<ChannelTypeFeatures> = None;
2210 let mut require_confirmed_inputs: Option<()> = None;
2211 decode_tlv_stream!(r, {
2212 (0, shutdown_scriptpubkey, (option, encoding: (ScriptBuf, WithoutLength))),
2213 (1, channel_type, option),
2214 (2, require_confirmed_inputs, option),
2217 common_fields: CommonOpenChannelFields {
2219 temporary_channel_id,
2221 dust_limit_satoshis,
2222 max_htlc_value_in_flight_msat,
2224 commitment_feerate_sat_per_1000_weight,
2228 revocation_basepoint,
2230 delayed_payment_basepoint,
2232 first_per_commitment_point,
2234 shutdown_scriptpubkey,
2237 funding_feerate_sat_per_1000_weight,
2239 second_per_commitment_point,
2240 require_confirmed_inputs,
2245 #[cfg(not(taproot))]
2246 impl_writeable_msg!(RevokeAndACK, {
2248 per_commitment_secret,
2249 next_per_commitment_point
2253 impl_writeable_msg!(RevokeAndACK, {
2255 per_commitment_secret,
2256 next_per_commitment_point
2258 (4, next_local_nonce, option)
2261 impl_writeable_msg!(Shutdown, {
2266 impl_writeable_msg!(UpdateFailHTLC, {
2272 impl_writeable_msg!(UpdateFailMalformedHTLC, {
2279 impl_writeable_msg!(UpdateFee, {
2284 impl_writeable_msg!(UpdateFulfillHTLC, {
2290 // Note that this is written as a part of ChannelManager objects, and thus cannot change its
2291 // serialization format in a way which assumes we know the total serialized length/message end
2293 impl_writeable!(OnionErrorPacket, {
2297 // Note that this is written as a part of ChannelManager objects, and thus cannot change its
2298 // serialization format in a way which assumes we know the total serialized length/message end
2300 impl Writeable for OnionPacket {
2301 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2302 self.version.write(w)?;
2303 match self.public_key {
2304 Ok(pubkey) => pubkey.write(w)?,
2305 Err(_) => [0u8;33].write(w)?,
2307 w.write_all(&self.hop_data)?;
2308 self.hmac.write(w)?;
2313 impl Readable for OnionPacket {
2314 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
2316 version: Readable::read(r)?,
2318 let mut buf = [0u8;33];
2319 r.read_exact(&mut buf)?;
2320 PublicKey::from_slice(&buf)
2322 hop_data: Readable::read(r)?,
2323 hmac: Readable::read(r)?,
2328 impl_writeable_msg!(UpdateAddHTLC, {
2334 onion_routing_packet,
2336 (0, blinding_point, option),
2337 (65537, skimmed_fee_msat, option)
2340 impl Readable for OnionMessage {
2341 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
2342 let blinding_point: PublicKey = Readable::read(r)?;
2343 let len: u16 = Readable::read(r)?;
2344 let mut packet_reader = FixedLengthReader::new(r, len as u64);
2345 let onion_routing_packet: onion_message::packet::Packet =
2346 <onion_message::packet::Packet as LengthReadable>::read(&mut packet_reader)?;
2349 onion_routing_packet,
2354 impl Writeable for OnionMessage {
2355 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2356 self.blinding_point.write(w)?;
2357 let onion_packet_len = self.onion_routing_packet.serialized_length();
2358 (onion_packet_len as u16).write(w)?;
2359 self.onion_routing_packet.write(w)?;
2364 impl Writeable for FinalOnionHopData {
2365 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2366 self.payment_secret.0.write(w)?;
2367 HighZeroBytesDroppedBigSize(self.total_msat).write(w)
2371 impl Readable for FinalOnionHopData {
2372 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
2373 let secret: [u8; 32] = Readable::read(r)?;
2374 let amt: HighZeroBytesDroppedBigSize<u64> = Readable::read(r)?;
2375 Ok(Self { payment_secret: PaymentSecret(secret), total_msat: amt.0 })
2379 impl Writeable for OutboundOnionPayload {
2380 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2382 Self::Forward { short_channel_id, amt_to_forward, outgoing_cltv_value } => {
2383 _encode_varint_length_prefixed_tlv!(w, {
2384 (2, HighZeroBytesDroppedBigSize(*amt_to_forward), required),
2385 (4, HighZeroBytesDroppedBigSize(*outgoing_cltv_value), required),
2386 (6, short_channel_id, required)
2390 ref payment_data, ref payment_metadata, ref keysend_preimage, sender_intended_htlc_amt_msat,
2391 cltv_expiry_height, ref custom_tlvs,
2393 // We need to update [`ln::outbound_payment::RecipientOnionFields::with_custom_tlvs`]
2394 // to reject any reserved types in the experimental range if new ones are ever
2396 let keysend_tlv = keysend_preimage.map(|preimage| (5482373484, preimage.encode()));
2397 let mut custom_tlvs: Vec<&(u64, Vec<u8>)> = custom_tlvs.iter().chain(keysend_tlv.iter()).collect();
2398 custom_tlvs.sort_unstable_by_key(|(typ, _)| *typ);
2399 _encode_varint_length_prefixed_tlv!(w, {
2400 (2, HighZeroBytesDroppedBigSize(*sender_intended_htlc_amt_msat), required),
2401 (4, HighZeroBytesDroppedBigSize(*cltv_expiry_height), required),
2402 (8, payment_data, option),
2403 (16, payment_metadata.as_ref().map(|m| WithoutLength(m)), option)
2404 }, custom_tlvs.iter());
2406 Self::BlindedForward { encrypted_tlvs, intro_node_blinding_point } => {
2407 _encode_varint_length_prefixed_tlv!(w, {
2408 (10, *encrypted_tlvs, required_vec),
2409 (12, intro_node_blinding_point, option)
2412 Self::BlindedReceive {
2413 sender_intended_htlc_amt_msat, total_msat, cltv_expiry_height, encrypted_tlvs,
2414 intro_node_blinding_point,
2416 _encode_varint_length_prefixed_tlv!(w, {
2417 (2, HighZeroBytesDroppedBigSize(*sender_intended_htlc_amt_msat), required),
2418 (4, HighZeroBytesDroppedBigSize(*cltv_expiry_height), required),
2419 (10, *encrypted_tlvs, required_vec),
2420 (12, intro_node_blinding_point, option),
2421 (18, HighZeroBytesDroppedBigSize(*total_msat), required)
2429 impl<NS: Deref> ReadableArgs<(Option<PublicKey>, &NS)> for InboundOnionPayload where NS::Target: NodeSigner {
2430 fn read<R: Read>(r: &mut R, args: (Option<PublicKey>, &NS)) -> Result<Self, DecodeError> {
2431 let (update_add_blinding_point, node_signer) = args;
2434 let mut cltv_value = None;
2435 let mut short_id: Option<u64> = None;
2436 let mut payment_data: Option<FinalOnionHopData> = None;
2437 let mut encrypted_tlvs_opt: Option<WithoutLength<Vec<u8>>> = None;
2438 let mut intro_node_blinding_point = None;
2439 let mut payment_metadata: Option<WithoutLength<Vec<u8>>> = None;
2440 let mut total_msat = None;
2441 let mut keysend_preimage: Option<PaymentPreimage> = None;
2442 let mut custom_tlvs = Vec::new();
2444 let tlv_len = BigSize::read(r)?;
2445 let rd = FixedLengthReader::new(r, tlv_len.0);
2446 decode_tlv_stream_with_custom_tlv_decode!(rd, {
2447 (2, amt, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
2448 (4, cltv_value, (option, encoding: (u32, HighZeroBytesDroppedBigSize))),
2449 (6, short_id, option),
2450 (8, payment_data, option),
2451 (10, encrypted_tlvs_opt, option),
2452 (12, intro_node_blinding_point, option),
2453 (16, payment_metadata, option),
2454 (18, total_msat, (option, encoding: (u64, HighZeroBytesDroppedBigSize))),
2455 // See https://github.com/lightning/blips/blob/master/blip-0003.md
2456 (5482373484, keysend_preimage, option)
2457 }, |msg_type: u64, msg_reader: &mut FixedLengthReader<_>| -> Result<bool, DecodeError> {
2458 if msg_type < 1 << 16 { return Ok(false) }
2459 let mut value = Vec::new();
2460 msg_reader.read_to_end(&mut value)?;
2461 custom_tlvs.push((msg_type, value));
2465 if amt.unwrap_or(0) > MAX_VALUE_MSAT { return Err(DecodeError::InvalidValue) }
2466 if intro_node_blinding_point.is_some() && update_add_blinding_point.is_some() {
2467 return Err(DecodeError::InvalidValue)
2470 if let Some(blinding_point) = intro_node_blinding_point.or(update_add_blinding_point) {
2471 if short_id.is_some() || payment_data.is_some() || payment_metadata.is_some() ||
2472 keysend_preimage.is_some()
2474 return Err(DecodeError::InvalidValue)
2476 let enc_tlvs = encrypted_tlvs_opt.ok_or(DecodeError::InvalidValue)?.0;
2477 let enc_tlvs_ss = node_signer.ecdh(Recipient::Node, &blinding_point, None)
2478 .map_err(|_| DecodeError::InvalidValue)?;
2479 let rho = onion_utils::gen_rho_from_shared_secret(&enc_tlvs_ss.secret_bytes());
2480 let mut s = Cursor::new(&enc_tlvs);
2481 let mut reader = FixedLengthReader::new(&mut s, enc_tlvs.len() as u64);
2482 match ChaChaPolyReadAdapter::read(&mut reader, rho)? {
2483 ChaChaPolyReadAdapter { readable: BlindedPaymentTlvs::Forward(ForwardTlvs {
2484 short_channel_id, payment_relay, payment_constraints, features
2486 if amt.is_some() || cltv_value.is_some() || total_msat.is_some() {
2487 return Err(DecodeError::InvalidValue)
2489 Ok(Self::BlindedForward {
2492 payment_constraints,
2494 intro_node_blinding_point,
2497 ChaChaPolyReadAdapter { readable: BlindedPaymentTlvs::Receive(ReceiveTlvs {
2498 payment_secret, payment_constraints
2500 if total_msat.unwrap_or(0) > MAX_VALUE_MSAT { return Err(DecodeError::InvalidValue) }
2501 Ok(Self::BlindedReceive {
2502 sender_intended_htlc_amt_msat: amt.ok_or(DecodeError::InvalidValue)?,
2503 total_msat: total_msat.ok_or(DecodeError::InvalidValue)?,
2504 cltv_expiry_height: cltv_value.ok_or(DecodeError::InvalidValue)?,
2506 payment_constraints,
2507 intro_node_blinding_point,
2511 } else if let Some(short_channel_id) = short_id {
2512 if payment_data.is_some() || payment_metadata.is_some() || encrypted_tlvs_opt.is_some() ||
2513 total_msat.is_some()
2514 { return Err(DecodeError::InvalidValue) }
2517 amt_to_forward: amt.ok_or(DecodeError::InvalidValue)?,
2518 outgoing_cltv_value: cltv_value.ok_or(DecodeError::InvalidValue)?,
2521 if encrypted_tlvs_opt.is_some() || total_msat.is_some() {
2522 return Err(DecodeError::InvalidValue)
2524 if let Some(data) = &payment_data {
2525 if data.total_msat > MAX_VALUE_MSAT {
2526 return Err(DecodeError::InvalidValue);
2531 payment_metadata: payment_metadata.map(|w| w.0),
2533 sender_intended_htlc_amt_msat: amt.ok_or(DecodeError::InvalidValue)?,
2534 cltv_expiry_height: cltv_value.ok_or(DecodeError::InvalidValue)?,
2541 impl Writeable for Ping {
2542 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2543 self.ponglen.write(w)?;
2544 vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
2549 impl Readable for Ping {
2550 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
2552 ponglen: Readable::read(r)?,
2554 let byteslen = Readable::read(r)?;
2555 r.read_exact(&mut vec![0u8; byteslen as usize][..])?;
2562 impl Writeable for Pong {
2563 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2564 vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
2569 impl Readable for Pong {
2570 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
2573 let byteslen = Readable::read(r)?;
2574 r.read_exact(&mut vec![0u8; byteslen as usize][..])?;
2581 impl Writeable for UnsignedChannelAnnouncement {
2582 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2583 self.features.write(w)?;
2584 self.chain_hash.write(w)?;
2585 self.short_channel_id.write(w)?;
2586 self.node_id_1.write(w)?;
2587 self.node_id_2.write(w)?;
2588 self.bitcoin_key_1.write(w)?;
2589 self.bitcoin_key_2.write(w)?;
2590 w.write_all(&self.excess_data[..])?;
2595 impl Readable for UnsignedChannelAnnouncement {
2596 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
2598 features: Readable::read(r)?,
2599 chain_hash: Readable::read(r)?,
2600 short_channel_id: Readable::read(r)?,
2601 node_id_1: Readable::read(r)?,
2602 node_id_2: Readable::read(r)?,
2603 bitcoin_key_1: Readable::read(r)?,
2604 bitcoin_key_2: Readable::read(r)?,
2605 excess_data: read_to_end(r)?,
2610 impl_writeable!(ChannelAnnouncement, {
2613 bitcoin_signature_1,
2614 bitcoin_signature_2,
2618 impl Writeable for UnsignedChannelUpdate {
2619 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2620 // `message_flags` used to indicate presence of `htlc_maximum_msat`, but was deprecated in the spec.
2621 const MESSAGE_FLAGS: u8 = 1;
2622 self.chain_hash.write(w)?;
2623 self.short_channel_id.write(w)?;
2624 self.timestamp.write(w)?;
2625 let all_flags = self.flags as u16 | ((MESSAGE_FLAGS as u16) << 8);
2626 all_flags.write(w)?;
2627 self.cltv_expiry_delta.write(w)?;
2628 self.htlc_minimum_msat.write(w)?;
2629 self.fee_base_msat.write(w)?;
2630 self.fee_proportional_millionths.write(w)?;
2631 self.htlc_maximum_msat.write(w)?;
2632 w.write_all(&self.excess_data[..])?;
2637 impl Readable for UnsignedChannelUpdate {
2638 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
2640 chain_hash: Readable::read(r)?,
2641 short_channel_id: Readable::read(r)?,
2642 timestamp: Readable::read(r)?,
2644 let flags: u16 = Readable::read(r)?;
2645 // Note: we ignore the `message_flags` for now, since it was deprecated by the spec.
2648 cltv_expiry_delta: Readable::read(r)?,
2649 htlc_minimum_msat: Readable::read(r)?,
2650 fee_base_msat: Readable::read(r)?,
2651 fee_proportional_millionths: Readable::read(r)?,
2652 htlc_maximum_msat: Readable::read(r)?,
2653 excess_data: read_to_end(r)?,
2658 impl_writeable!(ChannelUpdate, {
2663 impl Writeable for ErrorMessage {
2664 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2665 self.channel_id.write(w)?;
2666 (self.data.len() as u16).write(w)?;
2667 w.write_all(self.data.as_bytes())?;
2672 impl Readable for ErrorMessage {
2673 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
2675 channel_id: Readable::read(r)?,
2677 let sz: usize = <u16 as Readable>::read(r)? as usize;
2678 let mut data = Vec::with_capacity(sz);
2680 r.read_exact(&mut data)?;
2681 match String::from_utf8(data) {
2683 Err(_) => return Err(DecodeError::InvalidValue),
2690 impl Writeable for WarningMessage {
2691 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2692 self.channel_id.write(w)?;
2693 (self.data.len() as u16).write(w)?;
2694 w.write_all(self.data.as_bytes())?;
2699 impl Readable for WarningMessage {
2700 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
2702 channel_id: Readable::read(r)?,
2704 let sz: usize = <u16 as Readable>::read(r)? as usize;
2705 let mut data = Vec::with_capacity(sz);
2707 r.read_exact(&mut data)?;
2708 match String::from_utf8(data) {
2710 Err(_) => return Err(DecodeError::InvalidValue),
2717 impl Writeable for UnsignedNodeAnnouncement {
2718 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2719 self.features.write(w)?;
2720 self.timestamp.write(w)?;
2721 self.node_id.write(w)?;
2722 w.write_all(&self.rgb)?;
2723 self.alias.write(w)?;
2725 let mut addr_len = 0;
2726 for addr in self.addresses.iter() {
2727 addr_len += 1 + addr.len();
2729 (addr_len + self.excess_address_data.len() as u16).write(w)?;
2730 for addr in self.addresses.iter() {
2733 w.write_all(&self.excess_address_data[..])?;
2734 w.write_all(&self.excess_data[..])?;
2739 impl Readable for UnsignedNodeAnnouncement {
2740 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
2741 let features: NodeFeatures = Readable::read(r)?;
2742 let timestamp: u32 = Readable::read(r)?;
2743 let node_id: NodeId = Readable::read(r)?;
2744 let mut rgb = [0; 3];
2745 r.read_exact(&mut rgb)?;
2746 let alias: NodeAlias = Readable::read(r)?;
2748 let addr_len: u16 = Readable::read(r)?;
2749 let mut addresses: Vec<SocketAddress> = Vec::new();
2750 let mut addr_readpos = 0;
2751 let mut excess = false;
2752 let mut excess_byte = 0;
2754 if addr_len <= addr_readpos { break; }
2755 match Readable::read(r) {
2757 if addr_len < addr_readpos + 1 + addr.len() {
2758 return Err(DecodeError::BadLengthDescriptor);
2760 addr_readpos += (1 + addr.len()) as u16;
2761 addresses.push(addr);
2763 Ok(Err(unknown_descriptor)) => {
2765 excess_byte = unknown_descriptor;
2768 Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
2769 Err(e) => return Err(e),
2773 let mut excess_data = vec![];
2774 let excess_address_data = if addr_readpos < addr_len {
2775 let mut excess_address_data = vec![0; (addr_len - addr_readpos) as usize];
2776 r.read_exact(&mut excess_address_data[if excess { 1 } else { 0 }..])?;
2778 excess_address_data[0] = excess_byte;
2783 excess_data.push(excess_byte);
2787 excess_data.extend(read_to_end(r)?.iter());
2788 Ok(UnsignedNodeAnnouncement {
2795 excess_address_data,
2801 impl_writeable!(NodeAnnouncement, {
2806 impl Readable for QueryShortChannelIds {
2807 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
2808 let chain_hash: ChainHash = Readable::read(r)?;
2810 let encoding_len: u16 = Readable::read(r)?;
2811 let encoding_type: u8 = Readable::read(r)?;
2813 // Must be encoding_type=0 uncompressed serialization. We do not
2814 // support encoding_type=1 zlib serialization.
2815 if encoding_type != EncodingType::Uncompressed as u8 {
2816 return Err(DecodeError::UnsupportedCompression);
2819 // We expect the encoding_len to always includes the 1-byte
2820 // encoding_type and that short_channel_ids are 8-bytes each
2821 if encoding_len == 0 || (encoding_len - 1) % 8 != 0 {
2822 return Err(DecodeError::InvalidValue);
2825 // Read short_channel_ids (8-bytes each), for the u16 encoding_len
2826 // less the 1-byte encoding_type
2827 let short_channel_id_count: u16 = (encoding_len - 1)/8;
2828 let mut short_channel_ids = Vec::with_capacity(short_channel_id_count as usize);
2829 for _ in 0..short_channel_id_count {
2830 short_channel_ids.push(Readable::read(r)?);
2833 Ok(QueryShortChannelIds {
2840 impl Writeable for QueryShortChannelIds {
2841 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2842 // Calculated from 1-byte encoding_type plus 8-bytes per short_channel_id
2843 let encoding_len: u16 = 1 + self.short_channel_ids.len() as u16 * 8;
2845 self.chain_hash.write(w)?;
2846 encoding_len.write(w)?;
2848 // We only support type=0 uncompressed serialization
2849 (EncodingType::Uncompressed as u8).write(w)?;
2851 for scid in self.short_channel_ids.iter() {
2859 impl_writeable_msg!(ReplyShortChannelIdsEnd, {
2864 impl QueryChannelRange {
2865 /// Calculates the overflow safe ending block height for the query.
2867 /// Overflow returns `0xffffffff`, otherwise returns `first_blocknum + number_of_blocks`.
2868 pub fn end_blocknum(&self) -> u32 {
2869 match self.first_blocknum.checked_add(self.number_of_blocks) {
2870 Some(block) => block,
2871 None => u32::max_value(),
2876 impl_writeable_msg!(QueryChannelRange, {
2882 impl Readable for ReplyChannelRange {
2883 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
2884 let chain_hash: ChainHash = Readable::read(r)?;
2885 let first_blocknum: u32 = Readable::read(r)?;
2886 let number_of_blocks: u32 = Readable::read(r)?;
2887 let sync_complete: bool = Readable::read(r)?;
2889 let encoding_len: u16 = Readable::read(r)?;
2890 let encoding_type: u8 = Readable::read(r)?;
2892 // Must be encoding_type=0 uncompressed serialization. We do not
2893 // support encoding_type=1 zlib serialization.
2894 if encoding_type != EncodingType::Uncompressed as u8 {
2895 return Err(DecodeError::UnsupportedCompression);
2898 // We expect the encoding_len to always includes the 1-byte
2899 // encoding_type and that short_channel_ids are 8-bytes each
2900 if encoding_len == 0 || (encoding_len - 1) % 8 != 0 {
2901 return Err(DecodeError::InvalidValue);
2904 // Read short_channel_ids (8-bytes each), for the u16 encoding_len
2905 // less the 1-byte encoding_type
2906 let short_channel_id_count: u16 = (encoding_len - 1)/8;
2907 let mut short_channel_ids = Vec::with_capacity(short_channel_id_count as usize);
2908 for _ in 0..short_channel_id_count {
2909 short_channel_ids.push(Readable::read(r)?);
2912 Ok(ReplyChannelRange {
2922 impl Writeable for ReplyChannelRange {
2923 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2924 let encoding_len: u16 = 1 + self.short_channel_ids.len() as u16 * 8;
2925 self.chain_hash.write(w)?;
2926 self.first_blocknum.write(w)?;
2927 self.number_of_blocks.write(w)?;
2928 self.sync_complete.write(w)?;
2930 encoding_len.write(w)?;
2931 (EncodingType::Uncompressed as u8).write(w)?;
2932 for scid in self.short_channel_ids.iter() {
2940 impl_writeable_msg!(GossipTimestampFilter, {
2948 use std::convert::TryFrom;
2949 use bitcoin::{Transaction, TxIn, ScriptBuf, Sequence, Witness, TxOut};
2950 use hex::DisplayHex;
2951 use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
2952 use crate::ln::ChannelId;
2953 use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
2954 use crate::ln::msgs::{self, FinalOnionHopData, OnionErrorPacket, CommonOpenChannelFields};
2955 use crate::ln::msgs::SocketAddress;
2956 use crate::routing::gossip::{NodeAlias, NodeId};
2957 use crate::util::ser::{Writeable, Readable, ReadableArgs, Hostname, TransactionU16LenLimited};
2958 use crate::util::test_utils;
2960 use bitcoin::hashes::hex::FromHex;
2961 use bitcoin::address::Address;
2962 use bitcoin::network::constants::Network;
2963 use bitcoin::blockdata::constants::ChainHash;
2964 use bitcoin::blockdata::script::Builder;
2965 use bitcoin::blockdata::opcodes;
2966 use bitcoin::hash_types::Txid;
2967 use bitcoin::locktime::absolute::LockTime;
2969 use bitcoin::secp256k1::{PublicKey,SecretKey};
2970 use bitcoin::secp256k1::{Secp256k1, Message};
2972 use crate::io::{self, Cursor};
2973 use crate::prelude::*;
2974 use core::str::FromStr;
2975 use crate::chain::transaction::OutPoint;
2977 #[cfg(feature = "std")]
2978 use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
2979 #[cfg(feature = "std")]
2980 use crate::ln::msgs::SocketAddressParseError;
2983 fn encoding_channel_reestablish() {
2985 let secp_ctx = Secp256k1::new();
2986 PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&<Vec<u8>>::from_hex("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap())
2989 let cr = msgs::ChannelReestablish {
2990 channel_id: ChannelId::from_bytes([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]),
2991 next_local_commitment_number: 3,
2992 next_remote_commitment_number: 4,
2993 your_last_per_commitment_secret: [9;32],
2994 my_current_per_commitment_point: public_key,
2995 next_funding_txid: None,
2998 let encoded_value = cr.encode();
3002 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, // channel_id
3003 0, 0, 0, 0, 0, 0, 0, 3, // next_local_commitment_number
3004 0, 0, 0, 0, 0, 0, 0, 4, // next_remote_commitment_number
3005 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, // your_last_per_commitment_secret
3006 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, // my_current_per_commitment_point
3012 fn encoding_channel_reestablish_with_next_funding_txid() {
3014 let secp_ctx = Secp256k1::new();
3015 PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&<Vec<u8>>::from_hex("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap())
3018 let cr = msgs::ChannelReestablish {
3019 channel_id: ChannelId::from_bytes([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]),
3020 next_local_commitment_number: 3,
3021 next_remote_commitment_number: 4,
3022 your_last_per_commitment_secret: [9;32],
3023 my_current_per_commitment_point: public_key,
3024 next_funding_txid: Some(Txid::from_raw_hash(bitcoin::hashes::Hash::from_slice(&[
3025 48, 167, 250, 69, 152, 48, 103, 172, 164, 99, 59, 19, 23, 11, 92, 84, 15, 80, 4, 12, 98, 82, 75, 31, 201, 11, 91, 23, 98, 23, 53, 124,
3029 let encoded_value = cr.encode();
3033 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, // channel_id
3034 0, 0, 0, 0, 0, 0, 0, 3, // next_local_commitment_number
3035 0, 0, 0, 0, 0, 0, 0, 4, // next_remote_commitment_number
3036 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, // your_last_per_commitment_secret
3037 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, // my_current_per_commitment_point
3038 0, // Type (next_funding_txid)
3040 48, 167, 250, 69, 152, 48, 103, 172, 164, 99, 59, 19, 23, 11, 92, 84, 15, 80, 4, 12, 98, 82, 75, 31, 201, 11, 91, 23, 98, 23, 53, 124, // Value
3045 macro_rules! get_keys_from {
3046 ($slice: expr, $secp_ctx: expr) => {
3048 let privkey = SecretKey::from_slice(&<Vec<u8>>::from_hex($slice).unwrap()[..]).unwrap();
3049 let pubkey = PublicKey::from_secret_key(&$secp_ctx, &privkey);
3055 macro_rules! get_sig_on {
3056 ($privkey: expr, $ctx: expr, $string: expr) => {
3058 let sighash = Message::from_slice(&$string.into_bytes()[..]).unwrap();
3059 $ctx.sign_ecdsa(&sighash, &$privkey)
3065 fn encoding_announcement_signatures() {
3066 let secp_ctx = Secp256k1::new();
3067 let (privkey, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3068 let sig_1 = get_sig_on!(privkey, secp_ctx, String::from("01010101010101010101010101010101"));
3069 let sig_2 = get_sig_on!(privkey, secp_ctx, String::from("02020202020202020202020202020202"));
3070 let announcement_signatures = msgs::AnnouncementSignatures {
3071 channel_id: ChannelId::from_bytes([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]),
3072 short_channel_id: 2316138423780173,
3073 node_signature: sig_1,
3074 bitcoin_signature: sig_2,
3077 let encoded_value = announcement_signatures.encode();
3078 assert_eq!(encoded_value, <Vec<u8>>::from_hex("040000000000000005000000000000000600000000000000070000000000000000083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073acf9953cef4700860f5967838eba2bae89288ad188ebf8b20bf995c3ea53a26df1876d0a3a0e13172ba286a673140190c02ba9da60a2e43a745188c8a83c7f3ef").unwrap());
3081 fn do_encoding_channel_announcement(unknown_features_bits: bool, excess_data: bool) {
3082 let secp_ctx = Secp256k1::new();
3083 let (privkey_1, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3084 let (privkey_2, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
3085 let (privkey_3, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
3086 let (privkey_4, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
3087 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
3088 let sig_2 = get_sig_on!(privkey_2, secp_ctx, String::from("01010101010101010101010101010101"));
3089 let sig_3 = get_sig_on!(privkey_3, secp_ctx, String::from("01010101010101010101010101010101"));
3090 let sig_4 = get_sig_on!(privkey_4, secp_ctx, String::from("01010101010101010101010101010101"));
3091 let mut features = ChannelFeatures::empty();
3092 if unknown_features_bits {
3093 features = ChannelFeatures::from_le_bytes(vec![0xFF, 0xFF]);
3095 let unsigned_channel_announcement = msgs::UnsignedChannelAnnouncement {
3097 chain_hash: ChainHash::using_genesis_block(Network::Bitcoin),
3098 short_channel_id: 2316138423780173,
3099 node_id_1: NodeId::from_pubkey(&pubkey_1),
3100 node_id_2: NodeId::from_pubkey(&pubkey_2),
3101 bitcoin_key_1: NodeId::from_pubkey(&pubkey_3),
3102 bitcoin_key_2: NodeId::from_pubkey(&pubkey_4),
3103 excess_data: if excess_data { vec![10, 0, 0, 20, 0, 0, 30, 0, 0, 40] } else { Vec::new() },
3105 let channel_announcement = msgs::ChannelAnnouncement {
3106 node_signature_1: sig_1,
3107 node_signature_2: sig_2,
3108 bitcoin_signature_1: sig_3,
3109 bitcoin_signature_2: sig_4,
3110 contents: unsigned_channel_announcement,
3112 let encoded_value = channel_announcement.encode();
3113 let mut target_value = <Vec<u8>>::from_hex("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a1735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap();
3114 if unknown_features_bits {
3115 target_value.append(&mut <Vec<u8>>::from_hex("0002ffff").unwrap());
3117 target_value.append(&mut <Vec<u8>>::from_hex("0000").unwrap());
3119 target_value.append(&mut <Vec<u8>>::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap());
3120 target_value.append(&mut <Vec<u8>>::from_hex("00083a840000034d031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b").unwrap());
3122 target_value.append(&mut <Vec<u8>>::from_hex("0a00001400001e000028").unwrap());
3124 assert_eq!(encoded_value, target_value);
3128 fn encoding_channel_announcement() {
3129 do_encoding_channel_announcement(true, false);
3130 do_encoding_channel_announcement(false, true);
3131 do_encoding_channel_announcement(false, false);
3132 do_encoding_channel_announcement(true, true);
3135 fn do_encoding_node_announcement(unknown_features_bits: bool, ipv4: bool, ipv6: bool, onionv2: bool, onionv3: bool, hostname: bool, excess_address_data: bool, excess_data: bool) {
3136 let secp_ctx = Secp256k1::new();
3137 let (privkey_1, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3138 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
3139 let features = if unknown_features_bits {
3140 NodeFeatures::from_le_bytes(vec![0xFF, 0xFF])
3142 // Set to some features we may support
3143 NodeFeatures::from_le_bytes(vec![2 | 1 << 5])
3145 let mut addresses = Vec::new();
3147 addresses.push(SocketAddress::TcpIpV4 {
3148 addr: [255, 254, 253, 252],
3153 addresses.push(SocketAddress::TcpIpV6 {
3154 addr: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240],
3159 addresses.push(msgs::SocketAddress::OnionV2(
3160 [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 38, 7]
3164 addresses.push(msgs::SocketAddress::OnionV3 {
3165 ed25519_pubkey: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 237, 236, 235, 234, 233, 232, 231, 230, 229, 228, 227, 226, 225, 224],
3172 addresses.push(SocketAddress::Hostname {
3173 hostname: Hostname::try_from(String::from("host")).unwrap(),
3177 let mut addr_len = 0;
3178 for addr in &addresses {
3179 addr_len += addr.len() + 1;
3181 let unsigned_node_announcement = msgs::UnsignedNodeAnnouncement {
3183 timestamp: 20190119,
3184 node_id: NodeId::from_pubkey(&pubkey_1),
3186 alias: NodeAlias([16;32]),
3188 excess_address_data: if excess_address_data { vec![33, 108, 40, 11, 83, 149, 162, 84, 110, 126, 75, 38, 99, 224, 79, 129, 22, 34, 241, 90, 79, 146, 232, 58, 162, 233, 43, 162, 165, 115, 193, 57, 20, 44, 84, 174, 99, 7, 42, 30, 193, 238, 125, 192, 192, 75, 222, 92, 132, 120, 6, 23, 42, 160, 92, 146, 194, 42, 232, 227, 8, 209, 210, 105] } else { Vec::new() },
3189 excess_data: if excess_data { vec![59, 18, 204, 25, 92, 224, 162, 209, 189, 166, 168, 139, 239, 161, 159, 160, 127, 81, 202, 167, 92, 232, 56, 55, 242, 137, 101, 96, 11, 138, 172, 171, 8, 85, 255, 176, 231, 65, 236, 95, 124, 65, 66, 30, 152, 41, 169, 212, 134, 17, 200, 200, 49, 247, 27, 229, 234, 115, 230, 101, 148, 151, 127, 253] } else { Vec::new() },
3191 addr_len += unsigned_node_announcement.excess_address_data.len() as u16;
3192 let node_announcement = msgs::NodeAnnouncement {
3194 contents: unsigned_node_announcement,
3196 let encoded_value = node_announcement.encode();
3197 let mut target_value = <Vec<u8>>::from_hex("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
3198 if unknown_features_bits {
3199 target_value.append(&mut <Vec<u8>>::from_hex("0002ffff").unwrap());
3201 target_value.append(&mut <Vec<u8>>::from_hex("000122").unwrap());
3203 target_value.append(&mut <Vec<u8>>::from_hex("013413a7031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f2020201010101010101010101010101010101010101010101010101010101010101010").unwrap());
3204 target_value.append(&mut vec![(addr_len >> 8) as u8, addr_len as u8]);
3206 target_value.append(&mut <Vec<u8>>::from_hex("01fffefdfc2607").unwrap());
3209 target_value.append(&mut <Vec<u8>>::from_hex("02fffefdfcfbfaf9f8f7f6f5f4f3f2f1f02607").unwrap());
3212 target_value.append(&mut <Vec<u8>>::from_hex("03fffefdfcfbfaf9f8f7f62607").unwrap());
3215 target_value.append(&mut <Vec<u8>>::from_hex("04fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e00020102607").unwrap());
3218 target_value.append(&mut <Vec<u8>>::from_hex("0504686f73742607").unwrap());
3220 if excess_address_data {
3221 target_value.append(&mut <Vec<u8>>::from_hex("216c280b5395a2546e7e4b2663e04f811622f15a4f92e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d269").unwrap());
3224 target_value.append(&mut <Vec<u8>>::from_hex("3b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap());
3226 assert_eq!(encoded_value, target_value);
3230 fn encoding_node_announcement() {
3231 do_encoding_node_announcement(true, true, true, true, true, true, true, true);
3232 do_encoding_node_announcement(false, false, false, false, false, false, false, false);
3233 do_encoding_node_announcement(false, true, false, false, false, false, false, false);
3234 do_encoding_node_announcement(false, false, true, false, false, false, false, false);
3235 do_encoding_node_announcement(false, false, false, true, false, false, false, false);
3236 do_encoding_node_announcement(false, false, false, false, true, false, false, false);
3237 do_encoding_node_announcement(false, false, false, false, false, true, false, false);
3238 do_encoding_node_announcement(false, false, false, false, false, false, true, false);
3239 do_encoding_node_announcement(false, true, false, true, false, false, true, false);
3240 do_encoding_node_announcement(false, false, true, false, true, false, false, false);
3243 fn do_encoding_channel_update(direction: bool, disable: bool, excess_data: bool) {
3244 let secp_ctx = Secp256k1::new();
3245 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3246 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
3247 let unsigned_channel_update = msgs::UnsignedChannelUpdate {
3248 chain_hash: ChainHash::using_genesis_block(Network::Bitcoin),
3249 short_channel_id: 2316138423780173,
3250 timestamp: 20190119,
3251 flags: if direction { 1 } else { 0 } | if disable { 1 << 1 } else { 0 },
3252 cltv_expiry_delta: 144,
3253 htlc_minimum_msat: 1000000,
3254 htlc_maximum_msat: 131355275467161,
3255 fee_base_msat: 10000,
3256 fee_proportional_millionths: 20,
3257 excess_data: if excess_data { vec![0, 0, 0, 0, 59, 154, 202, 0] } else { Vec::new() }
3259 let channel_update = msgs::ChannelUpdate {
3261 contents: unsigned_channel_update
3263 let encoded_value = channel_update.encode();
3264 let mut target_value = <Vec<u8>>::from_hex("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
3265 target_value.append(&mut <Vec<u8>>::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap());
3266 target_value.append(&mut <Vec<u8>>::from_hex("00083a840000034d013413a7").unwrap());
3267 target_value.append(&mut <Vec<u8>>::from_hex("01").unwrap());
3268 target_value.append(&mut <Vec<u8>>::from_hex("00").unwrap());
3270 let flag = target_value.last_mut().unwrap();
3274 let flag = target_value.last_mut().unwrap();
3275 *flag = *flag | 1 << 1;
3277 target_value.append(&mut <Vec<u8>>::from_hex("009000000000000f42400000271000000014").unwrap());
3278 target_value.append(&mut <Vec<u8>>::from_hex("0000777788889999").unwrap());
3280 target_value.append(&mut <Vec<u8>>::from_hex("000000003b9aca00").unwrap());
3282 assert_eq!(encoded_value, target_value);
3286 fn encoding_channel_update() {
3287 do_encoding_channel_update(false, false, false);
3288 do_encoding_channel_update(false, false, true);
3289 do_encoding_channel_update(true, false, false);
3290 do_encoding_channel_update(true, false, true);
3291 do_encoding_channel_update(false, true, false);
3292 do_encoding_channel_update(false, true, true);
3293 do_encoding_channel_update(true, true, false);
3294 do_encoding_channel_update(true, true, true);
3297 fn do_encoding_open_channel(random_bit: bool, shutdown: bool, incl_chan_type: bool) {
3298 let secp_ctx = Secp256k1::new();
3299 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3300 let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
3301 let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
3302 let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
3303 let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
3304 let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
3305 let open_channel = msgs::OpenChannel {
3306 common_fields: CommonOpenChannelFields {
3307 chain_hash: ChainHash::using_genesis_block(Network::Bitcoin),
3308 temporary_channel_id: ChannelId::from_bytes([2; 32]),
3309 funding_satoshis: 1311768467284833366,
3310 dust_limit_satoshis: 3608586615801332854,
3311 max_htlc_value_in_flight_msat: 8517154655701053848,
3312 htlc_minimum_msat: 2316138423780173,
3313 commitment_feerate_sat_per_1000_weight: 821716,
3314 to_self_delay: 49340,
3315 max_accepted_htlcs: 49340,
3316 funding_pubkey: pubkey_1,
3317 revocation_basepoint: pubkey_2,
3318 payment_basepoint: pubkey_3,
3319 delayed_payment_basepoint: pubkey_4,
3320 htlc_basepoint: pubkey_5,
3321 first_per_commitment_point: pubkey_6,
3322 channel_flags: if random_bit { 1 << 5 } else { 0 },
3323 shutdown_scriptpubkey: if shutdown { Some(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, inner: pubkey_1}, Network::Testnet).script_pubkey()) } else { None },
3324 channel_type: if incl_chan_type { Some(ChannelTypeFeatures::empty()) } else { None },
3326 push_msat: 2536655962884945560,
3327 channel_reserve_satoshis: 8665828695742877976,
3329 let encoded_value = open_channel.encode();
3330 let mut target_value = Vec::new();
3331 target_value.append(&mut <Vec<u8>>::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap());
3332 target_value.append(&mut <Vec<u8>>::from_hex("02020202020202020202020202020202020202020202020202020202020202021234567890123456233403289122369832144668701144767633030896203198784335490624111800083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap());
3334 target_value.append(&mut <Vec<u8>>::from_hex("20").unwrap());
3336 target_value.append(&mut <Vec<u8>>::from_hex("00").unwrap());
3339 target_value.append(&mut <Vec<u8>>::from_hex("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
3342 target_value.append(&mut <Vec<u8>>::from_hex("0100").unwrap());
3344 assert_eq!(encoded_value, target_value);
3348 fn encoding_open_channel() {
3349 do_encoding_open_channel(false, false, false);
3350 do_encoding_open_channel(false, false, true);
3351 do_encoding_open_channel(false, true, false);
3352 do_encoding_open_channel(false, true, true);
3353 do_encoding_open_channel(true, false, false);
3354 do_encoding_open_channel(true, false, true);
3355 do_encoding_open_channel(true, true, false);
3356 do_encoding_open_channel(true, true, true);
3359 fn do_encoding_open_channelv2(random_bit: bool, shutdown: bool, incl_chan_type: bool, require_confirmed_inputs: bool) {
3360 let secp_ctx = Secp256k1::new();
3361 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3362 let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
3363 let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
3364 let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
3365 let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
3366 let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
3367 let (_, pubkey_7) = get_keys_from!("0707070707070707070707070707070707070707070707070707070707070707", secp_ctx);
3368 let open_channelv2 = msgs::OpenChannelV2 {
3369 common_fields: CommonOpenChannelFields {
3370 chain_hash: ChainHash::using_genesis_block(Network::Bitcoin),
3371 temporary_channel_id: ChannelId::from_bytes([2; 32]),
3372 commitment_feerate_sat_per_1000_weight: 821716,
3373 funding_satoshis: 1311768467284833366,
3374 dust_limit_satoshis: 3608586615801332854,
3375 max_htlc_value_in_flight_msat: 8517154655701053848,
3376 htlc_minimum_msat: 2316138423780173,
3377 to_self_delay: 49340,
3378 max_accepted_htlcs: 49340,
3379 funding_pubkey: pubkey_1,
3380 revocation_basepoint: pubkey_2,
3381 payment_basepoint: pubkey_3,
3382 delayed_payment_basepoint: pubkey_4,
3383 htlc_basepoint: pubkey_5,
3384 first_per_commitment_point: pubkey_6,
3385 channel_flags: if random_bit { 1 << 5 } else { 0 },
3386 shutdown_scriptpubkey: if shutdown { Some(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, inner: pubkey_1}, Network::Testnet).script_pubkey()) } else { None },
3387 channel_type: if incl_chan_type { Some(ChannelTypeFeatures::empty()) } else { None },
3389 funding_feerate_sat_per_1000_weight: 821716,
3390 locktime: 305419896,
3391 second_per_commitment_point: pubkey_7,
3392 require_confirmed_inputs: if require_confirmed_inputs { Some(()) } else { None },
3394 let encoded_value = open_channelv2.encode();
3395 let mut target_value = Vec::new();
3396 target_value.append(&mut <Vec<u8>>::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap());
3397 target_value.append(&mut <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202").unwrap());
3398 target_value.append(&mut <Vec<u8>>::from_hex("000c89d4").unwrap());
3399 target_value.append(&mut <Vec<u8>>::from_hex("000c89d4").unwrap());
3400 target_value.append(&mut <Vec<u8>>::from_hex("1234567890123456").unwrap());
3401 target_value.append(&mut <Vec<u8>>::from_hex("3214466870114476").unwrap());
3402 target_value.append(&mut <Vec<u8>>::from_hex("7633030896203198").unwrap());
3403 target_value.append(&mut <Vec<u8>>::from_hex("00083a840000034d").unwrap());
3404 target_value.append(&mut <Vec<u8>>::from_hex("c0bc").unwrap());
3405 target_value.append(&mut <Vec<u8>>::from_hex("c0bc").unwrap());
3406 target_value.append(&mut <Vec<u8>>::from_hex("12345678").unwrap());
3407 target_value.append(&mut <Vec<u8>>::from_hex("031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap());
3408 target_value.append(&mut <Vec<u8>>::from_hex("024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d0766").unwrap());
3409 target_value.append(&mut <Vec<u8>>::from_hex("02531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe337").unwrap());
3410 target_value.append(&mut <Vec<u8>>::from_hex("03462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b").unwrap());
3411 target_value.append(&mut <Vec<u8>>::from_hex("0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f7").unwrap());
3412 target_value.append(&mut <Vec<u8>>::from_hex("03f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap());
3413 target_value.append(&mut <Vec<u8>>::from_hex("02989c0b76cb563971fdc9bef31ec06c3560f3249d6ee9e5d83c57625596e05f6f").unwrap());
3416 target_value.append(&mut <Vec<u8>>::from_hex("20").unwrap());
3418 target_value.append(&mut <Vec<u8>>::from_hex("00").unwrap());
3421 target_value.append(&mut <Vec<u8>>::from_hex("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
3424 target_value.append(&mut <Vec<u8>>::from_hex("0100").unwrap());
3426 if require_confirmed_inputs {
3427 target_value.append(&mut <Vec<u8>>::from_hex("0200").unwrap());
3429 assert_eq!(encoded_value, target_value);
3433 fn encoding_open_channelv2() {
3434 do_encoding_open_channelv2(false, false, false, false);
3435 do_encoding_open_channelv2(false, false, false, true);
3436 do_encoding_open_channelv2(false, false, true, false);
3437 do_encoding_open_channelv2(false, false, true, true);
3438 do_encoding_open_channelv2(false, true, false, false);
3439 do_encoding_open_channelv2(false, true, false, true);
3440 do_encoding_open_channelv2(false, true, true, false);
3441 do_encoding_open_channelv2(false, true, true, true);
3442 do_encoding_open_channelv2(true, false, false, false);
3443 do_encoding_open_channelv2(true, false, false, true);
3444 do_encoding_open_channelv2(true, false, true, false);
3445 do_encoding_open_channelv2(true, false, true, true);
3446 do_encoding_open_channelv2(true, true, false, false);
3447 do_encoding_open_channelv2(true, true, false, true);
3448 do_encoding_open_channelv2(true, true, true, false);
3449 do_encoding_open_channelv2(true, true, true, true);
3452 fn do_encoding_accept_channel(shutdown: bool) {
3453 let secp_ctx = Secp256k1::new();
3454 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3455 let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
3456 let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
3457 let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
3458 let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
3459 let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
3460 let accept_channel = msgs::AcceptChannel {
3461 temporary_channel_id: ChannelId::from_bytes([2; 32]),
3462 dust_limit_satoshis: 1311768467284833366,
3463 max_htlc_value_in_flight_msat: 2536655962884945560,
3464 channel_reserve_satoshis: 3608586615801332854,
3465 htlc_minimum_msat: 2316138423780173,
3466 minimum_depth: 821716,
3467 to_self_delay: 49340,
3468 max_accepted_htlcs: 49340,
3469 funding_pubkey: pubkey_1,
3470 revocation_basepoint: pubkey_2,
3471 payment_point: pubkey_3,
3472 delayed_payment_basepoint: pubkey_4,
3473 htlc_basepoint: pubkey_5,
3474 first_per_commitment_point: pubkey_6,
3475 shutdown_scriptpubkey: if shutdown { Some(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, inner: pubkey_1}, Network::Testnet).script_pubkey()) } else { None },
3478 next_local_nonce: None,
3480 let encoded_value = accept_channel.encode();
3481 let mut target_value = <Vec<u8>>::from_hex("020202020202020202020202020202020202020202020202020202020202020212345678901234562334032891223698321446687011447600083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap();
3483 target_value.append(&mut <Vec<u8>>::from_hex("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
3485 assert_eq!(encoded_value, target_value);
3489 fn encoding_accept_channel() {
3490 do_encoding_accept_channel(false);
3491 do_encoding_accept_channel(true);
3494 fn do_encoding_accept_channelv2(shutdown: bool) {
3495 let secp_ctx = Secp256k1::new();
3496 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3497 let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
3498 let (_, pubkey_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
3499 let (_, pubkey_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
3500 let (_, pubkey_5) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505", secp_ctx);
3501 let (_, pubkey_6) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606", secp_ctx);
3502 let (_, pubkey_7) = get_keys_from!("0707070707070707070707070707070707070707070707070707070707070707", secp_ctx);
3503 let accept_channelv2 = msgs::AcceptChannelV2 {
3504 temporary_channel_id: ChannelId::from_bytes([2; 32]),
3505 funding_satoshis: 1311768467284833366,
3506 dust_limit_satoshis: 1311768467284833366,
3507 max_htlc_value_in_flight_msat: 2536655962884945560,
3508 htlc_minimum_msat: 2316138423780173,
3509 minimum_depth: 821716,
3510 to_self_delay: 49340,
3511 max_accepted_htlcs: 49340,
3512 funding_pubkey: pubkey_1,
3513 revocation_basepoint: pubkey_2,
3514 payment_basepoint: pubkey_3,
3515 delayed_payment_basepoint: pubkey_4,
3516 htlc_basepoint: pubkey_5,
3517 first_per_commitment_point: pubkey_6,
3518 second_per_commitment_point: pubkey_7,
3519 shutdown_scriptpubkey: if shutdown { Some(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, inner: pubkey_1}, Network::Testnet).script_pubkey()) } else { None },
3521 require_confirmed_inputs: None,
3523 let encoded_value = accept_channelv2.encode();
3524 let mut target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202").unwrap(); // temporary_channel_id
3525 target_value.append(&mut <Vec<u8>>::from_hex("1234567890123456").unwrap()); // funding_satoshis
3526 target_value.append(&mut <Vec<u8>>::from_hex("1234567890123456").unwrap()); // dust_limit_satoshis
3527 target_value.append(&mut <Vec<u8>>::from_hex("2334032891223698").unwrap()); // max_htlc_value_in_flight_msat
3528 target_value.append(&mut <Vec<u8>>::from_hex("00083a840000034d").unwrap()); // htlc_minimum_msat
3529 target_value.append(&mut <Vec<u8>>::from_hex("000c89d4").unwrap()); // minimum_depth
3530 target_value.append(&mut <Vec<u8>>::from_hex("c0bc").unwrap()); // to_self_delay
3531 target_value.append(&mut <Vec<u8>>::from_hex("c0bc").unwrap()); // max_accepted_htlcs
3532 target_value.append(&mut <Vec<u8>>::from_hex("031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap()); // funding_pubkey
3533 target_value.append(&mut <Vec<u8>>::from_hex("024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d0766").unwrap()); // revocation_basepoint
3534 target_value.append(&mut <Vec<u8>>::from_hex("02531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe337").unwrap()); // payment_basepoint
3535 target_value.append(&mut <Vec<u8>>::from_hex("03462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b").unwrap()); // delayed_payment_basepoint
3536 target_value.append(&mut <Vec<u8>>::from_hex("0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f7").unwrap()); // htlc_basepoint
3537 target_value.append(&mut <Vec<u8>>::from_hex("03f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap()); // first_per_commitment_point
3538 target_value.append(&mut <Vec<u8>>::from_hex("02989c0b76cb563971fdc9bef31ec06c3560f3249d6ee9e5d83c57625596e05f6f").unwrap()); // second_per_commitment_point
3540 target_value.append(&mut <Vec<u8>>::from_hex("001b").unwrap()); // Type 0 + Length 27
3541 target_value.append(&mut <Vec<u8>>::from_hex("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
3543 assert_eq!(encoded_value, target_value);
3547 fn encoding_accept_channelv2() {
3548 do_encoding_accept_channelv2(false);
3549 do_encoding_accept_channelv2(true);
3553 fn encoding_funding_created() {
3554 let secp_ctx = Secp256k1::new();
3555 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3556 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
3557 let funding_created = msgs::FundingCreated {
3558 temporary_channel_id: ChannelId::from_bytes([2; 32]),
3559 funding_txid: Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap(),
3560 funding_output_index: 255,
3563 partial_signature_with_nonce: None,
3565 next_local_nonce: None,
3567 let encoded_value = funding_created.encode();
3568 let target_value = <Vec<u8>>::from_hex("02020202020202020202020202020202020202020202020202020202020202026e96fe9f8b0ddcd729ba03cfafa5a27b050b39d354dd980814268dfa9a44d4c200ffd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
3569 assert_eq!(encoded_value, target_value);
3573 fn encoding_funding_signed() {
3574 let secp_ctx = Secp256k1::new();
3575 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3576 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
3577 let funding_signed = msgs::FundingSigned {
3578 channel_id: ChannelId::from_bytes([2; 32]),
3581 partial_signature_with_nonce: None,
3583 let encoded_value = funding_signed.encode();
3584 let target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
3585 assert_eq!(encoded_value, target_value);
3589 fn encoding_channel_ready() {
3590 let secp_ctx = Secp256k1::new();
3591 let (_, pubkey_1,) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3592 let channel_ready = msgs::ChannelReady {
3593 channel_id: ChannelId::from_bytes([2; 32]),
3594 next_per_commitment_point: pubkey_1,
3595 short_channel_id_alias: None,
3597 let encoded_value = channel_ready.encode();
3598 let target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
3599 assert_eq!(encoded_value, target_value);
3603 fn encoding_splice() {
3604 let secp_ctx = Secp256k1::new();
3605 let (_, pubkey_1,) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3606 let splice = msgs::Splice {
3607 chain_hash: ChainHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap(),
3608 channel_id: ChannelId::from_bytes([2; 32]),
3609 relative_satoshis: 123456,
3610 funding_feerate_perkw: 2000,
3612 funding_pubkey: pubkey_1,
3614 let encoded_value = splice.encode();
3615 assert_eq!(encoded_value.as_hex().to_string(), "02020202020202020202020202020202020202020202020202020202020202026fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000000000000001e240000007d000000000031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f");
3619 fn encoding_stfu() {
3620 let stfu = msgs::Stfu {
3621 channel_id: ChannelId::from_bytes([2; 32]),
3624 let encoded_value = stfu.encode();
3625 assert_eq!(encoded_value.as_hex().to_string(), "020202020202020202020202020202020202020202020202020202020202020201");
3629 fn encoding_splice_ack() {
3630 let secp_ctx = Secp256k1::new();
3631 let (_, pubkey_1,) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3632 let splice = msgs::SpliceAck {
3633 chain_hash: ChainHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap(),
3634 channel_id: ChannelId::from_bytes([2; 32]),
3635 relative_satoshis: 123456,
3636 funding_pubkey: pubkey_1,
3638 let encoded_value = splice.encode();
3639 assert_eq!(encoded_value.as_hex().to_string(), "02020202020202020202020202020202020202020202020202020202020202026fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000000000000001e240031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f");
3643 fn encoding_splice_locked() {
3644 let splice = msgs::SpliceLocked {
3645 channel_id: ChannelId::from_bytes([2; 32]),
3647 let encoded_value = splice.encode();
3648 assert_eq!(encoded_value.as_hex().to_string(), "0202020202020202020202020202020202020202020202020202020202020202");
3652 fn encoding_tx_add_input() {
3653 let tx_add_input = msgs::TxAddInput {
3654 channel_id: ChannelId::from_bytes([2; 32]),
3655 serial_id: 4886718345,
3656 prevtx: TransactionU16LenLimited::new(Transaction {
3658 lock_time: LockTime::ZERO,
3660 previous_output: OutPoint { txid: Txid::from_str("305bab643ee297b8b6b76b320792c8223d55082122cb606bf89382146ced9c77").unwrap(), index: 2 }.into_bitcoin_outpoint(),
3661 script_sig: ScriptBuf::new(),
3662 sequence: Sequence(0xfffffffd),
3663 witness: Witness::from_slice(&vec![
3664 <Vec<u8>>::from_hex("304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701").unwrap(),
3665 <Vec<u8>>::from_hex("0301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd6944").unwrap()]),
3670 script_pubkey: Address::from_str("bc1qzlffunw52jav8vwdu5x3jfk6sr8u22rmq3xzw2").unwrap().payload.script_pubkey(),
3674 script_pubkey: Address::from_str("bc1qxmk834g5marzm227dgqvynd23y2nvt2ztwcw2z").unwrap().payload.script_pubkey(),
3678 prevtx_out: 305419896,
3679 sequence: 305419896,
3681 let encoded_value = tx_add_input.encode();
3682 let target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202000000012345678900de02000000000101779ced6c148293f86b60cb222108553d22c89207326bb7b6b897e23e64ab5b300200000000fdffffff0236dbc1000000000016001417d29e4dd454bac3b1cde50d1926da80cfc5287b9cbd03000000000016001436ec78d514df462da95e6a00c24daa8915362d420247304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701210301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd6944000000001234567812345678").unwrap();
3683 assert_eq!(encoded_value, target_value);
3687 fn encoding_tx_add_output() {
3688 let tx_add_output = msgs::TxAddOutput {
3689 channel_id: ChannelId::from_bytes([2; 32]),
3690 serial_id: 4886718345,
3692 script: Address::from_str("bc1qxmk834g5marzm227dgqvynd23y2nvt2ztwcw2z").unwrap().payload.script_pubkey(),
3694 let encoded_value = tx_add_output.encode();
3695 let target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202000000012345678900000001234567890016001436ec78d514df462da95e6a00c24daa8915362d42").unwrap();
3696 assert_eq!(encoded_value, target_value);
3700 fn encoding_tx_remove_input() {
3701 let tx_remove_input = msgs::TxRemoveInput {
3702 channel_id: ChannelId::from_bytes([2; 32]),
3703 serial_id: 4886718345,
3705 let encoded_value = tx_remove_input.encode();
3706 let target_value = <Vec<u8>>::from_hex("02020202020202020202020202020202020202020202020202020202020202020000000123456789").unwrap();
3707 assert_eq!(encoded_value, target_value);
3711 fn encoding_tx_remove_output() {
3712 let tx_remove_output = msgs::TxRemoveOutput {
3713 channel_id: ChannelId::from_bytes([2; 32]),
3714 serial_id: 4886718345,
3716 let encoded_value = tx_remove_output.encode();
3717 let target_value = <Vec<u8>>::from_hex("02020202020202020202020202020202020202020202020202020202020202020000000123456789").unwrap();
3718 assert_eq!(encoded_value, target_value);
3722 fn encoding_tx_complete() {
3723 let tx_complete = msgs::TxComplete {
3724 channel_id: ChannelId::from_bytes([2; 32]),
3726 let encoded_value = tx_complete.encode();
3727 let target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202").unwrap();
3728 assert_eq!(encoded_value, target_value);
3732 fn encoding_tx_signatures() {
3733 let tx_signatures = msgs::TxSignatures {
3734 channel_id: ChannelId::from_bytes([2; 32]),
3735 tx_hash: Txid::from_str("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap(),
3737 Witness::from_slice(&vec![
3738 <Vec<u8>>::from_hex("304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701").unwrap(),
3739 <Vec<u8>>::from_hex("0301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd6944").unwrap()]),
3740 Witness::from_slice(&vec![
3741 <Vec<u8>>::from_hex("3045022100ee00dbf4a862463e837d7c08509de814d620e4d9830fa84818713e0fa358f145022021c3c7060c4d53fe84fd165d60208451108a778c13b92ca4c6bad439236126cc01").unwrap(),
3742 <Vec<u8>>::from_hex("028fbbf0b16f5ba5bcb5dd37cd4047ce6f726a21c06682f9ec2f52b057de1dbdb5").unwrap()]),
3745 let encoded_value = tx_signatures.encode();
3746 let mut target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202").unwrap(); // channel_id
3747 target_value.append(&mut <Vec<u8>>::from_hex("6e96fe9f8b0ddcd729ba03cfafa5a27b050b39d354dd980814268dfa9a44d4c2").unwrap()); // tx_hash (sha256) (big endian byte order)
3748 target_value.append(&mut <Vec<u8>>::from_hex("0002").unwrap()); // num_witnesses (u16)
3750 target_value.append(&mut <Vec<u8>>::from_hex("006b").unwrap()); // len of witness_data
3751 target_value.append(&mut <Vec<u8>>::from_hex("02").unwrap()); // num_witness_elements (VarInt)
3752 target_value.append(&mut <Vec<u8>>::from_hex("47").unwrap()); // len of witness element data (VarInt)
3753 target_value.append(&mut <Vec<u8>>::from_hex("304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701").unwrap());
3754 target_value.append(&mut <Vec<u8>>::from_hex("21").unwrap()); // len of witness element data (VarInt)
3755 target_value.append(&mut <Vec<u8>>::from_hex("0301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd6944").unwrap());
3757 target_value.append(&mut <Vec<u8>>::from_hex("006c").unwrap()); // len of witness_data
3758 target_value.append(&mut <Vec<u8>>::from_hex("02").unwrap()); // num_witness_elements (VarInt)
3759 target_value.append(&mut <Vec<u8>>::from_hex("48").unwrap()); // len of witness element data (VarInt)
3760 target_value.append(&mut <Vec<u8>>::from_hex("3045022100ee00dbf4a862463e837d7c08509de814d620e4d9830fa84818713e0fa358f145022021c3c7060c4d53fe84fd165d60208451108a778c13b92ca4c6bad439236126cc01").unwrap());
3761 target_value.append(&mut <Vec<u8>>::from_hex("21").unwrap()); // len of witness element data (VarInt)
3762 target_value.append(&mut <Vec<u8>>::from_hex("028fbbf0b16f5ba5bcb5dd37cd4047ce6f726a21c06682f9ec2f52b057de1dbdb5").unwrap());
3763 assert_eq!(encoded_value, target_value);
3766 fn do_encoding_tx_init_rbf(funding_value_with_hex_target: Option<(i64, &str)>) {
3767 let tx_init_rbf = msgs::TxInitRbf {
3768 channel_id: ChannelId::from_bytes([2; 32]),
3769 locktime: 305419896,
3770 feerate_sat_per_1000_weight: 20190119,
3771 funding_output_contribution: if let Some((value, _)) = funding_value_with_hex_target { Some(value) } else { None },
3773 let encoded_value = tx_init_rbf.encode();
3774 let mut target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202").unwrap(); // channel_id
3775 target_value.append(&mut <Vec<u8>>::from_hex("12345678").unwrap()); // locktime
3776 target_value.append(&mut <Vec<u8>>::from_hex("013413a7").unwrap()); // feerate_sat_per_1000_weight
3777 if let Some((_, target)) = funding_value_with_hex_target {
3778 target_value.push(0x00); // Type
3779 target_value.push(target.len() as u8 / 2); // Length
3780 target_value.append(&mut <Vec<u8>>::from_hex(target).unwrap()); // Value (i64)
3782 assert_eq!(encoded_value, target_value);
3786 fn encoding_tx_init_rbf() {
3787 do_encoding_tx_init_rbf(Some((1311768467284833366, "1234567890123456")));
3788 do_encoding_tx_init_rbf(Some((13117684672, "000000030DDFFBC0")));
3789 do_encoding_tx_init_rbf(None);
3792 fn do_encoding_tx_ack_rbf(funding_value_with_hex_target: Option<(i64, &str)>) {
3793 let tx_ack_rbf = msgs::TxAckRbf {
3794 channel_id: ChannelId::from_bytes([2; 32]),
3795 funding_output_contribution: if let Some((value, _)) = funding_value_with_hex_target { Some(value) } else { None },
3797 let encoded_value = tx_ack_rbf.encode();
3798 let mut target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202").unwrap();
3799 if let Some((_, target)) = funding_value_with_hex_target {
3800 target_value.push(0x00); // Type
3801 target_value.push(target.len() as u8 / 2); // Length
3802 target_value.append(&mut <Vec<u8>>::from_hex(target).unwrap()); // Value (i64)
3804 assert_eq!(encoded_value, target_value);
3808 fn encoding_tx_ack_rbf() {
3809 do_encoding_tx_ack_rbf(Some((1311768467284833366, "1234567890123456")));
3810 do_encoding_tx_ack_rbf(Some((13117684672, "000000030DDFFBC0")));
3811 do_encoding_tx_ack_rbf(None);
3815 fn encoding_tx_abort() {
3816 let tx_abort = msgs::TxAbort {
3817 channel_id: ChannelId::from_bytes([2; 32]),
3818 data: <Vec<u8>>::from_hex("54686520717569636B2062726F776E20666F78206A756D7073206F76657220746865206C617A7920646F672E").unwrap(),
3820 let encoded_value = tx_abort.encode();
3821 let target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202002C54686520717569636B2062726F776E20666F78206A756D7073206F76657220746865206C617A7920646F672E").unwrap();
3822 assert_eq!(encoded_value, target_value);
3825 fn do_encoding_shutdown(script_type: u8) {
3826 let secp_ctx = Secp256k1::new();
3827 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3828 let script = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
3829 let shutdown = msgs::Shutdown {
3830 channel_id: ChannelId::from_bytes([2; 32]),
3832 if script_type == 1 { Address::p2pkh(&::bitcoin::PublicKey{compressed: true, inner: pubkey_1}, Network::Testnet).script_pubkey() }
3833 else if script_type == 2 { Address::p2sh(&script, Network::Testnet).unwrap().script_pubkey() }
3834 else if script_type == 3 { Address::p2wpkh(&::bitcoin::PublicKey{compressed: true, inner: pubkey_1}, Network::Testnet).unwrap().script_pubkey() }
3835 else { Address::p2wsh(&script, Network::Testnet).script_pubkey() },
3837 let encoded_value = shutdown.encode();
3838 let mut target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202").unwrap();
3839 if script_type == 1 {
3840 target_value.append(&mut <Vec<u8>>::from_hex("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
3841 } else if script_type == 2 {
3842 target_value.append(&mut <Vec<u8>>::from_hex("0017a914da1745e9b549bd0bfa1a569971c77eba30cd5a4b87").unwrap());
3843 } else if script_type == 3 {
3844 target_value.append(&mut <Vec<u8>>::from_hex("0016001479b000887626b294a914501a4cd226b58b235983").unwrap());
3845 } else if script_type == 4 {
3846 target_value.append(&mut <Vec<u8>>::from_hex("002200204ae81572f06e1b88fd5ced7a1a000945432e83e1551e6f721ee9c00b8cc33260").unwrap());
3848 assert_eq!(encoded_value, target_value);
3852 fn encoding_shutdown() {
3853 do_encoding_shutdown(1);
3854 do_encoding_shutdown(2);
3855 do_encoding_shutdown(3);
3856 do_encoding_shutdown(4);
3860 fn encoding_closing_signed() {
3861 let secp_ctx = Secp256k1::new();
3862 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3863 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
3864 let closing_signed = msgs::ClosingSigned {
3865 channel_id: ChannelId::from_bytes([2; 32]),
3866 fee_satoshis: 2316138423780173,
3870 let encoded_value = closing_signed.encode();
3871 let target_value = <Vec<u8>>::from_hex("020202020202020202020202020202020202020202020202020202020202020200083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
3872 assert_eq!(encoded_value, target_value);
3873 assert_eq!(msgs::ClosingSigned::read(&mut Cursor::new(&target_value)).unwrap(), closing_signed);
3875 let closing_signed_with_range = msgs::ClosingSigned {
3876 channel_id: ChannelId::from_bytes([2; 32]),
3877 fee_satoshis: 2316138423780173,
3879 fee_range: Some(msgs::ClosingSignedFeeRange {
3880 min_fee_satoshis: 0xdeadbeef,
3881 max_fee_satoshis: 0x1badcafe01234567,
3884 let encoded_value_with_range = closing_signed_with_range.encode();
3885 let target_value_with_range = <Vec<u8>>::from_hex("020202020202020202020202020202020202020202020202020202020202020200083a840000034dd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a011000000000deadbeef1badcafe01234567").unwrap();
3886 assert_eq!(encoded_value_with_range, target_value_with_range);
3887 assert_eq!(msgs::ClosingSigned::read(&mut Cursor::new(&target_value_with_range)).unwrap(),
3888 closing_signed_with_range);
3892 fn encoding_update_add_htlc() {
3893 let secp_ctx = Secp256k1::new();
3894 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3895 let onion_routing_packet = msgs::OnionPacket {
3897 public_key: Ok(pubkey_1),
3898 hop_data: [1; 20*65],
3901 let update_add_htlc = msgs::UpdateAddHTLC {
3902 channel_id: ChannelId::from_bytes([2; 32]),
3903 htlc_id: 2316138423780173,
3904 amount_msat: 3608586615801332854,
3905 payment_hash: PaymentHash([1; 32]),
3906 cltv_expiry: 821716,
3907 onion_routing_packet,
3908 skimmed_fee_msat: None,
3909 blinding_point: None,
3911 let encoded_value = update_add_htlc.encode();
3912 let target_value = <Vec<u8>>::from_hex("020202020202020202020202020202020202020202020202020202020202020200083a840000034d32144668701144760101010101010101010101010101010101010101010101010101010101010101000c89d4ff031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202").unwrap();
3913 assert_eq!(encoded_value, target_value);
3917 fn encoding_update_fulfill_htlc() {
3918 let update_fulfill_htlc = msgs::UpdateFulfillHTLC {
3919 channel_id: ChannelId::from_bytes([2; 32]),
3920 htlc_id: 2316138423780173,
3921 payment_preimage: PaymentPreimage([1; 32]),
3923 let encoded_value = update_fulfill_htlc.encode();
3924 let target_value = <Vec<u8>>::from_hex("020202020202020202020202020202020202020202020202020202020202020200083a840000034d0101010101010101010101010101010101010101010101010101010101010101").unwrap();
3925 assert_eq!(encoded_value, target_value);
3929 fn encoding_update_fail_htlc() {
3930 let reason = OnionErrorPacket {
3931 data: [1; 32].to_vec(),
3933 let update_fail_htlc = msgs::UpdateFailHTLC {
3934 channel_id: ChannelId::from_bytes([2; 32]),
3935 htlc_id: 2316138423780173,
3938 let encoded_value = update_fail_htlc.encode();
3939 let target_value = <Vec<u8>>::from_hex("020202020202020202020202020202020202020202020202020202020202020200083a840000034d00200101010101010101010101010101010101010101010101010101010101010101").unwrap();
3940 assert_eq!(encoded_value, target_value);
3944 fn encoding_update_fail_malformed_htlc() {
3945 let update_fail_malformed_htlc = msgs::UpdateFailMalformedHTLC {
3946 channel_id: ChannelId::from_bytes([2; 32]),
3947 htlc_id: 2316138423780173,
3948 sha256_of_onion: [1; 32],
3951 let encoded_value = update_fail_malformed_htlc.encode();
3952 let target_value = <Vec<u8>>::from_hex("020202020202020202020202020202020202020202020202020202020202020200083a840000034d010101010101010101010101010101010101010101010101010101010101010100ff").unwrap();
3953 assert_eq!(encoded_value, target_value);
3956 fn do_encoding_commitment_signed(htlcs: bool) {
3957 let secp_ctx = Secp256k1::new();
3958 let (privkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3959 let (privkey_2, _) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
3960 let (privkey_3, _) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303", secp_ctx);
3961 let (privkey_4, _) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404", secp_ctx);
3962 let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
3963 let sig_2 = get_sig_on!(privkey_2, secp_ctx, String::from("01010101010101010101010101010101"));
3964 let sig_3 = get_sig_on!(privkey_3, secp_ctx, String::from("01010101010101010101010101010101"));
3965 let sig_4 = get_sig_on!(privkey_4, secp_ctx, String::from("01010101010101010101010101010101"));
3966 let commitment_signed = msgs::CommitmentSigned {
3967 channel_id: ChannelId::from_bytes([2; 32]),
3969 htlc_signatures: if htlcs { vec![sig_2, sig_3, sig_4] } else { Vec::new() },
3971 partial_signature_with_nonce: None,
3973 let encoded_value = commitment_signed.encode();
3974 let mut target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
3976 target_value.append(&mut <Vec<u8>>::from_hex("00031735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd").unwrap());
3978 target_value.append(&mut <Vec<u8>>::from_hex("0000").unwrap());
3980 assert_eq!(encoded_value, target_value);
3984 fn encoding_commitment_signed() {
3985 do_encoding_commitment_signed(true);
3986 do_encoding_commitment_signed(false);
3990 fn encoding_revoke_and_ack() {
3991 let secp_ctx = Secp256k1::new();
3992 let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3993 let raa = msgs::RevokeAndACK {
3994 channel_id: ChannelId::from_bytes([2; 32]),
3995 per_commitment_secret: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
3996 next_per_commitment_point: pubkey_1,
3998 next_local_nonce: None,
4000 let encoded_value = raa.encode();
4001 let target_value = <Vec<u8>>::from_hex("02020202020202020202020202020202020202020202020202020202020202020101010101010101010101010101010101010101010101010101010101010101031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
4002 assert_eq!(encoded_value, target_value);
4006 fn encoding_update_fee() {
4007 let update_fee = msgs::UpdateFee {
4008 channel_id: ChannelId::from_bytes([2; 32]),
4009 feerate_per_kw: 20190119,
4011 let encoded_value = update_fee.encode();
4012 let target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202013413a7").unwrap();
4013 assert_eq!(encoded_value, target_value);
4017 fn encoding_init() {
4018 let mainnet_hash = ChainHash::using_genesis_block(Network::Bitcoin);
4019 assert_eq!(msgs::Init {
4020 features: InitFeatures::from_le_bytes(vec![0xFF, 0xFF, 0xFF]),
4021 networks: Some(vec![mainnet_hash]),
4022 remote_network_address: None,
4023 }.encode(), <Vec<u8>>::from_hex("00023fff0003ffffff01206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap());
4024 assert_eq!(msgs::Init {
4025 features: InitFeatures::from_le_bytes(vec![0xFF]),
4027 remote_network_address: None,
4028 }.encode(), <Vec<u8>>::from_hex("0001ff0001ff").unwrap());
4029 assert_eq!(msgs::Init {
4030 features: InitFeatures::from_le_bytes(vec![]),
4031 networks: Some(vec![mainnet_hash]),
4032 remote_network_address: None,
4033 }.encode(), <Vec<u8>>::from_hex("0000000001206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap());
4034 assert_eq!(msgs::Init {
4035 features: InitFeatures::from_le_bytes(vec![]),
4036 networks: Some(vec![ChainHash::from(&[1; 32]), ChainHash::from(&[2; 32])]),
4037 remote_network_address: None,
4038 }.encode(), <Vec<u8>>::from_hex("00000000014001010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202").unwrap());
4039 let init_msg = msgs::Init { features: InitFeatures::from_le_bytes(vec![]),
4040 networks: Some(vec![mainnet_hash]),
4041 remote_network_address: Some(SocketAddress::TcpIpV4 {
4042 addr: [127, 0, 0, 1],
4046 let encoded_value = init_msg.encode();
4047 let target_value = <Vec<u8>>::from_hex("0000000001206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d61900000000000307017f00000103e8").unwrap();
4048 assert_eq!(encoded_value, target_value);
4049 assert_eq!(msgs::Init::read(&mut Cursor::new(&target_value)).unwrap(), init_msg);
4053 fn encoding_error() {
4054 let error = msgs::ErrorMessage {
4055 channel_id: ChannelId::from_bytes([2; 32]),
4056 data: String::from("rust-lightning"),
4058 let encoded_value = error.encode();
4059 let target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202000e727573742d6c696768746e696e67").unwrap();
4060 assert_eq!(encoded_value, target_value);
4064 fn encoding_warning() {
4065 let error = msgs::WarningMessage {
4066 channel_id: ChannelId::from_bytes([2; 32]),
4067 data: String::from("rust-lightning"),
4069 let encoded_value = error.encode();
4070 let target_value = <Vec<u8>>::from_hex("0202020202020202020202020202020202020202020202020202020202020202000e727573742d6c696768746e696e67").unwrap();
4071 assert_eq!(encoded_value, target_value);
4075 fn encoding_ping() {
4076 let ping = msgs::Ping {
4080 let encoded_value = ping.encode();
4081 let target_value = <Vec<u8>>::from_hex("0040004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
4082 assert_eq!(encoded_value, target_value);
4086 fn encoding_pong() {
4087 let pong = msgs::Pong {
4090 let encoded_value = pong.encode();
4091 let target_value = <Vec<u8>>::from_hex("004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
4092 assert_eq!(encoded_value, target_value);
4096 fn encoding_nonfinal_onion_hop_data() {
4097 let outbound_msg = msgs::OutboundOnionPayload::Forward {
4098 short_channel_id: 0xdeadbeef1bad1dea,
4099 amt_to_forward: 0x0badf00d01020304,
4100 outgoing_cltv_value: 0xffffffff,
4102 let encoded_value = outbound_msg.encode();
4103 let target_value = <Vec<u8>>::from_hex("1a02080badf00d010203040404ffffffff0608deadbeef1bad1dea").unwrap();
4104 assert_eq!(encoded_value, target_value);
4106 let node_signer = test_utils::TestKeysInterface::new(&[42; 32], Network::Testnet);
4107 let inbound_msg = ReadableArgs::read(&mut Cursor::new(&target_value[..]), (None, &&node_signer)).unwrap();
4108 if let msgs::InboundOnionPayload::Forward {
4109 short_channel_id, amt_to_forward, outgoing_cltv_value
4111 assert_eq!(short_channel_id, 0xdeadbeef1bad1dea);
4112 assert_eq!(amt_to_forward, 0x0badf00d01020304);
4113 assert_eq!(outgoing_cltv_value, 0xffffffff);
4114 } else { panic!(); }
4118 fn encoding_final_onion_hop_data() {
4119 let outbound_msg = msgs::OutboundOnionPayload::Receive {
4121 payment_metadata: None,
4122 keysend_preimage: None,
4123 sender_intended_htlc_amt_msat: 0x0badf00d01020304,
4124 cltv_expiry_height: 0xffffffff,
4125 custom_tlvs: vec![],
4127 let encoded_value = outbound_msg.encode();
4128 let target_value = <Vec<u8>>::from_hex("1002080badf00d010203040404ffffffff").unwrap();
4129 assert_eq!(encoded_value, target_value);
4131 let node_signer = test_utils::TestKeysInterface::new(&[42; 32], Network::Testnet);
4132 let inbound_msg = ReadableArgs::read(&mut Cursor::new(&target_value[..]), (None, &&node_signer)).unwrap();
4133 if let msgs::InboundOnionPayload::Receive {
4134 payment_data: None, sender_intended_htlc_amt_msat, cltv_expiry_height, ..
4136 assert_eq!(sender_intended_htlc_amt_msat, 0x0badf00d01020304);
4137 assert_eq!(cltv_expiry_height, 0xffffffff);
4138 } else { panic!(); }
4142 fn encoding_final_onion_hop_data_with_secret() {
4143 let expected_payment_secret = PaymentSecret([0x42u8; 32]);
4144 let outbound_msg = msgs::OutboundOnionPayload::Receive {
4145 payment_data: Some(FinalOnionHopData {
4146 payment_secret: expected_payment_secret,
4147 total_msat: 0x1badca1f
4149 payment_metadata: None,
4150 keysend_preimage: None,
4151 sender_intended_htlc_amt_msat: 0x0badf00d01020304,
4152 cltv_expiry_height: 0xffffffff,
4153 custom_tlvs: vec![],
4155 let encoded_value = outbound_msg.encode();
4156 let target_value = <Vec<u8>>::from_hex("3602080badf00d010203040404ffffffff082442424242424242424242424242424242424242424242424242424242424242421badca1f").unwrap();
4157 assert_eq!(encoded_value, target_value);
4159 let node_signer = test_utils::TestKeysInterface::new(&[42; 32], Network::Testnet);
4160 let inbound_msg = ReadableArgs::read(&mut Cursor::new(&target_value[..]), (None, &&node_signer)).unwrap();
4161 if let msgs::InboundOnionPayload::Receive {
4162 payment_data: Some(FinalOnionHopData {
4164 total_msat: 0x1badca1f
4166 sender_intended_htlc_amt_msat, cltv_expiry_height,
4167 payment_metadata: None,
4168 keysend_preimage: None,
4171 assert_eq!(payment_secret, expected_payment_secret);
4172 assert_eq!(sender_intended_htlc_amt_msat, 0x0badf00d01020304);
4173 assert_eq!(cltv_expiry_height, 0xffffffff);
4174 assert_eq!(custom_tlvs, vec![]);
4175 } else { panic!(); }
4179 fn encoding_final_onion_hop_data_with_bad_custom_tlvs() {
4180 // If custom TLVs have type number within the range reserved for protocol, treat them as if
4182 let bad_type_range_tlvs = vec![
4183 ((1 << 16) - 4, vec![42]),
4184 ((1 << 16) - 2, vec![42; 32]),
4186 let mut msg = msgs::OutboundOnionPayload::Receive {
4188 payment_metadata: None,
4189 keysend_preimage: None,
4190 custom_tlvs: bad_type_range_tlvs,
4191 sender_intended_htlc_amt_msat: 0x0badf00d01020304,
4192 cltv_expiry_height: 0xffffffff,
4194 let encoded_value = msg.encode();
4195 let node_signer = test_utils::TestKeysInterface::new(&[42; 32], Network::Testnet);
4196 assert!(msgs::InboundOnionPayload::read(&mut Cursor::new(&encoded_value[..]), (None, &&node_signer)).is_err());
4197 let good_type_range_tlvs = vec![
4198 ((1 << 16) - 3, vec![42]),
4199 ((1 << 16) - 1, vec![42; 32]),
4201 if let msgs::OutboundOnionPayload::Receive { ref mut custom_tlvs, .. } = msg {
4202 *custom_tlvs = good_type_range_tlvs.clone();
4204 let encoded_value = msg.encode();
4205 let inbound_msg = ReadableArgs::read(&mut Cursor::new(&encoded_value[..]), (None, &&node_signer)).unwrap();
4207 msgs::InboundOnionPayload::Receive { custom_tlvs, .. } => assert!(custom_tlvs.is_empty()),
4213 fn encoding_final_onion_hop_data_with_custom_tlvs() {
4214 let expected_custom_tlvs = vec![
4215 (5482373483, vec![0x12, 0x34]),
4216 (5482373487, vec![0x42u8; 8]),
4218 let msg = msgs::OutboundOnionPayload::Receive {
4220 payment_metadata: None,
4221 keysend_preimage: None,
4222 custom_tlvs: expected_custom_tlvs.clone(),
4223 sender_intended_htlc_amt_msat: 0x0badf00d01020304,
4224 cltv_expiry_height: 0xffffffff,
4226 let encoded_value = msg.encode();
4227 let target_value = <Vec<u8>>::from_hex("2e02080badf00d010203040404ffffffffff0000000146c6616b021234ff0000000146c6616f084242424242424242").unwrap();
4228 assert_eq!(encoded_value, target_value);
4229 let node_signer = test_utils::TestKeysInterface::new(&[42; 32], Network::Testnet);
4230 let inbound_msg: msgs::InboundOnionPayload = ReadableArgs::read(&mut Cursor::new(&target_value[..]), (None, &&node_signer)).unwrap();
4231 if let msgs::InboundOnionPayload::Receive {
4233 payment_metadata: None,
4234 keysend_preimage: None,
4236 sender_intended_htlc_amt_msat,
4237 cltv_expiry_height: outgoing_cltv_value,
4240 assert_eq!(custom_tlvs, expected_custom_tlvs);
4241 assert_eq!(sender_intended_htlc_amt_msat, 0x0badf00d01020304);
4242 assert_eq!(outgoing_cltv_value, 0xffffffff);
4243 } else { panic!(); }
4247 fn query_channel_range_end_blocknum() {
4248 let tests: Vec<(u32, u32, u32)> = vec![
4249 (10000, 1500, 11500),
4250 (0, 0xffffffff, 0xffffffff),
4251 (1, 0xffffffff, 0xffffffff),
4254 for (first_blocknum, number_of_blocks, expected) in tests.into_iter() {
4255 let sut = msgs::QueryChannelRange {
4256 chain_hash: ChainHash::using_genesis_block(Network::Regtest),
4260 assert_eq!(sut.end_blocknum(), expected);
4265 fn encoding_query_channel_range() {
4266 let mut query_channel_range = msgs::QueryChannelRange {
4267 chain_hash: ChainHash::using_genesis_block(Network::Regtest),
4268 first_blocknum: 100000,
4269 number_of_blocks: 1500,
4271 let encoded_value = query_channel_range.encode();
4272 let target_value = <Vec<u8>>::from_hex("06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f000186a0000005dc").unwrap();
4273 assert_eq!(encoded_value, target_value);
4275 query_channel_range = Readable::read(&mut Cursor::new(&target_value[..])).unwrap();
4276 assert_eq!(query_channel_range.first_blocknum, 100000);
4277 assert_eq!(query_channel_range.number_of_blocks, 1500);
4281 fn encoding_reply_channel_range() {
4282 do_encoding_reply_channel_range(0);
4283 do_encoding_reply_channel_range(1);
4286 fn do_encoding_reply_channel_range(encoding_type: u8) {
4287 let mut target_value = <Vec<u8>>::from_hex("06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f000b8a06000005dc01").unwrap();
4288 let expected_chain_hash = ChainHash::using_genesis_block(Network::Regtest);
4289 let mut reply_channel_range = msgs::ReplyChannelRange {
4290 chain_hash: expected_chain_hash,
4291 first_blocknum: 756230,
4292 number_of_blocks: 1500,
4293 sync_complete: true,
4294 short_channel_ids: vec![0x000000000000008e, 0x0000000000003c69, 0x000000000045a6c4],
4297 if encoding_type == 0 {
4298 target_value.append(&mut <Vec<u8>>::from_hex("001900000000000000008e0000000000003c69000000000045a6c4").unwrap());
4299 let encoded_value = reply_channel_range.encode();
4300 assert_eq!(encoded_value, target_value);
4302 reply_channel_range = Readable::read(&mut Cursor::new(&target_value[..])).unwrap();
4303 assert_eq!(reply_channel_range.chain_hash, expected_chain_hash);
4304 assert_eq!(reply_channel_range.first_blocknum, 756230);
4305 assert_eq!(reply_channel_range.number_of_blocks, 1500);
4306 assert_eq!(reply_channel_range.sync_complete, true);
4307 assert_eq!(reply_channel_range.short_channel_ids[0], 0x000000000000008e);
4308 assert_eq!(reply_channel_range.short_channel_ids[1], 0x0000000000003c69);
4309 assert_eq!(reply_channel_range.short_channel_ids[2], 0x000000000045a6c4);
4311 target_value.append(&mut <Vec<u8>>::from_hex("001601789c636000833e08659309a65878be010010a9023a").unwrap());
4312 let result: Result<msgs::ReplyChannelRange, msgs::DecodeError> = Readable::read(&mut Cursor::new(&target_value[..]));
4313 assert!(result.is_err(), "Expected decode failure with unsupported zlib encoding");
4318 fn encoding_query_short_channel_ids() {
4319 do_encoding_query_short_channel_ids(0);
4320 do_encoding_query_short_channel_ids(1);
4323 fn do_encoding_query_short_channel_ids(encoding_type: u8) {
4324 let mut target_value = <Vec<u8>>::from_hex("06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f").unwrap();
4325 let expected_chain_hash = ChainHash::using_genesis_block(Network::Regtest);
4326 let mut query_short_channel_ids = msgs::QueryShortChannelIds {
4327 chain_hash: expected_chain_hash,
4328 short_channel_ids: vec![0x0000000000008e, 0x0000000000003c69, 0x000000000045a6c4],
4331 if encoding_type == 0 {
4332 target_value.append(&mut <Vec<u8>>::from_hex("001900000000000000008e0000000000003c69000000000045a6c4").unwrap());
4333 let encoded_value = query_short_channel_ids.encode();
4334 assert_eq!(encoded_value, target_value);
4336 query_short_channel_ids = Readable::read(&mut Cursor::new(&target_value[..])).unwrap();
4337 assert_eq!(query_short_channel_ids.chain_hash, expected_chain_hash);
4338 assert_eq!(query_short_channel_ids.short_channel_ids[0], 0x000000000000008e);
4339 assert_eq!(query_short_channel_ids.short_channel_ids[1], 0x0000000000003c69);
4340 assert_eq!(query_short_channel_ids.short_channel_ids[2], 0x000000000045a6c4);
4342 target_value.append(&mut <Vec<u8>>::from_hex("001601789c636000833e08659309a65878be010010a9023a").unwrap());
4343 let result: Result<msgs::QueryShortChannelIds, msgs::DecodeError> = Readable::read(&mut Cursor::new(&target_value[..]));
4344 assert!(result.is_err(), "Expected decode failure with unsupported zlib encoding");
4349 fn encoding_reply_short_channel_ids_end() {
4350 let expected_chain_hash = ChainHash::using_genesis_block(Network::Regtest);
4351 let mut reply_short_channel_ids_end = msgs::ReplyShortChannelIdsEnd {
4352 chain_hash: expected_chain_hash,
4353 full_information: true,
4355 let encoded_value = reply_short_channel_ids_end.encode();
4356 let target_value = <Vec<u8>>::from_hex("06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f01").unwrap();
4357 assert_eq!(encoded_value, target_value);
4359 reply_short_channel_ids_end = Readable::read(&mut Cursor::new(&target_value[..])).unwrap();
4360 assert_eq!(reply_short_channel_ids_end.chain_hash, expected_chain_hash);
4361 assert_eq!(reply_short_channel_ids_end.full_information, true);
4365 fn encoding_gossip_timestamp_filter(){
4366 let expected_chain_hash = ChainHash::using_genesis_block(Network::Regtest);
4367 let mut gossip_timestamp_filter = msgs::GossipTimestampFilter {
4368 chain_hash: expected_chain_hash,
4369 first_timestamp: 1590000000,
4370 timestamp_range: 0xffff_ffff,
4372 let encoded_value = gossip_timestamp_filter.encode();
4373 let target_value = <Vec<u8>>::from_hex("06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f5ec57980ffffffff").unwrap();
4374 assert_eq!(encoded_value, target_value);
4376 gossip_timestamp_filter = Readable::read(&mut Cursor::new(&target_value[..])).unwrap();
4377 assert_eq!(gossip_timestamp_filter.chain_hash, expected_chain_hash);
4378 assert_eq!(gossip_timestamp_filter.first_timestamp, 1590000000);
4379 assert_eq!(gossip_timestamp_filter.timestamp_range, 0xffff_ffff);
4383 fn decode_onion_hop_data_len_as_bigsize() {
4384 // Tests that we can decode an onion payload that is >253 bytes.
4385 // Previously, receiving a payload of this size could've caused us to fail to decode a valid
4386 // payload, because we were decoding the length (a BigSize, big-endian) as a VarInt
4389 // Encode a test onion payload with a big custom TLV such that it's >253 bytes, forcing the
4390 // payload length to be encoded over multiple bytes rather than a single u8.
4391 let big_payload = encode_big_payload().unwrap();
4392 let mut rd = Cursor::new(&big_payload[..]);
4394 let node_signer = test_utils::TestKeysInterface::new(&[42; 32], Network::Testnet);
4395 <msgs::InboundOnionPayload as ReadableArgs<(Option<PublicKey>, &&test_utils::TestKeysInterface)>>
4396 ::read(&mut rd, (None, &&node_signer)).unwrap();
4398 // see above test, needs to be a separate method for use of the serialization macros.
4399 fn encode_big_payload() -> Result<Vec<u8>, io::Error> {
4400 use crate::util::ser::HighZeroBytesDroppedBigSize;
4401 let payload = msgs::OutboundOnionPayload::Forward {
4402 short_channel_id: 0xdeadbeef1bad1dea,
4403 amt_to_forward: 1000,
4404 outgoing_cltv_value: 0xffffffff,
4406 let mut encoded_payload = Vec::new();
4407 let test_bytes = vec![42u8; 1000];
4408 if let msgs::OutboundOnionPayload::Forward { short_channel_id, amt_to_forward, outgoing_cltv_value } = payload {
4409 _encode_varint_length_prefixed_tlv!(&mut encoded_payload, {
4410 (1, test_bytes, required_vec),
4411 (2, HighZeroBytesDroppedBigSize(amt_to_forward), required),
4412 (4, HighZeroBytesDroppedBigSize(outgoing_cltv_value), required),
4413 (6, short_channel_id, required)
4420 #[cfg(feature = "std")]
4421 fn test_socket_address_from_str() {
4422 let tcpip_v4 = SocketAddress::TcpIpV4 {
4423 addr: Ipv4Addr::new(127, 0, 0, 1).octets(),
4426 assert_eq!(tcpip_v4, SocketAddress::from_str("127.0.0.1:1234").unwrap());
4427 assert_eq!(tcpip_v4, SocketAddress::from_str(&tcpip_v4.to_string()).unwrap());
4429 let tcpip_v6 = SocketAddress::TcpIpV6 {
4430 addr: Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).octets(),
4433 assert_eq!(tcpip_v6, SocketAddress::from_str("[0:0:0:0:0:0:0:1]:1234").unwrap());
4434 assert_eq!(tcpip_v6, SocketAddress::from_str(&tcpip_v6.to_string()).unwrap());
4436 let hostname = SocketAddress::Hostname {
4437 hostname: Hostname::try_from("lightning-node.mydomain.com".to_string()).unwrap(),
4440 assert_eq!(hostname, SocketAddress::from_str("lightning-node.mydomain.com:1234").unwrap());
4441 assert_eq!(hostname, SocketAddress::from_str(&hostname.to_string()).unwrap());
4443 let onion_v2 = SocketAddress::OnionV2 ([40, 4, 64, 185, 202, 19, 162, 75, 90, 200, 38, 7],);
4444 assert_eq!("OnionV2([40, 4, 64, 185, 202, 19, 162, 75, 90, 200, 38, 7])", &onion_v2.to_string());
4445 assert_eq!(Err(SocketAddressParseError::InvalidOnionV3), SocketAddress::from_str("FACEBOOKCOREWWWI.onion:9735"));
4447 let onion_v3 = SocketAddress::OnionV3 {
4448 ed25519_pubkey: [37, 24, 75, 5, 25, 73, 117, 194, 139, 102, 182, 107, 4, 105, 247, 246, 85,
4449 111, 177, 172, 49, 137, 167, 155, 64, 221, 163, 47, 31, 33, 71, 3],
4454 assert_eq!(onion_v3, SocketAddress::from_str("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion:1234").unwrap());
4455 assert_eq!(onion_v3, SocketAddress::from_str(&onion_v3.to_string()).unwrap());
4457 assert_eq!(Err(SocketAddressParseError::InvalidOnionV3), SocketAddress::from_str("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6.onion:1234"));
4458 assert_eq!(Err(SocketAddressParseError::InvalidInput), SocketAddress::from_str("127.0.0.1@1234"));
4459 assert_eq!(Err(SocketAddressParseError::InvalidInput), "".parse::<SocketAddress>());
4460 assert!(SocketAddress::from_str("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion.onion:9735:94").is_err());
4461 assert!(SocketAddress::from_str("wrong$%#.com:1234").is_err());
4462 assert_eq!(Err(SocketAddressParseError::InvalidPort), SocketAddress::from_str("example.com:wrong"));
4463 assert!("localhost".parse::<SocketAddress>().is_err());
4464 assert!("localhost:invalid-port".parse::<SocketAddress>().is_err());
4465 assert!( "invalid-onion-v3-hostname.onion:8080".parse::<SocketAddress>().is_err());
4466 assert!("b32.example.onion:invalid-port".parse::<SocketAddress>().is_err());
4467 assert!("invalid-address".parse::<SocketAddress>().is_err());
4468 assert!(SocketAddress::from_str("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion.onion:1234").is_err());
4472 #[cfg(feature = "std")]
4473 fn test_socket_address_to_socket_addrs() {
4474 assert_eq!(SocketAddress::TcpIpV4 {addr:[0u8; 4], port: 1337,}.to_socket_addrs().unwrap().next().unwrap(),
4475 SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0,0,0,0), 1337)));
4476 assert_eq!(SocketAddress::TcpIpV6 {addr:[0u8; 16], port: 1337,}.to_socket_addrs().unwrap().next().unwrap(),
4477 SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::from([0u8; 16]), 1337, 0, 0)));
4478 assert_eq!(SocketAddress::Hostname { hostname: Hostname::try_from("0.0.0.0".to_string()).unwrap(), port: 0 }
4479 .to_socket_addrs().unwrap().next().unwrap(), SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::from([0u8; 4]),0)));
4480 assert!(SocketAddress::OnionV2([0u8; 12]).to_socket_addrs().is_err());
4481 assert!(SocketAddress::OnionV3{ ed25519_pubkey: [37, 24, 75, 5, 25, 73, 117, 194, 139, 102,
4482 182, 107, 4, 105, 247, 246, 85, 111, 177, 172, 49, 137, 167, 155, 64, 221, 163, 47, 31,
4486 port: 1234 }.to_socket_addrs().is_err());