Merge pull request #1910 from arik-so/2022-12-keys-interface-name-split
[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 //! High level lightning structs and impls live here.
11 //!
12 //! You probably want to create a [`ChannelManager`], and a [`P2PGossipSync`] first.
13 //! Then, you probably want to pass them both on to a peer_handler::PeerManager and use that to
14 //! create/manage connections and call get_and_clear_pending_events after each action, handling
15 //! them appropriately.
16 //!
17 //! When you want to open/close a channel or send a payment, call into your [`ChannelManager`] and
18 //! when you want to learn things about the network topology (eg get a route for sending a payment),
19 //! call into your [`P2PGossipSync`].
20 //!
21 //! [`ChannelManager`]: channelmanager::ChannelManager
22 //! [`P2PGossipSync`]: crate::routing::gossip::P2PGossipSync
23
24 #[cfg(any(test, feature = "_test_utils"))]
25 #[macro_use]
26 pub mod functional_test_utils;
27
28 pub mod channelmanager;
29 pub mod inbound_payment;
30 pub mod msgs;
31 pub mod peer_handler;
32 pub mod chan_utils;
33 pub mod features;
34 pub mod script;
35
36 #[cfg(fuzzing)]
37 pub mod peer_channel_encryptor;
38 #[cfg(not(fuzzing))]
39 pub(crate) mod peer_channel_encryptor;
40
41 #[cfg(fuzzing)]
42 pub mod channel;
43 #[cfg(not(fuzzing))]
44 pub(crate) mod channel;
45
46 pub(crate) mod onion_utils;
47 mod outbound_payment;
48 pub mod wire;
49
50 // Older rustc (which we support) refuses to let us call the get_payment_preimage_hash!() macro
51 // without the node parameter being mut. This is incorrect, and thus newer rustcs will complain
52 // about an unnecessary mut. Thus, we silence the unused_mut warning in two test modules below.
53
54 #[cfg(test)]
55 #[allow(unused_mut)]
56 mod functional_tests;
57 #[cfg(test)]
58 #[allow(unused_mut)]
59 mod payment_tests;
60 #[cfg(test)]
61 #[allow(unused_mut)]
62 mod priv_short_conf_tests;
63 #[cfg(test)]
64 #[allow(unused_mut)]
65 mod chanmon_update_fail_tests;
66 #[cfg(test)]
67 #[allow(unused_mut)]
68 mod reorg_tests;
69 #[cfg(test)]
70 #[allow(unused_mut)]
71 mod reload_tests;
72 #[cfg(test)]
73 #[allow(unused_mut)]
74 mod onion_route_tests;
75 #[cfg(test)]
76 #[allow(unused_mut)]
77 mod monitor_tests;
78 #[cfg(test)]
79 #[allow(unused_mut)]
80 mod shutdown_tests;
81
82 pub use self::peer_channel_encryptor::LN_MAX_MSG_LEN;
83
84 /// payment_hash type, use to cross-lock hop
85 /// (C-not exported) as we just use [u8; 32] directly
86 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
87 pub struct PaymentHash(pub [u8; 32]);
88 /// payment_preimage type, use to route payment between hop
89 /// (C-not exported) as we just use [u8; 32] directly
90 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
91 pub struct PaymentPreimage(pub [u8; 32]);
92 /// payment_secret type, use to authenticate sender to the receiver and tie MPP HTLCs together
93 /// (C-not exported) as we just use [u8; 32] directly
94 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
95 pub struct PaymentSecret(pub [u8; 32]);
96
97 use crate::prelude::*;
98 use bitcoin::bech32;
99 use bitcoin::bech32::{Base32Len, FromBase32, ToBase32, WriteBase32, u5};
100
101 impl FromBase32 for PaymentSecret {
102         type Err = bech32::Error;
103
104         fn from_base32(field_data: &[u5]) -> Result<PaymentSecret, bech32::Error> {
105                 if field_data.len() != 52 {
106                         return Err(bech32::Error::InvalidLength)
107                 } else {
108                         let data_bytes = Vec::<u8>::from_base32(field_data)?;
109                         let mut payment_secret = [0; 32];
110                         payment_secret.copy_from_slice(&data_bytes);
111                         Ok(PaymentSecret(payment_secret))
112                 }
113         }
114 }
115
116 impl ToBase32 for PaymentSecret {
117         fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
118                 (&self.0[..]).write_base32(writer)
119         }
120 }
121
122 impl Base32Len for PaymentSecret {
123         fn base32_len(&self) -> usize {
124                 52
125         }
126 }