diff --git a/src/components/run_window/key_store_tab.rs b/src/components/run_window/key_store_tab.rs index 4154531..6f3d76e 100644 --- a/src/components/run_window/key_store_tab.rs +++ b/src/components/run_window/key_store_tab.rs @@ -9,36 +9,38 @@ use leptos::{ SignalWith, View, }; use simfony::{elements, simplicity}; +use elements::secp256k1_zkp::rand; use crate::components::copy_to_clipboard::CopyToClipboard; #[derive(Copy, Clone, Debug)] pub struct SigningKeys { + pub key_offset: RwSignal, pub key_count: RwSignal, pub secret_keys: Signal>, } impl Default for SigningKeys { fn default() -> Self { - Self::new(1) + Self::new(rand::random(), 1) } } impl SigningKeys { - pub fn new(key_count: u32) -> Self { - let key_count = create_rw_signal(key_count); + pub fn new(key_offset: u32, key_count: u32) -> Self { let secret_keys = Signal::derive(move || -> Vec { let mut index = 0; - (0..key_count.get()) + (0..key_count) .map(|_| { - let (key, new_index) = new_key(index); + let (key, new_index) = new_key(key_offset, index); index = new_index; key }) .collect() }); Self { - key_count, + key_offset: create_rw_signal(key_offset), + key_count: create_rw_signal(key_count), secret_keys, } } @@ -82,11 +84,12 @@ impl SigningKeys { } } -fn new_key(start_index: u32) -> (secp256k1_zkp::Keypair, u32) { +fn new_key(start_offset: u32, start_index: u32) -> (secp256k1_zkp::Keypair, u32) { let mut offset = 1; loop { let index = start_index + offset; let mut secret_key_bytes = [0u8; 32]; + secret_key_bytes[24..28].copy_from_slice(&start_offset.to_be_bytes()); secret_key_bytes[28..].copy_from_slice(&index.to_be_bytes()); match secp256k1_zkp::Keypair::from_seckey_slice(secp256k1_zkp::SECP256K1, &secret_key_bytes) { diff --git a/src/components/state.rs b/src/components/state.rs index e940c33..0ef09b9 100644 --- a/src/components/state.rs +++ b/src/components/state.rs @@ -18,15 +18,18 @@ pub trait ToParams { impl FromParams for SigningKeys { fn from_map(map: &ParamsMap) -> Option { - map.get("keys") - .and_then(|s| s.parse::().ok()) - .map(Self::new) + let key_offset = map.get("seed").and_then(|s| s.parse::().ok())?; + let key_count = map.get("keys").and_then(|s| s.parse::().ok())?; + Some(Self::new(key_offset, key_count)) } } impl ToParams for SigningKeys { fn to_params(&self) -> impl Iterator { - [("keys", self.key_count.get_untracked().to_string())].into_iter() + [ + ("seed", self.key_offset.get_untracked().to_string()), + ("keys", self.key_count.get_untracked().to_string()), + ].into_iter() } }