Skip to content

Commit 5cc6a77

Browse files
author
Antoine Riard
committed
Add a KeysInterface to bridge between user wallet and others lightning
componenents, responsible to get seed from the former and derive appropriate keyring materials Move ChannelKeys into keysinterface for generate a set of it from master_seed
1 parent ba56c95 commit 5cc6a77

File tree

3 files changed

+93
-57
lines changed

3 files changed

+93
-57
lines changed

src/chain/keysinterface.rs

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
use bitcoin::blockdata::transaction::OutPoint;
1010
use bitcoin::blockdata::script::Script;
1111

12-
use secp256k1::key::SecretKey;
12+
use secp256k1::key::{SecretKey, PublicKey};
13+
use secp256k1::Secp256k1;
14+
use secp256k1;
15+
16+
use crypto::hkdf::{hkdf_extract,hkdf_expand};
1317

1418
use util::events;
19+
use util::sha2::Sha256;
1520

1621
/// A trait to describe a wallet which sould receive data to be able to spend onchain outputs
1722
/// fron a lightning channel
@@ -66,3 +71,87 @@ impl CustomOutputScriptDescriptor {
6671
}
6772
}
6873
}
74+
75+
/// A trait to describe an object which should get secrets from user wallet and apply derivation
76+
/// to provide keys materials downstream
77+
/// node_id /0'
78+
/// destination_pubkey /1'
79+
/// shutdown_pubkey /2'
80+
/// channel_master_pubkey /3/N'
81+
pub trait KeysInterface: Send + Sync {
82+
/// Get node secret key to derive node_id
83+
fn get_node_secret(&self) -> SecretKey;
84+
/// Get destination redeemScript to encumber static protocol exit points. For now
85+
/// redeemScript is a pay-2-public-key-hash.
86+
fn get_destination_script(&self) -> Script;
87+
/// Get shutdown_pubkey to use as PublicKey at channel closure
88+
fn get_shutdown_pubkey(&self) -> PublicKey;
89+
/// Get a new set of ChannelKeys from per-channel random key /3/N'
90+
/// For Channel N, keys correspond to ChannelKeys::new_from_seed(/3/N')
91+
fn get_channel_keys(&self) -> Option<ChannelKeys>;
92+
}
93+
94+
/// Set of lightning keys needed to operate a channel as described in BOLT 3
95+
pub struct ChannelKeys {
96+
/// Private key of anchor tx
97+
pub funding_key: SecretKey,
98+
/// Local secret key for blinded revocation pubkey
99+
pub revocation_base_key: SecretKey,
100+
/// Local secret key used in commitment tx htlc outputs
101+
pub payment_base_key: SecretKey,
102+
/// Local secret key used in HTLC tx
103+
pub delayed_payment_base_key: SecretKey,
104+
/// Local htlc secret key used in commitment tx htlc outputs
105+
pub htlc_base_key: SecretKey,
106+
/// Local secret key used for closing tx
107+
pub channel_close_key: SecretKey,
108+
/// Local secret key used in justice tx, claim tx and preimage tx outputs
109+
pub channel_monitor_claim_key: SecretKey,
110+
/// Commitment seed
111+
pub commitment_seed: [u8; 32],
112+
}
113+
114+
impl ChannelKeys {
115+
/// Generate a set of lightning keys needed to operate a channel as described in BOLT 3 from
116+
/// used-provided seed
117+
pub fn new_from_seed(seed: &[u8; 32]) -> Result<ChannelKeys, secp256k1::Error> {
118+
let mut prk = [0; 32];
119+
hkdf_extract(Sha256::new(), b"rust-lightning key gen salt", seed, &mut prk);
120+
let secp_ctx = Secp256k1::without_caps();
121+
122+
let mut okm = [0; 32];
123+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning funding key info", &mut okm);
124+
let funding_key = SecretKey::from_slice(&secp_ctx, &okm)?;
125+
126+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning revocation base key info", &mut okm);
127+
let revocation_base_key = SecretKey::from_slice(&secp_ctx, &okm)?;
128+
129+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning payment base key info", &mut okm);
130+
let payment_base_key = SecretKey::from_slice(&secp_ctx, &okm)?;
131+
132+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning delayed payment base key info", &mut okm);
133+
let delayed_payment_base_key = SecretKey::from_slice(&secp_ctx, &okm)?;
134+
135+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning htlc base key info", &mut okm);
136+
let htlc_base_key = SecretKey::from_slice(&secp_ctx, &okm)?;
137+
138+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning channel close key info", &mut okm);
139+
let channel_close_key = SecretKey::from_slice(&secp_ctx, &okm)?;
140+
141+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning channel monitor claim key info", &mut okm);
142+
let channel_monitor_claim_key = SecretKey::from_slice(&secp_ctx, &okm)?;
143+
144+
hkdf_expand(Sha256::new(), &prk, b"rust-lightning local commitment seed info", &mut okm);
145+
146+
Ok(ChannelKeys {
147+
funding_key: funding_key,
148+
revocation_base_key: revocation_base_key,
149+
payment_base_key: payment_base_key,
150+
delayed_payment_base_key: delayed_payment_base_key,
151+
htlc_base_key: htlc_base_key,
152+
channel_close_key: channel_close_key,
153+
channel_monitor_claim_key: channel_monitor_claim_key,
154+
commitment_seed: okm
155+
})
156+
}
157+
}

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: 2 additions & 1 deletion
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};

0 commit comments

Comments
 (0)