Skip to content

Commit 6c36e01

Browse files
committed
Add anchor support to build_htlc_transaction
1 parent c077f36 commit 6c36e01

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ impl BaseSign for InMemorySigner {
599599

600600
let mut htlc_sigs = Vec::with_capacity(commitment_tx.htlcs().len());
601601
for htlc in commitment_tx.htlcs() {
602-
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);
602+
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);
603603
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, self.opt_anchors(), &keys);
604604
let htlc_sighash = hash_to_message!(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, SigHashType::All)[..]);
605605
let holder_htlc_key = chan_utils::derive_private_key(&secp_ctx, &keys.per_commitment_point, &self.htlc_base_key).map_err(|_| ())?;

lightning/src/ln/chan_utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,15 +586,15 @@ pub fn make_funding_redeemscript(broadcaster: &PublicKey, countersignatory: &Pub
586586
///
587587
/// Panics if htlc.transaction_output_index.is_none() (as such HTLCs do not appear in the
588588
/// commitment transaction).
589-
pub fn build_htlc_transaction(commitment_txid: &Txid, feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, broadcaster_delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
589+
pub fn build_htlc_transaction(commitment_txid: &Txid, feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, opt_anchors: bool, broadcaster_delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
590590
let mut txins: Vec<TxIn> = Vec::new();
591591
txins.push(TxIn {
592592
previous_output: OutPoint {
593593
txid: commitment_txid.clone(),
594594
vout: htlc.transaction_output_index.expect("Can't build an HTLC transaction for a dust output"),
595595
},
596596
script_sig: Script::new(),
597-
sequence: 0,
597+
sequence: if opt_anchors { 1 } else { 0 },
598598
witness: Vec::new(),
599599
});
600600

@@ -1387,7 +1387,7 @@ impl<'a> TrustedCommitmentTransaction<'a> {
13871387

13881388
for this_htlc in inner.htlcs.iter() {
13891389
assert!(this_htlc.transaction_output_index.is_some());
1390-
let htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
1390+
let htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, self.opt_anchors(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
13911391

13921392
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);
13931393

@@ -1409,7 +1409,7 @@ impl<'a> TrustedCommitmentTransaction<'a> {
14091409
// Further, we should never be provided the preimage for an HTLC-Timeout transaction.
14101410
if this_htlc.offered && preimage.is_some() { unreachable!(); }
14111411

1412-
let mut htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
1412+
let mut htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, self.opt_anchors(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
14131413

14141414
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);
14151415

lightning/src/ln/channel.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,7 +2581,7 @@ impl<Signer: Sign> Channel<Signer> {
25812581
for (idx, (htlc, source)) in htlcs_cloned.drain(..).enumerate() {
25822582
if let Some(_) = htlc.transaction_output_index {
25832583
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_stats.feerate_per_kw,
2584-
self.get_counterparty_selected_contest_delay().unwrap(), &htlc,
2584+
self.get_counterparty_selected_contest_delay().unwrap(), &htlc, self.opt_anchors(),
25852585
&keys.broadcaster_delayed_payment_key, &keys.revocation_key);
25862586

25872587
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, self.opt_anchors(), &keys);
@@ -4927,7 +4927,7 @@ impl<Signer: Sign> Channel<Signer> {
49274927

49284928
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
49294929
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
4930-
encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, commitment_stats.feerate_per_kw, self.get_holder_selected_contest_delay(), htlc, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
4930+
encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, commitment_stats.feerate_per_kw, self.get_holder_selected_contest_delay(), htlc, self.opt_anchors(), &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
49314931
encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, self.opt_anchors(), &counterparty_keys)),
49324932
log_bytes!(counterparty_keys.broadcaster_htlc_key.serialize()),
49334933
log_bytes!(htlc_sig.serialize_compact()[..]), log_bytes!(self.channel_id()));
@@ -6223,10 +6223,10 @@ mod tests {
62236223
let remote_signature = Signature::from_der(&hex::decode($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();
62246224

62256225
let ref htlc = htlcs[$htlc_idx];
6226+
let opt_anchors = false;
62266227
let htlc_tx = chan_utils::build_htlc_transaction(&unsigned_tx.txid, chan.feerate_per_kw,
62276228
chan.get_counterparty_selected_contest_delay().unwrap(),
6228-
&htlc, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
6229-
let opt_anchors = false;
6229+
&htlc, opt_anchors, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
62306230
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, opt_anchors, &keys);
62316231
let htlc_sighash = Message::from_slice(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, SigHashType::All)[..]).unwrap();
62326232
secp_ctx.verify(&htlc_sighash, &remote_signature, &keys.countersignatory_htlc_key).unwrap();

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl BaseSign for EnforcingSigner {
156156
for (this_htlc, sig) in trusted_tx.htlcs().iter().zip(&commitment_tx.counterparty_htlc_sigs) {
157157
assert!(this_htlc.transaction_output_index.is_some());
158158
let keys = trusted_tx.keys();
159-
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, trusted_tx.feerate_per_kw(), holder_csv, &this_htlc, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
159+
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, trusted_tx.feerate_per_kw(), holder_csv, &this_htlc, self.opt_anchors(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
160160

161161
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&this_htlc, self.opt_anchors(), &keys);
162162

0 commit comments

Comments
 (0)