Skip to content

Commit 874d86f

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 2c5b90a commit 874d86f

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

236236
impl SignerProvider for KeyProvider {
237237
type EcdsaSigner = TestChannelSigner;
238+
#[cfg(taproot)]
239+
type TaprootSigner = TestChannelSigner;
238240

239241
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] {
240242
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
@@ -175,6 +175,8 @@ impl NodeSigner for KeyProvider {
175175

176176
impl SignerProvider for KeyProvider {
177177
type EcdsaSigner = TestChannelSigner;
178+
#[cfg(taproot)]
179+
type TaprootSigner = TestChannelSigner;
178180

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

lightning/src/ln/channel.rs

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

76137613
impl SignerProvider for Keys {
76147614
type EcdsaSigner = InMemorySigner;
7615+
#[cfg(taproot)]
7616+
type TaprootSigner = InMemorySigner;
76157617

76167618
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] {
76177619
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};
@@ -41,6 +43,8 @@ use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
4143
use crate::ln::{chan_utils, PaymentPreimage};
4244
use crate::ln::chan_utils::{HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, HolderCommitmentTransaction, ChannelTransactionParameters, CommitmentTransaction, ClosingTransaction};
4345
use crate::ln::msgs::{UnsignedChannelAnnouncement, UnsignedGossipMessage};
46+
#[cfg(taproot)]
47+
use crate::ln::msgs::PartialSignatureWithNonce;
4448
use crate::ln::script::ShutdownScript;
4549
use crate::offers::invoice::UnsignedBolt12Invoice;
4650
use crate::offers::invoice_request::UnsignedInvoiceRequest;
@@ -49,9 +53,13 @@ use crate::prelude::*;
4953
use core::convert::TryInto;
5054
use core::ops::Deref;
5155
use core::sync::atomic::{AtomicUsize, Ordering};
56+
#[cfg(taproot)]
57+
use musig2::types::{PartialSignature, PublicNonce, SecretNonce};
5258
use crate::io::{self, Error};
5359
use crate::ln::features::ChannelTypeFeatures;
5460
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
61+
#[cfg(taproot)]
62+
use crate::sign::taproot::TaprootChannelSigner;
5563
use crate::util::atomic_counter::AtomicCounter;
5664
use crate::util::chacha20::ChaCha20;
5765
use crate::util::invoice::construct_invoice_preimage;
@@ -670,6 +678,9 @@ pub trait NodeSigner {
670678
pub trait SignerProvider {
671679
/// A type which implements [`WriteableEcdsaChannelSigner`] which will be returned by [`Self::derive_channel_signer`].
672680
type EcdsaSigner: WriteableEcdsaChannelSigner;
681+
#[cfg(taproot)]
682+
/// A type which implements [`TaprootChannelSigner`]
683+
type TaprootSigner: TaprootChannelSigner;
673684

674685
/// Generates a unique `channel_keys_id` that can be used to obtain a [`Self::EcdsaSigner`] through
675686
/// [`SignerProvider::derive_channel_signer`]. The `user_channel_id` is provided to allow
@@ -1112,6 +1123,45 @@ impl EcdsaChannelSigner for InMemorySigner {
11121123
}
11131124
}
11141125

1126+
#[cfg(taproot)]
1127+
impl TaprootChannelSigner for InMemorySigner {
1128+
fn generate_local_nonce_pair(&self, commitment_number: u64, secp_ctx: &Secp256k1<All>) -> PublicNonce {
1129+
todo!()
1130+
}
1131+
1132+
fn partially_sign_counterparty_commitment(&self, counterparty_nonce: PublicNonce, commitment_tx: &CommitmentTransaction, preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<All>) -> Result<(PartialSignatureWithNonce, Vec<schnorr::Signature>), ()> {
1133+
todo!()
1134+
}
1135+
1136+
fn finalize_holder_commitment_and_htlc_signatures(&self, commitment_number: u64, commitment_tx: &HolderCommitmentTransaction, counterparty_partial_signature: PartialSignatureWithNonce, secp_ctx: &Secp256k1<All>) -> Result<(PartialSignature, Vec<schnorr::Signature>), ()> {
1137+
todo!()
1138+
}
1139+
1140+
fn sign_justice_revoked_output(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, secp_ctx: &Secp256k1<All>) -> Result<schnorr::Signature, ()> {
1141+
todo!()
1142+
}
1143+
1144+
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, ()> {
1145+
todo!()
1146+
}
1147+
1148+
fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1<All>) -> Result<schnorr::Signature, ()> {
1149+
todo!()
1150+
}
1151+
1152+
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, ()> {
1153+
todo!()
1154+
}
1155+
1156+
fn partially_sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<All>) -> Result<PartialSignature, ()> {
1157+
todo!()
1158+
}
1159+
1160+
fn sign_holder_anchor_input(&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<All>) -> Result<schnorr::Signature, ()> {
1161+
todo!()
1162+
}
1163+
}
1164+
11151165
const SERIALIZATION_VERSION: u8 = 1;
11161166

11171167
const MIN_SERIALIZATION_VERSION: u8 = 1;
@@ -1513,6 +1563,8 @@ impl NodeSigner for KeysManager {
15131563

15141564
impl SignerProvider for KeysManager {
15151565
type EcdsaSigner = InMemorySigner;
1566+
#[cfg(taproot)]
1567+
type TaprootSigner = InMemorySigner;
15161568

15171569
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, user_channel_id: u128) -> [u8; 32] {
15181570
let child_idx = self.channel_child_index.fetch_add(1, Ordering::AcqRel);
@@ -1632,6 +1684,8 @@ impl NodeSigner for PhantomKeysManager {
16321684

16331685
impl SignerProvider for PhantomKeysManager {
16341686
type EcdsaSigner = InMemorySigner;
1687+
#[cfg(taproot)]
1688+
type TaprootSigner = InMemorySigner;
16351689

16361690
fn generate_channel_keys_id(&self, inbound: bool, channel_value_satoshis: u64, user_channel_id: u128) -> [u8; 32] {
16371691
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::events::bump_transaction::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(
@@ -211,11 +219,11 @@ impl EcdsaChannelSigner for TestChannelSigner {
211219
) -> Result<Signature, ()> {
212220
assert_eq!(htlc_tx.input[input], htlc_descriptor.unsigned_tx_input());
213221
assert_eq!(htlc_tx.output[input], htlc_descriptor.tx_output(secp_ctx));
214-
Ok(self.inner.sign_holder_htlc_transaction(htlc_tx, input, htlc_descriptor, secp_ctx).unwrap())
222+
Ok(EcdsaChannelSigner::sign_holder_htlc_transaction(&self.inner, htlc_tx, input, htlc_descriptor, secp_ctx).unwrap())
215223
}
216224

217225
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, ()> {
218-
Ok(self.inner.sign_counterparty_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx).unwrap())
226+
Ok(EcdsaChannelSigner::sign_counterparty_htlc_transaction(&self.inner, htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx).unwrap())
219227
}
220228

221229
fn sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
@@ -231,7 +239,7 @@ impl EcdsaChannelSigner for TestChannelSigner {
231239
// As long as our minimum dust limit is enforced and is greater than our anchor output
232240
// value, an anchor output can only have an index within [0, 1].
233241
assert!(anchor_tx.input[input].previous_output.vout == 0 || anchor_tx.input[input].previous_output.vout == 1);
234-
self.inner.sign_holder_anchor_input(anchor_tx, input, secp_ctx)
242+
EcdsaChannelSigner::sign_holder_anchor_input(&self.inner, anchor_tx, input, secp_ctx)
235243
}
236244

237245
fn sign_channel_announcement_with_funding_key(
@@ -243,6 +251,45 @@ impl EcdsaChannelSigner for TestChannelSigner {
243251

244252
impl WriteableEcdsaChannelSigner for TestChannelSigner {}
245253

254+
#[cfg(taproot)]
255+
impl TaprootChannelSigner for TestChannelSigner {
256+
fn generate_local_nonce_pair(&self, commitment_number: u64, secp_ctx: &Secp256k1<All>) -> PublicNonce {
257+
todo!()
258+
}
259+
260+
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>), ()> {
261+
todo!()
262+
}
263+
264+
fn finalize_holder_commitment_and_htlc_signatures(&self, commitment_number: u64, commitment_tx: &HolderCommitmentTransaction, counterparty_partial_signature: PartialSignatureWithNonce, secp_ctx: &Secp256k1<All>) -> Result<(PartialSignature, Vec<secp256k1::schnorr::Signature>), ()> {
265+
todo!()
266+
}
267+
268+
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, ()> {
269+
todo!()
270+
}
271+
272+
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, ()> {
273+
todo!()
274+
}
275+
276+
fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1<All>) -> Result<secp256k1::schnorr::Signature, ()> {
277+
todo!()
278+
}
279+
280+
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, ()> {
281+
todo!()
282+
}
283+
284+
fn partially_sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<All>) -> Result<PartialSignature, ()> {
285+
todo!()
286+
}
287+
288+
fn sign_holder_anchor_input(&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<All>) -> Result<secp256k1::schnorr::Signature, ()> {
289+
todo!()
290+
}
291+
}
292+
246293
impl Writeable for TestChannelSigner {
247294
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
248295
// 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

@@ -1072,6 +1074,8 @@ impl NodeSigner for TestKeysInterface {
10721074

10731075
impl SignerProvider for TestKeysInterface {
10741076
type EcdsaSigner = TestChannelSigner;
1077+
#[cfg(taproot)]
1078+
type TaprootSigner = TestChannelSigner;
10751079

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

0 commit comments

Comments
 (0)