Add wallet.rs

This commit is contained in:
Sosthene 2024-07-11 12:39:25 +02:00
parent 1e79dfd7f6
commit 6f74996f39
3 changed files with 30 additions and 27 deletions

View File

@ -50,6 +50,7 @@ use sdk_common::sp_client::spclient::{
derive_keys_from_seed, OutputList, OutputSpendStatus, OwnedOutput, Recipient, SpClient,
};
use sdk_common::sp_client::spclient::{SpWallet, SpendKey};
use crate::wallet::generate_sp_wallet;
use crate::user::{lock_connected_user, User, UserWallets, CONNECTED_USER};
use crate::{images, lock_messages, CACHEDMESSAGES};
@ -164,33 +165,6 @@ pub fn setup() {
wasm_logger::init(wasm_logger::Config::default());
}
// Should be transfered to annother module
pub fn generate_sp_wallet(
label: Option<String>,
birthday: u32,
network: Network,
) -> ApiResult<SpWallet> {
let mut seed = [0u8; 64];
rand::thread_rng().fill(&mut seed);
let (scan_sk, spend_sk) = derive_keys_from_seed(&seed, network)?;
let sp_client = SpClient::new(
label.unwrap_or("default".into()),
scan_sk,
SpendKey::Secret(spend_sk),
None,
network,
)?;
let our_address: SilentPaymentAddress = sp_client.get_receiving_address().try_into()?;
log::info!(
"Created client for sp with address: {}",
our_address.to_string()
);
let res = SpWallet::new(sp_client, None)?;
Ok(res)
}
#[wasm_bindgen]
pub fn get_recover_address() -> ApiResult<String> {
if let Ok(my_wallets) = lock_connected_user() {

View File

@ -13,6 +13,7 @@ mod images;
mod peers;
mod process;
mod user;
mod wallet;
pub static CACHEDMESSAGES: OnceLock<Mutex<Vec<CachedMessage>>> = OnceLock::new();

28
src/wallet.rs Normal file
View File

@ -0,0 +1,28 @@
use rand::Rng;
use sdk_common::sp_client::{
bitcoin::Network,
silentpayments::utils::SilentPaymentAddress,
spclient::{derive_keys_from_seed, SpClient, SpWallet, SpendKey},
};
pub fn generate_sp_wallet(label: Option<String>, network: Network) -> anyhow::Result<SpWallet> {
let mut seed = [0u8; 64];
rand::thread_rng().fill(&mut seed);
let (scan_sk, spend_sk) = derive_keys_from_seed(&seed, network)?;
let sp_client = SpClient::new(
label.unwrap_or("default".into()),
scan_sk,
SpendKey::Secret(spend_sk),
None,
network,
)?;
let our_address: SilentPaymentAddress = sp_client.get_receiving_address().try_into()?;
log::info!(
"Created client for sp with address: {}",
our_address.to_string()
);
let res = SpWallet::new(sp_client, None)?;
Ok(res)
}