Skip to content

Commit 64381a5

Browse files
committed
Added anchor support to commitment HTLC outputs
# Conflicts: # lightning/src/ln/chan_utils.
1 parent 4bb81ff commit 64381a5

File tree

7 files changed

+100
-39
lines changed

7 files changed

+100
-39
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3385,6 +3385,7 @@ mod tests {
33853385
selected_contest_delay: 67,
33863386
}),
33873387
funding_outpoint: Some(funding_outpoint),
3388+
opt_anchors: None,
33883389
};
33893390
// Prune with one old state and a holder commitment tx holding a few overlaps with the
33903391
// old state.
@@ -3450,15 +3451,15 @@ mod tests {
34503451
let mut sum_actual_sigs = 0;
34513452

34523453
macro_rules! sign_input {
3453-
($sighash_parts: expr, $idx: expr, $amount: expr, $weight: expr, $sum_actual_sigs: expr) => {
3454+
($sighash_parts: expr, $idx: expr, $amount: expr, $weight: expr, $sum_actual_sigs: expr, $opt_anchors: expr) => {
34543455
let htlc = HTLCOutputInCommitment {
34553456
offered: if *$weight == WEIGHT_REVOKED_OFFERED_HTLC || *$weight == WEIGHT_OFFERED_HTLC { true } else { false },
34563457
amount_msat: 0,
34573458
cltv_expiry: 2 << 16,
34583459
payment_hash: PaymentHash([1; 32]),
34593460
transaction_output_index: Some($idx as u32),
34603461
};
3461-
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) };
3462+
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) };
34623463
let sighash = hash_to_message!(&$sighash_parts.signature_hash($idx, &redeem_script, $amount, SigHashType::All)[..]);
34633464
let sig = secp_ctx.sign(&sighash, &privkey);
34643465
$sighash_parts.access_witness($idx).push(sig.serialize_der().to_vec());
@@ -3506,7 +3507,7 @@ mod tests {
35063507
{
35073508
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
35083509
for (idx, inp) in inputs_weight.iter().enumerate() {
3509-
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
3510+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, false);
35103511
inputs_total_weight += inp;
35113512
}
35123513
}
@@ -3532,7 +3533,7 @@ mod tests {
35323533
{
35333534
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
35343535
for (idx, inp) in inputs_weight.iter().enumerate() {
3535-
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
3536+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, false);
35363537
inputs_total_weight += inp;
35373538
}
35383539
}
@@ -3556,7 +3557,7 @@ mod tests {
35563557
{
35573558
let mut sighash_parts = bip143::SigHashCache::new(&mut claim_tx);
35583559
for (idx, inp) in inputs_weight.iter().enumerate() {
3559-
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs);
3560+
sign_input!(sighash_parts, idx, 0, inp, sum_actual_sigs, false);
35603561
inputs_total_weight += inp;
35613562
}
35623563
}

lightning/src/chain/keysinterface.rs

Lines changed: 7 additions & 3 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 3 deletions
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)