Skip to content

Commit 5478ef6

Browse files
committed
Add holder anchor signing support to InMemorySigner
1 parent 626a612 commit 5478ef6

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 20 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;
@@ -645,6 +646,25 @@ impl InMemorySigner {
645646
witness.push(witness_script.clone().into_bytes());
646647
Ok(witness)
647648
}
649+
650+
/// Computes the fully-signed witness for a commitment transaction's anchor output used as an
651+
/// input within `anchor_tx`, which spends the commitment transaction, at index `input`.
652+
///
653+
/// Panics if `anchor_tx`'s sighash cannot be computed or index `input` is not valid within
654+
/// `anchor_tx`.
655+
pub fn sign_holder_anchor_output(
656+
&self, anchor_tx: &mut Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
657+
) {
658+
let witness_script = chan_utils::get_anchor_redeemscript(&self.holder_channel_pubkeys.funding_pubkey);
659+
let sighash = sighash::SighashCache::new(&*anchor_tx).segwit_signature_hash(
660+
input, &witness_script, ANCHOR_OUTPUT_VALUE_SATOSHI, EcdsaSighashType::All,
661+
).unwrap();
662+
let msg = hash_to_message!(&sighash[..]);
663+
let sig = sign(secp_ctx, &msg, &self.funding_key);
664+
let mut funding_sig = sig.serialize_der().to_vec();
665+
funding_sig.push(EcdsaSighashType::All as u8);
666+
anchor_tx.input[input].witness = Witness::from_vec(vec![funding_sig, witness_script.to_bytes()]);
667+
}
648668
}
649669

650670
impl BaseSign for InMemorySigner {

0 commit comments

Comments
 (0)