Skip to content

Commit

Permalink
Random privateKey generation
Browse files Browse the repository at this point in the history
  • Loading branch information
KaffinPX committed Oct 26, 2024
1 parent 60e5bd7 commit 4778195
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions wallet/keys/src/privatekey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -39,6 +40,11 @@ impl PrivateKey {
pub fn try_new(key: &str) -> Result<PrivateKey> {
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 {
Expand All @@ -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<Keypair, JsError> {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4778195

Please sign in to comment.