lightning/
lib.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#![crate_name = "lightning"]
11
12//! Rust-Lightning, not Rusty's Lightning!
13//!
14//! A full-featured but also flexible lightning implementation, in library form. This allows the
15//! user (you) to decide how they wish to use it instead of being a fully self-contained daemon.
16//! This means there is no built-in threading/execution environment and it's up to the user to
17//! figure out how best to make networking happen/timers fire/things get written to disk/keys get
18//! generated/etc. This makes it a good candidate for tight integration into an existing wallet
19//! instead of having a rather-separate lightning appendage to a wallet.
20//!
21//! `default` features are:
22//!
23//! * `std` - enables functionalities which require `std`, including `std::io` trait implementations and things which utilize time
24//! * `grind_signatures` - enables generation of [low-r bitcoin signatures](https://bitcoin.stackexchange.com/questions/111660/what-is-signature-grinding),
25//! which saves 1 byte per signature in 50% of the cases (see [bitcoin PR #13666](https://github.com/bitcoin/bitcoin/pull/13666))
26//!
27//! Available features are:
28//!
29//! * `std`
30//! * `grind_signatures`
31
32#![cfg_attr(not(any(test, fuzzing, feature = "_test_utils")), deny(missing_docs))]
33#![deny(rustdoc::broken_intra_doc_links)]
34#![deny(rustdoc::private_intra_doc_links)]
35// In general, rust is absolutely horrid at supporting users doing things like,
36// for example, compiling Rust code for real environments. Disable useless lints
37// that don't do anything but annoy us and cant actually ever be resolved.
38#![allow(bare_trait_objects)]
39#![allow(ellipsis_inclusive_range_patterns)]
40#![cfg_attr(docsrs, feature(doc_cfg))]
41#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
42
43#[cfg(all(fuzzing, test))]
44compile_error!("Tests will always fail with cfg=fuzzing");
45
46#[macro_use]
47extern crate alloc;
48
49pub extern crate lightning_types as types;
50
51pub extern crate bitcoin;
52
53pub extern crate lightning_invoice as bolt11_invoice;
54
55#[cfg(any(test, feature = "std"))]
56extern crate core;
57
58#[cfg(any(test, feature = "_test_utils"))]
59extern crate regex;
60
61#[cfg(not(feature = "std"))]
62extern crate libm;
63
64#[cfg(ldk_bench)]
65extern crate criterion;
66
67#[cfg(all(feature = "std", test))]
68extern crate parking_lot;
69
70#[macro_use]
71pub mod util;
72
73pub mod blinded_path;
74pub mod chain;
75pub mod events;
76pub mod ln;
77pub mod offers;
78pub mod onion_message;
79pub mod routing;
80pub mod sign;
81
82pub(crate) mod crypto;
83
84/// Extension of the bitcoin::io module
85pub mod io;
86
87#[doc(hidden)]
88/// IO utilities public only for use by in-crate macros. These should not be used externally
89///
90/// This is not exported to bindings users as it is not intended for public consumption.
91pub mod io_extras {
92	use bitcoin::io::{self, Read, Write};
93
94	/// Creates an instance of a writer which will successfully consume all data.
95	pub use bitcoin::io::sink;
96
97	pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<u64, io::Error>
98	where
99		R: Read,
100		W: Write,
101	{
102		let mut count = 0;
103		let mut buf = [0u8; 64];
104
105		loop {
106			match reader.read(&mut buf) {
107				Ok(0) => break,
108				Ok(n) => {
109					writer.write_all(&buf[0..n])?;
110					count += n as u64;
111				},
112				Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {},
113				Err(e) => return Err(e.into()),
114			};
115		}
116		Ok(count)
117	}
118
119	pub fn read_to_end<D: Read>(d: &mut D) -> Result<alloc::vec::Vec<u8>, io::Error> {
120		let mut result = vec![];
121		let mut buf = [0u8; 64];
122		loop {
123			match d.read(&mut buf) {
124				Ok(0) => break,
125				Ok(n) => result.extend_from_slice(&buf[0..n]),
126				Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {},
127				Err(e) => return Err(e.into()),
128			};
129		}
130		Ok(result)
131	}
132}
133
134mod prelude {
135	#![allow(unused_imports)]
136
137	pub use alloc::{boxed::Box, collections::VecDeque, string::String, vec, vec::Vec};
138
139	pub use alloc::borrow::ToOwned;
140	pub use alloc::string::ToString;
141
142	pub use core::convert::{AsMut, AsRef, TryFrom, TryInto};
143	pub use core::default::Default;
144	pub use core::marker::Sized;
145
146	pub(crate) use crate::util::hash_tables::*;
147}
148
149#[cfg(all(not(ldk_bench), feature = "backtrace", feature = "std", test))]
150extern crate backtrace;
151
152mod sync;
153
154#[cfg(feature = "_externalize_tests")]
155lightning_macros::xtest_inventory!();