Skip to content

Commit d33cb3c

Browse files
Antoine RiardTheBlueMatt
authored andcommitted
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 8c235d9 commit d33cb3c

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

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)