Skip to content

Commit e315d22

Browse files
committed
Add a KeysInterface which provides keys from user
Move ChannelKeys into keysinterface for generate a set of it from master_seed and change return type to panic on bogus data
1 parent 81ba452 commit e315d22

File tree

3 files changed

+89
-65
lines changed

3 files changed

+89
-65
lines changed

src/chain/keysinterface.rs

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
use bitcoin::blockdata::transaction::{OutPoint, TxOut};
66
use bitcoin::blockdata::script::Script;
77

8-
use secp256k1::key::SecretKey;
8+
use secp256k1::key::{SecretKey, PublicKey};
9+
use secp256k1::Secp256k1;
10+
11+
use crypto::hkdf::{hkdf_extract,hkdf_expand};
12+
13+
use util::sha2::Sha256;
914

1015
/// When on-chain outputs are created by rust-lightning an event is generated which informs the
1116
/// user thereof. This enum descibes the format of the output and provides the OutPoint.
@@ -32,3 +37,81 @@ pub enum SpendableOutputDescriptor {
3237
to_self_delay: u16,
3338
}
3439
}
40+
41+
/// A trait to describe an object which can get user secrets and key material.
42+
pub trait KeysInterface: Send + Sync {
43+
/// Get node secret key (aka node_id or network_key)
44+
fn get_node_secret(&self) -> SecretKey;
45+
/// Get destination redeemScript to encumber static protocol exit points.
46+
fn get_destination_script(&self) -> Script;
47+
/// Get shutdown_pubkey to use as PublicKey at channel closure
48+
fn get_shutdown_pubkey(&self) -> PublicKey;
49+
/// Get a new set of ChannelKeys for per-channel secrets. These MUST be unique even if you
50+
/// restarted with some stale data!
51+
fn get_channel_keys(&self, inbound: bool) -> ChannelKeys;
52+
}
53+
54+
/// Set of lightning keys needed to operate a channel as described in BOLT 3
55+
pub struct ChannelKeys {
56+
/// Private key of anchor tx
57+
pub funding_key: SecretKey,
58+
/// Local secret key for blinded revocation pubkey
59+
pub revocation_base_key: SecretKey,
60+
/// Local secret key used in commitment tx htlc outputs
61+
pub payment_base_key: SecretKey,
62+
/// Local secret key used in HTLC tx
63+
pub delayed_payment_base_key: SecretKey,
64+
/// Local htlc secret key used in commitment tx htlc outputs
65+
pub htlc_base_key: SecretKey,
66+
/// Local secret key used for closing tx
67+
pub channel_close_key: SecretKey,
68+
/// Local secret key used in justice tx, claim tx and preimage tx outputs
69+
pub channel_monitor_claim_key: SecretKey,
70+
/// Commitment seed
71+
pub commitment_seed: [u8; 32],
72+
}
73+
74+
impl ChannelKeys {
75+
/// Generate a set of lightning keys needed to operate a channel by HKDF-expanding a given
76+
/// random 32-byte seed
77+
pub fn new_from_seed(seed: &[u8; 32]) -> ChannelKeys {
78+
let mut prk = [0; 32];
79+
hkdf_extract(Sha256::new(), b"rust-lightning key gen salt", seed, &mut prk);
80+
let secp_ctx = Secp256k1::without_caps();
81+
82+
let mut okm = [0; 32];
83+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning funding key info", &mut okm);
84+
let funding_key = SecretKey::from_slice(&secp_ctx, &okm).expect("Sha256 is broken");
85+
86+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning revocation base key info", &mut okm);
87+
let revocation_base_key = SecretKey::from_slice(&secp_ctx, &okm).expect("Sha256 is broken");
88+
89+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning payment base key info", &mut okm);
90+
let payment_base_key = SecretKey::from_slice(&secp_ctx, &okm).expect("Sha256 is broken");
91+
92+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning delayed payment base key info", &mut okm);
93+
let delayed_payment_base_key = SecretKey::from_slice(&secp_ctx, &okm).expect("Sha256 is broken");
94+
95+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning htlc base key info", &mut okm);
96+
let htlc_base_key = SecretKey::from_slice(&secp_ctx, &okm).expect("Sha256 is broken");
97+
98+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning channel close key info", &mut okm);
99+
let channel_close_key = SecretKey::from_slice(&secp_ctx, &okm).expect("Sha256 is broken");
100+
101+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning channel monitor claim key info", &mut okm);
102+
let channel_monitor_claim_key = SecretKey::from_slice(&secp_ctx, &okm).expect("Sha256 is broken");
103+
104+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning local commitment seed info", &mut okm);
105+
106+
ChannelKeys {
107+
funding_key: funding_key,
108+
revocation_base_key: revocation_base_key,
109+
payment_base_key: payment_base_key,
110+
delayed_payment_base_key: delayed_payment_base_key,
111+
htlc_base_key: htlc_base_key,
112+
channel_close_key: channel_close_key,
113+
channel_monitor_claim_key: channel_monitor_claim_key,
114+
commitment_seed: okm
115+
}
116+
}
117+
}

src/ln/channel.rs

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use secp256k1::{Secp256k1,Message,Signature};
1111
use secp256k1;
1212

1313
use crypto::digest::Digest;
14-
use crypto::hkdf::{hkdf_extract,hkdf_expand};
1514

1615
use ln::msgs;
1716
use ln::msgs::{ErrorAction, HandleError, RAACommitmentOrder};
@@ -21,6 +20,7 @@ use ln::chan_utils::{TxCreationKeys,HTLCOutputInCommitment,HTLC_SUCCESS_TX_WEIGH
2120
use ln::chan_utils;
2221
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
2322
use chain::transaction::OutPoint;
23+
use chain::keysinterface::ChannelKeys;
2424
use util::{transaction_utils,rng};
2525
use util::ser::Writeable;
2626
use util::sha2::Sha256;
@@ -33,17 +33,6 @@ use std::{cmp,mem};
3333
use std::time::Instant;
3434
use std::sync::{Arc};
3535

36-
pub struct ChannelKeys {
37-
pub funding_key: SecretKey,
38-
pub revocation_base_key: SecretKey,
39-
pub payment_base_key: SecretKey,
40-
pub delayed_payment_base_key: SecretKey,
41-
pub htlc_base_key: SecretKey,
42-
pub channel_close_key: SecretKey,
43-
pub channel_monitor_claim_key: SecretKey,
44-
pub commitment_seed: [u8; 32],
45-
}
46-
4736
#[cfg(test)]
4837
pub struct ChannelValueStat {
4938
pub value_to_self_msat: u64,
@@ -55,49 +44,6 @@ pub struct ChannelValueStat {
5544
pub their_max_htlc_value_in_flight_msat: u64, // outgoing
5645
}
5746

58-
impl ChannelKeys {
59-
pub fn new_from_seed(seed: &[u8; 32]) -> Result<ChannelKeys, secp256k1::Error> {
60-
let mut prk = [0; 32];
61-
hkdf_extract(Sha256::new(), b"rust-lightning key gen salt", seed, &mut prk);
62-
let secp_ctx = Secp256k1::without_caps();
63-
64-
let mut okm = [0; 32];
65-
hkdf_expand(Sha256::new(), &prk, b"rust-lightning funding key info", &mut okm);
66-
let funding_key = SecretKey::from_slice(&secp_ctx, &okm)?;
67-
68-
hkdf_expand(Sha256::new(), &prk, b"rust-lightning revocation base key info", &mut okm);
69-
let revocation_base_key = SecretKey::from_slice(&secp_ctx, &okm)?;
70-
71-
hkdf_expand(Sha256::new(), &prk, b"rust-lightning payment base key info", &mut okm);
72-
let payment_base_key = SecretKey::from_slice(&secp_ctx, &okm)?;
73-
74-
hkdf_expand(Sha256::new(), &prk, b"rust-lightning delayed payment base key info", &mut okm);
75-
let delayed_payment_base_key = SecretKey::from_slice(&secp_ctx, &okm)?;
76-
77-
hkdf_expand(Sha256::new(), &prk, b"rust-lightning htlc base key info", &mut okm);
78-
let htlc_base_key = SecretKey::from_slice(&secp_ctx, &okm)?;
79-
80-
hkdf_expand(Sha256::new(), &prk, b"rust-lightning channel close key info", &mut okm);
81-
let channel_close_key = SecretKey::from_slice(&secp_ctx, &okm)?;
82-
83-
hkdf_expand(Sha256::new(), &prk, b"rust-lightning channel monitor claim key info", &mut okm);
84-
let channel_monitor_claim_key = SecretKey::from_slice(&secp_ctx, &okm)?;
85-
86-
hkdf_expand(Sha256::new(), &prk, b"rust-lightning local commitment seed info", &mut okm);
87-
88-
Ok(ChannelKeys {
89-
funding_key: funding_key,
90-
revocation_base_key: revocation_base_key,
91-
payment_base_key: payment_base_key,
92-
delayed_payment_base_key: delayed_payment_base_key,
93-
htlc_base_key: htlc_base_key,
94-
channel_close_key: channel_close_key,
95-
channel_monitor_claim_key: channel_monitor_claim_key,
96-
commitment_seed: okm
97-
})
98-
}
99-
}
100-
10147
enum InboundHTLCRemovalReason {
10248
FailRelay(msgs::OnionErrorPacket),
10349
FailMalformed(([u8; 32], u16)),

src/ln/channelmanager.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ use secp256k1;
2222

2323
use chain::chaininterface::{BroadcasterInterface,ChainListener,ChainWatchInterface,FeeEstimator};
2424
use chain::transaction::OutPoint;
25-
use ln::channel::{Channel, ChannelError, ChannelKeys};
25+
use ln::channel::{Channel, ChannelError};
2626
use ln::channelmonitor::{ChannelMonitorUpdateErr, ManyChannelMonitor, CLTV_CLAIM_BUFFER, HTLC_FAIL_TIMEOUT_BLOCKS};
2727
use ln::router::{Route,RouteHop};
2828
use ln::msgs;
2929
use ln::msgs::{ChannelMessageHandler, HandleError, RAACommitmentOrder};
30+
use chain::keysinterface::ChannelKeys;
3031
use util::{byte_utils, events, internal_traits, rng};
3132
use util::sha2::Sha256;
3233
use util::ser::{Readable, Writeable};
@@ -432,10 +433,7 @@ impl ChannelManager {
432433
} else {
433434
let mut key_seed = [0u8; 32];
434435
rng::fill_bytes(&mut key_seed);
435-
match ChannelKeys::new_from_seed(&key_seed) {
436-
Ok(key) => key,
437-
Err(_) => panic!("RNG is busted!")
438-
}
436+
ChannelKeys::new_from_seed(&key_seed)
439437
};
440438

441439
let channel = Channel::new_outbound(&*self.fee_estimator, chan_keys, their_network_key, channel_value_satoshis, push_msat, self.announce_channels_publicly, user_id, Arc::clone(&self.logger))?;
@@ -1689,10 +1687,7 @@ impl ChannelManager {
16891687
} else {
16901688
let mut key_seed = [0u8; 32];
16911689
rng::fill_bytes(&mut key_seed);
1692-
match ChannelKeys::new_from_seed(&key_seed) {
1693-
Ok(key) => key,
1694-
Err(_) => panic!("RNG is busted!")
1695-
}
1690+
ChannelKeys::new_from_seed(&key_seed)
16961691
};
16971692

16981693
let channel = Channel::new_from_req(&*self.fee_estimator, chan_keys, their_node_id.clone(), msg, 0, false, self.announce_channels_publicly, Arc::clone(&self.logger))

0 commit comments

Comments
 (0)