Skip to content

Commit

Permalink
taproot
Browse files Browse the repository at this point in the history
  • Loading branch information
devrandom committed Apr 25, 2024
1 parent 3d13ccd commit 9d4e233
Showing 1 changed file with 138 additions and 57 deletions.
195 changes: 138 additions & 57 deletions lightning/src/util/dyn_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -46,6 +51,17 @@ pub trait InnerSign: EcdsaChannelSigner + Send + Sync {
fn vwrite(&self, writer: &mut Vec<u8>) -> 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<dyn crate::util::dyn_signer::InnerSign>;
/// Cast to Any for runtime type checking
fn as_any(&self) -> &dyn Any;
/// Serialize the signer
fn vwrite(&self, writer: &mut Vec<u8>) -> Result<(), Error>;
}

/// A ChannelSigner derived struct allowing run-time selection of a signer
pub struct DynSigner {
/// The inner signer
Expand All @@ -61,6 +77,45 @@ impl DynSigner {

impl WriteableEcdsaChannelSigner for DynSigner {}

#[allow(unused_variables)]
impl TaprootChannelSigner for DynSigner {
fn generate_local_nonce_pair(&self, commitment_number: u64, secp_ctx: &Secp256k1<All>) -> PublicNonce {
todo!()
}

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>), ()> {
todo!();
}

fn finalize_holder_commitment(&self, commitment_tx: &HolderCommitmentTransaction, counterparty_partial_signature: crate::ln::msgs::PartialSignatureWithNonce, secp_ctx: &Secp256k1<All>) -> Result<PartialSignature, ()> {
todo!();
}

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, ()> {
todo!();
}

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, ()> {
todo!();
}

fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1<All>) -> Result<secp256k1::schnorr::Signature, ()> {
todo!();
}

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, ()> {
todo!();
}

fn partially_sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<All>) -> Result<PartialSignature, ()> {
todo!();
}

fn sign_holder_anchor_input(&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<All>) -> Result<secp256k1::schnorr::Signature, ()> {
todo!();
}
}

impl Clone for DynSigner {
fn clone(&self) -> Self {
DynSigner { inner: self.inner.box_clone() }
Expand All @@ -75,69 +130,85 @@ impl Readable for DynSigner {
}

impl EcdsaChannelSigner for DynSigner {
delegate! {
to self.inner {
fn sign_holder_commitment(
&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>,
) -> Result<Signature, ()>;
fn sign_holder_commitment(
&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>,
) -> Result<Signature, ()> {
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<secp256k1::All>,
) -> Result<Signature, ()>;
#[cfg(any(test, feature = "unsafe_revoked_tx_signing"))]
fn unsafe_sign_holder_commitment(
&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>,
) -> Result<Signature, ()> {
self.inner.unsafe_sign_holder_commitment(commitment_tx, secp_ctx)
}

fn sign_counterparty_commitment(
&self, commitment_tx: &CommitmentTransaction, inbound_htlc_preimages: Vec<PaymentPreimage>,
outbound_htlc_preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<secp256k1::All>,
) -> Result<(Signature, Vec<Signature>), ()>;
fn sign_counterparty_commitment(
&self, commitment_tx: &CommitmentTransaction, inbound_htlc_preimages: Vec<PaymentPreimage>,
outbound_htlc_preimages: Vec<PaymentPreimage>, secp_ctx: &Secp256k1<secp256k1::All>,
) -> Result<(Signature, Vec<Signature>), ()> {
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<secp256k1::All>,
) -> Result<Signature, ()>;
fn sign_justice_revoked_output(
&self,
justice_tx: &Transaction,
input: usize,
amount: u64,
per_commitment_key: &SecretKey,
secp_ctx: &Secp256k1<secp256k1::All>,
) -> Result<Signature, ()> {
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<secp256k1::All>,
) -> Result<Signature, ()>;
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, ()> {
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<secp256k1::All>,
) -> Result<Signature, ()>;
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, ()> {
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<secp256k1::All>,
) -> Result<Signature, ()>;
fn sign_closing_transaction(
&self,
closing_tx: &ClosingTransaction,
secp_ctx: &Secp256k1<secp256k1::All>,
) -> Result<Signature, ()> {
self.inner.sign_closing_transaction(closing_tx, secp_ctx)
}

fn sign_channel_announcement_with_funding_key(
&self,
msg: &UnsignedChannelAnnouncement,
secp_ctx: &Secp256k1<secp256k1::All>,
) -> Result<Signature, ()>;
fn sign_channel_announcement_with_funding_key(
&self,
msg: &UnsignedChannelAnnouncement,
secp_ctx: &Secp256k1<secp256k1::All>,
) -> Result<Signature, ()> {
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<secp256k1::All>,
) -> Result<Signature, ()>;
fn sign_holder_anchor_input(
&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
) -> Result<Signature, ()> {
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<All>) -> Result<Signature, ()>;
}
fn sign_holder_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, secp_ctx: &Secp256k1<All>) -> Result<Signature, ()> {
EcdsaChannelSigner::sign_holder_htlc_transaction(&*self.inner, htlc_tx, input, htlc_descriptor, secp_ctx)
}
}

Expand Down Expand Up @@ -200,12 +271,12 @@ impl InnerSign for InMemorySigner {
/// A convenience wrapper for DynKeysInterfaceTrait
pub struct DynKeysInterface {
/// The inner dyn keys interface
pub inner: Box<dyn DynKeysInterfaceTrait<EcdsaSigner = DynSigner>>,
pub inner: Box<dyn DynKeysInterfaceTrait>,
}

impl DynKeysInterface {
/// Create a new DynKeysInterface
pub fn new(inner: Box<dyn DynKeysInterfaceTrait<EcdsaSigner = DynSigner>>) -> Self {
pub fn new(inner: Box<dyn DynKeysInterfaceTrait>) -> Self {
DynKeysInterface { inner }
}
}
Expand Down Expand Up @@ -239,6 +310,8 @@ impl NodeSigner for DynKeysInterface {

impl SignerProvider for DynKeysInterface {
type EcdsaSigner = DynSigner;
#[cfg(taproot)]
type TaprootSigner = DynSigner;

delegate! {
to self.inner {
Expand Down Expand Up @@ -275,10 +348,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<EcdsaSigner=DynSigner> + EntropySource + Send + Sync {
}

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

/// A dyn wrapper for PhantomKeysManager
pub struct DynPhantomKeysInterface {
inner: PhantomKeysManager,
Expand Down Expand Up @@ -320,6 +399,8 @@ impl NodeSigner for DynPhantomKeysInterface {

impl SignerProvider for DynPhantomKeysInterface {
type EcdsaSigner = DynSigner;
#[cfg(taproot)]
type TaprootSigner = DynSigner;

delegate! {
to self.inner {
Expand Down

0 comments on commit 9d4e233

Please sign in to comment.