From 09d45900e294782aacfdc087e063e19815fdf979 Mon Sep 17 00:00:00 2001 From: optout <13562139+optout21@users.noreply.github.com> Date: Wed, 9 Oct 2024 15:27:13 +0200 Subject: [PATCH] Signer extended with method to sign prev funding transaction input --- lightning/src/sign/ecdsa.rs | 11 +++++++++++ lightning/src/sign/mod.rs | 21 +++++++++++++++++++++ lightning/src/util/test_channel_signer.rs | 7 +++++++ 3 files changed, 39 insertions(+) diff --git a/lightning/src/sign/ecdsa.rs b/lightning/src/sign/ecdsa.rs index ecdd45aa3f5..2bbd6ea4381 100644 --- a/lightning/src/sign/ecdsa.rs +++ b/lightning/src/sign/ecdsa.rs @@ -209,4 +209,15 @@ pub trait EcdsaChannelSigner: ChannelSigner { fn sign_channel_announcement_with_funding_key( &self, msg: &UnsignedChannelAnnouncement, secp_ctx: &Secp256k1, ) -> Result; + /// Sign an input of a transaction with our funding key. + /// Used for splicing, when signing the previous funding transaction. + /// The previous funding transaction becomes an input to the new funding transaction, + /// and it is a multisig, which we also need to sign. + /// [`input_index`]: The index of the input that was the previous funding transaction, + /// within the new funding transaction. + /// [`input_value`]: The value of the previous funding transaction. + fn sign_splicing_funding_input( + &self, tx: &Transaction, input_index: u16, input_value: u64, + secp_ctx: &Secp256k1, + ) -> Result; } diff --git a/lightning/src/sign/mod.rs b/lightning/src/sign/mod.rs index 4b8a9e0255a..7265e25eff3 100644 --- a/lightning/src/sign/mod.rs +++ b/lightning/src/sign/mod.rs @@ -1695,6 +1695,27 @@ impl EcdsaChannelSigner for InMemorySigner { let msghash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]); Ok(secp_ctx.sign_ecdsa(&msghash, &self.funding_key)) } + + fn sign_splicing_funding_input( + &self, tx: &Transaction, input_index: u16, input_value: u64, + secp_ctx: &Secp256k1, + ) -> Result { + let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key); + let counterparty_funding_key = + &self.counterparty_pubkeys().expect(MISSING_PARAMS_ERR).funding_pubkey; + let funding_redeemscript = + make_funding_redeemscript(&funding_pubkey, counterparty_funding_key); + let sighash = &sighash::SighashCache::new(tx) + .p2wsh_signature_hash( + input_index as usize, + &funding_redeemscript, + Amount::from_sat(input_value), + EcdsaSighashType::All, + ) + .unwrap()[..]; + let msg = hash_to_message!(sighash); + Ok(sign(secp_ctx, &msg, &self.funding_key)) + } } #[cfg(taproot)] diff --git a/lightning/src/util/test_channel_signer.rs b/lightning/src/util/test_channel_signer.rs index 9aa34fd4ca6..163b03e7fbb 100644 --- a/lightning/src/util/test_channel_signer.rs +++ b/lightning/src/util/test_channel_signer.rs @@ -353,6 +353,13 @@ impl EcdsaChannelSigner for TestChannelSigner { ) -> Result { self.inner.sign_channel_announcement_with_funding_key(msg, secp_ctx) } + + fn sign_splicing_funding_input( + &self, tx: &Transaction, input_index: u16, input_value: u64, + secp_ctx: &Secp256k1, + ) -> Result { + self.inner.sign_splicing_funding_input(tx, input_index, input_value, secp_ctx) + } } #[cfg(taproot)]