230383f4f7d6496bcb1b7d40829b086ea6d39d05
[rust-lightning] / lightning / src / sign / taproot.rs
1 //! Defines a Taproot-specific signer type.
2
3 use alloc::vec::Vec;
4 use bitcoin::blockdata::transaction::Transaction;
5 use bitcoin::secp256k1;
6 use bitcoin::secp256k1::{PublicKey, schnorr::Signature, Secp256k1, SecretKey};
7
8 use musig2::types::{PartialSignature, PublicNonce};
9
10 use crate::ln::chan_utils::{ClosingTransaction, CommitmentTransaction, HolderCommitmentTransaction, HTLCOutputInCommitment};
11 use crate::ln::msgs::PartialSignatureWithNonce;
12 use crate::ln::PaymentPreimage;
13 use crate::sign::{ChannelSigner, HTLCDescriptor};
14
15 /// A Taproot-specific signer type that defines signing-related methods that are either unique to
16 /// Taproot or have argument or return types that differ from the ones an ECDSA signer would be
17 /// expected to have.
18 pub trait TaprootChannelSigner: ChannelSigner {
19         /// Generate a local nonce pair, which requires committing to ahead of time.
20         /// The counterparty needs the public nonce generated herein to compute a partial signature.
21         fn generate_local_nonce_pair(&self, commitment_number: u64, secp_ctx: &Secp256k1<secp256k1::All>) -> PublicNonce;
22
23         /// Create a signature for a counterparty's commitment transaction and associated HTLC transactions.
24         ///
25         /// Note that if signing fails or is rejected, the channel will be force-closed.
26         ///
27         /// Policy checks should be implemented in this function, including checking the amount
28         /// sent to us and checking the HTLCs.
29         ///
30         /// The preimages of outbound and inbound HTLCs that were fulfilled since the last commitment
31         /// are provided. A validating signer should ensure that an outbound HTLC output is removed
32         /// only when the matching preimage is provided and after the corresponding inbound HTLC has
33         /// been removed for forwarded payments.
34         ///
35         /// Note that all the relevant preimages will be provided, but there may also be additional
36         /// irrelevant or duplicate preimages.
37         //
38         // TODO: Document the things someone using this interface should enforce before signing.
39         fn partially_sign_counterparty_commitment(&self, counterparty_nonce: PublicNonce,
40                 commitment_tx: &CommitmentTransaction,
41                 inbound_htlc_preimages: Vec<PaymentPreimage>,
42                 outbound_htlc_preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<secp256k1::All>,
43         ) -> Result<(PartialSignatureWithNonce, Vec<Signature>), ()>;
44
45         /// Creates a signature for a holder's commitment transaction.
46         ///
47         /// This will be called
48         /// - with a non-revoked `commitment_tx`.
49         /// - with the latest `commitment_tx` when we initiate a force-close.
50         ///
51         /// This may be called multiple times for the same transaction.
52         ///
53         /// An external signer implementation should check that the commitment has not been revoked.
54         ///
55         // TODO: Document the things someone using this interface should enforce before signing.
56         fn finalize_holder_commitment(&self, commitment_tx: &HolderCommitmentTransaction,
57                 counterparty_partial_signature: PartialSignatureWithNonce,
58                 secp_ctx: &Secp256k1<secp256k1::All>
59         ) -> Result<PartialSignature, ()>;
60
61         /// Create a signature for the given input in a transaction spending an HTLC transaction output
62         /// or a commitment transaction `to_local` output when our counterparty broadcasts an old state.
63         ///
64         /// A justice transaction may claim multiple outputs at the same time if timelocks are
65         /// similar, but only a signature for the input at index `input` should be signed for here.
66         /// It may be called multiple times for same output(s) if a fee-bump is needed with regards
67         /// to an upcoming timelock expiration.
68         ///
69         /// Amount is value of the output spent by this input, committed to in the BIP 341 signature.
70         ///
71         /// `per_commitment_key` is revocation secret which was provided by our counterparty when they
72         /// revoked the state which they eventually broadcast. It's not a _holder_ secret key and does
73         /// not allow the spending of any funds by itself (you need our holder `revocation_secret` to do
74         /// so).
75         fn sign_justice_revoked_output(&self, justice_tx: &Transaction, input: usize, amount: u64,
76                 per_commitment_key: &SecretKey, secp_ctx: &Secp256k1<secp256k1::All>,
77         ) -> Result<Signature, ()>;
78
79         /// Create a signature for the given input in a transaction spending a commitment transaction
80         /// HTLC output when our counterparty broadcasts an old state.
81         ///
82         /// A justice transaction may claim multiple outputs at the same time if timelocks are
83         /// similar, but only a signature for the input at index `input` should be signed for here.
84         /// It may be called multiple times for same output(s) if a fee-bump is needed with regards
85         /// to an upcoming timelock expiration.
86         ///
87         /// `amount` is the value of the output spent by this input, committed to in the BIP 341
88         /// signature.
89         ///
90         /// `per_commitment_key` is revocation secret which was provided by our counterparty when they
91         /// revoked the state which they eventually broadcast. It's not a _holder_ secret key and does
92         /// not allow the spending of any funds by itself (you need our holder revocation_secret to do
93         /// so).
94         ///
95         /// `htlc` holds HTLC elements (hash, timelock), thus changing the format of the witness script
96         /// (which is committed to in the BIP 341 signatures).
97         fn sign_justice_revoked_htlc(&self, justice_tx: &Transaction, input: usize, amount: u64,
98                 per_commitment_key: &SecretKey, htlc: &HTLCOutputInCommitment,
99                 secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
100
101         /// Computes the signature for a commitment transaction's HTLC output used as an input within
102         /// `htlc_tx`, which spends the commitment transaction at index `input`. The signature returned
103         /// must be be computed using [`TapSighashType::Default`].
104         ///
105         /// Note that this may be called for HTLCs in the penultimate commitment transaction if a
106         /// [`ChannelMonitor`] [replica](https://github.com/lightningdevkit/rust-lightning/blob/main/GLOSSARY.md#monitor-replicas)
107         /// broadcasts it before receiving the update for the latest commitment transaction.
108         ///
109         ///
110         /// [`TapSighashType::Default`]: bitcoin::sighash::TapSighashType::Default
111         /// [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
112         fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize,
113                 htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1<secp256k1::All>,
114         ) -> Result<Signature, ()>;
115
116         /// Create a signature for a claiming transaction for a HTLC output on a counterparty's commitment
117         /// transaction, either offered or received.
118         ///
119         /// Such a transaction may claim multiples offered outputs at same time if we know the
120         /// preimage for each when we create it, but only the input at index `input` should be
121         /// signed for here. It may be called multiple times for same output(s) if a fee-bump is
122         /// needed with regards to an upcoming timelock expiration.
123         ///
124         /// `witness_script` is either an offered or received script as defined in BOLT3 for HTLC
125         /// outputs.
126         ///
127         /// `amount` is value of the output spent by this input, committed to in the BIP 341 signature.
128         ///
129         /// `per_commitment_point` is the dynamic point corresponding to the channel state
130         /// detected onchain. It has been generated by our counterparty and is used to derive
131         /// channel state keys, which are then included in the witness script and committed to in the
132         /// BIP 341 signature.
133         fn sign_counterparty_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, amount: u64,
134                 per_commitment_point: &PublicKey, htlc: &HTLCOutputInCommitment,
135                 secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
136
137         /// Create a signature for a (proposed) closing transaction.
138         ///
139         /// Note that, due to rounding, there may be one "missing" satoshi, and either party may have
140         /// chosen to forgo their output as dust.
141         fn partially_sign_closing_transaction(&self, closing_tx: &ClosingTransaction,
142                 secp_ctx: &Secp256k1<secp256k1::All>) -> Result<PartialSignature, ()>;
143
144         /// Computes the signature for a commitment transaction's anchor output used as an
145         /// input within `anchor_tx`, which spends the commitment transaction, at index `input`.
146         fn sign_holder_anchor_input(
147                 &self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
148         ) -> Result<Signature, ()>;
149
150         // TODO: sign channel announcement
151 }