Skip to content

Commit ac64b68

Browse files
author
Antoine Riard
committed
Extend ChannelKeys API with sign_cpfp
A bumping CPFP must spend the holder anchor output which is encumbered by the funding public key. An external signer can thus verify that the CPFP feerate transaction is "reasonable".
1 parent d77d8e4 commit ac64b68

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,17 @@ pub trait ChannelKeys : Send+Clone {
311311
/// chosen to forgo their output as dust.
312312
fn sign_closing_transaction<T: secp256k1::Signing>(&self, closing_tx: &Transaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()>;
313313

314+
/// Create a signature for a Child-Pay-For-Parent transaction.
315+
///
316+
/// Such a transaction is used to unilaterally bump a commitment transaction feerate when this
317+
/// is one isn't confirming quickly.
318+
///
319+
/// Input index is the position of the spending input committed by this BIP 143 signature.
320+
///
321+
/// Spent amount is the value of the output spent by this input committed by this BIP 143
322+
/// signature.
323+
fn sign_cpfp<T: secp256k1::Signing>(&self, cpfp: &Transaction, input_index: usize, spent_amount: u64, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()>;
324+
314325
/// Signs a channel announcement message with our funding key, proving it comes from one
315326
/// of the channel participants.
316327
///
@@ -583,6 +594,13 @@ impl ChannelKeys for InMemoryChannelKeys {
583594
Ok(secp_ctx.sign(&sighash, &self.funding_key))
584595
}
585596

597+
fn sign_cpfp<T: secp256k1::Signing>(&self, cpfp_tx: &Transaction, input_index: usize, spent_amount: u64, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
598+
let anchor_redeemscript = chan_utils::get_anchor_redeemscript(&self.pubkeys().funding_pubkey);
599+
let mut sighash_parts = bip143::SigHashCache::new(cpfp_tx);
600+
let sighash = hash_to_message!(&sighash_parts.signature_hash(input_index, &anchor_redeemscript, spent_amount, SigHashType::All)[..]);
601+
Ok(secp_ctx.sign(&sighash, &self.funding_key))
602+
}
603+
586604
fn sign_channel_announcement<T: secp256k1::Signing>(&self, msg: &UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
587605
let msghash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]);
588606
Ok(secp_ctx.sign(&msghash, &self.funding_key))

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ impl ChannelKeys for EnforcingChannelKeys {
128128
Ok(self.inner.sign_closing_transaction(closing_tx, secp_ctx).unwrap())
129129
}
130130

131+
fn sign_cpfp<T: secp256k1::Signing>(&self, cpfp_tx: &Transaction, input_index: usize, spent_amount: u64, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
132+
Ok(self.inner.sign_cpfp(cpfp_tx, input_index, spent_amount, secp_ctx).unwrap())
133+
}
134+
131135
fn sign_channel_announcement<T: secp256k1::Signing>(&self, msg: &msgs::UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
132136
self.inner.sign_channel_announcement(msg, secp_ctx)
133137
}

0 commit comments

Comments
 (0)