Skip to content

Commit c6bcf75

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 f862aa9 commit c6bcf75

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
@@ -7863,6 +7863,8 @@ use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
78637863

78647864
impl SignerProvider for Keys {
78657865
type EcdsaSigner = InMemorySigner;
7866+
#[cfg(taproot)]
7867+
type TaprootSigner = InMemorySigner;
78667868

78677869
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] {
78687870
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};
@@ -44,6 +46,8 @@ 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::channel_keys::{DelayedPaymentBasepoint, DelayedPaymentKey, HtlcKey, HtlcBasepoint, RevocationKey, RevocationBasepoint};
4648
use crate::ln::msgs::{UnsignedChannelAnnouncement, UnsignedGossipMessage};
49+
#[cfg(taproot)]
50+
use crate::ln::msgs::PartialSignatureWithNonce;
4751
use crate::ln::script::ShutdownScript;
4852
use crate::offers::invoice::UnsignedBolt12Invoice;
4953
use crate::offers::invoice_request::UnsignedInvoiceRequest;
@@ -52,9 +56,13 @@ use crate::prelude::*;
5256
use core::convert::TryInto;
5357
use core::ops::Deref;
5458
use core::sync::atomic::{AtomicUsize, Ordering};
59+
#[cfg(taproot)]
60+
use musig2::types::{PartialSignature, PublicNonce, SecretNonce};
5561
use crate::io::{self, Error};
5662
use crate::ln::features::ChannelTypeFeatures;
5763
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
64+
#[cfg(taproot)]
65+
use crate::sign::taproot::TaprootChannelSigner;
5866
use crate::util::atomic_counter::AtomicCounter;
5967
use crate::util::chacha20::ChaCha20;
6068
use crate::util::invoice::construct_invoice_preimage;
@@ -869,6 +877,9 @@ pub trait NodeSigner {
869877
pub trait SignerProvider {
870878
/// A type which implements [`WriteableEcdsaChannelSigner`] which will be returned by [`Self::derive_channel_signer`].
871879
type EcdsaSigner: WriteableEcdsaChannelSigner;
880+
#[cfg(taproot)]
881+
/// A type which implements [`TaprootChannelSigner`]
882+
type TaprootSigner: TaprootChannelSigner;
872883

873884
/// Generates a unique `channel_keys_id` that can be used to obtain a [`Self::EcdsaSigner`] through
874885
/// [`SignerProvider::derive_channel_signer`]. The `user_channel_id` is provided to allow
@@ -1380,6 +1391,45 @@ impl EcdsaChannelSigner for InMemorySigner {
13801391
}
13811392
}
13821393

1394+
#[cfg(taproot)]
1395+
impl TaprootChannelSigner for InMemorySigner {
1396+
fn generate_local_nonce_pair(&self, commitment_number: u64, secp_ctx: &Secp256k1<All>) -> PublicNonce {
1397+
todo!()
1398+
}
1399+
1400+
fn partially_sign_counterparty_commitment(&self, counterparty_nonce: PublicNonce, commitment_tx: &CommitmentTransaction, preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<All>) -> Result<(PartialSignatureWithNonce, Vec<schnorr::Signature>), ()> {
1401+
todo!()
1402+
}
1403+
1404+
fn finalize_holder_commitment(&self, commitment_number: u64, commitment_tx: &HolderCommitmentTransaction, counterparty_partial_signature: PartialSignatureWithNonce, secp_ctx: &Secp256k1<All>) -> Result<PartialSignature, ()> {
1405+
todo!()
1406+
}
1407+
1408+
fn sign_justice_revoked_output(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, secp_ctx: &Secp256k1<All>) -> Result<schnorr::Signature, ()> {
1409+
todo!()
1410+
}
1411+
1412+
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, ()> {
1413+
todo!()
1414+
}
1415+
1416+
fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1<All>) -> Result<schnorr::Signature, ()> {
1417+
todo!()
1418+
}
1419+
1420+
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, ()> {
1421+
todo!()
1422+
}
1423+
1424+
fn partially_sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<All>) -> Result<PartialSignature, ()> {
1425+
todo!()
1426+
}
1427+
1428+
fn sign_holder_anchor_input(&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<All>) -> Result<schnorr::Signature, ()> {
1429+
todo!()
1430+
}
1431+
}
1432+
13831433
const SERIALIZATION_VERSION: u8 = 1;
13841434

13851435
const MIN_SERIALIZATION_VERSION: u8 = 1;
@@ -1783,6 +1833,8 @@ impl NodeSigner for KeysManager {
17831833

17841834
impl SignerProvider for KeysManager {
17851835
type EcdsaSigner = InMemorySigner;
1836+
#[cfg(taproot)]
1837+
type TaprootSigner = InMemorySigner;
17861838

17871839
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, user_channel_id: u128) -> [u8; 32] {
17881840
let child_idx = self.channel_child_index.fetch_add(1, Ordering::AcqRel);
@@ -1902,6 +1954,8 @@ impl NodeSigner for PhantomKeysManager {
19021954

19031955
impl SignerProvider for PhantomKeysManager {
19041956
type EcdsaSigner = InMemorySigner;
1957+
#[cfg(taproot)]
1958+
type TaprootSigner = InMemorySigner;
19051959

19061960
fn generate_channel_keys_id(&self, inbound: bool, channel_value_satoshis: u64, user_channel_id: u128) -> [u8; 32] {
19071961
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
@@ -24,12 +24,20 @@ use bitcoin::sighash;
2424
use bitcoin::sighash::EcdsaSighashType;
2525

2626
use bitcoin::secp256k1;
27+
#[cfg(taproot)]
28+
use bitcoin::secp256k1::All;
2729
use bitcoin::secp256k1::{SecretKey, PublicKey};
2830
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
31+
#[cfg(taproot)]
32+
use musig2::types::{PartialSignature, PublicNonce, SecretNonce};
2933
use crate::sign::HTLCDescriptor;
3034
use crate::util::ser::{Writeable, Writer};
3135
use crate::io::Error;
3236
use crate::ln::features::ChannelTypeFeatures;
37+
#[cfg(taproot)]
38+
use crate::ln::msgs::PartialSignatureWithNonce;
39+
#[cfg(taproot)]
40+
use crate::sign::taproot::TaprootChannelSigner;
3341

3442
/// Initial value for revoked commitment downward counter
3543
pub const INITIAL_REVOKED_COMMITMENT_NUMBER: u64 = 1 << 48;
@@ -201,11 +209,11 @@ impl EcdsaChannelSigner for TestChannelSigner {
201209
}
202210

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

207215
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, ()> {
208-
Ok(self.inner.sign_justice_revoked_htlc(justice_tx, input, amount, per_commitment_key, htlc, secp_ctx).unwrap())
216+
Ok(EcdsaChannelSigner::sign_justice_revoked_htlc(&self.inner, justice_tx, input, amount, per_commitment_key, htlc, secp_ctx).unwrap())
209217
}
210218

211219
fn sign_holder_htlc_transaction(
@@ -241,11 +249,11 @@ impl EcdsaChannelSigner for TestChannelSigner {
241249
&hash_to_message!(sighash.as_byte_array()), &htlc_descriptor.counterparty_sig, &countersignatory_htlc_key.to_public_key()
242250
).unwrap();
243251
}
244-
Ok(self.inner.sign_holder_htlc_transaction(htlc_tx, input, htlc_descriptor, secp_ctx).unwrap())
252+
Ok(EcdsaChannelSigner::sign_holder_htlc_transaction(&self.inner, htlc_tx, input, htlc_descriptor, secp_ctx).unwrap())
245253
}
246254

247255
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, ()> {
248-
Ok(self.inner.sign_counterparty_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx).unwrap())
256+
Ok(EcdsaChannelSigner::sign_counterparty_htlc_transaction(&self.inner, htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx).unwrap())
249257
}
250258

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

267275
fn sign_channel_announcement_with_funding_key(
@@ -273,6 +281,45 @@ impl EcdsaChannelSigner for TestChannelSigner {
273281

274282
impl WriteableEcdsaChannelSigner for TestChannelSigner {}
275283

284+
#[cfg(taproot)]
285+
impl TaprootChannelSigner for TestChannelSigner {
286+
fn generate_local_nonce_pair(&self, commitment_number: u64, secp_ctx: &Secp256k1<All>) -> PublicNonce {
287+
todo!()
288+
}
289+
290+
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>), ()> {
291+
todo!()
292+
}
293+
294+
fn finalize_holder_commitment(&self, commitment_number: u64, commitment_tx: &HolderCommitmentTransaction, counterparty_partial_signature: PartialSignatureWithNonce, secp_ctx: &Secp256k1<All>) -> Result<PartialSignature, ()> {
295+
todo!()
296+
}
297+
298+
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, ()> {
299+
todo!()
300+
}
301+
302+
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, ()> {
303+
todo!()
304+
}
305+
306+
fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1<All>) -> Result<secp256k1::schnorr::Signature, ()> {
307+
todo!()
308+
}
309+
310+
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, ()> {
311+
todo!()
312+
}
313+
314+
fn partially_sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<All>) -> Result<PartialSignature, ()> {
315+
todo!()
316+
}
317+
318+
fn sign_holder_anchor_input(&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<All>) -> Result<secp256k1::schnorr::Signature, ()> {
319+
todo!()
320+
}
321+
}
322+
276323
impl Writeable for TestChannelSigner {
277324
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
278325
// 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)