Skip to content

Commit b1ed0ee

Browse files
committed
Sign channel_announcements via a new ChannelKeys API
1 parent ae16c5c commit b1ed0ee

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use bitcoin::util::bip143;
1212
use bitcoin_hashes::{Hash, HashEngine};
1313
use bitcoin_hashes::sha256::HashEngine as Sha256State;
1414
use bitcoin_hashes::sha256::Hash as Sha256;
15+
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
1516
use bitcoin_hashes::hash160::Hash as Hash160;
1617

1718
use secp256k1::key::{SecretKey, PublicKey};
@@ -20,9 +21,11 @@ use secp256k1;
2021

2122
use util::byte_utils;
2223
use util::logger::Logger;
24+
use util::ser::Writeable;
2325

2426
use ln::chan_utils;
2527
use ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment};
28+
use ln::msgs;
2629

2730
use std::sync::Arc;
2831
use std::sync::atomic::{AtomicUsize, Ordering};
@@ -140,6 +143,14 @@ pub trait ChannelKeys : Send {
140143
/// TODO: Add more input vars to enable better checking (preferably removing commitment_tx and
141144
/// making the callee generate it via some util function we expose)!
142145
fn sign_remote_commitment<T: secp256k1::Signing>(&self, channel_value_satoshis: u64, channel_funding_script: &Script, feerate_per_kw: u64, commitment_tx: &Transaction, keys: &TxCreationKeys, htlcs: &[&HTLCOutputInCommitment], to_self_delay: u16, secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()>;
146+
147+
/// Signs a channel announcement message with our funding key, proving it comes from one
148+
/// of the channel participants.
149+
///
150+
/// Note that if this fails or is rejected, the channel will not be publicly announced and
151+
/// our counterparty may (though likely will not) close the channel on us for violating the
152+
/// protocol.
153+
fn sign_channel_announcement<T: secp256k1::Signing>(&self, msg: &msgs::UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()>;
143154
}
144155

145156
#[derive(Clone)]
@@ -167,7 +178,6 @@ impl ChannelKeys for InMemoryChannelKeys {
167178
fn htlc_base_key(&self) -> &SecretKey { &self.htlc_base_key }
168179
fn commitment_seed(&self) -> &[u8; 32] { &self.commitment_seed }
169180

170-
171181
fn sign_remote_commitment<T: secp256k1::Signing>(&self, channel_value_satoshis: u64, channel_funding_script: &Script, feerate_per_kw: u64, commitment_tx: &Transaction, keys: &TxCreationKeys, htlcs: &[&HTLCOutputInCommitment], to_self_delay: u16, secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()> {
172182
if commitment_tx.input.len() != 1 { return Err(()); }
173183
let commitment_sighash = hash_to_message!(&bip143::SighashComponents::new(&commitment_tx).sighash_all(&commitment_tx.input[0], &channel_funding_script, channel_value_satoshis)[..]);
@@ -191,6 +201,11 @@ impl ChannelKeys for InMemoryChannelKeys {
191201

192202
Ok((commitment_sig, htlc_sigs))
193203
}
204+
205+
fn sign_channel_announcement<T: secp256k1::Signing>(&self, msg: &msgs::UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
206+
let msghash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]);
207+
Ok(secp_ctx.sign(&msghash, &self.funding_key))
208+
}
194209
}
195210

196211
impl_writeable!(InMemoryChannelKeys, 0, {

lightning/src/ln/channel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3310,8 +3310,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
33103310
excess_data: Vec::new(),
33113311
};
33123312

3313-
let msghash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]);
3314-
let sig = self.secp_ctx.sign(&msghash, self.local_keys.funding_key());
3313+
let sig = self.local_keys.sign_channel_announcement(&msg, &self.secp_ctx)
3314+
.map_err(|_| ChannelError::Ignore("Signer rejected channel_announcement"))?;
33153315

33163316
Ok((msg, sig))
33173317
}

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use ln::chan_utils::{HTLCOutputInCommitment, TxCreationKeys};
2+
use ln::msgs;
23
use chain::keysinterface::{ChannelKeys, InMemoryChannelKeys};
34

45
use std::cmp;
@@ -50,6 +51,10 @@ impl ChannelKeys for EnforcingChannelKeys {
5051

5152
Ok(self.inner.sign_remote_commitment(channel_value_satoshis, channel_funding_script, feerate_per_kw, commitment_tx, keys, htlcs, to_self_delay, secp_ctx).unwrap())
5253
}
54+
55+
fn sign_channel_announcement<T: secp256k1::Signing>(&self, msg: &msgs::UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
56+
self.inner.sign_channel_announcement(msg, secp_ctx)
57+
}
5358
}
5459

5560
impl_writeable!(EnforcingChannelKeys, 0, {

0 commit comments

Comments
 (0)