Skip to content

Commit 759eecc

Browse files
committed
Create TaprootSigner trait.
1 parent 818dbdf commit 759eecc

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

lightning/src/sign/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ use crate::util::atomic_counter::AtomicCounter;
5353
use crate::util::chacha20::ChaCha20;
5454
use crate::util::invoice::construct_invoice_preimage;
5555

56+
#[cfg(all(anchors, taproot))]
57+
pub mod taproot;
58+
5659
/// Used as initial key material, to be expanded into multiple secret keys (but not to be used
5760
/// directly). This is used within LDK to encrypt/decrypt inbound payment data.
5861
///

lightning/src/sign/taproot.rs

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

0 commit comments

Comments
 (0)