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