Skip to content

Commit 0e079cd

Browse files
committed
Add holder anchor signing support to BaseSign
1 parent 772d839 commit 0e079cd

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use util::crypto::{hkdf_extract_expand_twice, sign};
3636
use util::ser::{Writeable, Writer, Readable, ReadableArgs};
3737

3838
use chain::transaction::OutPoint;
39+
use ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
3940
use ln::{chan_utils, PaymentPreimage};
4041
use ln::chan_utils::{HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, HolderCommitmentTransaction, ChannelTransactionParameters, CommitmentTransaction, ClosingTransaction};
4142
use ln::msgs::UnsignedChannelAnnouncement;
@@ -288,6 +289,13 @@ pub trait BaseSign {
288289
#[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
289290
fn unsafe_sign_holder_commitment_and_htlcs(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<(Signature, Vec<Signature>), ()>;
290291

292+
/// Computes the signature for a commitment transaction's anchor output used as an input within
293+
/// `anchor_tx` at index `input`.
294+
/// This may be called multiple times for the same transaction.
295+
fn sign_holder_anchor_output(
296+
&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
297+
) -> Result<Signature, ()>;
298+
291299
/// Create a signature for the given input in a transaction spending an HTLC transaction output
292300
/// or a commitment transaction `to_local` output when our counterparty broadcasts an old state.
293301
///
@@ -713,6 +721,21 @@ impl BaseSign for InMemorySigner {
713721
Ok((sig, htlc_sigs))
714722
}
715723

724+
fn sign_holder_anchor_output(
725+
&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
726+
) -> Result<Signature, ()> {
727+
let witness_script = {
728+
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
729+
chan_utils::get_anchor_redeemscript(&funding_pubkey)
730+
};
731+
let mut sighash_parts = sighash::SighashCache::new(anchor_tx);
732+
let sighash = sighash_parts.segwit_signature_hash(
733+
input, &witness_script, ANCHOR_OUTPUT_VALUE_SATOSHI, EcdsaSighashType::All,
734+
).unwrap();
735+
let msg = hash_to_message!(&sighash[..]);
736+
Ok(sign(secp_ctx, &msg, &self.funding_key))
737+
}
738+
716739
fn sign_justice_revoked_output(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
717740
let revocation_key = chan_utils::derive_private_revocation_key(&secp_ctx, &per_commitment_key, &self.revocation_base_key).map_err(|_| ())?;
718741
let per_commitment_point = PublicKey::from_secret_key(secp_ctx, &per_commitment_key);

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ impl BaseSign for EnforcingSigner {
181181
Ok(self.inner.unsafe_sign_holder_commitment_and_htlcs(commitment_tx, secp_ctx).unwrap())
182182
}
183183

184+
fn sign_holder_anchor_output(
185+
&self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
186+
) -> Result<Signature, ()> {
187+
if !self.opt_anchors() {
188+
panic!("can only sign anchor inputs for commitment transactions with anchor outputs");
189+
}
190+
self.inner.sign_holder_anchor_output(anchor_tx, input, secp_ctx)
191+
}
192+
184193
fn sign_justice_revoked_output(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
185194
Ok(self.inner.sign_justice_revoked_output(justice_tx, input, amount, per_commitment_key, secp_ctx).unwrap())
186195
}

0 commit comments

Comments
 (0)