Skip to content

Commit c026313

Browse files
committed
Move ECDSA-specific signers into ecdsa.rs
To separate out the logic in the `sign` module, which will start to be convoluted with multiple signer types, we're splitting out each signer type into its own submodule, following the taproot.rs example from a previous commit.
1 parent ea469ab commit c026313

15 files changed

+204
-178
lines changed

lightning/src/chain/chainmonitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::chain::{ChannelMonitorUpdateStatus, Filter, WatchedOutput};
3131
use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
3232
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, Balance, MonitorEvent, TransactionOutputs, LATENCY_GRACE_PERIOD_BLOCKS};
3333
use crate::chain::transaction::{OutPoint, TransactionData};
34-
use crate::sign::WriteableEcdsaChannelSigner;
34+
use crate::sign::ecdsa::WriteableEcdsaChannelSigner;
3535
use crate::events;
3636
use crate::events::{Event, EventHandler};
3737
use crate::util::atomic_counter::AtomicCounter;

lightning/src/chain/channelmonitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use crate::chain;
4343
use crate::chain::{BestBlock, WatchedOutput};
4444
use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator, LowerBoundedFeeEstimator};
4545
use crate::chain::transaction::{OutPoint, TransactionData};
46-
use crate::sign::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, WriteableEcdsaChannelSigner, SignerProvider, EntropySource};
46+
use crate::sign::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, ecdsa::WriteableEcdsaChannelSigner, SignerProvider, EntropySource};
4747
use crate::chain::onchaintx::{ClaimEvent, OnchainTxHandler};
4848
use crate::chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderFundingOutput, HolderHTLCOutput, PackageSolvingData, PackageTemplate, RevokedOutput, RevokedHTLCOutput};
4949
use crate::chain::Filter;
@@ -1462,7 +1462,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
14621462
/// to the commitment transaction being revoked, this will return a signed transaction, but
14631463
/// the signature will not be valid.
14641464
///
1465-
/// [`EcdsaChannelSigner::sign_justice_revoked_output`]: crate::sign::EcdsaChannelSigner::sign_justice_revoked_output
1465+
/// [`EcdsaChannelSigner::sign_justice_revoked_output`]: crate::sign::ecdsa::EcdsaChannelSigner::sign_justice_revoked_output
14661466
/// [`Persist`]: crate::chain::chainmonitor::Persist
14671467
pub fn sign_to_local_justice_tx(&self, justice_tx: Transaction, input_idx: usize, value: u64, commitment_number: u64) -> Result<Transaction, ()> {
14681468
self.inner.lock().unwrap().sign_to_local_justice_tx(justice_tx, input_idx, value, commitment_number)

lightning/src/chain/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use bitcoin::network::constants::Network;
1717
use bitcoin::secp256k1::PublicKey;
1818

1919
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, MonitorEvent};
20-
use crate::sign::WriteableEcdsaChannelSigner;
20+
use crate::sign::ecdsa::WriteableEcdsaChannelSigner;
2121
use crate::chain::transaction::{OutPoint, TransactionData};
2222

2323
use crate::prelude::*;

lightning/src/chain/onchaintx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::ln::chan_utils::{self, ChannelTransactionParameters, HTLCOutputInComm
3030
use crate::chain::ClaimId;
3131
use crate::chain::chaininterface::{ConfirmationTarget, FeeEstimator, BroadcasterInterface, LowerBoundedFeeEstimator};
3232
use crate::chain::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER};
33-
use crate::sign::WriteableEcdsaChannelSigner;
33+
use crate::sign::ecdsa::WriteableEcdsaChannelSigner;
3434
use crate::chain::package::{PackageSolvingData, PackageTemplate};
3535
use crate::util::logger::Logger;
3636
use crate::util::ser::{Readable, ReadableArgs, MaybeReadable, UpgradableRequired, Writer, Writeable, VecWriter};

lightning/src/chain/package.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment};
2525
use crate::ln::chan_utils;
2626
use crate::ln::msgs::DecodeError;
2727
use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
28-
use crate::sign::WriteableEcdsaChannelSigner;
28+
use crate::sign::ecdsa::WriteableEcdsaChannelSigner;
2929
use crate::chain::onchaintx::{ExternalHTLCClaim, OnchainTxHandler};
3030
use crate::util::logger::Logger;
3131
use crate::util::ser::{Readable, Writer, Writeable, RequiredWrapper};

lightning/src/events/bump_transaction.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ use crate::ln::chan_utils::{
2626
use crate::ln::features::ChannelTypeFeatures;
2727
use crate::ln::PaymentPreimage;
2828
use crate::prelude::*;
29-
use crate::sign::{EcdsaChannelSigner, SignerProvider, WriteableEcdsaChannelSigner};
29+
use crate::sign::SignerProvider;
30+
use crate::sign::ecdsa::{EcdsaChannelSigner, WriteableEcdsaChannelSigner};
3031
use crate::sync::Mutex;
3132
use crate::util::logger::Logger;
3233

@@ -263,8 +264,8 @@ pub enum BumpTransactionEvent {
263264
/// an empty `pending_htlcs`), confirmation of the commitment transaction can be considered to
264265
/// be not urgent.
265266
///
266-
/// [`EcdsaChannelSigner`]: crate::sign::EcdsaChannelSigner
267-
/// [`EcdsaChannelSigner::sign_holder_anchor_input`]: crate::sign::EcdsaChannelSigner::sign_holder_anchor_input
267+
/// [`EcdsaChannelSigner`]: crate::sign::ecdsa::EcdsaChannelSigner
268+
/// [`EcdsaChannelSigner::sign_holder_anchor_input`]: crate::sign::ecdsa::EcdsaChannelSigner::sign_holder_anchor_input
268269
/// [`build_anchor_input_witness`]: crate::ln::chan_utils::build_anchor_input_witness
269270
ChannelClose {
270271
/// The unique identifier for the claim of the anchor output in the commitment transaction.
@@ -317,8 +318,8 @@ pub enum BumpTransactionEvent {
317318
/// longer able to commit external confirmed funds to the HTLC transaction or the fee committed
318319
/// to the HTLC transaction is greater in value than the HTLCs being claimed.
319320
///
320-
/// [`EcdsaChannelSigner`]: crate::sign::EcdsaChannelSigner
321-
/// [`EcdsaChannelSigner::sign_holder_htlc_transaction`]: crate::sign::EcdsaChannelSigner::sign_holder_htlc_transaction
321+
/// [`EcdsaChannelSigner`]: crate::sign::ecdsa::EcdsaChannelSigner
322+
/// [`EcdsaChannelSigner::sign_holder_htlc_transaction`]: crate::sign::ecdsa::EcdsaChannelSigner::sign_holder_htlc_transaction
322323
/// [`HTLCDescriptor::tx_input_witness`]: HTLCDescriptor::tx_input_witness
323324
HTLCResolution {
324325
/// The unique identifier for the claim of the HTLCs in the confirmed commitment

lightning/src/ln/channel.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ use crate::chain::BestBlock;
3535
use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, LowerBoundedFeeEstimator};
3636
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS, CLOSED_CHANNEL_UPDATE_ID};
3737
use crate::chain::transaction::{OutPoint, TransactionData};
38-
use crate::sign::{EcdsaChannelSigner, WriteableEcdsaChannelSigner, EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient};
38+
use crate::sign::{EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient};
39+
use crate::sign::ecdsa::{EcdsaChannelSigner, WriteableEcdsaChannelSigner};
3940
use crate::events::ClosureReason;
4041
use crate::routing::gossip::NodeId;
4142
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter};
@@ -8107,7 +8108,7 @@ mod tests {
81078108
use bitcoin::hashes::hex::FromHex;
81088109
use bitcoin::hash_types::Txid;
81098110
use bitcoin::secp256k1::Message;
8110-
use crate::sign::EcdsaChannelSigner;
8111+
use crate::sign::ecdsa::EcdsaChannelSigner;
81118112
use crate::ln::PaymentPreimage;
81128113
use crate::ln::channel::{HTLCOutputInCommitment ,TxCreationKeys};
81138114
use crate::ln::chan_utils::{ChannelPublicKeys, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};

lightning/src/ln/channelmanager.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError};
5555
use crate::ln::outbound_payment;
5656
use crate::ln::outbound_payment::{OutboundPayments, PaymentAttempts, PendingOutboundPayment, SendAlongPathArgs};
5757
use crate::ln::wire::Encode;
58-
use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient, SignerProvider, WriteableEcdsaChannelSigner};
58+
use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient, SignerProvider};
59+
use crate::sign::ecdsa::WriteableEcdsaChannelSigner;
5960
use crate::util::config::{UserConfig, ChannelConfig, ChannelConfigUpdate};
6061
use crate::util::wakers::{Future, Notifier};
6162
use crate::util::scid_utils::fake_scid;

lightning/src/ln/functional_tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use crate::chain::chaininterface::LowerBoundedFeeEstimator;
1717
use crate::chain::channelmonitor;
1818
use crate::chain::channelmonitor::{CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY};
1919
use crate::chain::transaction::OutPoint;
20-
use crate::sign::{ChannelSigner, EcdsaChannelSigner, EntropySource, SignerProvider};
20+
use crate::sign::{ChannelSigner, EntropySource, SignerProvider};
21+
use crate::sign::ecdsa::EcdsaChannelSigner;
2122
use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, ClosureReason, HTLCDestination, PaymentFailureReason};
2223
use crate::ln::{ChannelId, PaymentPreimage, PaymentSecret, PaymentHash};
2324
use crate::ln::channel::{commitment_tx_base_weight, COMMITMENT_TX_WEIGHT_PER_HTLC, CONCURRENT_INBOUND_HTLC_FEE_BUFFER, FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE, MIN_AFFORDABLE_HTLC_COUNT, get_holder_selected_channel_reserve_satoshis, OutboundV1Channel, InboundV1Channel, COINBASE_MATURITY, ChannelPhase};

lightning/src/ln/monitor_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
//! Further functional tests which test blockchain reorganizations.
1111
12-
use crate::sign::EcdsaChannelSigner;
12+
use crate::sign::ecdsa::EcdsaChannelSigner;
1313
use crate::chain::channelmonitor::{ANTI_REORG_DELAY, LATENCY_GRACE_PERIOD_BLOCKS, Balance};
1414
use crate::chain::transaction::OutPoint;
1515
use crate::chain::chaininterface::{LowerBoundedFeeEstimator, compute_feerate_sat_per_1000_weight};

lightning/src/sign/ecdsa.rs

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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::events::bump_transaction::HTLCDescriptor;
11+
use crate::ln::PaymentPreimage;
12+
use crate::ln::chan_utils::{HTLCOutputInCommitment, HolderCommitmentTransaction, CommitmentTransaction, ClosingTransaction};
13+
use crate::ln::msgs::UnsignedChannelAnnouncement;
14+
15+
use crate::prelude::*;
16+
use crate::sign::ChannelSigner;
17+
18+
/// A trait to sign Lightning channel transactions as described in
19+
/// [BOLT 3](https://github.com/lightning/bolts/blob/master/03-transactions.md).
20+
///
21+
/// Signing services could be implemented on a hardware wallet and should implement signing
22+
/// policies in order to be secure. Please refer to the [VLS Policy
23+
/// Controls](https://gitlab.com/lightning-signer/validating-lightning-signer/-/blob/main/docs/policy-controls.md)
24+
/// for an example of such policies.
25+
pub trait EcdsaChannelSigner: ChannelSigner {
26+
/// Create a signature for a counterparty's commitment transaction and associated HTLC transactions.
27+
///
28+
/// Note that if signing fails or is rejected, the channel will be force-closed.
29+
///
30+
/// Policy checks should be implemented in this function, including checking the amount
31+
/// sent to us and checking the HTLCs.
32+
///
33+
/// The preimages of outgoing HTLCs that were fulfilled since the last commitment are provided.
34+
/// A validating signer should ensure that an HTLC output is removed only when the matching
35+
/// preimage is provided, or when the value to holder is restored.
36+
///
37+
/// Note that all the relevant preimages will be provided, but there may also be additional
38+
/// irrelevant or duplicate preimages.
39+
//
40+
// TODO: Document the things someone using this interface should enforce before signing.
41+
fn sign_counterparty_commitment(&self, commitment_tx: &CommitmentTransaction,
42+
preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<secp256k1::All>
43+
) -> Result<(Signature, Vec<Signature>), ()>;
44+
/// Validate the counterparty's revocation.
45+
///
46+
/// This is required in order for the signer to make sure that the state has moved
47+
/// forward and it is safe to sign the next counterparty commitment.
48+
fn validate_counterparty_revocation(&self, idx: u64, secret: &SecretKey) -> Result<(), ()>;
49+
/// Creates a signature for a holder's commitment transaction and its claiming HTLC transactions.
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+
/// - with the previous `commitment_tx`, just to get claiming HTLC
55+
/// signatures, if we are reacting to a [`ChannelMonitor`]
56+
/// [replica](https://github.com/lightningdevkit/rust-lightning/blob/main/GLOSSARY.md#monitor-replicas)
57+
/// that decided to broadcast before it had been updated to the latest `commitment_tx`.
58+
///
59+
/// This may be called multiple times for the same transaction.
60+
///
61+
/// An external signer implementation should check that the commitment has not been revoked.
62+
///
63+
/// [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
64+
// TODO: Document the things someone using this interface should enforce before signing.
65+
fn sign_holder_commitment_and_htlcs(&self, commitment_tx: &HolderCommitmentTransaction,
66+
secp_ctx: &Secp256k1<secp256k1::All>) -> Result<(Signature, Vec<Signature>), ()>;
67+
/// Same as [`sign_holder_commitment_and_htlcs`], but exists only for tests to get access to
68+
/// holder commitment transactions which will be broadcasted later, after the channel has moved
69+
/// on to a newer state. Thus, needs its own method as [`sign_holder_commitment_and_htlcs`] may
70+
/// enforce that we only ever get called once.
71+
#[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
72+
fn unsafe_sign_holder_commitment_and_htlcs(&self, commitment_tx: &HolderCommitmentTransaction,
73+
secp_ctx: &Secp256k1<secp256k1::All>) -> Result<(Signature, Vec<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+
fn sign_justice_revoked_output(&self, justice_tx: &Transaction, input: usize, amount: u64,
89+
per_commitment_key: &SecretKey, secp_ctx: &Secp256k1<secp256k1::All>
90+
) -> Result<Signature, ()>;
91+
/// Create a signature for the given input in a transaction spending a commitment transaction
92+
/// HTLC output when our counterparty broadcasts an old state.
93+
///
94+
/// A justice transaction may claim multiple outputs at the same time if timelocks are
95+
/// similar, but only a signature for the input at index `input` should be signed for here.
96+
/// It may be called multiple times for same output(s) if a fee-bump is needed with regards
97+
/// to an upcoming timelock expiration.
98+
///
99+
/// `amount` is the value of the output spent by this input, committed to in the BIP 143
100+
/// signature.
101+
///
102+
/// `per_commitment_key` is revocation secret which was provided by our counterparty when they
103+
/// revoked the state which they eventually broadcast. It's not a _holder_ secret key and does
104+
/// not allow the spending of any funds by itself (you need our holder revocation_secret to do
105+
/// so).
106+
///
107+
/// `htlc` holds HTLC elements (hash, timelock), thus changing the format of the witness script
108+
/// (which is committed to in the BIP 143 signatures).
109+
fn sign_justice_revoked_htlc(&self, justice_tx: &Transaction, input: usize, amount: u64,
110+
per_commitment_key: &SecretKey, htlc: &HTLCOutputInCommitment,
111+
secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
112+
/// Computes the signature for a commitment transaction's HTLC output used as an input within
113+
/// `htlc_tx`, which spends the commitment transaction at index `input`. The signature returned
114+
/// must be be computed using [`EcdsaSighashType::All`]. Note that this should only be used to
115+
/// sign HTLC transactions from channels supporting anchor outputs after all additional
116+
/// inputs/outputs have been added to the transaction.
117+
///
118+
/// [`EcdsaSighashType::All`]: bitcoin::blockdata::transaction::EcdsaSighashType::All
119+
fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize,
120+
htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1<secp256k1::All>
121+
) -> Result<Signature, ()>;
122+
/// Create a signature for a claiming transaction for a HTLC output on a counterparty's commitment
123+
/// transaction, either offered or received.
124+
///
125+
/// Such a transaction may claim multiples offered outputs at same time if we know the
126+
/// preimage for each when we create it, but only the input at index `input` should be
127+
/// signed for here. It may be called multiple times for same output(s) if a fee-bump is
128+
/// needed with regards to an upcoming timelock expiration.
129+
///
130+
/// `witness_script` is either an offered or received script as defined in BOLT3 for HTLC
131+
/// outputs.
132+
///
133+
/// `amount` is value of the output spent by this input, committed to in the BIP 143 signature.
134+
///
135+
/// `per_commitment_point` is the dynamic point corresponding to the channel state
136+
/// detected onchain. It has been generated by our counterparty and is used to derive
137+
/// channel state keys, which are then included in the witness script and committed to in the
138+
/// BIP 143 signature.
139+
fn sign_counterparty_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, amount: u64,
140+
per_commitment_point: &PublicKey, htlc: &HTLCOutputInCommitment,
141+
secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
142+
/// Create a signature for a (proposed) closing transaction.
143+
///
144+
/// Note that, due to rounding, there may be one "missing" satoshi, and either party may have
145+
/// chosen to forgo their output as dust.
146+
fn sign_closing_transaction(&self, closing_tx: &ClosingTransaction,
147+
secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
148+
/// Computes the signature for a commitment transaction's anchor output used as an
149+
/// input within `anchor_tx`, which spends the commitment transaction, at index `input`.
150+
fn sign_holder_anchor_input(
151+
&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
152+
) -> Result<Signature, ()>;
153+
/// Signs a channel announcement message with our funding key proving it comes from one of the
154+
/// channel participants.
155+
///
156+
/// Channel announcements also require a signature from each node's network key. Our node
157+
/// signature is computed through [`NodeSigner::sign_gossip_message`].
158+
///
159+
/// Note that if this fails or is rejected, the channel will not be publicly announced and
160+
/// our counterparty may (though likely will not) close the channel on us for violating the
161+
/// protocol.
162+
///
163+
/// [`NodeSigner::sign_gossip_message`]: crate::sign::NodeSigner::sign_gossip_message
164+
fn sign_channel_announcement_with_funding_key(
165+
&self, msg: &UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<secp256k1::All>
166+
) -> Result<Signature, ()>;
167+
}
168+
169+
/// A writeable signer.
170+
///
171+
/// There will always be two instances of a signer per channel, one occupied by the
172+
/// [`ChannelManager`] and another by the channel's [`ChannelMonitor`].
173+
///
174+
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
175+
/// [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
176+
pub trait WriteableEcdsaChannelSigner: EcdsaChannelSigner + Writeable {}

0 commit comments

Comments
 (0)