25b32c4c79dda078d3daaac73b3ece86e9414c48
[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 onion_payment;
17 pub mod channelmanager;
18 pub mod channel_keys;
19 pub mod inbound_payment;
20 pub mod msgs;
21 pub mod peer_handler;
22 pub mod chan_utils;
23 pub mod features;
24 pub mod script;
25 mod channel_id;
26
27 #[cfg(fuzzing)]
28 pub mod peer_channel_encryptor;
29 #[cfg(not(fuzzing))]
30 pub(crate) mod peer_channel_encryptor;
31
32 #[cfg(fuzzing)]
33 pub mod channel;
34 #[cfg(not(fuzzing))]
35 pub(crate) mod channel;
36
37 // Re-export ChannelId
38 pub use channel_id::ChannelId;
39
40 pub(crate) mod onion_utils;
41 mod outbound_payment;
42 pub mod wire;
43
44 pub use onion_utils::create_payment_onion;
45 // Older rustc (which we support) refuses to let us call the get_payment_preimage_hash!() macro
46 // without the node parameter being mut. This is incorrect, and thus newer rustcs will complain
47 // about an unnecessary mut. Thus, we silence the unused_mut warning in two test modules below.
48
49 #[cfg(test)]
50 #[allow(unused_mut)]
51 mod blinded_payment_tests;
52 #[cfg(test)]
53 #[allow(unused_mut)]
54 mod functional_tests;
55 #[cfg(test)]
56 #[allow(unused_mut)]
57 mod payment_tests;
58 #[cfg(test)]
59 #[allow(unused_mut)]
60 mod priv_short_conf_tests;
61 #[cfg(test)]
62 #[allow(unused_mut)]
63 mod chanmon_update_fail_tests;
64 #[cfg(test)]
65 #[allow(unused_mut)]
66 mod reorg_tests;
67 #[cfg(test)]
68 #[allow(unused_mut)]
69 mod reload_tests;
70 #[cfg(test)]
71 #[allow(unused_mut)]
72 mod onion_route_tests;
73 #[cfg(test)]
74 #[allow(unused_mut)]
75 mod monitor_tests;
76 #[cfg(test)]
77 #[allow(unused_mut)]
78 mod shutdown_tests;
79 #[cfg(all(test, async_signing))]
80 #[allow(unused_mut)]
81 mod async_signer_tests;
82 #[cfg(test)]
83 #[allow(unused_mut)]
84 mod offers_tests;
85 #[allow(dead_code)] // TODO(dual_funding): Exchange for dual_funding cfg
86 pub(crate) mod interactivetxs;
87
88 pub use self::peer_channel_encryptor::LN_MAX_MSG_LEN;
89
90 use bitcoin::hashes::{sha256::Hash as Sha256, Hash};
91
92 /// payment_hash type, use to cross-lock hop
93 ///
94 /// This is not exported to bindings users as we just use [u8; 32] directly
95 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)]
96 pub struct PaymentHash(pub [u8; 32]);
97
98 impl core::fmt::Display for PaymentHash {
99         fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
100                 crate::util::logger::DebugBytes(&self.0).fmt(f)
101         }
102 }
103
104 /// payment_preimage type, use to route payment between hop
105 ///
106 /// This is not exported to bindings users as we just use [u8; 32] directly
107 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)]
108 pub struct PaymentPreimage(pub [u8; 32]);
109
110 impl core::fmt::Display for PaymentPreimage {
111         fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
112                 crate::util::logger::DebugBytes(&self.0).fmt(f)
113         }
114 }
115
116 /// Converts a `PaymentPreimage` into a `PaymentHash` by hashing the preimage with SHA256.
117 impl From<PaymentPreimage> for PaymentHash {
118         fn from(value: PaymentPreimage) -> Self {
119                 PaymentHash(Sha256::hash(&value.0).to_byte_array())
120         }
121 }
122
123 /// payment_secret type, use to authenticate sender to the receiver and tie MPP HTLCs together
124 ///
125 /// This is not exported to bindings users as we just use [u8; 32] directly
126 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)]
127 pub struct PaymentSecret(pub [u8; 32]);
128
129 use crate::prelude::*;
130 use bitcoin::bech32;
131 use bitcoin::bech32::{Base32Len, FromBase32, ToBase32, WriteBase32, u5};
132
133 impl FromBase32 for PaymentSecret {
134         type Err = bech32::Error;
135
136         fn from_base32(field_data: &[u5]) -> Result<PaymentSecret, bech32::Error> {
137                 if field_data.len() != 52 {
138                         return Err(bech32::Error::InvalidLength)
139                 } else {
140                         let data_bytes = Vec::<u8>::from_base32(field_data)?;
141                         let mut payment_secret = [0; 32];
142                         payment_secret.copy_from_slice(&data_bytes);
143                         Ok(PaymentSecret(payment_secret))
144                 }
145         }
146 }
147
148 impl ToBase32 for PaymentSecret {
149         fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
150                 (&self.0[..]).write_base32(writer)
151         }
152 }
153
154 impl Base32Len for PaymentSecret {
155         fn base32_len(&self) -> usize {
156                 52
157         }
158 }