Skip to content

Commit 897f881

Browse files
committed
Separate out the methods that would apply to any channel into a ChannelSigner, leaving the implementation-dependent ones in EcdsaChannelSigner.
1 parent 454f6ba commit 897f881

File tree

6 files changed

+53
-40
lines changed

6 files changed

+53
-40
lines changed

lightning/src/chain/keysinterface.rs

+39-29
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,12 @@ impl_writeable_tlv_based_enum!(SpendableOutputDescriptor,
212212
(2, StaticPaymentOutput),
213213
);
214214

215-
/// A trait to sign Lightning channel transactions as described in
216-
/// [BOLT 3](https://github.com/lightning/bolts/blob/master/03-transactions.md).
217-
///
218-
/// Signing services could be implemented on a hardware wallet and should implement signing
219-
/// policies in order to be secure. Please refer to the [VLS Policy
220-
/// Controls](https://gitlab.com/lightning-signer/validating-lightning-signer/-/blob/main/docs/policy-controls.md)
221-
/// for an example of such policies.
222-
pub trait EcdsaChannelSigner {
215+
pub trait ChannelSigner {
223216
/// Gets the per-commitment point for a specific commitment number
224217
///
225218
/// Note that the commitment number starts at `(1 << 48) - 1` and counts backwards.
226219
fn get_per_commitment_point(&self, idx: u64, secp_ctx: &Secp256k1<secp256k1::All>) -> PublicKey;
220+
227221
/// Gets the commitment secret for a specific commitment number as part of the revocation process
228222
///
229223
/// An external signer implementation should error here if the commitment was already signed
@@ -234,6 +228,7 @@ pub trait EcdsaChannelSigner {
234228
/// Note that the commitment number starts at `(1 << 48) - 1` and counts backwards.
235229
// TODO: return a Result so we can signal a validation error
236230
fn release_commitment_secret(&self, idx: u64) -> [u8; 32];
231+
237232
/// Validate the counterparty's signatures on the holder commitment transaction and HTLCs.
238233
///
239234
/// This is required in order for the signer to make sure that releasing a commitment
@@ -249,12 +244,35 @@ pub trait EcdsaChannelSigner {
249244
/// irrelevant or duplicate preimages.
250245
fn validate_holder_commitment(&self, holder_tx: &HolderCommitmentTransaction,
251246
preimages: Vec<PaymentPreimage>) -> Result<(), ()>;
247+
252248
/// Returns the holder's channel public keys and basepoints.
253249
fn pubkeys(&self) -> &ChannelPublicKeys;
250+
254251
/// Returns an arbitrary identifier describing the set of keys which are provided back to you in
255252
/// some [`SpendableOutputDescriptor`] types. This should be sufficient to identify this
256253
/// [`BaseSign`] object uniquely and lookup or re-derive its keys.
257254
fn channel_keys_id(&self) -> [u8; 32];
255+
256+
/// Set the counterparty static channel data, including basepoints,
257+
/// `counterparty_selected`/`holder_selected_contest_delay` and funding outpoint.
258+
///
259+
/// This data is static, and will never change for a channel once set. For a given [`BaseSign`]
260+
/// instance, LDK will call this method exactly once - either immediately after construction
261+
/// (not including if done via [`SignerProvider::read_chan_signer`]) or when the funding
262+
/// information has been generated.
263+
///
264+
/// channel_parameters.is_populated() MUST be true.
265+
fn provide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters);
266+
}
267+
268+
/// A trait to sign Lightning channel transactions as described in
269+
/// [BOLT 3](https://github.com/lightning/bolts/blob/master/03-transactions.md).
270+
///
271+
/// Signing services could be implemented on a hardware wallet and should implement signing
272+
/// policies in order to be secure. Please refer to the [VLS Policy
273+
/// Controls](https://gitlab.com/lightning-signer/validating-lightning-signer/-/blob/main/docs/policy-controls.md)
274+
/// for an example of such policies.
275+
pub trait EcdsaChannelSigner: ChannelSigner {
258276
/// Create a signature for a counterparty's commitment transaction and associated HTLC transactions.
259277
///
260278
/// Note that if signing fails or is rejected, the channel will be force-closed.
@@ -395,16 +413,6 @@ pub trait EcdsaChannelSigner {
395413
fn sign_channel_announcement_with_funding_key(
396414
&self, msg: &UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<secp256k1::All>
397415
) -> Result<Signature, ()>;
398-
/// Set the counterparty static channel data, including basepoints,
399-
/// `counterparty_selected`/`holder_selected_contest_delay` and funding outpoint.
400-
///
401-
/// This data is static, and will never change for a channel once set. For a given [`BaseSign`]
402-
/// instance, LDK will call this method exactly once - either immediately after construction
403-
/// (not including if done via [`SignerProvider::read_chan_signer`]) or when the funding
404-
/// information has been generated.
405-
///
406-
/// channel_parameters.is_populated() MUST be true.
407-
fn provide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters);
408416
}
409417

410418
/// A writeable signer.
@@ -725,7 +733,7 @@ impl InMemorySigner {
725733
}
726734
}
727735

728-
impl EcdsaChannelSigner for InMemorySigner {
736+
impl ChannelSigner for InMemorySigner {
729737
fn get_per_commitment_point(&self, idx: u64, secp_ctx: &Secp256k1<secp256k1::All>) -> PublicKey {
730738
let commitment_secret = SecretKey::from_slice(&chan_utils::build_commitment_secret(&self.commitment_seed, idx)).unwrap();
731739
PublicKey::from_secret_key(secp_ctx, &commitment_secret)
@@ -743,6 +751,18 @@ impl EcdsaChannelSigner for InMemorySigner {
743751

744752
fn channel_keys_id(&self) -> [u8; 32] { self.channel_keys_id }
745753

754+
fn provide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters) {
755+
assert!(self.channel_parameters.is_none() || self.channel_parameters.as_ref().unwrap() == channel_parameters);
756+
if self.channel_parameters.is_some() {
757+
// The channel parameters were already set and they match, return early.
758+
return;
759+
}
760+
assert!(channel_parameters.is_populated(), "Channel parameters must be fully populated");
761+
self.channel_parameters = Some(channel_parameters.clone());
762+
}
763+
}
764+
765+
impl EcdsaChannelSigner for InMemorySigner {
746766
fn sign_counterparty_commitment(&self, commitment_tx: &CommitmentTransaction, _preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<(Signature, Vec<Signature>), ()> {
747767
let trusted_tx = commitment_tx.trust();
748768
let keys = trusted_tx.keys();
@@ -871,16 +891,6 @@ impl EcdsaChannelSigner for InMemorySigner {
871891
let msghash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]);
872892
Ok(sign(secp_ctx, &msghash, &self.funding_key))
873893
}
874-
875-
fn provide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters) {
876-
assert!(self.channel_parameters.is_none() || self.channel_parameters.as_ref().unwrap() == channel_parameters);
877-
if self.channel_parameters.is_some() {
878-
// The channel parameters were already set and they match, return early.
879-
return;
880-
}
881-
assert!(channel_parameters.is_populated(), "Channel parameters must be fully populated");
882-
self.channel_parameters = Some(channel_parameters.clone());
883-
}
884894
}
885895

886896
const SERIALIZATION_VERSION: u8 = 1;

lightning/src/chain/onchaintx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use bitcoin::hash_types::{Txid, BlockHash};
2121
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
2222
use bitcoin::secp256k1;
2323

24-
use crate::chain::keysinterface::{EcdsaChannelSigner, EntropySource, SignerProvider};
24+
use crate::chain::keysinterface::{ChannelSigner, EntropySource, SignerProvider};
2525
use crate::ln::msgs::DecodeError;
2626
use crate::ln::PaymentPreimage;
2727
#[cfg(anchors)]

lightning/src/ln/chan_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,7 @@ mod tests {
16351635
use crate::ln::chan_utils::{get_htlc_redeemscript, get_to_countersignatory_with_anchors_redeemscript, CommitmentTransaction, TxCreationKeys, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, HTLCOutputInCommitment};
16361636
use bitcoin::secp256k1::{PublicKey, SecretKey, Secp256k1};
16371637
use crate::util::test_utils;
1638-
use crate::chain::keysinterface::{EcdsaChannelSigner, SignerProvider};
1638+
use crate::chain::keysinterface::{ChannelSigner, SignerProvider};
16391639
use bitcoin::{Network, Txid};
16401640
use bitcoin::hashes::Hash;
16411641
use crate::ln::PaymentHash;

lightning/src/ln/channel.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::chain::BestBlock;
3535
use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, LowerBoundedFeeEstimator};
3636
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS};
3737
use crate::chain::transaction::{OutPoint, TransactionData};
38-
use crate::chain::keysinterface::{Sign, EntropySource, EcdsaChannelSigner, SignerProvider, NodeSigner, Recipient};
38+
use crate::chain::keysinterface::{Sign, EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient};
3939
use crate::util::events::ClosureReason;
4040
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter};
4141
use crate::util::logger::Logger;
@@ -6871,7 +6871,7 @@ mod tests {
68716871
use crate::ln::chan_utils::{htlc_success_tx_weight, htlc_timeout_tx_weight};
68726872
use crate::chain::BestBlock;
68736873
use crate::chain::chaininterface::{FeeEstimator, LowerBoundedFeeEstimator, ConfirmationTarget};
6874-
use crate::chain::keysinterface::{EcdsaChannelSigner, InMemorySigner, EntropySource, SignerProvider};
6874+
use crate::chain::keysinterface::{ChannelSigner, InMemorySigner, EntropySource, SignerProvider};
68756875
use crate::chain::transaction::OutPoint;
68766876
use crate::util::config::UserConfig;
68776877
use crate::util::enforcing_trait_impls::EnforcingSigner;

lightning/src/ln/functional_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ 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::chain::keysinterface::{EcdsaChannelSigner, EntropySource};
20+
use crate::chain::keysinterface::{ChannelSigner, EcdsaChannelSigner, EntropySource};
2121
use crate::ln::{PaymentPreimage, PaymentSecret, PaymentHash};
2222
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};
2323
use crate::ln::channelmanager::{self, PaymentId, RAACommitmentOrder, PaymentSendFailure, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA};

lightning/src/util/enforcing_trait_impls.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use crate::ln::channel::{ANCHOR_OUTPUT_VALUE_SATOSHI, MIN_CHAN_DUST_LIMIT_SATOSHIS};
1111
use crate::ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitmentTransaction, CommitmentTransaction, ChannelTransactionParameters, TrustedCommitmentTransaction, ClosingTransaction};
1212
use crate::ln::{chan_utils, msgs, PaymentPreimage};
13-
use crate::chain::keysinterface::{Sign, InMemorySigner, EcdsaChannelSigner};
13+
use crate::chain::keysinterface::{Sign, InMemorySigner, ChannelSigner, EcdsaChannelSigner};
1414

1515
use crate::prelude::*;
1616
use core::cmp;
@@ -90,7 +90,7 @@ impl EnforcingSigner {
9090
}
9191
}
9292

93-
impl EcdsaChannelSigner for EnforcingSigner {
93+
impl ChannelSigner for EnforcingSigner {
9494
fn get_per_commitment_point(&self, idx: u64, secp_ctx: &Secp256k1<secp256k1::All>) -> PublicKey {
9595
self.inner.get_per_commitment_point(idx, secp_ctx)
9696
}
@@ -114,8 +114,15 @@ impl EcdsaChannelSigner for EnforcingSigner {
114114
}
115115

116116
fn pubkeys(&self) -> &ChannelPublicKeys { self.inner.pubkeys() }
117+
117118
fn channel_keys_id(&self) -> [u8; 32] { self.inner.channel_keys_id() }
118119

120+
fn provide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters) {
121+
self.inner.provide_channel_parameters(channel_parameters)
122+
}
123+
}
124+
125+
impl EcdsaChannelSigner for EnforcingSigner {
119126
fn sign_counterparty_commitment(&self, commitment_tx: &CommitmentTransaction, preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<(Signature, Vec<Signature>), ()> {
120127
self.verify_counterparty_commitment_tx(commitment_tx, secp_ctx);
121128

@@ -228,10 +235,6 @@ impl EcdsaChannelSigner for EnforcingSigner {
228235
) -> Result<Signature, ()> {
229236
self.inner.sign_channel_announcement_with_funding_key(msg, secp_ctx)
230237
}
231-
232-
fn provide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters) {
233-
self.inner.provide_channel_parameters(channel_parameters)
234-
}
235238
}
236239

237240
impl Sign for EnforcingSigner {}

0 commit comments

Comments
 (0)