Skip to content

Commit 8f09e5a

Browse files
committed
Set the SigHashType of remote htlc signatures w/ anchors to SinglePlusAnyoneCanPay
1 parent b62b244 commit 8f09e5a

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,8 @@ impl BaseSign for InMemorySigner {
612612
for htlc in commitment_tx.htlcs() {
613613
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_tx.feerate_per_kw(), self.holder_selected_contest_delay(), htlc, self.opt_anchors(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
614614
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, self.opt_anchors(), &keys);
615-
let htlc_sighash = hash_to_message!(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, SigHashType::All)[..]);
615+
let htlc_sighashtype = if self.opt_anchors() { SigHashType::SinglePlusAnyoneCanPay } else { SigHashType::All };
616+
let htlc_sighash = hash_to_message!(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, htlc_sighashtype)[..]);
616617
let holder_htlc_key = chan_utils::derive_private_key(&secp_ctx, &keys.per_commitment_point, &self.htlc_base_key).map_err(|_| ())?;
617618
htlc_sigs.push(secp_ctx.sign(&htlc_sighash, &holder_htlc_key));
618619
}

lightning/src/ln/chan_utils.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,8 @@ impl<'a> TrustedCommitmentTransaction<'a> {
13941394
/// which HTLCOutputInCommitment::transaction_output_index.is_some()).
13951395
///
13961396
/// The returned Vec has one entry for each HTLC, and in the same order.
1397+
///
1398+
/// This function is only valid in the holder commitment context, it always uses SigHashType::All.
13971399
pub fn get_htlc_sigs<T: secp256k1::Signing>(&self, htlc_base_key: &SecretKey, channel_parameters: &DirectedChannelTransactionParameters, secp_ctx: &Secp256k1<T>) -> Result<Vec<Signature>, ()> {
13981400
let inner = self.inner;
13991401
let keys = &inner.keys;
@@ -1429,12 +1431,14 @@ impl<'a> TrustedCommitmentTransaction<'a> {
14291431

14301432
let htlc_redeemscript = get_htlc_redeemscript_with_explicit_keys(&this_htlc, self.opt_anchors(), &keys.broadcaster_htlc_key, &keys.countersignatory_htlc_key, &keys.revocation_key);
14311433

1434+
let sighashtype = if self.opt_anchors() { SigHashType::SinglePlusAnyoneCanPay } else { SigHashType::All };
1435+
14321436
// First push the multisig dummy, note that due to BIP147 (NULLDUMMY) it must be a zero-length element.
14331437
htlc_tx.input[0].witness.push(Vec::new());
14341438

14351439
htlc_tx.input[0].witness.push(counterparty_signature.serialize_der().to_vec());
14361440
htlc_tx.input[0].witness.push(signature.serialize_der().to_vec());
1437-
htlc_tx.input[0].witness[1].push(SigHashType::All as u8);
1441+
htlc_tx.input[0].witness[1].push(sighashtype as u8);
14381442
htlc_tx.input[0].witness[2].push(SigHashType::All as u8);
14391443

14401444
if this_htlc.offered {

lightning/src/ln/channel.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,7 +2625,8 @@ impl<Signer: Sign> Channel<Signer> {
26252625
&keys.broadcaster_delayed_payment_key, &keys.revocation_key);
26262626

26272627
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, self.opt_anchors(), &keys);
2628-
let htlc_sighash = hash_to_message!(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, SigHashType::All)[..]);
2628+
let htlc_sighashtype = if self.opt_anchors() { SigHashType::SinglePlusAnyoneCanPay } else { SigHashType::All };
2629+
let htlc_sighash = hash_to_message!(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, htlc_sighashtype)[..]);
26292630
log_trace!(logger, "Checking HTLC tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} in channel {}.",
26302631
log_bytes!(msg.htlc_signatures[idx].serialize_compact()[..]), log_bytes!(keys.countersignatory_htlc_key.serialize()),
26312632
encode::serialize_hex(&htlc_tx), log_bytes!(htlc_sighash[..]), encode::serialize_hex(&htlc_redeemscript), log_bytes!(self.channel_id()));
@@ -6288,7 +6289,8 @@ mod tests {
62886289
chan.get_counterparty_selected_contest_delay().unwrap(),
62896290
&htlc, opt_anchors, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
62906291
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, opt_anchors, &keys);
6291-
let htlc_sighash = Message::from_slice(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, SigHashType::All)[..]).unwrap();
6292+
let htlc_sighashtype = if opt_anchors { SigHashType::SinglePlusAnyoneCanPay } else { SigHashType::All };
6293+
let htlc_sighash = Message::from_slice(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, htlc_sighashtype)[..]).unwrap();
62926294
secp_ctx.verify(&htlc_sighash, &remote_signature, &keys.countersignatory_htlc_key).unwrap();
62936295

62946296
let mut preimage: Option<PaymentPreimage> = None;

0 commit comments

Comments
 (0)