Skip to content

Commit

Permalink
Signer extended with method to sign prev funding transaction input
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Sep 16, 2024
1 parent 02973ea commit 2c2f9f1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lightning/src/sign/ecdsa.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Defines ECDSA-specific signer types.

use bitcoin::transaction::Transaction;
use bitcoin::{Script, Transaction};

use bitcoin::secp256k1;
use bitcoin::secp256k1::ecdsa::Signature;
Expand Down Expand Up @@ -209,4 +209,12 @@ pub trait EcdsaChannelSigner: ChannelSigner {
fn sign_channel_announcement_with_funding_key(
&self, msg: &UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<secp256k1::All>,
) -> Result<Signature, ()>;
/// 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.
fn sign_transaction_input(
&self, tx: &Transaction, input_index: u16, input_value: u64, input_redeem_script: &Script,
secp_ctx: &Secp256k1<secp256k1::All>,
) -> Result<Signature, ()>;
}
16 changes: 16 additions & 0 deletions lightning/src/sign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,22 @@ impl EcdsaChannelSigner for InMemorySigner {
let msghash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]);
Ok(secp_ctx.sign_ecdsa(&msghash, &self.funding_key))
}

fn sign_transaction_input(
&self, tx: &Transaction, input_index: u16, input_value: u64, input_redeem_script: &Script,
secp_ctx: &Secp256k1<secp256k1::All>,
) -> Result<Signature, ()> {
let sighash = &sighash::SighashCache::new(tx)
.p2wsh_signature_hash(
input_index as usize,
&input_redeem_script,
Amount::from_sat(input_value),
EcdsaSighashType::All,
)
.unwrap()[..];
let msg = hash_to_message!(sighash);
Ok(sign(secp_ctx, &msg, &self.funding_key))
}
}

#[cfg(taproot)]
Expand Down
14 changes: 13 additions & 1 deletion lightning/src/util/test_channel_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use core::cmp;
use crate::sync::{Mutex, Arc};
#[cfg(test)] use crate::sync::MutexGuard;

use bitcoin::transaction::Transaction;
use bitcoin::{Script, Transaction};
use bitcoin::hashes::Hash;
use bitcoin::sighash;
use bitcoin::sighash::EcdsaSighashType;
Expand Down Expand Up @@ -353,6 +353,18 @@ impl EcdsaChannelSigner for TestChannelSigner {
) -> Result<Signature, ()> {
self.inner.sign_channel_announcement_with_funding_key(msg, secp_ctx)
}

fn sign_transaction_input(
&self, tx: &Transaction, input_index: u16, input_value: u64, input_redeem_script: &Script, secp_ctx: &Secp256k1<secp256k1::All>
) -> Result<Signature, ()> {
self.inner.sign_transaction_input(
tx,
input_index,
input_value,
input_redeem_script,
secp_ctx,
)
}
}

#[cfg(taproot)]
Expand Down

0 comments on commit 2c2f9f1

Please sign in to comment.