Add wallet.rs
This commit is contained in:
parent
1e79dfd7f6
commit
6f74996f39
28
src/api.rs
28
src/api.rs
@ -50,6 +50,7 @@ use sdk_common::sp_client::spclient::{
|
|||||||
derive_keys_from_seed, OutputList, OutputSpendStatus, OwnedOutput, Recipient, SpClient,
|
derive_keys_from_seed, OutputList, OutputSpendStatus, OwnedOutput, Recipient, SpClient,
|
||||||
};
|
};
|
||||||
use sdk_common::sp_client::spclient::{SpWallet, SpendKey};
|
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::user::{lock_connected_user, User, UserWallets, CONNECTED_USER};
|
||||||
use crate::{images, lock_messages, CACHEDMESSAGES};
|
use crate::{images, lock_messages, CACHEDMESSAGES};
|
||||||
@ -164,33 +165,6 @@ pub fn setup() {
|
|||||||
wasm_logger::init(wasm_logger::Config::default());
|
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]
|
#[wasm_bindgen]
|
||||||
pub fn get_recover_address() -> ApiResult<String> {
|
pub fn get_recover_address() -> ApiResult<String> {
|
||||||
if let Ok(my_wallets) = lock_connected_user() {
|
if let Ok(my_wallets) = lock_connected_user() {
|
||||||
|
@ -13,6 +13,7 @@ mod images;
|
|||||||
mod peers;
|
mod peers;
|
||||||
mod process;
|
mod process;
|
||||||
mod user;
|
mod user;
|
||||||
|
mod wallet;
|
||||||
|
|
||||||
pub static CACHEDMESSAGES: OnceLock<Mutex<Vec<CachedMessage>>> = OnceLock::new();
|
pub static CACHEDMESSAGES: OnceLock<Mutex<Vec<CachedMessage>>> = OnceLock::new();
|
||||||
|
|
||||||
|
28
src/wallet.rs
Normal file
28
src/wallet.rs
Normal 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)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user