Skip to content

Commit f30dba3

Browse files
committed
taproot
1 parent 0fa3174 commit f30dba3

File tree

2 files changed

+194
-59
lines changed

2 files changed

+194
-59
lines changed

lightning/src/util/dyn_signer.rs

Lines changed: 193 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use crate::ln::msgs::{DecodeError, UnsignedChannelAnnouncement, UnsignedGossipMe
1616
use crate::ln::script::ShutdownScript;
1717
use crate::ln::PaymentPreimage;
1818
use crate::sign::ecdsa::EcdsaChannelSigner;
19+
#[cfg(taproot)]
20+
use crate::sign::taproot::TaprootChannelSigner;
1921
use crate::sign::ChannelSigner;
2022
use crate::sign::InMemorySigner;
2123
use crate::sign::{
@@ -32,16 +34,28 @@ use bitcoin::absolute::LockTime;
3234
use bitcoin::bech32::u5;
3335
use bitcoin::secp256k1::All;
3436
use bitcoin::{secp256k1, ScriptBuf, Transaction, TxOut};
37+
#[cfg(taproot)]
38+
use musig2::types::{PartialSignature, PublicNonce};
3539
use secp256k1::ecdsa::RecoverableSignature;
3640
use secp256k1::{ecdh::SharedSecret, ecdsa::Signature, PublicKey, Scalar, Secp256k1, SecretKey};
3741

42+
#[cfg(not(taproot))]
43+
/// A super-trait for all the traits that a dyn signer backing implements
44+
pub trait DynSignerTrait: EcdsaChannelSigner + Send + Sync {}
45+
46+
#[cfg(taproot)]
47+
/// A super-trait for all the traits that a dyn signer backing implements
48+
pub trait DynSignerTrait: EcdsaChannelSigner + TaprootChannelSigner + Send + Sync {}
49+
3850
/// Helper to allow DynSigner to clone itself
39-
pub trait InnerSign: EcdsaChannelSigner + Send + Sync {
51+
pub trait InnerSign: DynSignerTrait {
4052
/// Clone into a Box
4153
fn box_clone(&self) -> Box<dyn InnerSign>;
4254
/// Cast to Any for runtime type checking
4355
fn as_any(&self) -> &dyn Any;
44-
/// Serialize the signer
56+
/// Serialize the signer.
57+
/// We can't have a write method with a generic (i.e. `Writeable`) because that would make signers
58+
/// dyn object incompatible.
4559
fn vwrite(&self, writer: &mut Vec<u8>) -> Result<(), Error>;
4660
}
4761

@@ -60,6 +74,73 @@ impl DynSigner {
6074

6175
impl WriteableEcdsaChannelSigner for DynSigner {}
6276

77+
#[cfg(taproot)]
78+
#[allow(unused_variables)]
79+
impl TaprootChannelSigner for DynSigner {
80+
fn generate_local_nonce_pair(
81+
&self, commitment_number: u64, secp_ctx: &Secp256k1<All>,
82+
) -> PublicNonce {
83+
todo!()
84+
}
85+
86+
fn partially_sign_counterparty_commitment(
87+
&self, counterparty_nonce: PublicNonce, commitment_tx: &CommitmentTransaction,
88+
inbound_htlc_preimages: Vec<PaymentPreimage>,
89+
outbound_htlc_preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<All>,
90+
) -> Result<(crate::ln::msgs::PartialSignatureWithNonce, Vec<secp256k1::schnorr::Signature>), ()>
91+
{
92+
todo!();
93+
}
94+
95+
fn finalize_holder_commitment(
96+
&self, commitment_tx: &HolderCommitmentTransaction,
97+
counterparty_partial_signature: crate::ln::msgs::PartialSignatureWithNonce,
98+
secp_ctx: &Secp256k1<All>,
99+
) -> Result<PartialSignature, ()> {
100+
todo!();
101+
}
102+
103+
fn sign_justice_revoked_output(
104+
&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey,
105+
secp_ctx: &Secp256k1<All>,
106+
) -> Result<secp256k1::schnorr::Signature, ()> {
107+
todo!();
108+
}
109+
110+
fn sign_justice_revoked_htlc(
111+
&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey,
112+
htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<All>,
113+
) -> Result<secp256k1::schnorr::Signature, ()> {
114+
todo!();
115+
}
116+
117+
fn sign_holder_htlc_transaction(
118+
&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor,
119+
secp_ctx: &Secp256k1<All>,
120+
) -> Result<secp256k1::schnorr::Signature, ()> {
121+
todo!();
122+
}
123+
124+
fn sign_counterparty_htlc_transaction(
125+
&self, htlc_tx: &Transaction, input: usize, amount: u64, per_commitment_point: &PublicKey,
126+
htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<All>,
127+
) -> Result<secp256k1::schnorr::Signature, ()> {
128+
todo!();
129+
}
130+
131+
fn partially_sign_closing_transaction(
132+
&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<All>,
133+
) -> Result<PartialSignature, ()> {
134+
todo!();
135+
}
136+
137+
fn sign_holder_anchor_input(
138+
&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<All>,
139+
) -> Result<secp256k1::schnorr::Signature, ()> {
140+
todo!();
141+
}
142+
}
143+
63144
impl Clone for DynSigner {
64145
fn clone(&self) -> Self {
65146
DynSigner { inner: self.inner.box_clone() }
@@ -74,69 +155,104 @@ impl Readable for DynSigner {
74155
}
75156

76157
impl EcdsaChannelSigner for DynSigner {
77-
delegate! {
78-
to self.inner {
79-
fn sign_holder_commitment(
80-
&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>,
81-
) -> Result<Signature, ()>;
158+
fn sign_holder_commitment(
159+
&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>,
160+
) -> Result<Signature, ()> {
161+
self.inner.sign_holder_commitment(commitment_tx, secp_ctx)
162+
}
82163

83-
#[cfg(any(test, feature = "unsafe_revoked_tx_signing"))]
84-
fn unsafe_sign_holder_commitment(
85-
&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>,
86-
) -> Result<Signature, ()>;
164+
#[cfg(any(test, feature = "unsafe_revoked_tx_signing"))]
165+
fn unsafe_sign_holder_commitment(
166+
&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>,
167+
) -> Result<Signature, ()> {
168+
self.inner.unsafe_sign_holder_commitment(commitment_tx, secp_ctx)
169+
}
87170

88-
fn sign_counterparty_commitment(
89-
&self, commitment_tx: &CommitmentTransaction, inbound_htlc_preimages: Vec<PaymentPreimage>,
90-
outbound_htlc_preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<secp256k1::All>,
91-
) -> Result<(Signature, Vec<Signature>), ()>;
171+
fn sign_counterparty_commitment(
172+
&self, commitment_tx: &CommitmentTransaction, inbound_htlc_preimages: Vec<PaymentPreimage>,
173+
outbound_htlc_preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<secp256k1::All>,
174+
) -> Result<(Signature, Vec<Signature>), ()> {
175+
self.inner.sign_counterparty_commitment(
176+
commitment_tx,
177+
inbound_htlc_preimages,
178+
outbound_htlc_preimages,
179+
secp_ctx,
180+
)
181+
}
92182

93-
fn sign_justice_revoked_output(
94-
&self,
95-
justice_tx: &Transaction,
96-
input: usize,
97-
amount: u64,
98-
per_commitment_key: &SecretKey,
99-
secp_ctx: &Secp256k1<secp256k1::All>,
100-
) -> Result<Signature, ()>;
183+
fn sign_justice_revoked_output(
184+
&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey,
185+
secp_ctx: &Secp256k1<secp256k1::All>,
186+
) -> Result<Signature, ()> {
187+
EcdsaChannelSigner::sign_justice_revoked_output(
188+
&*self.inner,
189+
justice_tx,
190+
input,
191+
amount,
192+
per_commitment_key,
193+
secp_ctx,
194+
)
195+
}
101196

102-
fn sign_justice_revoked_htlc(
103-
&self,
104-
justice_tx: &Transaction,
105-
input: usize,
106-
amount: u64,
107-
per_commitment_key: &SecretKey,
108-
htlc: &HTLCOutputInCommitment,
109-
secp_ctx: &Secp256k1<secp256k1::All>,
110-
) -> Result<Signature, ()>;
197+
fn sign_justice_revoked_htlc(
198+
&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey,
199+
htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<secp256k1::All>,
200+
) -> Result<Signature, ()> {
201+
EcdsaChannelSigner::sign_justice_revoked_htlc(
202+
&*self.inner,
203+
justice_tx,
204+
input,
205+
amount,
206+
per_commitment_key,
207+
htlc,
208+
secp_ctx,
209+
)
210+
}
111211

112-
fn sign_counterparty_htlc_transaction(
113-
&self,
114-
htlc_tx: &Transaction,
115-
input: usize,
116-
amount: u64,
117-
per_commitment_point: &PublicKey,
118-
htlc: &HTLCOutputInCommitment,
119-
secp_ctx: &Secp256k1<secp256k1::All>,
120-
) -> Result<Signature, ()>;
212+
fn sign_counterparty_htlc_transaction(
213+
&self, htlc_tx: &Transaction, input: usize, amount: u64, per_commitment_point: &PublicKey,
214+
htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<secp256k1::All>,
215+
) -> Result<Signature, ()> {
216+
EcdsaChannelSigner::sign_counterparty_htlc_transaction(
217+
&*self.inner,
218+
htlc_tx,
219+
input,
220+
amount,
221+
per_commitment_point,
222+
htlc,
223+
secp_ctx,
224+
)
225+
}
121226

122-
fn sign_closing_transaction(
123-
&self,
124-
closing_tx: &ClosingTransaction,
125-
secp_ctx: &Secp256k1<secp256k1::All>,
126-
) -> Result<Signature, ()>;
227+
fn sign_closing_transaction(
228+
&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<secp256k1::All>,
229+
) -> Result<Signature, ()> {
230+
self.inner.sign_closing_transaction(closing_tx, secp_ctx)
231+
}
127232

128-
fn sign_channel_announcement_with_funding_key(
129-
&self,
130-
msg: &UnsignedChannelAnnouncement,
131-
secp_ctx: &Secp256k1<secp256k1::All>,
132-
) -> Result<Signature, ()>;
233+
fn sign_channel_announcement_with_funding_key(
234+
&self, msg: &UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<secp256k1::All>,
235+
) -> Result<Signature, ()> {
236+
self.inner.sign_channel_announcement_with_funding_key(msg, secp_ctx)
237+
}
133238

134-
fn sign_holder_anchor_input(
135-
&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
136-
) -> Result<Signature, ()>;
239+
fn sign_holder_anchor_input(
240+
&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
241+
) -> Result<Signature, ()> {
242+
EcdsaChannelSigner::sign_holder_anchor_input(&*self.inner, anchor_tx, input, secp_ctx)
243+
}
137244

138-
fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1<All>) -> Result<Signature, ()>;
139-
}
245+
fn sign_holder_htlc_transaction(
246+
&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor,
247+
secp_ctx: &Secp256k1<All>,
248+
) -> Result<Signature, ()> {
249+
EcdsaChannelSigner::sign_holder_htlc_transaction(
250+
&*self.inner,
251+
htlc_tx,
252+
input,
253+
htlc_descriptor,
254+
secp_ctx,
255+
)
140256
}
141257
}
142258

@@ -181,6 +297,8 @@ impl Writeable for DynSigner {
181297
}
182298
}
183299

300+
impl DynSignerTrait for InMemorySigner {}
301+
184302
impl InnerSign for InMemorySigner {
185303
fn box_clone(&self) -> Box<dyn InnerSign> {
186304
Box::new(self.clone())
@@ -198,12 +316,12 @@ impl InnerSign for InMemorySigner {
198316
/// A convenience wrapper for DynKeysInterfaceTrait
199317
pub struct DynKeysInterface {
200318
/// The inner dyn keys interface
201-
pub inner: Box<dyn DynKeysInterfaceTrait<EcdsaSigner = DynSigner>>,
319+
pub inner: Box<dyn DynKeysInterfaceTrait>,
202320
}
203321

204322
impl DynKeysInterface {
205323
/// Create a new DynKeysInterface
206-
pub fn new(inner: Box<dyn DynKeysInterfaceTrait<EcdsaSigner = DynSigner>>) -> Self {
324+
pub fn new(inner: Box<dyn DynKeysInterfaceTrait>) -> Self {
207325
DynKeysInterface { inner }
208326
}
209327
}
@@ -237,6 +355,8 @@ impl NodeSigner for DynKeysInterface {
237355

238356
impl SignerProvider for DynKeysInterface {
239357
type EcdsaSigner = DynSigner;
358+
#[cfg(taproot)]
359+
type TaprootSigner = DynSigner;
240360

241361
delegate! {
242362
to self.inner {
@@ -273,12 +393,25 @@ impl OutputSpender for DynKeysInterface {
273393
}
274394
}
275395

396+
#[cfg(not(taproot))]
276397
/// A supertrait for all the traits that a keys interface implements
277398
pub trait DynKeysInterfaceTrait:
278399
NodeSigner + OutputSpender + SignerProvider<EcdsaSigner = DynSigner> + EntropySource + Send + Sync
279400
{
280401
}
281402

403+
#[cfg(taproot)]
404+
/// A supertrait for all the traits that a keys interface implements
405+
pub trait DynKeysInterfaceTrait:
406+
NodeSigner
407+
+ OutputSpender
408+
+ SignerProvider<EcdsaSigner = DynSigner, TaprootSigner = DynSigner>
409+
+ EntropySource
410+
+ Send
411+
+ Sync
412+
{
413+
}
414+
282415
/// A dyn wrapper for PhantomKeysManager
283416
pub struct DynPhantomKeysInterface {
284417
inner: PhantomKeysManager,
@@ -320,6 +453,8 @@ impl NodeSigner for DynPhantomKeysInterface {
320453

321454
impl SignerProvider for DynPhantomKeysInterface {
322455
type EcdsaSigner = DynSigner;
456+
#[cfg(taproot)]
457+
type TaprootSigner = DynSigner;
323458

324459
delegate! {
325460
to self.inner {

lightning/src/util/test_channel_signer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use bitcoin::secp256k1::All;
3232
use bitcoin::secp256k1::{SecretKey, PublicKey};
3333
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
3434
#[cfg(taproot)]
35-
use musig2::types::{PartialSignature, PublicNonce, SecretNonce};
35+
use musig2::types::{PartialSignature, PublicNonce};
3636
use crate::chain::transaction::OutPoint;
3737
use crate::sign::HTLCDescriptor;
3838
use crate::util::ser::{Writeable, Writer};

0 commit comments

Comments
 (0)