Skip to content

Commit 2fa45ae

Browse files
committed
Add holder anchor signing support to BaseSign
1 parent 843b826 commit 2fa45ae

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use util::crypto::{hkdf_extract_expand_twice, sign};
3636
use util::ser::{Writeable, Writer, Readable, ReadableArgs};
3737

3838
use chain::transaction::OutPoint;
39+
use ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
3940
use ln::{chan_utils, PaymentPreimage};
4041
use ln::chan_utils::{HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, HolderCommitmentTransaction, ChannelTransactionParameters, CommitmentTransaction, ClosingTransaction};
4142
use ln::msgs::UnsignedChannelAnnouncement;
@@ -348,6 +349,12 @@ pub trait BaseSign {
348349
/// chosen to forgo their output as dust.
349350
fn sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
350351

352+
/// Computes the signature for a commitment transaction's anchor output used as an
353+
/// input within `anchor_tx`, which spends the commitment transaction, at index `input`.
354+
fn sign_holder_anchor_input(
355+
&self, anchor_tx: &mut Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
356+
) -> Result<Signature, ()>;
357+
351358
/// Signs a channel announcement message with our funding key and our node secret key (aka
352359
/// node_id or network_key), proving it comes from one of the channel participants.
353360
///
@@ -645,6 +652,7 @@ impl InMemorySigner {
645652
witness.push(witness_script.clone().into_bytes());
646653
Ok(witness)
647654
}
655+
648656
}
649657

650658
impl BaseSign for InMemorySigner {
@@ -762,6 +770,16 @@ impl BaseSign for InMemorySigner {
762770
Ok(closing_tx.trust().sign(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx))
763771
}
764772

773+
fn sign_holder_anchor_input(
774+
&self, anchor_tx: &mut Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
775+
) -> Result<Signature, ()> {
776+
let witness_script = chan_utils::get_anchor_redeemscript(&self.holder_channel_pubkeys.funding_pubkey);
777+
let sighash = sighash::SighashCache::new(&*anchor_tx).segwit_signature_hash(
778+
input, &witness_script, ANCHOR_OUTPUT_VALUE_SATOSHI, EcdsaSighashType::All,
779+
).unwrap();
780+
Ok(sign(secp_ctx, &hash_to_message!(&sighash[..]), &self.funding_key))
781+
}
782+
765783
fn sign_channel_announcement(&self, msg: &UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<secp256k1::All>)
766784
-> Result<(Signature, Signature), ()> {
767785
let msghash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]);

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10+
use ln::channel::{ANCHOR_OUTPUT_VALUE_SATOSHI, MIN_CHAN_DUST_LIMIT_SATOSHIS};
1011
use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitmentTransaction, CommitmentTransaction, ChannelTransactionParameters, TrustedCommitmentTransaction, ClosingTransaction};
1112
use ln::{chan_utils, msgs, PaymentPreimage};
1213
use chain::keysinterface::{Sign, InMemorySigner, BaseSign};
@@ -199,6 +200,16 @@ impl BaseSign for EnforcingSigner {
199200
Ok(self.inner.sign_closing_transaction(closing_tx, secp_ctx).unwrap())
200201
}
201202

203+
fn sign_holder_anchor_input(
204+
&self, anchor_tx: &mut Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
205+
) -> Result<Signature, ()> {
206+
debug_assert!(MIN_CHAN_DUST_LIMIT_SATOSHIS > ANCHOR_OUTPUT_VALUE_SATOSHI);
207+
// As long as our minimum dust limit is enforced and is greater than our anchor output
208+
// value, an anchor output can only have an index within [0, 1].
209+
assert!(anchor_tx.input[input].previous_output.vout == 0 || anchor_tx.input[input].previous_output.vout == 1);
210+
self.inner.sign_holder_anchor_input(anchor_tx, input, secp_ctx)
211+
}
212+
202213
fn sign_channel_announcement(&self, msg: &msgs::UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<secp256k1::All>)
203214
-> Result<(Signature, Signature), ()> {
204215
self.inner.sign_channel_announcement(msg, secp_ctx)

0 commit comments

Comments
 (0)