From f341a0ce358590b31624ad18e004e460974b6b7c Mon Sep 17 00:00:00 2001 From: Devrandom Date: Thu, 25 Apr 2024 11:00:36 +0200 Subject: [PATCH] taproot --- lightning/src/util/dyn_signer.rs | 196 ++++++++++++++++++++++--------- 1 file changed, 139 insertions(+), 57 deletions(-) diff --git a/lightning/src/util/dyn_signer.rs b/lightning/src/util/dyn_signer.rs index d71b37b20f1..3ab7492d011 100644 --- a/lightning/src/util/dyn_signer.rs +++ b/lightning/src/util/dyn_signer.rs @@ -21,8 +21,12 @@ use crate::sign::ecdsa::EcdsaChannelSigner; use crate::sign::InMemorySigner; use crate::sign::{ KeyMaterial, NodeSigner, Recipient, SignerProvider, SpendableOutputDescriptor, - ecdsa::WriteableEcdsaChannelSigner, + ecdsa::WriteableEcdsaChannelSigner }; +#[cfg(taproot)] +use crate::sign::taproot::{TaprootChannelSigner}; +#[cfg(taproot)] +use musig2::types::{PartialSignature, PublicNonce}; use crate::util::ser::{Readable, ReadableArgs}; use crate::util::ser::{Writeable, Writer}; use bitcoin; @@ -36,6 +40,7 @@ use crate::ln::features::ChannelTypeFeatures; #[cfg(any(test, feature = "_test_utils"))] use crate::util::test_utils::OnlyReadsKeysInterface; +#[cfg(not(taproot))] /// Helper to allow DynSigner to clone itself pub trait InnerSign: EcdsaChannelSigner + Send + Sync { /// Clone into a Box @@ -46,6 +51,17 @@ pub trait InnerSign: EcdsaChannelSigner + Send + Sync { fn vwrite(&self, writer: &mut Vec) -> Result<(), Error>; } +#[cfg(taproot)] +/// Helper to allow DynSigner to clone itself +pub trait InnerSign: EcdsaChannelSigner + TaprootChannelSigner + Send + Sync { + /// Clone into a Box + fn box_clone(&self) -> Box; + /// Cast to Any for runtime type checking + fn as_any(&self) -> &dyn Any; + /// Serialize the signer + fn vwrite(&self, writer: &mut Vec) -> Result<(), Error>; +} + /// A ChannelSigner derived struct allowing run-time selection of a signer pub struct DynSigner { /// The inner signer @@ -61,6 +77,46 @@ impl DynSigner { impl WriteableEcdsaChannelSigner for DynSigner {} +#[cfg(taproot)] +#[allow(unused_variables)] +impl TaprootChannelSigner for DynSigner { + fn generate_local_nonce_pair(&self, commitment_number: u64, secp_ctx: &Secp256k1) -> PublicNonce { + todo!() + } + + fn partially_sign_counterparty_commitment(&self, counterparty_nonce: PublicNonce, commitment_tx: &CommitmentTransaction, inbound_htlc_preimages: Vec, outbound_htlc_preimages: Vec, secp_ctx: &Secp256k1) -> Result<(crate::ln::msgs::PartialSignatureWithNonce, Vec), ()> { + todo!(); + } + + fn finalize_holder_commitment(&self, commitment_tx: &HolderCommitmentTransaction, counterparty_partial_signature: crate::ln::msgs::PartialSignatureWithNonce, secp_ctx: &Secp256k1) -> Result { + todo!(); + } + + fn sign_justice_revoked_output(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, secp_ctx: &Secp256k1) -> Result { + todo!(); + } + + fn sign_justice_revoked_htlc(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1) -> Result { + todo!(); + } + + fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1) -> Result { + todo!(); + } + + fn sign_counterparty_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, amount: u64, per_commitment_point: &PublicKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1) -> Result { + todo!(); + } + + fn partially_sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1) -> Result { + todo!(); + } + + fn sign_holder_anchor_input(&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1) -> Result { + todo!(); + } +} + impl Clone for DynSigner { fn clone(&self) -> Self { DynSigner { inner: self.inner.box_clone() } @@ -75,69 +131,85 @@ impl Readable for DynSigner { } impl EcdsaChannelSigner for DynSigner { - delegate! { - to self.inner { - fn sign_holder_commitment( - &self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1, - ) -> Result; + fn sign_holder_commitment( + &self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1, + ) -> Result { + self.inner.sign_holder_commitment(commitment_tx, secp_ctx) + } - #[cfg(any(test, feature = "unsafe_revoked_tx_signing"))] - fn unsafe_sign_holder_commitment( - &self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1, - ) -> Result; + #[cfg(any(test, feature = "unsafe_revoked_tx_signing"))] + fn unsafe_sign_holder_commitment( + &self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1, + ) -> Result { + self.inner.unsafe_sign_holder_commitment(commitment_tx, secp_ctx) + } - fn sign_counterparty_commitment( - &self, commitment_tx: &CommitmentTransaction, inbound_htlc_preimages: Vec, - outbound_htlc_preimages: Vec, secp_ctx: &Secp256k1, - ) -> Result<(Signature, Vec), ()>; + fn sign_counterparty_commitment( + &self, commitment_tx: &CommitmentTransaction, inbound_htlc_preimages: Vec, + outbound_htlc_preimages: Vec, secp_ctx: &Secp256k1, + ) -> Result<(Signature, Vec), ()> { + self.inner.sign_counterparty_commitment(commitment_tx, inbound_htlc_preimages, outbound_htlc_preimages, secp_ctx) + } - fn sign_justice_revoked_output( - &self, - justice_tx: &Transaction, - input: usize, - amount: u64, - per_commitment_key: &SecretKey, - secp_ctx: &Secp256k1, - ) -> Result; + fn sign_justice_revoked_output( + &self, + justice_tx: &Transaction, + input: usize, + amount: u64, + per_commitment_key: &SecretKey, + secp_ctx: &Secp256k1, + ) -> Result { + EcdsaChannelSigner::sign_justice_revoked_output(&*self.inner, justice_tx, input, amount, per_commitment_key, secp_ctx) + } - fn sign_justice_revoked_htlc( - &self, - justice_tx: &Transaction, - input: usize, - amount: u64, - per_commitment_key: &SecretKey, - htlc: &HTLCOutputInCommitment, - secp_ctx: &Secp256k1, - ) -> Result; + fn sign_justice_revoked_htlc( + &self, + justice_tx: &Transaction, + input: usize, + amount: u64, + per_commitment_key: &SecretKey, + htlc: &HTLCOutputInCommitment, + secp_ctx: &Secp256k1, + ) -> Result { + EcdsaChannelSigner::sign_justice_revoked_htlc(&*self.inner, justice_tx, input, amount, per_commitment_key, htlc, secp_ctx) + } - fn sign_counterparty_htlc_transaction( - &self, - htlc_tx: &Transaction, - input: usize, - amount: u64, - per_commitment_point: &PublicKey, - htlc: &HTLCOutputInCommitment, - secp_ctx: &Secp256k1, - ) -> Result; + fn sign_counterparty_htlc_transaction( + &self, + htlc_tx: &Transaction, + input: usize, + amount: u64, + per_commitment_point: &PublicKey, + htlc: &HTLCOutputInCommitment, + secp_ctx: &Secp256k1, + ) -> Result { + EcdsaChannelSigner::sign_counterparty_htlc_transaction(&*self.inner, htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx) + } - fn sign_closing_transaction( - &self, - closing_tx: &ClosingTransaction, - secp_ctx: &Secp256k1, - ) -> Result; + fn sign_closing_transaction( + &self, + closing_tx: &ClosingTransaction, + secp_ctx: &Secp256k1, + ) -> Result { + self.inner.sign_closing_transaction(closing_tx, secp_ctx) + } - fn sign_channel_announcement_with_funding_key( - &self, - msg: &UnsignedChannelAnnouncement, - secp_ctx: &Secp256k1, - ) -> Result; + fn sign_channel_announcement_with_funding_key( + &self, + msg: &UnsignedChannelAnnouncement, + secp_ctx: &Secp256k1, + ) -> Result { + self.inner.sign_channel_announcement_with_funding_key(msg, secp_ctx) + } - fn sign_holder_anchor_input( - &self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1, - ) -> Result; + fn sign_holder_anchor_input( + &self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1, + ) -> Result { + EcdsaChannelSigner::sign_holder_anchor_input(&*self.inner, anchor_tx, input, secp_ctx) + } - fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1) -> Result; - } + fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1) -> Result { + EcdsaChannelSigner::sign_holder_htlc_transaction(&*self.inner, htlc_tx, input, htlc_descriptor, secp_ctx) } } @@ -200,12 +272,12 @@ impl InnerSign for InMemorySigner { /// A convenience wrapper for DynKeysInterfaceTrait pub struct DynKeysInterface { /// The inner dyn keys interface - pub inner: Box>, + pub inner: Box, } impl DynKeysInterface { /// Create a new DynKeysInterface - pub fn new(inner: Box>) -> Self { + pub fn new(inner: Box) -> Self { DynKeysInterface { inner } } } @@ -239,6 +311,8 @@ impl NodeSigner for DynKeysInterface { impl SignerProvider for DynKeysInterface { type EcdsaSigner = DynSigner; + #[cfg(taproot)] + type TaprootSigner = DynSigner; delegate! { to self.inner { @@ -275,10 +349,16 @@ impl OutputSpender for DynKeysInterface { } } +#[cfg(not(taproot))] /// A supertrait for all the traits that a keys interface implements pub trait DynKeysInterfaceTrait: NodeSigner + OutputSpender + SignerProvider + EntropySource + Send + Sync { } +#[cfg(taproot)] +/// A supertrait for all the traits that a keys interface implements +pub trait DynKeysInterfaceTrait: NodeSigner + OutputSpender + SignerProvider + EntropySource + Send + Sync { +} + /// A dyn wrapper for PhantomKeysManager pub struct DynPhantomKeysInterface { inner: PhantomKeysManager, @@ -320,6 +400,8 @@ impl NodeSigner for DynPhantomKeysInterface { impl SignerProvider for DynPhantomKeysInterface { type EcdsaSigner = DynSigner; + #[cfg(taproot)] + type TaprootSigner = DynSigner; delegate! { to self.inner {