Skip to content

Commit e204c72

Browse files
author
Antoine Riard
committed
Dry-up witnessScript in sign_justice_transaction
Instead of blindly signing provided witnessScript, signer must derive channel keys corresponding to the provided per-commitment-point and regenerate templated witnessScript to ensure its syntax correctness.
1 parent 9eb0620 commit e204c72

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,6 @@ pub trait ChannelKeys : Send+Clone {
256256
/// It may be called multiples time for same output(s) if a fee-bump is needed with regards
257257
/// to an upcoming timelock expiration.
258258
///
259-
/// Witness_script is a revokable witness script as defined in BOLT3 for `to_local`/HTLC
260-
/// outputs.
261-
///
262259
/// Input index is a pointer towards outpoint spent, commited by sigs (BIP 143).
263260
///
264261
/// Amount is value of the output spent by this input, committed by sigs (BIP 143).
@@ -267,8 +264,14 @@ pub trait ChannelKeys : Send+Clone {
267264
/// revocating detected onchain transaction. It's not a _local_ secret key, therefore
268265
/// it may cross interfaces, a node compromise won't allow to spend revoked output without
269266
/// also compromissing revocation key.
270-
//TODO: dry-up witness_script and pass pubkeys
271-
fn sign_justice_transaction<T: secp256k1::Signing>(&self, justice_tx: &Transaction, input: usize, witness_script: &Script, amount: u64, per_commitment_key: &SecretKey, revocation_pubkey: &PublicKey, is_htlc: bool, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()>;
267+
///
268+
/// htlc holds HTLC elements (hash, timelock) if output spent is a HTLC one, committed as
269+
/// part of witnessScript by sigs (BIP 143).
270+
///
271+
/// on_remote_tx_csv is the relative lock-time challenge if output spent is on remote
272+
/// balance or 2nd-stage HTLC transactions, committed as part of witnessScript by sigs
273+
/// (BIP 143).
274+
fn sign_justice_transaction<T: secp256k1::Signing + secp256k1::Verification>(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, htlc: &Option<HTLCOutputInCommitment>, on_remote_tx_csv: u16, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()>;
272275

273276
/// Create a signature for a claiming transaction for a HTLC output on a remote commitment
274277
/// transaction, either offered or received.
@@ -475,8 +478,22 @@ impl ChannelKeys for InMemoryChannelKeys {
475478
local_commitment_tx.get_htlc_sigs(&self.htlc_base_key, local_csv, secp_ctx)
476479
}
477480

478-
fn sign_justice_transaction<T: secp256k1::Signing>(&self, justice_tx: &Transaction, input: usize, witness_script: &Script, amount: u64, per_commitment_key: &SecretKey, revocation_pubkey: &PublicKey, is_htlc: bool, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
481+
fn sign_justice_transaction<T: secp256k1::Signing + secp256k1::Verification>(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, htlc: &Option<HTLCOutputInCommitment>, on_remote_tx_csv: u16, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
479482
if let Ok(revocation_key) = chan_utils::derive_private_revocation_key(&secp_ctx, &per_commitment_key, &self.revocation_base_key) {
483+
let per_commitment_point = PublicKey::from_secret_key(secp_ctx, &per_commitment_key);
484+
let witness_script = if let Ok(revocation_pubkey) = chan_utils::derive_public_revocation_key(&secp_ctx, &per_commitment_point, &self.pubkeys().revocation_basepoint) {
485+
if let Some(htlc) = htlc {
486+
if let Ok(remote_htlcpubkey) = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.remote_pubkeys().htlc_basepoint) {
487+
if let Ok(local_htlcpubkey) = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.pubkeys().htlc_basepoint) {
488+
chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &remote_htlcpubkey, &local_htlcpubkey, &revocation_pubkey)
489+
} else { return Err(()) }
490+
} else { return Err(()) }
491+
} else {
492+
if let Ok(remote_delayedpubkey) = chan_utils::derive_public_key(&secp_ctx, &per_commitment_point, &self.remote_pubkeys().delayed_payment_basepoint) {
493+
chan_utils::get_revokeable_redeemscript(&revocation_pubkey, on_remote_tx_csv, &remote_delayedpubkey)
494+
} else { return Err(()) }
495+
}
496+
} else { return Err(()) };
480497
let sighash_parts = bip143::SighashComponents::new(&justice_tx);
481498
let sighash = hash_to_message!(&sighash_parts.sighash_all(&justice_tx.input[input], &witness_script, amount)[..]);
482499
return Ok(secp_ctx.sign(&sighash, &revocation_key))

lightning/src/ln/onchaintx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
585585
chan_utils::get_revokeable_redeemscript(&chan_keys.revocation_key, *on_remote_tx_csv, &chan_keys.a_delayed_payment_key)
586586
};
587587

588-
if let Ok(sig) = self.key_storage.sign_justice_transaction(&bumped_tx, i, &witness_script, *amount, &per_commitment_key, &chan_keys.revocation_key, htlc.is_some(), &self.secp_ctx) {
588+
if let Ok(sig) = self.key_storage.sign_justice_transaction(&bumped_tx, i, *amount, &per_commitment_key, htlc, *on_remote_tx_csv, &self.secp_ctx) {
589589
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
590590
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
591591
if htlc.is_some() {

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ impl ChannelKeys for EnforcingChannelKeys {
110110
Ok(self.inner.sign_local_commitment_htlc_transactions(local_commitment_tx, local_csv, secp_ctx).unwrap())
111111
}
112112

113-
fn sign_justice_transaction<T: secp256k1::Signing>(&self, justice_tx: &Transaction, input: usize, witness_script: &Script, amount: u64, per_commitment_key: &SecretKey, revocation_pubkey: &PublicKey, is_htlc: bool, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
114-
Ok(self.inner.sign_justice_transaction(justice_tx, input, witness_script, amount, per_commitment_key, revocation_pubkey, is_htlc, secp_ctx).unwrap())
113+
fn sign_justice_transaction<T: secp256k1::Signing + secp256k1::Verification>(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, htlc: &Option<HTLCOutputInCommitment>, on_remote_tx_csv: u16, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
114+
Ok(self.inner.sign_justice_transaction(justice_tx, input, amount, per_commitment_key, htlc, on_remote_tx_csv, secp_ctx).unwrap())
115115
}
116116

117117
fn sign_remote_htlc_transaction<T: secp256k1::Signing>(&self, htlc_tx: &Transaction, input: usize, witness_script: &Script, amount: u64, per_commitment_point: &PublicKey, preimage: &Option<PaymentPreimage>, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {

0 commit comments

Comments
 (0)