lnp2p 0.9.2

LN P2P Library: rust implementation of lightning network peer protocols (BOLT and Bifrost)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// LNP P2P library, plmeneting both bolt (BOLT) and Bifrost P2P messaging
// system for Lightning network protocol (LNP)
//
// Written in 2020-2021 by
//     Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

use std::borrow::Borrow;
use std::fmt::{self, Display, Formatter};
use std::io::{Read, Write};
use std::str::FromStr;

use amplify::Wrapper;
use bitcoin::bech32::{self, FromBase32, ToBase32};
use bitcoin::hashes::{sha256, sha256t, Hash, HashEngine};
use bitcoin::secp256k1::XOnlyPublicKey;
use strict_encoding::net::{
    AddrFormat, DecodeError, RawAddr, Transport, Uniform, UniformAddr, ADDR_LEN,
};
use strict_encoding::{self, StrictDecode, StrictEncode};

use crate::bifrost::ChannelParams;

// SHA256("bifrost:channel")
const CHANNEL_ID_MIDSTATE: [u8; 32] = [
    0, 79, 153, 191, 3, 7, 224, 35, 234, 47, 114, 213, 138, 22, 15, 17, 27,
    122, 131, 124, 85, 22, 92, 114, 24, 218, 119, 230, 233, 173, 106, 218,
];

/// Bech32m prefix for channel id encoding
pub const CHANNEL_BECH32_HRP: &str = "lnch";

/// Tag used for [`ChannelId`] hash type
pub struct ChannelIdTag;

impl sha256t::Tag for ChannelIdTag {
    #[inline]
    fn engine() -> sha256::HashEngine {
        let midstate = sha256::Midstate::from_inner(CHANNEL_ID_MIDSTATE);
        sha256::HashEngine::from_midstate(midstate, 64)
    }
}

/// A channel identifier
///
/// Represents commitment to the channel parameters and channel coordinator
/// node; any two distinct channels are guaranteed (with SHA256 collision
/// resistance level) to have a distinct channel ids.
#[derive(
    Wrapper, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, From
)]
#[wrapper(Debug, LowerHex, BorrowSlice)]
#[wrapper(Index, IndexRange, IndexFrom, IndexTo, IndexFull)]
pub struct ChannelId(sha256t::Hash<ChannelIdTag>);

impl ChannelId {
    /// Computes ChannelId from the provided data
    pub fn with(
        params: ChannelParams,
        node_pubkey: XOnlyPublicKey,
    ) -> ChannelId {
        let mut engine = ChannelId::engine();
        params
            .strict_encode(&mut engine)
            .expect("memory encoding of channel parameters");
        engine.input(&node_pubkey.serialize());
        ChannelId::from_engine(engine)
    }

    /// Constructs library id from a binary representation of the hash data
    #[inline]
    pub fn from_bytes(array: [u8; ChannelId::LEN]) -> ChannelId {
        ChannelId(sha256t::Hash::<ChannelIdTag>::from_inner(array))
    }

    /// Returns fixed-size array of inner representation of the library id
    #[inline]
    pub fn as_bytes(&self) -> &[u8; 32] {
        self.0.as_inner()
    }
}

impl Hash for ChannelId {
    type Engine = <sha256t::Hash<ChannelIdTag> as Hash>::Engine;
    type Inner = <sha256t::Hash<ChannelIdTag> as Hash>::Inner;

    const LEN: usize = 32;
    const DISPLAY_BACKWARD: bool = false;

    #[inline]
    fn engine() -> Self::Engine {
        sha256t::Hash::<ChannelIdTag>::engine()
    }

    #[inline]
    fn from_engine(e: Self::Engine) -> Self {
        Self(sha256t::Hash::from_engine(e))
    }

    #[inline]
    fn from_slice(sl: &[u8]) -> Result<Self, bitcoin::hashes::Error> {
        Ok(Self(sha256t::Hash::from_slice(sl)?))
    }

    #[inline]
    fn into_inner(self) -> Self::Inner {
        self.0.into_inner()
    }

    #[inline]
    fn as_inner(&self) -> &Self::Inner {
        self.0.as_inner()
    }

    #[inline]
    fn from_inner(inner: Self::Inner) -> Self {
        Self(sha256t::Hash::from_inner(inner))
    }

    fn all_zeros() -> Self {
        Self(sha256t::Hash::all_zeros())
    }
}

impl strict_encoding::Strategy for ChannelId {
    type Strategy = strict_encoding::strategies::HashFixedBytes;
}

/// Error parsing [`ChannelId`] bech32m representation
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, From)]
#[display(doc_comments)]
pub enum ChannelIdError {
    /// Error reported by bech32 library
    #[display(inner)]
    #[from]
    Bech32(bech32::Error),

    /// ChannelId must start with `lnch1` and not `{0}`
    InvalidHrp(String),

    /// ChannelId must be encoded with Bech32m variant and not Bech32
    InvalidVariant,

    /// ChannelId data must have length of 32 bytes
    #[from]
    InvalidLength(bitcoin::hashes::Error),
}

impl ::std::error::Error for ChannelIdError {
    fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> {
        match self {
            ChannelIdError::Bech32(err) => Some(err),
            ChannelIdError::InvalidLength(err) => Some(err),
            ChannelIdError::InvalidHrp(_) | ChannelIdError::InvalidVariant => {
                None
            }
        }
    }
}

impl Display for ChannelId {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let bytes: &[u8] = self.borrow();
        let s = bech32::encode(
            CHANNEL_BECH32_HRP,
            bytes.to_base32(),
            bech32::Variant::Bech32m,
        )
        .map_err(|_| fmt::Error)?;
        f.write_str(&s)
    }
}

impl FromStr for ChannelId {
    type Err = ChannelIdError;

    fn from_str(s: &str) -> Result<Self, ChannelIdError> {
        let (hrp, b32, variant) = bech32::decode(s)?;
        if hrp != CHANNEL_BECH32_HRP {
            return Err(ChannelIdError::InvalidHrp(hrp));
        }
        if variant != bech32::Variant::Bech32m {
            return Err(ChannelIdError::InvalidVariant);
        }
        let data = Vec::<u8>::from_base32(&b32)?;
        ChannelId::from_slice(&data).map_err(ChannelIdError::from)
    }
}

#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Display, Debug, Error)]
#[display(doc_comments)]
/// incorrect naming for protocol {_0}: protocol name in Bifrost can contain
/// only ASCII alphanumeric characters and dashes
pub struct ProtocolNameError(pub String);

#[cfg_attr(
    feature = "serde",
    serde_as,
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate", transparent)
)]
#[derive(
    Wrapper, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default, From
)]
pub struct ProtocolList(Vec<ProtocolName>);

impl IntoIterator for ProtocolList {
    type Item = ProtocolName;
    type IntoIter = std::vec::IntoIter<ProtocolName>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl<'a> IntoIterator for &'a ProtocolList {
    type Item = &'a ProtocolName;
    type IntoIter = std::slice::Iter<'a, ProtocolName>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl FromIterator<ProtocolName> for ProtocolList {
    fn from_iter<T: IntoIterator<Item = ProtocolName>>(iter: T) -> Self {
        Self(iter.into_iter().collect())
    }
}

impl ProtocolList {
    pub fn new(protocol: impl Into<ProtocolName>) -> Self {
        Self(vec![protocol.into()])
    }

    pub fn with<I>(list: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<ProtocolName>,
    {
        Self(list.into_iter().map(I::Item::into).collect())
    }

    pub fn add(&mut self, protocol: impl Into<ProtocolName>) {
        self.0.push(protocol.into())
    }

    pub fn extend<I>(&mut self, list: I)
    where
        I: IntoIterator,
        I::Item: Into<ProtocolName>,
    {
        for protocol in list {
            self.add(protocol)
        }
    }

    #[inline]
    pub fn iter(&self) -> std::slice::Iter<ProtocolName> {
        self.0.iter()
    }
}

impl Display for ProtocolList {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let mut len = self.0.len();
        for protocol in self {
            Display::fmt(protocol, f)?;
            len -= 1;
            if len > 0 {
                f.write_str(" ")?;
            }
        }
        Ok(())
    }
}

impl FromStr for ProtocolList {
    type Err = ProtocolNameError;

    #[inline]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        s.split(' ')
            .filter(|s| !s.is_empty())
            .map(ProtocolName::from_str)
            .collect()
    }
}

impl StrictEncode for ProtocolList {
    #[inline]
    fn strict_encode<E: Write>(
        &self,
        e: E,
    ) -> Result<usize, strict_encoding::Error> {
        self.to_string().strict_encode(e)
    }
}

impl StrictDecode for ProtocolList {
    #[inline]
    fn strict_decode<D: Read>(d: D) -> Result<Self, strict_encoding::Error> {
        let s = String::strict_decode(d)?;
        Self::from_str(&s).map_err(|_| {
            strict_encoding::Error::DataIntegrityError(format!(
                "invalid Bifrost protocol list value `{}`",
                s
            ))
        })
    }
}

#[cfg_attr(
    feature = "serde",
    serde_as,
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate", transparent)
)]
#[derive(
    Wrapper, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display, From
)]
#[derive(NetworkEncode, NetworkDecode)]
#[display(inner)]
pub struct ProtocolName(String);

impl FromStr for ProtocolName {
    type Err = ProtocolNameError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.chars().any(|ch| {
            ch.is_ascii_uppercase() || ch.is_ascii_digit() || ch == '-'
        }) {
            Ok(ProtocolName(s.to_owned()))
        } else {
            Err(ProtocolNameError(s.to_owned()))
        }
    }
}

#[derive(Clone, Debug, From, PartialEq, Eq, Hash, PartialOrd, Ord, Copy)]
pub enum AnnouncedNodeAddr {
    /// An IPv4 address/port on which the peer is listening.
    IpV4 {
        /// The 4-byte IPv4 address
        addr: [u8; 4],
        /// The port on which the node is listening
        port: u16,
    },
    /// An IPv6 address/port on which the peer is listening.
    IpV6 {
        /// The 16-byte IPv6 address
        addr: [u8; 16],
        /// The port on which the node is listening
        port: u16,
    },
    /// A modern Tor onion address/port on which the peer is listening.
    /// To create the human-readable "hostname", concatenate ed25519_pubkey,
    /// checksum, and version, wrap as base32 and append ".onion".
    OnionV3 {
        /// The ed25519 long-term public key of the peer
        ed25519_pubkey: [u8; 32],
    },
}

impl Uniform for AnnouncedNodeAddr {
    fn addr_format(&self) -> AddrFormat {
        match self {
            AnnouncedNodeAddr::IpV4 { .. } => AddrFormat::IpV4,
            AnnouncedNodeAddr::IpV6 { .. } => AddrFormat::IpV6,
            AnnouncedNodeAddr::OnionV3 { .. } => AddrFormat::OnionV3,
        }
    }

    fn addr(&self) -> RawAddr {
        match self {
            AnnouncedNodeAddr::IpV4 { addr, .. } => {
                let mut ip = [0u8; ADDR_LEN];
                ip[29..].copy_from_slice(addr);
                ip
            }

            AnnouncedNodeAddr::IpV6 { addr, .. } => {
                let mut ip = [0u8; ADDR_LEN];
                ip[17..].copy_from_slice(addr);
                ip
            }

            AnnouncedNodeAddr::OnionV3 { ed25519_pubkey, .. } => {
                let mut ip = [0u8; ADDR_LEN];
                ip[1..].copy_from_slice(ed25519_pubkey);
                ip
            }
        }
    }

    fn port(&self) -> Option<u16> {
        match self {
            // How to remove these unused variables?
            AnnouncedNodeAddr::IpV4 { port, .. } => Some(*port),
            AnnouncedNodeAddr::IpV6 { port, .. } => Some(*port),
            AnnouncedNodeAddr::OnionV3 { .. } => None,
        }
    }

    #[inline]
    fn transport(&self) -> Option<Transport> {
        Some(Transport::Tcp)
    }

    fn from_uniform_addr_lossy(addr: UniformAddr) -> Result<Self, DecodeError>
    where
        Self: Sized,
    {
        match addr.addr_format() {
            AddrFormat::IpV4 => {
                let mut ip = [0u8; 4];
                ip.copy_from_slice(&addr.addr[29..]);
                Ok(AnnouncedNodeAddr::IpV4 {
                    addr: ip,
                    port: match addr.port {
                        Some(p) => p,
                        _ => return Err(DecodeError::InsufficientData),
                    },
                })
            }

            AddrFormat::IpV6 => {
                let mut ip = [0u8; 16];
                ip.copy_from_slice(&addr.addr[17..]);
                Ok(AnnouncedNodeAddr::IpV6 {
                    addr: ip,
                    port: match addr.port {
                        Some(p) => p,
                        _ => return Err(DecodeError::InsufficientData),
                    },
                })
            }

            AddrFormat::OnionV3 => {
                let mut ip = [0u8; 32];
                ip.copy_from_slice(&addr.addr[1..]);
                Ok(AnnouncedNodeAddr::OnionV3 { ed25519_pubkey: ip })
            }

            _ => Err(DecodeError::InvalidAddr),
        }
    }

    fn from_uniform_addr(addr: UniformAddr) -> Result<Self, DecodeError>
    where
        Self: Sized,
    {
        AnnouncedNodeAddr::from_uniform_addr_lossy(addr)
    }
}

impl strict_encoding::Strategy for AnnouncedNodeAddr {
    type Strategy = strict_encoding::strategies::UsingUniformAddr;
}

#[derive(Wrapper, Clone, Debug, Display, Hash, Default, From, PartialEq, Eq)]
#[derive(NetworkEncode, NetworkDecode)]
#[display(Debug)]
pub struct AddressList(Vec<AnnouncedNodeAddr>);

#[cfg(test)]
mod test {
    use bitcoin::hashes::hex::FromHex;

    use super::*;

    #[test]
    fn test_address_encodings() {
        // Test vectors taken from https://github.com/rust-bitcoin/rust-lightning/blob/main/lightning/src/ln/msgs.rs
        let ipv4 = AnnouncedNodeAddr::IpV4 {
            addr: [255, 254, 253, 252],
            port: 9735,
        };

        let ipv6 = AnnouncedNodeAddr::IpV6 {
            addr: [
                255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244,
                243, 242, 241, 240,
            ],
            port: 9735,
        };

        let onion_v3 = AnnouncedNodeAddr::OnionV3 {
            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,
            ],
        };

        let ipv4_target = Vec::<u8>::from_hex("000000000000000000000000000000000000000000000000000000000000fffefdfc260701").unwrap();
        let ipv6_target =
            Vec::<u8>::from_hex("010000000000000000000000000000000000fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0260701")
                .unwrap();
        let onionv3_target = Vec::<u8>::from_hex("0300fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e0000001").unwrap();

        // Check strict encoding/decoding
        let ipv4_encoded = ipv4.strict_serialize().unwrap();
        let ipv6_encoded = ipv6.strict_serialize().unwrap();
        let onionv3_encoded = onion_v3.strict_serialize().unwrap();

        let ipv4_decoded =
            AnnouncedNodeAddr::strict_deserialize(&ipv4_target).unwrap();
        let ipv6_decoded =
            AnnouncedNodeAddr::strict_deserialize(&ipv6_target).unwrap();
        let onionv3_decoded =
            AnnouncedNodeAddr::strict_deserialize(&onionv3_target).unwrap();

        assert_eq!(ipv4, ipv4_decoded);
        assert_eq!(ipv6, ipv6_decoded);
        assert_eq!(onion_v3, onionv3_decoded);

        assert_eq!(ipv4_encoded, ipv4_target);
        assert_eq!(ipv6_encoded, ipv6_target);
        assert_eq!(onionv3_encoded, onionv3_target);

        // Check Uniform encoding/decoding
        let uniform_ipv4 = ipv4.to_uniform_addr();
        let uniform_ipv6 = ipv6.to_uniform_addr();
        let uniform_onionv3 = onion_v3.to_uniform_addr();

        let uniform_ipv4_decoded =
            AnnouncedNodeAddr::from_uniform_addr(uniform_ipv4).unwrap();
        let uniform_ipv6_decoded =
            AnnouncedNodeAddr::from_uniform_addr(uniform_ipv6).unwrap();
        let uniform_onionv3_decoded =
            AnnouncedNodeAddr::from_uniform_addr(uniform_onionv3).unwrap();

        // IPV4, IPV6 and OnionV2 should match
        assert_eq!(ipv4, uniform_ipv4_decoded);
        assert_eq!(ipv6, uniform_ipv6_decoded);

        // OnionV3 will have None as checksum and version
        let uniform_v3_target = AnnouncedNodeAddr::OnionV3 {
            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,
            ],
        };
        assert_eq!(uniform_v3_target, uniform_onionv3_decoded);

        // AddressList encoding/decoding
        let address_list = AddressList(vec![ipv4, ipv6, onion_v3]);
        let address_list_target = Vec::<u8>::from_hex(
            "0300000000000000000000000000000000000000000000000000000000000000\
            fffefdfc260701010000000000000000000000000000000000fffefdfcfbfaf9f8f\
            7f6f5f4f3f2f1f02607010300fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedeceb\
            eae9e8e7e6e5e4e3e2e1e0000001").unwrap();

        let address_list_encoded = address_list.strict_serialize().unwrap();

        assert_eq!(address_list_encoded, address_list_target)
    }
}