Skip to content

Commit c9c5084

Browse files
committed
Add TaprootSigner variant to SignerProvider.
Previously, SignerProvider was not laid out to support multiple signer types. However, with the distinction between ECDSA and Taproot signers, we now need to account for SignerProviders needing to support both. This approach does mean that if ever we introduced another signer type in the future, all implementers of SignerProvider would need to add it as an associated type, and would also need to write a set of dummy implementations for any Signer trait they do not wish to support. For the time being, the TaprootSigner associated type is cfg-gated.
1 parent e7399c4 commit c9c5084

File tree

7 files changed

+118
-5
lines changed

7 files changed

+118
-5
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ impl NodeSigner for KeyProvider {
231231

232232
impl SignerProvider for KeyProvider {
233233
type EcdsaSigner = TestChannelSigner;
234+
#[cfg(taproot)]
235+
type TaprootSigner = TestChannelSigner;
234236

235237
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] {
236238
let id = self.rand_bytes_id.fetch_add(1, atomic::Ordering::Relaxed) as u8;

fuzz/src/full_stack.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ impl NodeSigner for KeyProvider {
341341

342342
impl SignerProvider for KeyProvider {
343343
type EcdsaSigner = TestChannelSigner;
344+
#[cfg(taproot)]
345+
type TaprootSigner = TestChannelSigner;
344346

345347
fn generate_channel_keys_id(&self, inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] {
346348
let ctr = self.counter.fetch_add(1, Ordering::Relaxed) as u8;

fuzz/src/onion_message.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ impl NodeSigner for KeyProvider {
190190

191191
impl SignerProvider for KeyProvider {
192192
type EcdsaSigner = TestChannelSigner;
193+
#[cfg(taproot)]
194+
type TaprootSigner = TestChannelSigner;
193195

194196
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] { unreachable!() }
195197

lightning/src/ln/channel.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7851,6 +7851,8 @@ mod tests {
78517851

78527852
impl SignerProvider for Keys {
78537853
type EcdsaSigner = InMemorySigner;
7854+
#[cfg(taproot)]
7855+
type TaprootSigner = InMemorySigner;
78547856

78557857
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] {
78567858
self.signer.channel_keys_id()

lightning/src/sign/mod.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use bitcoin::hashes::sha256::Hash as Sha256;
2929
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
3030
use bitcoin::hash_types::WPubkeyHash;
3131

32+
#[cfg(taproot)]
33+
use bitcoin::secp256k1::All;
3234
use bitcoin::secp256k1::{KeyPair, PublicKey, Scalar, Secp256k1, SecretKey, Signing};
3335
use bitcoin::secp256k1::ecdh::SharedSecret;
3436
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
@@ -43,6 +45,8 @@ use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
4345
use crate::ln::{chan_utils, PaymentPreimage};
4446
use crate::ln::chan_utils::{HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, HolderCommitmentTransaction, ChannelTransactionParameters, CommitmentTransaction, ClosingTransaction};
4547
use crate::ln::msgs::{UnsignedChannelAnnouncement, UnsignedGossipMessage};
48+
#[cfg(taproot)]
49+
use crate::ln::msgs::PartialSignatureWithNonce;
4650
use crate::ln::script::ShutdownScript;
4751
use crate::offers::invoice::UnsignedBolt12Invoice;
4852
use crate::offers::invoice_request::UnsignedInvoiceRequest;
@@ -51,9 +55,13 @@ use crate::prelude::*;
5155
use core::convert::TryInto;
5256
use core::ops::Deref;
5357
use core::sync::atomic::{AtomicUsize, Ordering};
58+
#[cfg(taproot)]
59+
use musig2::types::{PartialSignature, PublicNonce, SecretNonce};
5460
use crate::io::{self, Error};
5561
use crate::ln::features::ChannelTypeFeatures;
5662
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
63+
#[cfg(taproot)]
64+
use crate::sign::taproot::TaprootChannelSigner;
5765
use crate::util::atomic_counter::AtomicCounter;
5866
use crate::util::chacha20::ChaCha20;
5967
use crate::util::invoice::construct_invoice_preimage;
@@ -872,6 +880,9 @@ pub trait NodeSigner {
872880
pub trait SignerProvider {
873881
/// A type which implements [`WriteableEcdsaChannelSigner`] which will be returned by [`Self::derive_channel_signer`].
874882
type EcdsaSigner: WriteableEcdsaChannelSigner;
883+
#[cfg(taproot)]
884+
/// A type which implements [`TaprootChannelSigner`]
885+
type TaprootSigner: TaprootChannelSigner;
875886

876887
/// Generates a unique `channel_keys_id` that can be used to obtain a [`Self::EcdsaSigner`] through
877888
/// [`SignerProvider::derive_channel_signer`]. The `user_channel_id` is provided to allow
@@ -1370,6 +1381,45 @@ impl EcdsaChannelSigner for InMemorySigner {
13701381
}
13711382
}
13721383

1384+
#[cfg(taproot)]
1385+
impl TaprootChannelSigner for InMemorySigner {
1386+
fn generate_local_nonce_pair(&self, commitment_number: u64, secp_ctx: &Secp256k1<All>) -> PublicNonce {
1387+
todo!()
1388+
}
1389+
1390+
fn partially_sign_counterparty_commitment(&self, counterparty_nonce: PublicNonce, commitment_tx: &CommitmentTransaction, preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<All>) -> Result<(PartialSignatureWithNonce, Vec<schnorr::Signature>), ()> {
1391+
todo!()
1392+
}
1393+
1394+
fn finalize_holder_commitment(&self, commitment_number: u64, commitment_tx: &HolderCommitmentTransaction, counterparty_partial_signature: PartialSignatureWithNonce, secp_ctx: &Secp256k1<All>) -> Result<PartialSignature, ()> {
1395+
todo!()
1396+
}
1397+
1398+
fn sign_justice_revoked_output(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, secp_ctx: &Secp256k1<All>) -> Result<schnorr::Signature, ()> {
1399+
todo!()
1400+
}
1401+
1402+
fn sign_justice_revoked_htlc(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<All>) -> Result<schnorr::Signature, ()> {
1403+
todo!()
1404+
}
1405+
1406+
fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1<All>) -> Result<schnorr::Signature, ()> {
1407+
todo!()
1408+
}
1409+
1410+
fn sign_counterparty_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, amount: u64, per_commitment_point: &PublicKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<All>) -> Result<schnorr::Signature, ()> {
1411+
todo!()
1412+
}
1413+
1414+
fn partially_sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<All>) -> Result<PartialSignature, ()> {
1415+
todo!()
1416+
}
1417+
1418+
fn sign_holder_anchor_input(&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<All>) -> Result<schnorr::Signature, ()> {
1419+
todo!()
1420+
}
1421+
}
1422+
13731423
const SERIALIZATION_VERSION: u8 = 1;
13741424

13751425
const MIN_SERIALIZATION_VERSION: u8 = 1;
@@ -1773,6 +1823,8 @@ impl NodeSigner for KeysManager {
17731823

17741824
impl SignerProvider for KeysManager {
17751825
type EcdsaSigner = InMemorySigner;
1826+
#[cfg(taproot)]
1827+
type TaprootSigner = InMemorySigner;
17761828

17771829
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, user_channel_id: u128) -> [u8; 32] {
17781830
let child_idx = self.channel_child_index.fetch_add(1, Ordering::AcqRel);
@@ -1892,6 +1944,8 @@ impl NodeSigner for PhantomKeysManager {
18921944

18931945
impl SignerProvider for PhantomKeysManager {
18941946
type EcdsaSigner = InMemorySigner;
1947+
#[cfg(taproot)]
1948+
type TaprootSigner = InMemorySigner;
18951949

18961950
fn generate_channel_keys_id(&self, inbound: bool, channel_value_satoshis: u64, user_channel_id: u128) -> [u8; 32] {
18971951
self.inner.generate_channel_keys_id(inbound, channel_value_satoshis, user_channel_id)

lightning/src/util/test_channel_signer.rs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@ use bitcoin::sighash;
2323
use bitcoin::sighash::EcdsaSighashType;
2424

2525
use bitcoin::secp256k1;
26+
#[cfg(taproot)]
27+
use bitcoin::secp256k1::All;
2628
use bitcoin::secp256k1::{SecretKey, PublicKey};
2729
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
30+
#[cfg(taproot)]
31+
use musig2::types::{PartialSignature, PublicNonce, SecretNonce};
2832
use crate::sign::HTLCDescriptor;
2933
use crate::util::ser::{Writeable, Writer};
3034
use crate::io::Error;
3135
use crate::ln::features::ChannelTypeFeatures;
36+
#[cfg(taproot)]
37+
use crate::ln::msgs::PartialSignatureWithNonce;
38+
#[cfg(taproot)]
39+
use crate::sign::taproot::TaprootChannelSigner;
3240

3341
/// Initial value for revoked commitment downward counter
3442
pub const INITIAL_REVOKED_COMMITMENT_NUMBER: u64 = 1 << 48;
@@ -200,11 +208,11 @@ impl EcdsaChannelSigner for TestChannelSigner {
200208
}
201209

202210
fn sign_justice_revoked_output(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
203-
Ok(self.inner.sign_justice_revoked_output(justice_tx, input, amount, per_commitment_key, secp_ctx).unwrap())
211+
Ok(EcdsaChannelSigner::sign_justice_revoked_output(&self.inner, justice_tx, input, amount, per_commitment_key, secp_ctx).unwrap())
204212
}
205213

206214
fn sign_justice_revoked_htlc(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
207-
Ok(self.inner.sign_justice_revoked_htlc(justice_tx, input, amount, per_commitment_key, htlc, secp_ctx).unwrap())
215+
Ok(EcdsaChannelSigner::sign_justice_revoked_htlc(&self.inner, justice_tx, input, amount, per_commitment_key, htlc, secp_ctx).unwrap())
208216
}
209217

210218
fn sign_holder_htlc_transaction(
@@ -239,11 +247,11 @@ impl EcdsaChannelSigner for TestChannelSigner {
239247
&hash_to_message!(sighash.as_byte_array()), &htlc_descriptor.counterparty_sig, &countersignatory_htlc_key
240248
).unwrap();
241249
}
242-
Ok(self.inner.sign_holder_htlc_transaction(htlc_tx, input, htlc_descriptor, secp_ctx).unwrap())
250+
Ok(EcdsaChannelSigner::sign_holder_htlc_transaction(&self.inner, htlc_tx, input, htlc_descriptor, secp_ctx).unwrap())
243251
}
244252

245253
fn sign_counterparty_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, amount: u64, per_commitment_point: &PublicKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
246-
Ok(self.inner.sign_counterparty_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx).unwrap())
254+
Ok(EcdsaChannelSigner::sign_counterparty_htlc_transaction(&self.inner, htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx).unwrap())
247255
}
248256

249257
fn sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
@@ -259,7 +267,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
259267
// As long as our minimum dust limit is enforced and is greater than our anchor output
260268
// value, an anchor output can only have an index within [0, 1].
261269
assert!(anchor_tx.input[input].previous_output.vout == 0 || anchor_tx.input[input].previous_output.vout == 1);
262-
self.inner.sign_holder_anchor_input(anchor_tx, input, secp_ctx)
270+
EcdsaChannelSigner::sign_holder_anchor_input(&self.inner, anchor_tx, input, secp_ctx)
263271
}
264272

265273
fn sign_channel_announcement_with_funding_key(
@@ -271,6 +279,45 @@ impl EcdsaChannelSigner for TestChannelSigner {
271279

272280
impl WriteableEcdsaChannelSigner for TestChannelSigner {}
273281

282+
#[cfg(taproot)]
283+
impl TaprootChannelSigner for TestChannelSigner {
284+
fn generate_local_nonce_pair(&self, commitment_number: u64, secp_ctx: &Secp256k1<All>) -> PublicNonce {
285+
todo!()
286+
}
287+
288+
fn partially_sign_counterparty_commitment(&self, counterparty_nonce: PublicNonce, commitment_tx: &CommitmentTransaction, preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<All>) -> Result<(PartialSignatureWithNonce, Vec<secp256k1::schnorr::Signature>), ()> {
289+
todo!()
290+
}
291+
292+
fn finalize_holder_commitment(&self, commitment_number: u64, commitment_tx: &HolderCommitmentTransaction, counterparty_partial_signature: PartialSignatureWithNonce, secp_ctx: &Secp256k1<All>) -> Result<PartialSignature, ()> {
293+
todo!()
294+
}
295+
296+
fn sign_justice_revoked_output(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, secp_ctx: &Secp256k1<All>) -> Result<secp256k1::schnorr::Signature, ()> {
297+
todo!()
298+
}
299+
300+
fn sign_justice_revoked_htlc(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<All>) -> Result<secp256k1::schnorr::Signature, ()> {
301+
todo!()
302+
}
303+
304+
fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1<All>) -> Result<secp256k1::schnorr::Signature, ()> {
305+
todo!()
306+
}
307+
308+
fn sign_counterparty_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, amount: u64, per_commitment_point: &PublicKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<All>) -> Result<secp256k1::schnorr::Signature, ()> {
309+
todo!()
310+
}
311+
312+
fn partially_sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<All>) -> Result<PartialSignature, ()> {
313+
todo!()
314+
}
315+
316+
fn sign_holder_anchor_input(&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<All>) -> Result<secp256k1::schnorr::Signature, ()> {
317+
todo!()
318+
}
319+
}
320+
274321
impl Writeable for TestChannelSigner {
275322
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
276323
// TestChannelSigner has two fields - `inner` ([`InMemorySigner`]) and `state`

lightning/src/util/test_utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ impl EntropySource for OnlyReadsKeysInterface {
176176

177177
impl SignerProvider for OnlyReadsKeysInterface {
178178
type EcdsaSigner = TestChannelSigner;
179+
#[cfg(taproot)]
180+
type TaprootSigner = TestChannelSigner;
179181

180182
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] { unreachable!(); }
181183

@@ -1097,6 +1099,8 @@ impl NodeSigner for TestKeysInterface {
10971099

10981100
impl SignerProvider for TestKeysInterface {
10991101
type EcdsaSigner = TestChannelSigner;
1102+
#[cfg(taproot)]
1103+
type TaprootSigner = TestChannelSigner;
11001104

11011105
fn generate_channel_keys_id(&self, inbound: bool, channel_value_satoshis: u64, user_channel_id: u128) -> [u8; 32] {
11021106
self.backing.generate_channel_keys_id(inbound, channel_value_satoshis, user_channel_id)

0 commit comments

Comments
 (0)