Allow(unused_imports) on prelude imports
[rust-lightning] / lightning / src / sign / ecdsa.rs
1 //! Defines ECDSA-specific signer types.
2
3 use bitcoin::blockdata::transaction::Transaction;
4
5 use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
6 use bitcoin::secp256k1::ecdsa::Signature;
7 use bitcoin::secp256k1;
8
9 use crate::util::ser::Writeable;
10 use crate::ln::PaymentPreimage;
11 use crate::ln::chan_utils::{HTLCOutputInCommitment, HolderCommitmentTransaction, CommitmentTransaction, ClosingTransaction};
12 use crate::ln::msgs::UnsignedChannelAnnouncement;
13
14 #[allow(unused_imports)]
15 use crate::prelude::*;
16
17 use crate::sign::{ChannelSigner, HTLCDescriptor};
18
19 /// A trait to sign Lightning channel transactions as described in
20 /// [BOLT 3](https://github.com/lightning/bolts/blob/master/03-transactions.md).
21 ///
22 /// Signing services could be implemented on a hardware wallet and should implement signing
23 /// policies in order to be secure. Please refer to the [VLS Policy
24 /// Controls](https://gitlab.com/lightning-signer/validating-lightning-signer/-/blob/main/docs/policy-controls.md)
25 /// for an example of such policies.
26 pub trait EcdsaChannelSigner: ChannelSigner {
27         /// Create a signature for a counterparty's commitment transaction and associated HTLC transactions.
28         ///
29         /// Note that if signing fails or is rejected, the channel will be force-closed.
30         ///
31         /// Policy checks should be implemented in this function, including checking the amount
32         /// sent to us and checking the HTLCs.
33         ///
34         /// The preimages of outbound and inbound HTLCs that were fulfilled since the last commitment
35         /// are provided. A validating signer should ensure that an outbound HTLC output is removed
36         /// only when the matching preimage is provided and after the corresponding inbound HTLC has
37         /// been removed for forwarded payments.
38         ///
39         /// Note that all the relevant preimages will be provided, but there may also be additional
40         /// irrelevant or duplicate preimages.
41         //
42         // TODO: Document the things someone using this interface should enforce before signing.
43         fn sign_counterparty_commitment(&self, commitment_tx: &CommitmentTransaction,
44                 inbound_htlc_preimages: Vec<PaymentPreimage>,
45                 outbound_htlc_preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<secp256k1::All>,
46         ) -> Result<(Signature, Vec<Signature>), ()>;
47         /// Creates a signature for a holder's commitment transaction.
48         ///
49         /// This will be called
50         /// - with a non-revoked `commitment_tx`.
51         /// - with the latest `commitment_tx` when we initiate a force-close.
52         ///
53         /// This may be called multiple times for the same transaction.
54         ///
55         /// An external signer implementation should check that the commitment has not been revoked.
56         ///
57         /// An `Err` can be returned to signal that the signer is unavailable/cannot produce a valid
58         /// signature and should be retried later. Once the signer is ready to provide a signature after
59         /// previously returning an `Err`, [`ChannelMonitor::signer_unblocked`] must be called on its
60         /// monitor.
61         ///
62         /// [`ChannelMonitor::signer_unblocked`]: crate::chain::channelmonitor::ChannelMonitor::signer_unblocked
63         //
64         // TODO: Document the things someone using this interface should enforce before signing.
65         fn sign_holder_commitment(&self, commitment_tx: &HolderCommitmentTransaction,
66                 secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
67         /// Same as [`sign_holder_commitment`], but exists only for tests to get access to holder
68         /// commitment transactions which will be broadcasted later, after the channel has moved on to a
69         /// newer state. Thus, needs its own method as [`sign_holder_commitment`] may enforce that we
70         /// only ever get called once.
71         #[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
72         fn unsafe_sign_holder_commitment(&self, commitment_tx: &HolderCommitmentTransaction,
73                 secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
74         /// Create a signature for the given input in a transaction spending an HTLC transaction output
75         /// or a commitment transaction `to_local` output when our counterparty broadcasts an old state.
76         ///
77         /// A justice transaction may claim multiple outputs at the same time if timelocks are
78         /// similar, but only a signature for the input at index `input` should be signed for here.
79         /// It may be called multiple times for same output(s) if a fee-bump is needed with regards
80         /// to an upcoming timelock expiration.
81         ///
82         /// Amount is value of the output spent by this input, committed to in the BIP 143 signature.
83         ///
84         /// `per_commitment_key` is revocation secret which was provided by our counterparty when they
85         /// revoked the state which they eventually broadcast. It's not a _holder_ secret key and does
86         /// not allow the spending of any funds by itself (you need our holder `revocation_secret` to do
87         /// so).
88         ///
89         /// An `Err` can be returned to signal that the signer is unavailable/cannot produce a valid
90         /// signature and should be retried later. Once the signer is ready to provide a signature after
91         /// previously returning an `Err`, [`ChannelMonitor::signer_unblocked`] must be called on its
92         /// monitor.
93         ///
94         /// [`ChannelMonitor::signer_unblocked`]: crate::chain::channelmonitor::ChannelMonitor::signer_unblocked
95         fn sign_justice_revoked_output(&self, justice_tx: &Transaction, input: usize, amount: u64,
96                 per_commitment_key: &SecretKey, secp_ctx: &Secp256k1<secp256k1::All>
97         ) -> Result<Signature, ()>;
98         /// Create a signature for the given input in a transaction spending a commitment transaction
99         /// HTLC output when our counterparty broadcasts an old state.
100         ///
101         /// A justice transaction may claim multiple outputs at the same time if timelocks are
102         /// similar, but only a signature for the input at index `input` should be signed for here.
103         /// It may be called multiple times for same output(s) if a fee-bump is needed with regards
104         /// to an upcoming timelock expiration.
105         ///
106         /// `amount` is the value of the output spent by this input, committed to in the BIP 143
107         /// signature.
108         ///
109         /// `per_commitment_key` is revocation secret which was provided by our counterparty when they
110         /// revoked the state which they eventually broadcast. It's not a _holder_ secret key and does
111         /// not allow the spending of any funds by itself (you need our holder revocation_secret to do
112         /// so).
113         ///
114         /// `htlc` holds HTLC elements (hash, timelock), thus changing the format of the witness script
115         /// (which is committed to in the BIP 143 signatures).
116         ///
117         /// An `Err` can be returned to signal that the signer is unavailable/cannot produce a valid
118         /// signature and should be retried later. Once the signer is ready to provide a signature after
119         /// previously returning an `Err`, [`ChannelMonitor::signer_unblocked`] must be called on its
120         /// monitor.
121         ///
122         /// [`ChannelMonitor::signer_unblocked`]: crate::chain::channelmonitor::ChannelMonitor::signer_unblocked
123         fn sign_justice_revoked_htlc(&self, justice_tx: &Transaction, input: usize, amount: u64,
124                 per_commitment_key: &SecretKey, htlc: &HTLCOutputInCommitment,
125                 secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
126         /// Computes the signature for a commitment transaction's HTLC output used as an input within
127         /// `htlc_tx`, which spends the commitment transaction at index `input`. The signature returned
128         /// must be be computed using [`EcdsaSighashType::All`].
129         ///
130         /// Note that this may be called for HTLCs in the penultimate commitment transaction if a
131         /// [`ChannelMonitor`] [replica](https://github.com/lightningdevkit/rust-lightning/blob/main/GLOSSARY.md#monitor-replicas)
132         /// broadcasts it before receiving the update for the latest commitment transaction.
133         ///
134         /// An `Err` can be returned to signal that the signer is unavailable/cannot produce a valid
135         /// signature and should be retried later. Once the signer is ready to provide a signature after
136         /// previously returning an `Err`, [`ChannelMonitor::signer_unblocked`] must be called on its
137         /// monitor.
138         ///
139         /// [`EcdsaSighashType::All`]: bitcoin::sighash::EcdsaSighashType::All
140         /// [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
141         /// [`ChannelMonitor::signer_unblocked`]: crate::chain::channelmonitor::ChannelMonitor::signer_unblocked
142         fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize,
143                 htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1<secp256k1::All>
144         ) -> Result<Signature, ()>;
145         /// Create a signature for a claiming transaction for a HTLC output on a counterparty's commitment
146         /// transaction, either offered or received.
147         ///
148         /// Such a transaction may claim multiples offered outputs at same time if we know the
149         /// preimage for each when we create it, but only the input at index `input` should be
150         /// signed for here. It may be called multiple times for same output(s) if a fee-bump is
151         /// needed with regards to an upcoming timelock expiration.
152         ///
153         /// `witness_script` is either an offered or received script as defined in BOLT3 for HTLC
154         /// outputs.
155         ///
156         /// `amount` is value of the output spent by this input, committed to in the BIP 143 signature.
157         ///
158         /// `per_commitment_point` is the dynamic point corresponding to the channel state
159         /// detected onchain. It has been generated by our counterparty and is used to derive
160         /// channel state keys, which are then included in the witness script and committed to in the
161         /// BIP 143 signature.
162         ///
163         /// An `Err` can be returned to signal that the signer is unavailable/cannot produce a valid
164         /// signature and should be retried later. Once the signer is ready to provide a signature after
165         /// previously returning an `Err`, [`ChannelMonitor::signer_unblocked`] must be called on its
166         /// monitor.
167         ///
168         /// [`ChannelMonitor::signer_unblocked`]: crate::chain::channelmonitor::ChannelMonitor::signer_unblocked
169         fn sign_counterparty_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, amount: u64,
170                 per_commitment_point: &PublicKey, htlc: &HTLCOutputInCommitment,
171                 secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
172         /// Create a signature for a (proposed) closing transaction.
173         ///
174         /// Note that, due to rounding, there may be one "missing" satoshi, and either party may have
175         /// chosen to forgo their output as dust.
176         fn sign_closing_transaction(&self, closing_tx: &ClosingTransaction,
177                 secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
178         /// Computes the signature for a commitment transaction's anchor output used as an
179         /// input within `anchor_tx`, which spends the commitment transaction, at index `input`.
180         ///
181         /// An `Err` can be returned to signal that the signer is unavailable/cannot produce a valid
182         /// signature and should be retried later. Once the signer is ready to provide a signature after
183         /// previously returning an `Err`, [`ChannelMonitor::signer_unblocked`] must be called on its
184         /// monitor.
185         ///
186         /// [`ChannelMonitor::signer_unblocked`]: crate::chain::channelmonitor::ChannelMonitor::signer_unblocked
187         fn sign_holder_anchor_input(
188                 &self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
189         ) -> Result<Signature, ()>;
190         /// Signs a channel announcement message with our funding key proving it comes from one of the
191         /// channel participants.
192         ///
193         /// Channel announcements also require a signature from each node's network key. Our node
194         /// signature is computed through [`NodeSigner::sign_gossip_message`].
195         ///
196         /// Note that if this fails or is rejected, the channel will not be publicly announced and
197         /// our counterparty may (though likely will not) close the channel on us for violating the
198         /// protocol.
199         ///
200         /// [`NodeSigner::sign_gossip_message`]: crate::sign::NodeSigner::sign_gossip_message
201         fn sign_channel_announcement_with_funding_key(
202                 &self, msg: &UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<secp256k1::All>
203         ) -> Result<Signature, ()>;
204 }
205
206 /// A writeable signer.
207 ///
208 /// There will always be two instances of a signer per channel, one occupied by the
209 /// [`ChannelManager`] and another by the channel's [`ChannelMonitor`].
210 ///
211 /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
212 /// [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
213 pub trait WriteableEcdsaChannelSigner: EcdsaChannelSigner + Writeable {}