From 477819576af7df7435f6209b9f7a421c98661fd9 Mon Sep 17 00:00:00 2001 From: KaffinPX Date: Sat, 26 Oct 2024 04:01:42 +0300 Subject: [PATCH] Random privateKey generation --- wallet/keys/src/privatekey.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/wallet/keys/src/privatekey.rs b/wallet/keys/src/privatekey.rs index 554bdf36e..3f83052c1 100644 --- a/wallet/keys/src/privatekey.rs +++ b/wallet/keys/src/privatekey.rs @@ -5,6 +5,7 @@ use crate::imports::*; use crate::keypair::Keypair; use js_sys::{Array, Uint8Array}; +use rand::thread_rng; /// Data structure that envelops a Private Key. /// @category Wallet SDK @@ -39,6 +40,11 @@ impl PrivateKey { pub fn try_new(key: &str) -> Result { Ok(Self { inner: secp256k1::SecretKey::from_str(key)? }) } + + #[wasm_bindgen(js_name = random)] + pub fn create_new() -> PrivateKey { + Self { inner: secp256k1::SecretKey::new(&mut thread_rng()) } + } } impl PrivateKey { @@ -49,13 +55,6 @@ impl PrivateKey { #[wasm_bindgen] impl PrivateKey { - /// Returns the [`PrivateKey`] key encoded as a hex string. - #[wasm_bindgen(js_name = toString)] - pub fn to_hex(&self) -> String { - use kaspa_utils::hex::ToHex; - self.secret_bytes().to_vec().to_hex() - } - /// Generate a [`Keypair`] from this [`PrivateKey`]. #[wasm_bindgen(js_name = toKeypair)] pub fn to_keypair(&self) -> Result { @@ -91,6 +90,13 @@ impl PrivateKey { let address = Address::new(network.try_into()?, AddressVersion::PubKeyECDSA, &payload); Ok(address) } + + /// Returns the [`PrivateKey`] key encoded as a hex string. + #[wasm_bindgen(js_name = toString)] + pub fn to_hex(&self) -> String { + use kaspa_utils::hex::ToHex; + self.secret_bytes().to_vec().to_hex() + } } impl TryCastFromJs for PrivateKey {