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