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