Skip to content

Commit f341a0c

Browse files
committed
taproot
1 parent 3d13ccd commit f341a0c

File tree

1 file changed

+139
-57
lines changed

1 file changed

+139
-57
lines changed

lightning/src/util/dyn_signer.rs

Lines changed: 139 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,46 @@ impl DynSigner {
6177

6278
impl WriteableEcdsaChannelSigner for DynSigner {}
6379

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

77133
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, ()>;
134+
fn sign_holder_commitment(
135+
&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>,
136+
) -> Result<Signature, ()> {
137+
self.inner.sign_holder_commitment(commitment_tx, secp_ctx)
138+
}
83139

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, ()>;
140+
#[cfg(any(test, feature = "unsafe_revoked_tx_signing"))]
141+
fn unsafe_sign_holder_commitment(
142+
&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>,
143+
) -> Result<Signature, ()> {
144+
self.inner.unsafe_sign_holder_commitment(commitment_tx, secp_ctx)
145+
}
88146

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>), ()>;
147+
fn sign_counterparty_commitment(
148+
&self, commitment_tx: &CommitmentTransaction, inbound_htlc_preimages: Vec<PaymentPreimage>,
149+
outbound_htlc_preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<secp256k1::All>,
150+
) -> Result<(Signature, Vec<Signature>), ()> {
151+
self.inner.sign_counterparty_commitment(commitment_tx, inbound_htlc_preimages, outbound_htlc_preimages, secp_ctx)
152+
}
93153

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, ()>;
154+
fn sign_justice_revoked_output(
155+
&self,
156+
justice_tx: &Transaction,
157+
input: usize,
158+
amount: u64,
159+
per_commitment_key: &SecretKey,
160+
secp_ctx: &Secp256k1<secp256k1::All>,
161+
) -> Result<Signature, ()> {
162+
EcdsaChannelSigner::sign_justice_revoked_output(&*self.inner, justice_tx, input, amount, per_commitment_key, secp_ctx)
163+
}
102164

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, ()>;
165+
fn sign_justice_revoked_htlc(
166+
&self,
167+
justice_tx: &Transaction,
168+
input: usize,
169+
amount: u64,
170+
per_commitment_key: &SecretKey,
171+
htlc: &HTLCOutputInCommitment,
172+
secp_ctx: &Secp256k1<secp256k1::All>,
173+
) -> Result<Signature, ()> {
174+
EcdsaChannelSigner::sign_justice_revoked_htlc(&*self.inner, justice_tx, input, amount, per_commitment_key, htlc, secp_ctx)
175+
}
112176

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, ()>;
177+
fn sign_counterparty_htlc_transaction(
178+
&self,
179+
htlc_tx: &Transaction,
180+
input: usize,
181+
amount: u64,
182+
per_commitment_point: &PublicKey,
183+
htlc: &HTLCOutputInCommitment,
184+
secp_ctx: &Secp256k1<secp256k1::All>,
185+
) -> Result<Signature, ()> {
186+
EcdsaChannelSigner::sign_counterparty_htlc_transaction(&*self.inner, htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx)
187+
}
122188

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

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

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

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

@@ -200,12 +272,12 @@ impl InnerSign for InMemorySigner {
200272
/// A convenience wrapper for DynKeysInterfaceTrait
201273
pub struct DynKeysInterface {
202274
/// The inner dyn keys interface
203-
pub inner: Box<dyn DynKeysInterfaceTrait<EcdsaSigner = DynSigner>>,
275+
pub inner: Box<dyn DynKeysInterfaceTrait>,
204276
}
205277

206278
impl DynKeysInterface {
207279
/// Create a new DynKeysInterface
208-
pub fn new(inner: Box<dyn DynKeysInterfaceTrait<EcdsaSigner = DynSigner>>) -> Self {
280+
pub fn new(inner: Box<dyn DynKeysInterfaceTrait>) -> Self {
209281
DynKeysInterface { inner }
210282
}
211283
}
@@ -239,6 +311,8 @@ impl NodeSigner for DynKeysInterface {
239311

240312
impl SignerProvider for DynKeysInterface {
241313
type EcdsaSigner = DynSigner;
314+
#[cfg(taproot)]
315+
type TaprootSigner = DynSigner;
242316

243317
delegate! {
244318
to self.inner {
@@ -275,10 +349,16 @@ impl OutputSpender for DynKeysInterface {
275349
}
276350
}
277351

352+
#[cfg(not(taproot))]
278353
/// A supertrait for all the traits that a keys interface implements
279354
pub trait DynKeysInterfaceTrait: NodeSigner + OutputSpender + SignerProvider<EcdsaSigner=DynSigner> + EntropySource + Send + Sync {
280355
}
281356

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

321401
impl SignerProvider for DynPhantomKeysInterface {
322402
type EcdsaSigner = DynSigner;
403+
#[cfg(taproot)]
404+
type TaprootSigner = DynSigner;
323405

324406
delegate! {
325407
to self.inner {

0 commit comments

Comments
 (0)