Skip to content

Commit 1fdf6cf

Browse files
committed
Bug fix using same seed for channel keys generation
1 parent b297d5b commit 1fdf6cf

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/chain/keysinterface.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use util::sha2::Sha256;
1919
use util::logger::Logger;
2020

2121
use std::sync::Arc;
22+
use std::sync::atomic::{AtomicUsize, Ordering};
2223

2324
/// When on-chain outputs are created by rust-lightning an event is generated which informs the
2425
/// user thereof. This enum describes the format of the output and provides the OutPoint.
@@ -39,7 +40,7 @@ pub enum SpendableOutputDescriptor {
3940
DynamicOutput {
4041
/// Outpoint spendable by user wallet
4142
outpoint: OutPoint,
42-
/// local_delayedkey = delayed_payment_basepoint_secret + SHA256(per_commitment_point || delayed_payment_basepoint
43+
/// local_delayedkey = delayed_payment_basepoint_secret + SHA256(per_commitment_point || delayed_payment_basepoint)
4344
local_delayedkey: SecretKey,
4445
/// witness redeemScript encumbering output
4546
witness_script: Script,
@@ -137,6 +138,7 @@ pub struct KeysManager {
137138
destination_script: Script,
138139
shutdown_pubkey: PublicKey,
139140
channel_master_key: ExtendedPrivKey,
141+
channel_child_index: AtomicUsize,
140142

141143
logger: Arc<Logger>,
142144
}
@@ -169,6 +171,7 @@ impl KeysManager {
169171
destination_script,
170172
shutdown_pubkey,
171173
channel_master_key,
174+
channel_child_index: AtomicUsize::new(0),
172175

173176
logger,
174177
}
@@ -192,11 +195,11 @@ impl KeysInterface for KeysManager {
192195
}
193196

194197
fn get_channel_keys(&self, _inbound: bool) -> ChannelKeys {
195-
let channel_pubkey = ExtendedPubKey::from_private(&self.secp_ctx, &self. channel_master_key);
196-
let mut seed = [0; 32];
197-
for (arr, slice) in seed.iter_mut().zip((&channel_pubkey.public_key.serialize()[0..32]).iter()) {
198-
*arr = *slice;
199-
}
198+
let child_ix = self.channel_child_index.fetch_add(1, Ordering::SeqCst);
199+
let child_privkey = self.channel_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32)).expect("Your RNG is busted");
200+
let child_pubkey = ExtendedPubKey::from_private(&self.secp_ctx, &child_privkey);
201+
let mut seed = [0u8; 32];
202+
seed.copy_from_slice(&child_pubkey.public_key.serialize()[1..33]);
200203
ChannelKeys::new_from_seed(&seed)
201204
}
202205
}

0 commit comments

Comments
 (0)