Skip to content

Commit 435bdbe

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 1f41e68 commit 435bdbe

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
@@ -340,6 +340,8 @@ impl NodeSigner for KeyProvider {
340340

341341
impl SignerProvider for KeyProvider {
342342
type EcdsaSigner = TestChannelSigner;
343+
#[cfg(taproot)]
344+
type TaprootSigner = TestChannelSigner;
343345

344346
fn generate_channel_keys_id(&self, inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] {
345347
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
@@ -26,6 +26,8 @@ use bitcoin::hashes::sha256::Hash as Sha256;
2626
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
2727
use bitcoin::hash_types::WPubkeyHash;
2828

29+
#[cfg(taproot)]
30+
use bitcoin::secp256k1::All;
2931
use bitcoin::secp256k1::{KeyPair, PublicKey, Scalar, Secp256k1, SecretKey, Signing};
3032
use bitcoin::secp256k1::ecdh::SharedSecret;
3133
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
@@ -40,6 +42,8 @@ use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
4042
use crate::ln::{chan_utils, PaymentPreimage};
4143
use crate::ln::chan_utils::{HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, HolderCommitmentTransaction, ChannelTransactionParameters, CommitmentTransaction, ClosingTransaction};
4244
use crate::ln::msgs::{UnsignedChannelAnnouncement, UnsignedGossipMessage};
45+
#[cfg(taproot)]
46+
use crate::ln::msgs::PartialSignatureWithNonce;
4347
use crate::ln::script::ShutdownScript;
4448
use crate::offers::invoice::UnsignedBolt12Invoice;
4549
use crate::offers::invoice_request::UnsignedInvoiceRequest;
@@ -48,9 +52,13 @@ use crate::prelude::*;
4852
use core::convert::TryInto;
4953
use core::ops::Deref;
5054
use core::sync::atomic::{AtomicUsize, Ordering};
55+
#[cfg(taproot)]
56+
use musig2::types::{PartialSignature, PublicNonce, SecretNonce};
5157
use crate::io::{self, Error};
5258
use crate::ln::features::ChannelTypeFeatures;
5359
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
60+
#[cfg(taproot)]
61+
use crate::sign::taproot::TaprootChannelSigner;
5462
use crate::util::atomic_counter::AtomicCounter;
5563
use crate::util::chacha20::ChaCha20;
5664
use crate::util::invoice::construct_invoice_preimage;
@@ -870,6 +878,9 @@ pub trait NodeSigner {
870878
pub trait SignerProvider {
871879
/// A type which implements [`WriteableEcdsaChannelSigner`] which will be returned by [`Self::derive_channel_signer`].
872880
type EcdsaSigner: WriteableEcdsaChannelSigner;
881+
#[cfg(taproot)]
882+
/// A type which implements [`TaprootChannelSigner`]
883+
type TaprootSigner: TaprootChannelSigner;
873884

874885
/// Generates a unique `channel_keys_id` that can be used to obtain a [`Self::EcdsaSigner`] through
875886
/// [`SignerProvider::derive_channel_signer`]. The `user_channel_id` is provided to allow
@@ -1366,6 +1377,45 @@ impl EcdsaChannelSigner for InMemorySigner {
13661377
}
13671378
}
13681379

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

13711421
const MIN_SERIALIZATION_VERSION: u8 = 1;
@@ -1769,6 +1819,8 @@ impl NodeSigner for KeysManager {
17691819

17701820
impl SignerProvider for KeysManager {
17711821
type EcdsaSigner = InMemorySigner;
1822+
#[cfg(taproot)]
1823+
type TaprootSigner = InMemorySigner;
17721824

17731825
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, user_channel_id: u128) -> [u8; 32] {
17741826
let child_idx = self.channel_child_index.fetch_add(1, Ordering::AcqRel);
@@ -1888,6 +1940,8 @@ impl NodeSigner for PhantomKeysManager {
18881940

18891941
impl SignerProvider for PhantomKeysManager {
18901942
type EcdsaSigner = InMemorySigner;
1943+
#[cfg(taproot)]
1944+
type TaprootSigner = InMemorySigner;
18911945

18921946
fn generate_channel_keys_id(&self, inbound: bool, channel_value_satoshis: u64, user_channel_id: u128) -> [u8; 32] {
18931947
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
@@ -21,12 +21,20 @@ use bitcoin::blockdata::transaction::{Transaction, EcdsaSighashType};
2121
use bitcoin::util::sighash;
2222

2323
use bitcoin::secp256k1;
24+
#[cfg(taproot)]
25+
use bitcoin::secp256k1::All;
2426
use bitcoin::secp256k1::{SecretKey, PublicKey};
2527
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
28+
#[cfg(taproot)]
29+
use musig2::types::{PartialSignature, PublicNonce, SecretNonce};
2630
use crate::sign::HTLCDescriptor;
2731
use crate::util::ser::{Writeable, Writer};
2832
use crate::io::Error;
2933
use crate::ln::features::ChannelTypeFeatures;
34+
#[cfg(taproot)]
35+
use crate::ln::msgs::PartialSignatureWithNonce;
36+
#[cfg(taproot)]
37+
use crate::sign::taproot::TaprootChannelSigner;
3038

3139
/// Initial value for revoked commitment downward counter
3240
pub const INITIAL_REVOKED_COMMITMENT_NUMBER: u64 = 1 << 48;
@@ -198,11 +206,11 @@ impl EcdsaChannelSigner for TestChannelSigner {
198206
}
199207

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

204212
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, ()> {
205-
Ok(self.inner.sign_justice_revoked_htlc(justice_tx, input, amount, per_commitment_key, htlc, secp_ctx).unwrap())
213+
Ok(EcdsaChannelSigner::sign_justice_revoked_htlc(&self.inner, justice_tx, input, amount, per_commitment_key, htlc, secp_ctx).unwrap())
206214
}
207215

208216
fn sign_holder_htlc_transaction(
@@ -237,11 +245,11 @@ impl EcdsaChannelSigner for TestChannelSigner {
237245
&hash_to_message!(&sighash), &htlc_descriptor.counterparty_sig, &countersignatory_htlc_key
238246
).unwrap();
239247
}
240-
Ok(self.inner.sign_holder_htlc_transaction(htlc_tx, input, htlc_descriptor, secp_ctx).unwrap())
248+
Ok(EcdsaChannelSigner::sign_holder_htlc_transaction(&self.inner, htlc_tx, input, htlc_descriptor, secp_ctx).unwrap())
241249
}
242250

243251
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, ()> {
244-
Ok(self.inner.sign_counterparty_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx).unwrap())
252+
Ok(EcdsaChannelSigner::sign_counterparty_htlc_transaction(&self.inner, htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx).unwrap())
245253
}
246254

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

263271
fn sign_channel_announcement_with_funding_key(
@@ -269,6 +277,45 @@ impl EcdsaChannelSigner for TestChannelSigner {
269277

270278
impl WriteableEcdsaChannelSigner for TestChannelSigner {}
271279

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

178178
impl SignerProvider for OnlyReadsKeysInterface {
179179
type EcdsaSigner = TestChannelSigner;
180+
#[cfg(taproot)]
181+
type TaprootSigner = TestChannelSigner;
180182

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

@@ -1098,6 +1100,8 @@ impl NodeSigner for TestKeysInterface {
10981100

10991101
impl SignerProvider for TestKeysInterface {
11001102
type EcdsaSigner = TestChannelSigner;
1103+
#[cfg(taproot)]
1104+
type TaprootSigner = TestChannelSigner;
11011105

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

0 commit comments

Comments
 (0)