Skip to content

Commit b9a485f

Browse files
committed
Extend BaseSign with HTLC output signing support for external claims
1 parent 7f1cb8e commit b9a485f

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ use bitcoin::{PackedLockTime, secp256k1, Sequence, Witness};
3434
use crate::util::{byte_utils, transaction_utils};
3535
use crate::util::crypto::{hkdf_extract_expand_twice, sign};
3636
use crate::util::ser::{Writeable, Writer, Readable, ReadableArgs};
37-
37+
#[cfg(anchors)]
38+
use crate::util::events::HTLCDescriptor;
3839
use crate::chain::transaction::OutPoint;
3940
use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
4041
use crate::ln::{chan_utils, PaymentPreimage};
@@ -324,6 +325,16 @@ pub trait BaseSign {
324325
/// (which is committed to in the BIP 143 signatures).
325326
fn sign_justice_revoked_htlc(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
326327

328+
#[cfg(anchors)]
329+
/// Computes the signature for a commitment transaction's HTLC output used as an input within
330+
/// `htlc_tx`, which spends the commitment transaction, at index `input`.
331+
/// Note that this should only be used to sign HTLC transactions from channels supporting anchor
332+
/// outputs after all additional inputs/outputs have been added to the transaction.
333+
fn sign_holder_htlc_transaction(
334+
&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor,
335+
secp_ctx: &Secp256k1<secp256k1::All>
336+
) -> Result<Signature, ()>;
337+
327338
/// Create a signature for a claiming transaction for a HTLC output on a counterparty's commitment
328339
/// transaction, either offered or received.
329340
///
@@ -672,7 +683,6 @@ impl InMemorySigner {
672683
witness.push(witness_script.clone().into_bytes());
673684
Ok(witness)
674685
}
675-
676686
}
677687

678688
impl BaseSign for InMemorySigner {
@@ -769,6 +779,24 @@ impl BaseSign for InMemorySigner {
769779
return Ok(sign(secp_ctx, &sighash, &revocation_key))
770780
}
771781

782+
#[cfg(anchors)]
783+
fn sign_holder_htlc_transaction(
784+
&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor,
785+
secp_ctx: &Secp256k1<secp256k1::All>
786+
) -> Result<Signature, ()> {
787+
let per_commitment_point = self.get_per_commitment_point(
788+
htlc_descriptor.per_commitment_number, &secp_ctx
789+
);
790+
let witness_script = htlc_descriptor.witness_script(&per_commitment_point, secp_ctx)?;
791+
let sighash = &sighash::SighashCache::new(&*htlc_tx).segwit_signature_hash(
792+
input, &witness_script, htlc_descriptor.htlc.amount_msat / 1000, EcdsaSighashType::All
793+
).map_err(|_| ())?;
794+
let our_htlc_private_key = chan_utils::derive_private_key(
795+
&secp_ctx, &per_commitment_point, &self.htlc_base_key
796+
).map_err(|_| ())?;
797+
Ok(sign(&secp_ctx, &hash_to_message!(sighash), &our_htlc_private_key))
798+
}
799+
772800
fn sign_counterparty_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, amount: u64, per_commitment_point: &PublicKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
773801
if let Ok(htlc_key) = chan_utils::derive_private_key(&secp_ctx, &per_commitment_point, &self.htlc_base_key) {
774802
let witness_script = if let Ok(revocation_pubkey) = chan_utils::derive_public_revocation_key(&secp_ctx, &per_commitment_point, &self.pubkeys().revocation_basepoint) {

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use bitcoin::util::sighash;
2323
use bitcoin::secp256k1;
2424
use bitcoin::secp256k1::{SecretKey, PublicKey};
2525
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
26+
#[cfg(anchors)]
27+
use crate::util::events::HTLCDescriptor;
2628
use crate::util::ser::{Writeable, Writer};
2729
use crate::io::Error;
2830

@@ -190,6 +192,17 @@ impl BaseSign for EnforcingSigner {
190192
Ok(self.inner.sign_justice_revoked_htlc(justice_tx, input, amount, per_commitment_key, htlc, secp_ctx).unwrap())
191193
}
192194

195+
#[cfg(anchors)]
196+
fn sign_holder_htlc_transaction(
197+
&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor,
198+
secp_ctx: &Secp256k1<secp256k1::All>
199+
) -> Result<Signature, ()> {
200+
let per_commitment_point = self.get_per_commitment_point(htlc_descriptor.per_commitment_number, secp_ctx);
201+
assert_eq!(htlc_tx.input[input], htlc_descriptor.unsigned_tx_input());
202+
assert_eq!(htlc_tx.output[input], htlc_descriptor.tx_output(&per_commitment_point, secp_ctx).unwrap());
203+
Ok(self.inner.sign_holder_htlc_transaction(htlc_tx, input, htlc_descriptor, secp_ctx).unwrap())
204+
}
205+
193206
fn sign_counterparty_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, amount: u64, per_commitment_point: &PublicKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
194207
Ok(self.inner.sign_counterparty_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx).unwrap())
195208
}

0 commit comments

Comments
 (0)