Skip to content

Commit ed750b2

Browse files
committed
Added anchor support to commitment HTLC outputs
1 parent 2bf39a6 commit ed750b2

File tree

7 files changed

+100
-39
lines changed

7 files changed

+100
-39
lines changed

lightning/src/chain/channelmonitor.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -3239,6 +3239,7 @@ mod tests {
32393239
selected_contest_delay: 67,
32403240
}),
32413241
funding_outpoint: Some(funding_outpoint),
3242+
opt_anchors: None,
32423243
};
32433244
// Prune with one old state and a holder commitment tx holding a few overlaps with the
32443245
// old state.
@@ -3304,15 +3305,15 @@ mod tests {
33043305
let mut sum_actual_sigs = 0;
33053306

33063307
macro_rules! sign_input {
3307-
($sighash_parts: expr, $idx: expr, $amount: expr, $weight: expr, $sum_actual_sigs: expr) => {
3308+
($sighash_parts: expr, $idx: expr, $amount: expr, $weight: expr, $sum_actual_sigs: expr, $opt_anchors: expr) => {
33083309
let htlc = HTLCOutputInCommitment {
33093310
offered: if *$weight == WEIGHT_REVOKED_OFFERED_HTLC || *$weight == WEIGHT_OFFERED_HTLC { true } else { false },
33103311
amount_msat: 0,
33113312
cltv_expiry: 2 << 16,
33123313
payment_hash: PaymentHash([1; 32]),
33133314
transaction_output_index: Some($idx as u32),
33143315
};
3315-
let redeem_script = if *$weight == WEIGHT_REVOKED_OUTPUT { chan_utils::get_revokeable_redeemscript(&pubkey, 256, &pubkey) } else { chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &pubkey, &pubkey, &pubkey) };
3316+
let redeem_script = if *$weight == WEIGHT_REVOKED_OUTPUT { chan_utils::get_revokeable_redeemscript(&pubkey, 256, &pubkey) } else { chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, $opt_anchors, &pubkey, &pubkey, &pubkey) };
33163317
let sighash = hash_to_message!(&$sighash_parts.signature_hash($idx, &redeem_script, $amount, SigHashType::All)[..]);
33173318
let sig = secp_ctx.sign(&sighash, &privkey);
33183319
$sighash_parts.access_witness($idx).push(sig.serialize_der().to_vec());
@@ -3360,7 +3361,7 @@ mod tests {
33603361
{
33613362
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
33623363
for (idx, inp) in inputs_weight.iter().enumerate() {
3363-
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
3364+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, false);
33643365
inputs_total_weight += inp;
33653366
}
33663367
}
@@ -3386,7 +3387,7 @@ mod tests {
33863387
{
33873388
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
33883389
for (idx, inp) in inputs_weight.iter().enumerate() {
3389-
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
3390+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, false);
33903391
inputs_total_weight += inp;
33913392
}
33923393
}
@@ -3410,7 +3411,7 @@ mod tests {
34103411
{
34113412
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
34123413
for (idx, inp) in inputs_weight.iter().enumerate() {
3413-
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
3414+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, false);
34143415
inputs_total_weight += inp;
34153416
}
34163417
}

lightning/src/chain/keysinterface.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,10 @@ impl InMemorySigner {
505505
self.channel_parameters.as_ref().unwrap()
506506
}
507507

508+
/// Whether anchors should be used.
509+
/// Will panic if ready_channel wasn't called.
510+
pub fn opt_anchors(&self) -> bool { self.get_channel_parameters().opt_anchors.is_some() }
511+
508512
/// Sign the single input of spend_tx at index `input_idx` which spends the output
509513
/// described by descriptor, returning the witness stack for the input.
510514
///
@@ -594,7 +598,7 @@ impl BaseSign for InMemorySigner {
594598
let mut htlc_sigs = Vec::with_capacity(commitment_tx.htlcs().len());
595599
for htlc in commitment_tx.htlcs() {
596600
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_tx.feerate_per_kw(), self.holder_selected_contest_delay(), htlc, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
597-
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &keys);
601+
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, self.opt_anchors(), &keys);
598602
let htlc_sighash = hash_to_message!(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, SigHashType::All)[..]);
599603
let holder_htlc_key = chan_utils::derive_private_key(&secp_ctx, &keys.per_commitment_point, &self.htlc_base_key).map_err(|_| ())?;
600604
htlc_sigs.push(secp_ctx.sign(&htlc_sighash, &holder_htlc_key));
@@ -648,7 +652,7 @@ impl BaseSign for InMemorySigner {
648652
let witness_script = {
649653
let counterparty_htlcpubkey = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.counterparty_pubkeys().htlc_basepoint).map_err(|_| ())?;
650654
let holder_htlcpubkey = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.pubkeys().htlc_basepoint).map_err(|_| ())?;
651-
chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &counterparty_htlcpubkey, &holder_htlcpubkey, &revocation_pubkey)
655+
chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, self.opt_anchors(), &counterparty_htlcpubkey, &holder_htlcpubkey, &revocation_pubkey)
652656
};
653657
let mut sighash_parts = bip143::SigHashCache::new(justice_tx);
654658
let sighash = hash_to_message!(&sighash_parts.signature_hash(input, &witness_script, amount, SigHashType::All)[..]);
@@ -660,7 +664,7 @@ impl BaseSign for InMemorySigner {
660664
let witness_script = if let Ok(revocation_pubkey) = chan_utils::derive_public_revocation_key(&secp_ctx, &per_commitment_point, &self.pubkeys().revocation_basepoint) {
661665
if let Ok(counterparty_htlcpubkey) = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.counterparty_pubkeys().htlc_basepoint) {
662666
if let Ok(htlcpubkey) = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.pubkeys().htlc_basepoint) {
663-
chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &counterparty_htlcpubkey, &htlcpubkey, &revocation_pubkey)
667+
chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, self.opt_anchors(), &counterparty_htlcpubkey, &htlcpubkey, &revocation_pubkey)
664668
} else { return Err(()) }
665669
} else { return Err(()) }
666670
} else { return Err(()) };

lightning/src/chain/onchaintx.rs

+2
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,8 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
785785
htlc_tx
786786
}
787787

788+
pub(crate) fn opt_anchors(&self) -> bool { self.channel_transaction_parameters.opt_anchors.is_some() }
789+
788790
#[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
789791
pub(crate) fn unsafe_get_fully_signed_htlc_tx(&mut self, outp: &::bitcoin::OutPoint, preimage: &Option<PaymentPreimage>) -> Option<Transaction> {
790792
let latest_had_sigs = self.holder_htlc_sigs.is_some();

lightning/src/chain/package.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl PackageSolvingData {
341341
},
342342
PackageSolvingData::RevokedHTLCOutput(ref outp) => {
343343
if let Ok(chan_keys) = TxCreationKeys::derive_new(&onchain_handler.secp_ctx, &outp.per_commitment_point, &outp.counterparty_delayed_payment_base_key, &outp.counterparty_htlc_base_key, &onchain_handler.signer.pubkeys().revocation_basepoint, &onchain_handler.signer.pubkeys().htlc_basepoint) {
344-
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
344+
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, onchain_handler.opt_anchors(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
345345
//TODO: should we panic on signer failure ?
346346
if let Ok(sig) = onchain_handler.signer.sign_justice_revoked_htlc(&bumped_tx, i, outp.amount, &outp.per_commitment_key, &outp.htlc, &onchain_handler.secp_ctx) {
347347
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
@@ -353,7 +353,7 @@ impl PackageSolvingData {
353353
},
354354
PackageSolvingData::CounterpartyOfferedHTLCOutput(ref outp) => {
355355
if let Ok(chan_keys) = TxCreationKeys::derive_new(&onchain_handler.secp_ctx, &outp.per_commitment_point, &outp.counterparty_delayed_payment_base_key, &outp.counterparty_htlc_base_key, &onchain_handler.signer.pubkeys().revocation_basepoint, &onchain_handler.signer.pubkeys().htlc_basepoint) {
356-
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
356+
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, onchain_handler.opt_anchors(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
357357

358358
if let Ok(sig) = onchain_handler.signer.sign_counterparty_htlc_transaction(&bumped_tx, i, &outp.htlc.amount_msat / 1000, &outp.per_commitment_point, &outp.htlc, &onchain_handler.secp_ctx) {
359359
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
@@ -365,7 +365,7 @@ impl PackageSolvingData {
365365
},
366366
PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => {
367367
if let Ok(chan_keys) = TxCreationKeys::derive_new(&onchain_handler.secp_ctx, &outp.per_commitment_point, &outp.counterparty_delayed_payment_base_key, &outp.counterparty_htlc_base_key, &onchain_handler.signer.pubkeys().revocation_basepoint, &onchain_handler.signer.pubkeys().htlc_basepoint) {
368-
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
368+
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&outp.htlc, onchain_handler.opt_anchors(), &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
369369

370370
bumped_tx.lock_time = outp.htlc.cltv_expiry; // Right now we don't aggregate time-locked transaction, if we do we should set lock_time before to avoid breaking hash computation
371371
if let Ok(sig) = onchain_handler.signer.sign_counterparty_htlc_transaction(&bumped_tx, i, &outp.htlc.amount_msat / 1000, &outp.per_commitment_point, &outp.htlc, &onchain_handler.secp_ctx) {

0 commit comments

Comments
 (0)