Skip to content

Commit

Permalink
per_commitment_point
Browse files Browse the repository at this point in the history
  • Loading branch information
wpaulino committed Jun 15, 2023
1 parent a3e622d commit a9c0b45
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 57 deletions.
3 changes: 3 additions & 0 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2547,6 +2547,9 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
},
commitment_txid: htlc.commitment_txid,
per_commitment_number: htlc.per_commitment_number,
per_commitment_point: self.onchain_tx_handler.signer.get_per_commitment_point(
htlc.per_commitment_number, &self.onchain_tx_handler.secp_ctx
),
htlc: htlc.htlc,
preimage: htlc.preimage,
counterparty_sig: htlc.counterparty_sig,
Expand Down
55 changes: 19 additions & 36 deletions lightning/src/events/bump_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub struct HTLCDescriptor {
pub commitment_txid: Txid,
/// The number of the commitment transaction in which the HTLC output lives.
pub per_commitment_number: u64,
/// The point that maps to the number of the commitment transaction in which the HTLC output lives.
pub per_commitment_point: PublicKey,
/// The details of the HTLC as it appears in the commitment transaction.
pub htlc: HTLCOutputInCommitment,
/// The preimage, if `Some`, to claim the HTLC output with. If `None`, the timeout path must be
Expand All @@ -85,17 +87,15 @@ impl HTLCDescriptor {

/// Returns the delayed output created as a result of spending the HTLC output in the commitment
/// transaction.
pub fn tx_output<C: secp256k1::Signing + secp256k1::Verification>(
&self, per_commitment_point: &PublicKey, secp: &Secp256k1<C>
) -> TxOut {
pub fn tx_output<C: secp256k1::Signing + secp256k1::Verification>(&self, secp: &Secp256k1<C>) -> TxOut {
let channel_params = self.signer_descriptor.channel_parameters.as_holder_broadcastable();
let broadcaster_keys = channel_params.broadcaster_pubkeys();
let counterparty_keys = channel_params.countersignatory_pubkeys();
let broadcaster_delayed_key = chan_utils::derive_public_key(
secp, per_commitment_point, &broadcaster_keys.delayed_payment_basepoint
secp, &self.per_commitment_point, &broadcaster_keys.delayed_payment_basepoint
);
let counterparty_revocation_key = chan_utils::derive_public_revocation_key(
secp, per_commitment_point, &counterparty_keys.revocation_basepoint
secp, &self.per_commitment_point, &counterparty_keys.revocation_basepoint
);
chan_utils::build_htlc_output(
0 /* feerate_per_kw */, channel_params.contest_delay(), &self.htlc, true /* opt_anchors */,
Expand All @@ -104,20 +104,18 @@ impl HTLCDescriptor {
}

/// Returns the witness script of the HTLC output in the commitment transaction.
pub fn witness_script<C: secp256k1::Signing + secp256k1::Verification>(
&self, per_commitment_point: &PublicKey, secp: &Secp256k1<C>
) -> Script {
pub fn witness_script<C: secp256k1::Signing + secp256k1::Verification>(&self, secp: &Secp256k1<C>) -> Script {
let channel_params = self.signer_descriptor.channel_parameters.as_holder_broadcastable();
let broadcaster_keys = channel_params.broadcaster_pubkeys();
let counterparty_keys = channel_params.countersignatory_pubkeys();
let broadcaster_htlc_key = chan_utils::derive_public_key(
secp, per_commitment_point, &broadcaster_keys.htlc_basepoint
secp, &self.per_commitment_point, &broadcaster_keys.htlc_basepoint
);
let counterparty_htlc_key = chan_utils::derive_public_key(
secp, per_commitment_point, &counterparty_keys.htlc_basepoint
secp, &self.per_commitment_point, &counterparty_keys.htlc_basepoint
);
let counterparty_revocation_key = chan_utils::derive_public_revocation_key(
secp, per_commitment_point, &counterparty_keys.revocation_basepoint
secp, &self.per_commitment_point, &counterparty_keys.revocation_basepoint
);
chan_utils::get_htlc_redeemscript_with_explicit_keys(
&self.htlc, true /* opt_anchors */, &broadcaster_htlc_key, &counterparty_htlc_key,
Expand Down Expand Up @@ -669,31 +667,15 @@ where
fn build_htlc_tx(
&self, claim_id: ClaimId, target_feerate_sat_per_1000_weight: u32,
htlc_descriptors: &[HTLCDescriptor], tx_lock_time: PackedLockTime,
) -> Result<(Transaction, HashMap<[u8; 32], <SP::Target as SignerProvider>::Signer>), ()> {
) -> Result<Transaction, ()> {
let mut tx = Transaction {
version: 2,
lock_time: tx_lock_time,
input: vec![],
output: vec![],
};
// Unfortunately, we need to derive the signer for each HTLC ahead of time to obtain its
// input.
let mut signers = HashMap::new();
let mut must_spend = Vec::with_capacity(htlc_descriptors.len());
for htlc_descriptor in htlc_descriptors {
let signer = signers.entry(htlc_descriptor.signer_descriptor.channel_keys_id)
.or_insert_with(|| {
let mut signer = self.signer_provider.derive_channel_signer(
htlc_descriptor.signer_descriptor.channel_value_satoshis,
htlc_descriptor.signer_descriptor.channel_keys_id,
);
signer.provide_channel_parameters(&htlc_descriptor.signer_descriptor.channel_parameters);
signer
});
let per_commitment_point = signer.get_per_commitment_point(
htlc_descriptor.per_commitment_number, &self.secp
);

let htlc_input = htlc_descriptor.unsigned_tx_input();
must_spend.push(Input {
outpoint: htlc_input.previous_output.clone(),
Expand All @@ -704,15 +686,15 @@ where
},
});
tx.input.push(htlc_input);
let htlc_output = htlc_descriptor.tx_output(&per_commitment_point, &self.secp);
let htlc_output = htlc_descriptor.tx_output(&self.secp);
tx.output.push(htlc_output);
}

let coin_selection = self.utxo_source.select_confirmed_utxos(
claim_id, &must_spend, &tx.output, target_feerate_sat_per_1000_weight,
)?;
self.process_coin_selection(&mut tx, coin_selection);
Ok((tx, signers))
Ok(tx)
}

/// Handles a [`BumpTransactionEvent::HTLCResolution`] event variant by producing a
Expand All @@ -721,20 +703,21 @@ where
&self, claim_id: ClaimId, target_feerate_sat_per_1000_weight: u32,
htlc_descriptors: &[HTLCDescriptor], tx_lock_time: PackedLockTime,
) -> Result<(), ()> {
let (mut htlc_tx, signers) = self.build_htlc_tx(
let mut htlc_tx = self.build_htlc_tx(
claim_id, target_feerate_sat_per_1000_weight, htlc_descriptors, tx_lock_time,
)?;

self.utxo_source.sign_tx(&mut htlc_tx)?;
for (idx, htlc_descriptor) in htlc_descriptors.iter().enumerate() {
let signer = signers.get(&htlc_descriptor.signer_descriptor.channel_keys_id).unwrap();
let mut signer = self.signer_provider.derive_channel_signer(
htlc_descriptor.signer_descriptor.channel_value_satoshis,
htlc_descriptor.signer_descriptor.channel_keys_id,
);
signer.provide_channel_parameters(&htlc_descriptor.signer_descriptor.channel_parameters);
let htlc_sig = signer.sign_holder_htlc_transaction(
&htlc_tx, idx, htlc_descriptor, &self.secp
)?;
let per_commitment_point = signer.get_per_commitment_point(
htlc_descriptor.per_commitment_number, &self.secp
);
let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &self.secp);
let witness_script = htlc_descriptor.witness_script(&self.secp);
htlc_tx.input[idx].witness = htlc_descriptor.tx_input_witness(&htlc_sig, &witness_script);
}

Expand Down
18 changes: 4 additions & 14 deletions lightning/src/ln/monitor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1805,12 +1805,9 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
&descriptor.signer_descriptor.channel_keys_id,
);
signer.provide_channel_parameters(&descriptor.signer_descriptor.channel_parameters);
let per_commitment_point = signer.get_per_commitment_point(
descriptor.per_commitment_number, &secp
);
tx.output.push(descriptor.tx_output(&per_commitment_point, &secp));
tx.output.push(descriptor.tx_output(&secp));
let our_sig = signer.sign_holder_htlc_transaction(&mut tx, 0, &descriptor, &secp).unwrap();
let witness_script = descriptor.witness_script(&per_commitment_point, &secp);
let witness_script = descriptor.witness_script(&secp);
tx.input[0].witness = descriptor.tx_input_witness(&our_sig, &witness_script);
target_feerate_sat_per_1000_weight as u64
} else { panic!("unexpected event"); };
Expand Down Expand Up @@ -2236,14 +2233,8 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
assert_eq!(htlc_descriptors.len(), 2);
for htlc_descriptor in &htlc_descriptors {
assert!(!htlc_descriptor.htlc.offered);
let mut signer = nodes[1].keys_manager.derive_channel_keys(
htlc_descriptor.signer_descriptor.channel_value_satoshis,
&htlc_descriptor.signer_descriptor.channel_keys_id
);
signer.provide_channel_parameters(&htlc_descriptor.signer_descriptor.channel_parameters);
let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
htlc_tx.input.push(htlc_descriptor.unsigned_tx_input());
htlc_tx.output.push(htlc_descriptor.tx_output(&per_commitment_point, &secp));
htlc_tx.output.push(htlc_descriptor.tx_output(&secp));
}
descriptors.append(&mut htlc_descriptors);
htlc_tx.lock_time = tx_lock_time;
Expand All @@ -2259,8 +2250,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
);
signer.provide_channel_parameters(&htlc_descriptor.signer_descriptor.channel_parameters);
let our_sig = signer.sign_holder_htlc_transaction(&htlc_tx, htlc_input_idx, &htlc_descriptor, &secp).unwrap();
let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &secp);
let witness_script = htlc_descriptor.witness_script(&secp);
htlc_tx.input[htlc_input_idx].witness = htlc_descriptor.tx_input_witness(&our_sig, &witness_script);
}
let fee_utxo_sig = {
Expand Down
7 changes: 2 additions & 5 deletions lightning/src/sign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,15 +1030,12 @@ impl EcdsaChannelSigner for InMemorySigner {
&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor,
secp_ctx: &Secp256k1<secp256k1::All>
) -> Result<Signature, ()> {
let per_commitment_point = self.get_per_commitment_point(
htlc_descriptor.per_commitment_number, &secp_ctx
);
let witness_script = htlc_descriptor.witness_script(&per_commitment_point, secp_ctx);
let witness_script = htlc_descriptor.witness_script(secp_ctx);
let sighash = &sighash::SighashCache::new(&*htlc_tx).segwit_signature_hash(
input, &witness_script, htlc_descriptor.htlc.amount_msat / 1000, EcdsaSighashType::All
).map_err(|_| ())?;
let our_htlc_private_key = chan_utils::derive_private_key(
&secp_ctx, &per_commitment_point, &self.htlc_base_key
&secp_ctx, &htlc_descriptor.per_commitment_point, &self.htlc_base_key
);
Ok(sign_with_aux_rand(&secp_ctx, &hash_to_message!(sighash), &our_htlc_private_key, &self))
}
Expand Down
3 changes: 1 addition & 2 deletions lightning/src/util/enforcing_trait_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,8 @@ impl EcdsaChannelSigner for EnforcingSigner {
&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor,
secp_ctx: &Secp256k1<secp256k1::All>
) -> Result<Signature, ()> {
let per_commitment_point = self.get_per_commitment_point(htlc_descriptor.per_commitment_number, secp_ctx);
assert_eq!(htlc_tx.input[input], htlc_descriptor.unsigned_tx_input());
assert_eq!(htlc_tx.output[input], htlc_descriptor.tx_output(&per_commitment_point, secp_ctx));
assert_eq!(htlc_tx.output[input], htlc_descriptor.tx_output(secp_ctx));
Ok(self.inner.sign_holder_htlc_transaction(htlc_tx, input, htlc_descriptor, secp_ctx).unwrap())
}

Expand Down

0 comments on commit a9c0b45

Please sign in to comment.