Skip to content

Commit ed27dda

Browse files
committed
Add anchor support to build_htlc_transaction
1 parent 4c8b87d commit ed27dda

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
@@ -597,7 +597,7 @@ impl BaseSign for InMemorySigner {
597597

598598
let mut htlc_sigs = Vec::with_capacity(commitment_tx.htlcs().len());
599599
for htlc in commitment_tx.htlcs() {
600-
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);
600+
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);
601601
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, self.opt_anchors(), &keys);
602602
let htlc_sighash = hash_to_message!(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, SigHashType::All)[..]);
603603
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
@@ -2535,7 +2535,7 @@ impl<Signer: Sign> Channel<Signer> {
25352535
for (idx, (htlc, source)) in htlcs_cloned.drain(..).enumerate() {
25362536
if let Some(_) = htlc.transaction_output_index {
25372537
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, feerate_per_kw,
2538-
self.get_counterparty_selected_contest_delay().unwrap(), &htlc,
2538+
self.get_counterparty_selected_contest_delay().unwrap(), &htlc, self.opt_anchors(),
25392539
&keys.broadcaster_delayed_payment_key, &keys.revocation_key);
25402540

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

48574857
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
48584858
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
4859-
encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, feerate_per_kw, self.get_holder_selected_contest_delay(), htlc, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
4859+
encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, feerate_per_kw, self.get_holder_selected_contest_delay(), htlc, self.opt_anchors(), &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
48604860
encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, self.opt_anchors(), &counterparty_keys)),
48614861
log_bytes!(counterparty_keys.broadcaster_htlc_key.serialize()),
48624862
log_bytes!(htlc_sig.serialize_compact()[..]), log_bytes!(self.channel_id()));
@@ -6152,10 +6152,10 @@ mod tests {
61526152
let remote_signature = Signature::from_der(&hex::decode($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();
61536153

61546154
let ref htlc = htlcs[$htlc_idx];
6155+
let opt_anchors = false;
61556156
let htlc_tx = chan_utils::build_htlc_transaction(&unsigned_tx.txid, chan.feerate_per_kw,
61566157
chan.get_counterparty_selected_contest_delay().unwrap(),
6157-
&htlc, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
6158-
let opt_anchors = false;
6158+
&htlc, opt_anchors, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
61596159
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, opt_anchors, &keys);
61606160
let htlc_sighash = Message::from_slice(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, SigHashType::All)[..]).unwrap();
61616161
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)