Add get_new_keypair api

This commit is contained in:
Sosthene 2024-11-25 22:28:49 +01:00
parent 5c77ae1b4f
commit 6350b29379

View File

@ -31,7 +31,7 @@ use sdk_common::sp_client::bitcoin::hashes::{FromSliceError, HashEngine};
use sdk_common::sp_client::bitcoin::hex::{
self, parse, DisplayHex, FromHex, HexToArrayError, HexToBytesError,
};
use sdk_common::sp_client::bitcoin::key::{Parity, Secp256k1};
use sdk_common::sp_client::bitcoin::key::{Keypair, Parity, Secp256k1};
use sdk_common::sp_client::bitcoin::network::ParseNetworkError;
use sdk_common::sp_client::bitcoin::p2p::message::NetworkMessage;
use sdk_common::sp_client::bitcoin::psbt::raw;
@ -90,6 +90,15 @@ pub struct ApiReturn {
pub type ApiResult<T: FromWasmAbi> = Result<T, ApiError>;
#[derive(Debug, PartialEq, Tsify, Serialize, Deserialize, Default)]
#[tsify(into_wasm_abi)]
#[allow(non_camel_case_types)]
pub struct NewKey {
pub private_key: String,
pub x_only_public_key: String,
pub key_parity: bool
}
const IS_TESTNET: bool = true;
const DEFAULT_AMOUNT: Amount = Amount::from_sat(1000);
@ -211,6 +220,22 @@ pub fn get_address() -> ApiResult<String> {
.get_receiving_address())
}
#[wasm_bindgen]
pub fn get_new_keypair() -> NewKey {
let secp = Secp256k1::new();
let mut rng = thread_rng();
let keypair = Keypair::new(&secp, &mut rng);
let secret_hex = keypair.secret_bytes().to_lower_hex_string();
let (xonly, parity) = keypair.x_only_public_key();
NewKey {
private_key: secret_hex,
x_only_public_key: xonly.to_string(),
key_parity: if parity == Parity::Even { true } else { false }
}
}
#[wasm_bindgen]
pub fn restore_device(device_str: String) -> ApiResult<()> {
let device: Device = serde_json::from_str(&device_str)?;