f9d364ed0db848b746ae65266f68df83d8264e82
[rust-lightning] / lightning / src / ln / mod.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
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
8 // licenses.
9
10 //! Implementations of various parts of the Lightning protocol are in this module.
11
12 #[cfg(any(test, feature = "_test_utils"))]
13 #[macro_use]
14 pub mod functional_test_utils;
15
16 pub mod channelmanager;
17 pub mod inbound_payment;
18 pub mod msgs;
19 pub mod peer_handler;
20 pub mod chan_utils;
21 pub mod features;
22 pub mod script;
23 mod channel_id;
24
25 #[cfg(fuzzing)]
26 pub mod peer_channel_encryptor;
27 #[cfg(not(fuzzing))]
28 pub(crate) mod peer_channel_encryptor;
29
30 #[cfg(fuzzing)]
31 pub mod channel;
32 #[cfg(not(fuzzing))]
33 pub(crate) mod channel;
34
35 // Re-export ChannelId
36 pub use channel_id::ChannelId;
37
38 pub(crate) mod onion_utils;
39 mod outbound_payment;
40 pub mod wire;
41
42 pub use onion_utils::create_payment_onion;
43 // Older rustc (which we support) refuses to let us call the get_payment_preimage_hash!() macro
44 // without the node parameter being mut. This is incorrect, and thus newer rustcs will complain
45 // about an unnecessary mut. Thus, we silence the unused_mut warning in two test modules below.
46
47 #[cfg(test)]
48 #[allow(unused_mut)]
49 mod blinded_payment_tests;
50 #[cfg(test)]
51 #[allow(unused_mut)]
52 mod functional_tests;
53 #[cfg(test)]
54 #[allow(unused_mut)]
55 mod payment_tests;
56 #[cfg(test)]
57 #[allow(unused_mut)]
58 mod priv_short_conf_tests;
59 #[cfg(test)]
60 #[allow(unused_mut)]
61 mod chanmon_update_fail_tests;
62 #[cfg(test)]
63 #[allow(unused_mut)]
64 mod reorg_tests;
65 #[cfg(test)]
66 #[allow(unused_mut)]
67 mod reload_tests;
68 #[cfg(test)]
69 #[allow(unused_mut)]
70 mod onion_route_tests;
71 #[cfg(test)]
72 #[allow(unused_mut)]
73 mod monitor_tests;
74 #[cfg(test)]
75 #[allow(unused_mut)]
76 mod shutdown_tests;
77 #[cfg(test)]
78 #[allow(unused_mut)]
79 mod async_signer_tests;
80
81 pub use self::peer_channel_encryptor::LN_MAX_MSG_LEN;
82
83 /// payment_hash type, use to cross-lock hop
84 ///
85 /// This is not exported to bindings users as we just use [u8; 32] directly
86 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)]
87 pub struct PaymentHash(pub [u8; 32]);
88
89 impl core::fmt::Display for PaymentHash {
90         fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
91                 crate::util::logger::DebugBytes(&self.0).fmt(f)
92         }
93 }
94
95 /// payment_preimage type, use to route payment between hop
96 ///
97 /// This is not exported to bindings users as we just use [u8; 32] directly
98 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)]
99 pub struct PaymentPreimage(pub [u8; 32]);
100
101 impl core::fmt::Display for PaymentPreimage {
102         fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
103                 crate::util::logger::DebugBytes(&self.0).fmt(f)
104         }
105 }
106
107 /// payment_secret type, use to authenticate sender to the receiver and tie MPP HTLCs together
108 ///
109 /// This is not exported to bindings users as we just use [u8; 32] directly
110 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)]
111 pub struct PaymentSecret(pub [u8; 32]);
112
113 use crate::prelude::*;
114 use bitcoin::bech32;
115 use bitcoin::bech32::{Base32Len, FromBase32, ToBase32, WriteBase32, u5};
116
117 impl FromBase32 for PaymentSecret {
118         type Err = bech32::Error;
119
120         fn from_base32(field_data: &[u5]) -> Result<PaymentSecret, bech32::Error> {
121                 if field_data.len() != 52 {
122                         return Err(bech32::Error::InvalidLength)
123                 } else {
124                         let data_bytes = Vec::<u8>::from_base32(field_data)?;
125                         let mut payment_secret = [0; 32];
126                         payment_secret.copy_from_slice(&data_bytes);
127                         Ok(PaymentSecret(payment_secret))
128                 }
129         }
130 }
131
132 impl ToBase32 for PaymentSecret {
133         fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
134                 (&self.0[..]).write_base32(writer)
135         }
136 }
137
138 impl Base32Len for PaymentSecret {
139         fn base32_len(&self) -> usize {
140                 52
141         }
142 }