45 lines
1.2 KiB
Rust
45 lines
1.2 KiB
Rust
use std::{
|
|
collections::HashSet,
|
|
sync::{Mutex, MutexGuard, OnceLock},
|
|
};
|
|
|
|
use anyhow::Error;
|
|
use rand::Rng;
|
|
use sdk_common::sp_client::{
|
|
bitcoin::{Network, OutPoint},
|
|
silentpayments::utils::SilentPaymentAddress,
|
|
spclient::{derive_keys_from_seed, SpClient, SpWallet, SpendKey},
|
|
};
|
|
|
|
use crate::MutexExt;
|
|
|
|
pub static FREEZED_UTXOS: OnceLock<Mutex<HashSet<OutPoint>>> = OnceLock::new();
|
|
|
|
pub fn lock_freezed_utxos() -> Result<MutexGuard<'static, HashSet<OutPoint>>, Error> {
|
|
FREEZED_UTXOS
|
|
.get_or_init(|| Mutex::new(HashSet::new()))
|
|
.lock_anyhow()
|
|
}
|
|
|
|
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()?;
|
|
sdk_common::log::info!(
|
|
"Created client for sp with address: {}",
|
|
our_address.to_string()
|
|
);
|
|
|
|
let res = SpWallet::new(sp_client, None, vec![])?;
|
|
|
|
Ok(res)
|
|
}
|