1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#![crate_name = "lightning_signer"]

//! A policy-enforcing signer for Lightning
//! See [`node::Node`] for the entry point.

#![forbid(unsafe_code)]
#![allow(bare_trait_objects)]
#![allow(ellipsis_inclusive_range_patterns)]
#![warn(rustdoc::broken_intra_doc_links)]
#![warn(missing_docs)]
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]

#[cfg(not(any(feature = "std", feature = "no-std")))]
compile_error!("at least one of the `std` or `no-std` features must be enabled");

#[cfg(not(feature = "std"))]
extern crate core2;

#[macro_use]
extern crate alloc;
extern crate core;
#[cfg(feature = "grpc")]
extern crate tonic;

pub use bitcoin;
pub use lightning;
pub use lightning_invoice;
pub use txoo;

/// Chain tracking and validation
pub mod chain;
/// Various utilities
#[macro_use]
pub mod util;
/// Channel
#[macro_use]
pub mod channel;
/// Channel on-chain monitor
pub mod monitor;
/// Node
#[macro_use]
pub mod node;
/// Invoices
pub mod invoice;
/// Persistence
pub mod persist;
/// Policy enforcement
pub mod policy;
/// KeysManager
pub mod signer;
/// Transaction parsing and construction
pub mod tx;
/// Layer-1 wallet
pub mod wallet;

#[cfg(not(feature = "std"))]
mod io_extras {
    pub use core2::io::{self, Error, Read, Write};

    /// A writer which will move data into the void.
    pub struct Sink {
        _priv: (),
    }

    /// Creates an instance of a writer which will successfully consume all data.
    pub const fn sink() -> Sink {
        Sink { _priv: () }
    }

    impl core2::io::Write for Sink {
        #[inline]
        fn write(&mut self, buf: &[u8]) -> core2::io::Result<usize> {
            Ok(buf.len())
        }

        #[inline]
        fn flush(&mut self) -> core2::io::Result<()> {
            Ok(())
        }
    }
}

#[cfg(feature = "std")]
mod io_extras {
    pub use std::io::{self, sink, Error, Read};
}

pub use io_extras::io;

#[doc(hidden)]
pub use alloc::collections::BTreeSet as OrderedSet;
#[doc(hidden)]
pub use alloc::rc::Rc;
#[doc(hidden)]
pub use alloc::sync::{Arc, Weak};

use bitcoin::secp256k1::PublicKey;
use lightning::ln::chan_utils::ChannelTransactionParameters;

#[cfg(not(feature = "std"))]
mod nostd;

/// std / no_std compat
pub mod prelude {
    pub use alloc::{boxed::Box, string::String, vec, vec::Vec};

    // TODO clean up naming
    pub use hashbrown::HashMap as Map;
    pub use hashbrown::HashSet as UnorderedSet;

    pub use alloc::collections::BTreeMap as OrderedMap;
    pub use alloc::collections::BTreeSet as OrderedSet;

    pub use alloc::borrow::ToOwned;
    pub use alloc::string::ToString;

    #[cfg(not(feature = "std"))]
    pub use crate::nostd::*;

    #[cfg(feature = "std")]
    pub use std::sync::{Mutex, MutexGuard};

    /// Convenience trait for Send + Sync
    #[cfg(feature = "std")]
    pub trait SendSync: Send + Sync {}
}

#[doc(hidden)]
pub use prelude::SendSync;

use prelude::*;

#[cfg(feature = "std")]
mod sync {
    pub use ::std::sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard, Weak};
}

/// A trait for getting a commitment point for a given commitment number,
/// if known.
pub trait CommitmentPointProvider: SendSync {
    /// Get the commitment point for a holder commitment transaction
    fn get_holder_commitment_point(&self, commitment_number: u64) -> PublicKey;
    /// Get the commitment point for a counterparty commitment transaction, if known.
    /// It might not be known if we didn't reach that commitment number yet
    /// or it's a revoked commitment transaction and we don't store revocation secrets.
    fn get_counterparty_commitment_point(&self, commitment_number: u64) -> Option<PublicKey>;
    /// Get channel transaction parameters, for decoding on-chain transactions
    fn get_transaction_parameters(&self) -> ChannelTransactionParameters;
    /// Clone
    fn clone_box(&self) -> Box<dyn CommitmentPointProvider>;
}

impl Clone for Box<dyn CommitmentPointProvider> {
    fn clone(&self) -> Self {
        (**self).clone_box()
    }
}

#[cfg(not(feature = "std"))]
#[allow(unused)]
mod sync;

#[cfg(test)]
mod ready_channel_tests;
#[cfg(test)]
mod sign_counterparty_commitment_tests;
#[cfg(test)]
mod sign_counterparty_htlc_sweep_tests;
#[cfg(test)]
mod sign_delayed_sweep_tests;
#[cfg(test)]
mod sign_holder_commitment_tests;
#[cfg(test)]
mod sign_htlc_tx_tests;
#[cfg(test)]
mod sign_justice_sweep_tests;
#[cfg(test)]
mod sign_mutual_close_tests;
#[cfg(test)]
mod sign_onchain_tx_tests;
#[cfg(test)]
mod validate_counterparty_revocation_tests;
#[cfg(test)]
mod validate_holder_commitment_tests;