Skip to content

Commit 9d4e233

Browse files
committed
taproot
1 parent 3d13ccd commit 9d4e233

File tree

1 file changed

+138
-57
lines changed

1 file changed

+138
-57
lines changed

lightning/src/util/dyn_signer.rs

Lines changed: 138 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ use crate::sign::ecdsa::EcdsaChannelSigner;
2121
use crate::sign::InMemorySigner;
2222
use crate::sign::{
2323
KeyMaterial, NodeSigner, Recipient, SignerProvider, SpendableOutputDescriptor,
24-
ecdsa::WriteableEcdsaChannelSigner,
24+
ecdsa::WriteableEcdsaChannelSigner
2525
};
26+
#[cfg(taproot)]
27+
use crate::sign::taproot::{TaprootChannelSigner};
28+
#[cfg(taproot)]
29+
use musig2::types::{PartialSignature, PublicNonce};
2630
use crate::util::ser::{Readable, ReadableArgs};
2731
use crate::util::ser::{Writeable, Writer};
2832
use bitcoin;
@@ -36,6 +40,7 @@ use crate::ln::features::ChannelTypeFeatures;
3640
#[cfg(any(test, feature = "_test_utils"))]
3741
use crate::util::test_utils::OnlyReadsKeysInterface;
3842

43+
#[cfg(not(taproot))]
3944
/// Helper to allow DynSigner to clone itself
4045
pub trait InnerSign: EcdsaChannelSigner + Send + Sync {
4146
/// Clone into a Box
@@ -46,6 +51,17 @@ pub trait InnerSign: EcdsaChannelSigner + Send + Sync {
4651
fn vwrite(&self, writer: &mut Vec<u8>) -> Result<(), Error>;
4752
}
4853

54+
#[cfg(taproot)]
55+
/// Helper to allow DynSigner to clone itself
56+
pub trait InnerSign: EcdsaChannelSigner + TaprootChannelSigner + Send + Sync {
57+
/// Clone into a Box
58+
fn box_clone(&self) -> Box<dyn crate::util::dyn_signer::InnerSign>;
59+
/// Cast to Any for runtime type checking
60+
fn as_any(&self) -> &dyn Any;
61+
/// Serialize the signer
62+
fn vwrite(&self, writer: &mut Vec<u8>) -> Result<(), Error>;
63+
}
64+
4965
/// A ChannelSigner derived struct allowing run-time selection of a signer
5066
pub struct DynSigner {
5167
/// The inner signer
@@ -61,6 +77,45 @@ impl DynSigner {
6177

6278
impl WriteableEcdsaChannelSigner for DynSigner {}
6379

80+
#[allow(unused_variables)]
81+
impl TaprootChannelSigner for DynSigner {
82+
fn generate_local_nonce_pair(&self, commitment_number: u64, secp_ctx: &Secp256k1<All>) -> PublicNonce {
83+
todo!()
84+
}
85+
86+
fn partially_sign_counterparty_commitment(&self, counterparty_nonce: PublicNonce, commitment_tx: &CommitmentTransaction, inbound_htlc_preimages: Vec<PaymentPreimage>, outbound_htlc_preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<All>) -> Result<(crate::ln::msgs::PartialSignatureWithNonce, Vec<secp256k1::schnorr::Signature>), ()> {
87+
todo!();
88+
}
89+
90+
fn finalize_holder_commitment(&self, commitment_tx: &HolderCommitmentTransaction, counterparty_partial_signature: crate::ln::msgs::PartialSignatureWithNonce, secp_ctx: &Secp256k1<All>) -> Result<PartialSignature, ()> {
91+
todo!();
92+
}
93+
94+
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, ()> {
95+
todo!();
96+
}
97+
98+
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, ()> {
99+
todo!();
100+
}
101+
102+
fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1<All>) -> Result<secp256k1::schnorr::Signature, ()> {
103+
todo!();
104+
}
105+
106+
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, ()> {
107+
todo!();
108+
}
109+
110+
fn partially_sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<All>) -> Result<PartialSignature, ()> {
111+
todo!();
112+
}
113+
114+
fn sign_holder_anchor_input(&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<All>) -> Result<secp256k1::schnorr::Signature, ()> {
115+
todo!();
116+
}
117+
}
118+
64119
impl Clone for DynSigner {
65120
fn clone(&self) -> Self {
66121
DynSigner { inner: self.inner.box_clone() }
@@ -75,69 +130,85 @@ impl Readable for DynSigner {
75130
}
76131

77132
impl EcdsaChannelSigner for DynSigner {
78-
delegate! {
79-
to self.inner {
80-
fn sign_holder_commitment(
81-
&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>,
82-
) -> Result<Signature, ()>;
133+
fn sign_holder_commitment(
134+
&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>,
135+
) -> Result<Signature, ()> {
136+
self.inner.sign_holder_commitment(commitment_tx, secp_ctx)
137+
}
83138

84-
#[cfg(any(test, feature = "unsafe_revoked_tx_signing"))]
85-
fn unsafe_sign_holder_commitment(
86-
&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>,
87-
) -> Result<Signature, ()>;
139+
#[cfg(any(test, feature = "unsafe_revoked_tx_signing"))]
140+
fn unsafe_sign_holder_commitment(
141+
&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>,
142+
) -> Result<Signature, ()> {
143+
self.inner.unsafe_sign_holder_commitment(commitment_tx, secp_ctx)
144+
}
88145

89-
fn sign_counterparty_commitment(
90-
&self, commitment_tx: &CommitmentTransaction, inbound_htlc_preimages: Vec<PaymentPreimage>,
91-
outbound_htlc_preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<secp256k1::All>,
92-
) -> Result<(Signature, Vec<Signature>), ()>;
146+
fn sign_counterparty_commitment(
147+
&self, commitment_tx: &CommitmentTransaction, inbound_htlc_preimages: Vec<PaymentPreimage>,
148+
outbound_htlc_preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<secp256k1::All>,
149+
) -> Result<(Signature, Vec<Signature>), ()> {
150+
self.inner.sign_counterparty_commitment(commitment_tx, inbound_htlc_preimages, outbound_htlc_preimages, secp_ctx)
151+
}
93152

94-
fn sign_justice_revoked_output(
95-
&self,
96-
justice_tx: &Transaction,
97-
input: usize,
98-
amount: u64,
99-
per_commitment_key: &SecretKey,
100-
secp_ctx: &Secp256k1<secp256k1::All>,
101-
) -> Result<Signature, ()>;
153+
fn sign_justice_revoked_output(
154+
&self,
155+
justice_tx: &Transaction,
156+
input: usize,
157+
amount: u64,
158+
per_commitment_key: &SecretKey,
159+
secp_ctx: &Secp256k1<secp256k1::All>,
160+
) -> Result<Signature, ()> {
161+
EcdsaChannelSigner::sign_justice_revoked_output(&*self.inner, justice_tx, input, amount, per_commitment_key, secp_ctx)
162+
}
102163

103-
fn sign_justice_revoked_htlc(
104-
&self,
105-
justice_tx: &Transaction,
106-
input: usize,
107-
amount: u64,
108-
per_commitment_key: &SecretKey,
109-
htlc: &HTLCOutputInCommitment,
110-
secp_ctx: &Secp256k1<secp256k1::All>,
111-
) -> Result<Signature, ()>;
164+
fn sign_justice_revoked_htlc(
165+
&self,
166+
justice_tx: &Transaction,
167+
input: usize,
168+
amount: u64,
169+
per_commitment_key: &SecretKey,
170+
htlc: &HTLCOutputInCommitment,
171+
secp_ctx: &Secp256k1<secp256k1::All>,
172+
) -> Result<Signature, ()> {
173+
EcdsaChannelSigner::sign_justice_revoked_htlc(&*self.inner, justice_tx, input, amount, per_commitment_key, htlc, secp_ctx)
174+
}
112175

113-
fn sign_counterparty_htlc_transaction(
114-
&self,
115-
htlc_tx: &Transaction,
116-
input: usize,
117-
amount: u64,
118-
per_commitment_point: &PublicKey,
119-
htlc: &HTLCOutputInCommitment,
120-
secp_ctx: &Secp256k1<secp256k1::All>,
121-
) -> Result<Signature, ()>;
176+
fn sign_counterparty_htlc_transaction(
177+
&self,
178+
htlc_tx: &Transaction,
179+
input: usize,
180+
amount: u64,
181+
per_commitment_point: &PublicKey,
182+
htlc: &HTLCOutputInCommitment,
183+
secp_ctx: &Secp256k1<secp256k1::All>,
184+
) -> Result<Signature, ()> {
185+
EcdsaChannelSigner::sign_counterparty_htlc_transaction(&*self.inner, htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx)
186+
}
122187

123-
fn sign_closing_transaction(
124-
&self,
125-
closing_tx: &ClosingTransaction,
126-
secp_ctx: &Secp256k1<secp256k1::All>,
127-
) -> Result<Signature, ()>;
188+
fn sign_closing_transaction(
189+
&self,
190+
closing_tx: &ClosingTransaction,
191+
secp_ctx: &Secp256k1<secp256k1::All>,
192+
) -> Result<Signature, ()> {
193+
self.inner.sign_closing_transaction(closing_tx, secp_ctx)
194+
}
128195

129-
fn sign_channel_announcement_with_funding_key(
130-
&self,
131-
msg: &UnsignedChannelAnnouncement,
132-
secp_ctx: &Secp256k1<secp256k1::All>,
133-
) -> Result<Signature, ()>;
196+
fn sign_channel_announcement_with_funding_key(
197+
&self,
198+
msg: &UnsignedChannelAnnouncement,
199+
secp_ctx: &Secp256k1<secp256k1::All>,
200+
) -> Result<Signature, ()> {
201+
self.inner.sign_channel_announcement_with_funding_key(msg, secp_ctx)
202+
}
134203

135-
fn sign_holder_anchor_input(
136-
&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
137-
) -> Result<Signature, ()>;
204+
fn sign_holder_anchor_input(
205+
&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
206+
) -> Result<Signature, ()> {
207+
EcdsaChannelSigner::sign_holder_anchor_input(&*self.inner, anchor_tx, input, secp_ctx)
208+
}
138209

139-
fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1<All>) -> Result<Signature, ()>;
140-
}
210+
fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1<All>) -> Result<Signature, ()> {
211+
EcdsaChannelSigner::sign_holder_htlc_transaction(&*self.inner, htlc_tx, input, htlc_descriptor, secp_ctx)
141212
}
142213
}
143214

@@ -200,12 +271,12 @@ impl InnerSign for InMemorySigner {
200271
/// A convenience wrapper for DynKeysInterfaceTrait
201272
pub struct DynKeysInterface {
202273
/// The inner dyn keys interface
203-
pub inner: Box<dyn DynKeysInterfaceTrait<EcdsaSigner = DynSigner>>,
274+
pub inner: Box<dyn DynKeysInterfaceTrait>,
204275
}
205276

206277
impl DynKeysInterface {
207278
/// Create a new DynKeysInterface
208-
pub fn new(inner: Box<dyn DynKeysInterfaceTrait<EcdsaSigner = DynSigner>>) -> Self {
279+
pub fn new(inner: Box<dyn DynKeysInterfaceTrait>) -> Self {
209280
DynKeysInterface { inner }
210281
}
211282
}
@@ -239,6 +310,8 @@ impl NodeSigner for DynKeysInterface {
239310

240311
impl SignerProvider for DynKeysInterface {
241312
type EcdsaSigner = DynSigner;
313+
#[cfg(taproot)]
314+
type TaprootSigner = DynSigner;
242315

243316
delegate! {
244317
to self.inner {
@@ -275,10 +348,16 @@ impl OutputSpender for DynKeysInterface {
275348
}
276349
}
277350

351+
#[cfg(not(taproot))]
278352
/// A supertrait for all the traits that a keys interface implements
279353
pub trait DynKeysInterfaceTrait: NodeSigner + OutputSpender + SignerProvider<EcdsaSigner=DynSigner> + EntropySource + Send + Sync {
280354
}
281355

356+
#[cfg(taproot)]
357+
/// A supertrait for all the traits that a keys interface implements
358+
pub trait DynKeysInterfaceTrait: NodeSigner + OutputSpender + SignerProvider<EcdsaSigner=DynSigner, TaprootSigner=DynSigner> + EntropySource + Send + Sync {
359+
}
360+
282361
/// A dyn wrapper for PhantomKeysManager
283362
pub struct DynPhantomKeysInterface {
284363
inner: PhantomKeysManager,
@@ -320,6 +399,8 @@ impl NodeSigner for DynPhantomKeysInterface {
320399

321400
impl SignerProvider for DynPhantomKeysInterface {
322401
type EcdsaSigner = DynSigner;
402+
#[cfg(taproot)]
403+
type TaprootSigner = DynSigner;
323404

324405
delegate! {
325406
to self.inner {

0 commit comments

Comments
 (0)