ln_types/
lib.rs

1//! # Common types related to Lightning Network
2//!
3//! **Warning: while in a good state, this is still considered a preview version!**
4//! There are some planned changes.
5//!
6//! This library aims to provide Rust-idiomatic, ergonomic, and reasonably performant
7//! implementation of primitives used in applications interacting with Lightning Network.
8//! That means, they are not *just* types used to implement LN itself, they are supposed to be
9//! useful to any application that e.g. communicates with an LN implementation. Of course,
10//! they should still be useful for an LN implementation itself.
11//!
12//! ## Important types
13//!
14//! The most important types currently available:
15//!
16//! * [`Amount`] - similar to [`bitcoin_units::Amount`] but with millisatoshi precision
17//! * [`P2PAddress`] - address of a node usually represented in text as `node_id_hex@host:port`
18//! * [`NodeId`] - the byte representation of node's public key (no crypto operations)
19//! * [`NodePubkey`] - newtype around [`secp256k1::PublicKey`] to distinguish node public key from
20//!                    other keys. Requires `secp256k1` feature.
21//!
22//! Note: invoice is not here and isn't planned because it already exists in a separate crate.
23//!
24//! ## Integrations
25//!
26//! The crate aims to be interoperable with other crates via optional dependencies.
27//!
28//! ### Supported external crates
29//!
30//! Ativate using features of the same name.
31//!
32//! * `std` - enables [`P2PAddress`] and implements [`std::error::Error`] for error types.
33//!           Enabled by default, implies `alloc.
34//! * `alloc` - enables conversions from/to heap-allocated types as well as additional error
35//!             information.
36//! * [`bitcoin-units`] - converting between types
37//! * [`serde`] - serialization and deserialization of types
38//! * [`postgres-types`](postgres_types) - storing and retrieving from SQL
39//! * [`parse_arg`] - parsing arguments into types in this crate
40//! * [`secp256k1`] - provides `NodePubkey`
41//! * [`slog`] - provides `slog::Value` and (where relevant) `slog::KV` implementations for the types
42//!
43//! **Important: some combinations may benefit from additional features!**
44//! See below.
45//!
46//! ### Additional features
47//!
48//! * `hex-conservative` - improves the performance of displaying `NodeId`/`NodePubkey` at the cost
49//!                        of additional dependency
50//! * `node_pubkey_verify` - convenience function for verifying messages signed with
51//!                          [`NodePubkey`], implies `secp256k1/bitcoin_hashes`
52//! * `node_pubkey_recovery` - convenience function for verifying lightning messages
53//!                            signed with [`NodePubkey`], implies `node_pubkey_verify` and
54//!                            `secp256k1/recovery`
55//! * `secp256k1_std` - required for [`node_pubkey::ParseError`] to return `secp256k1::Error` from
56//!                     `source()` method of [`std::error::Error`]
57//! * `serde_alloc` - required for specialization of `serde::de::Visitor::visit_string` method to
58//!                   avoid allocation
59//! * `slog_std` - required for error types to use [`slog::Serializer::emit_error`] for logging
60//!
61//! Feel free to contribute your own!
62//!
63//! **Disclaimer**: Inclusion of any crate here is neither endorsment nor guarantee of it
64//! being secure, honest, non-backdoored or functioning! You're required to do your own review of
65//! any external crate.
66//!
67//! The rules around adding a new integration are lax: the dependency must be optional, must
68//! **not** be obviously broken or surprising, and must **not** interact with other implementations
69//! in surprising ways.
70//!
71//! ### `no_std` support
72//!
73//! This crate works without `std` or `alloc` with these limitations/differences:
74//!
75//! * [`P2PAddress`] is unavailable without `std` on Rust versions older than 1.77
76//! * Resolving a socket address requires `std`
77//! * Without `std` all error types display all sources delimited by `: `, with `std` they are
78//!   returned from the [`source()`](std::error::Error::source) method instead.
79//!
80//! ## Versioning
81//!
82//! This crate uses standard Rust Semver with one special exception:
83//! **Matching on fully private structs is not allowed and so changing an
84//! all-private struct to an enum is considered non-breaking change even
85//! though the consumer code matching on it would break!**
86//!
87//! See [Rust internals discussion](https://internals.rust-lang.org/t/disallow-matching-on-all-private-structs/15993) to learn more.
88//!
89//! ## MSRV
90//!
91//! The minimum supported Rust version is 1.56 and will be increased to 1.63 soon.
92//! Generally, the intention is to support at least the latest Debian stable.
93//!
94//! Note that external libraries may have higher MSRV - this is not considered a breakage.
95//! 
96//! ## License
97//! 
98//! MIT
99
100#![cfg_attr(docsrs, feature(doc_cfg))]
101#![cfg_attr(docsrs, feature(doc_auto_cfg))]
102#![deny(missing_docs)]
103
104#![no_std]
105
106#[cfg(any(feature = "std", test))]
107pub extern crate std;
108
109#[cfg(any(feature = "alloc", test))]
110pub extern crate alloc;
111
112#[cfg(feature = "bitcoin-units")]
113pub extern crate bitcoin_units;
114
115#[cfg(feature = "parse_arg")]
116pub extern crate parse_arg;
117
118#[cfg(feature = "postgres-types")]
119pub extern crate postgres_types_real as postgres_types;
120
121#[cfg(feature = "secp256k1")]
122pub extern crate secp256k1;
123
124#[cfg(feature = "serde")]
125pub extern crate serde;
126
127#[cfg(feature = "slog")]
128pub extern crate slog;
129
130#[macro_use]
131mod macros;
132#[macro_use]
133pub(crate) mod err_fmt;
134
135pub mod node_id;
136#[cfg(any(feature = "std", rust_v_1_77))]
137pub mod p2p_address;
138pub mod amount;
139#[cfg(feature = "secp256k1")]
140pub mod node_pubkey;
141
142pub use node_id::NodeId;
143#[cfg(any(feature = "std", rust_v_1_77))]
144pub use p2p_address::P2PAddress;
145pub use amount::Amount;
146#[cfg(feature = "secp256k1")]
147pub use node_pubkey::NodePubkey;