use anyhow::{Error, Result}; use rand::{self, thread_rng, Rng, RngCore}; use sdk_common::sp_client::bitcoin::consensus::{deserialize, serialize}; use sdk_common::sp_client::bitcoin::hashes::{Hash, HashEngine}; use sdk_common::sp_client::bitcoin::hex::{DisplayHex, FromHex}; use sdk_common::sp_client::bitcoin::key::{Parity, Secp256k1}; use sdk_common::sp_client::bitcoin::secp256k1::{PublicKey, SecretKey, ThirtyTwoByteHash}; use sdk_common::sp_client::bitcoin::{ Network, OutPoint, ScriptBuf, Transaction, Txid, XOnlyPublicKey, }; use sdk_common::sp_client::spclient::SpClient; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; use tsify::Tsify; use wasm_bindgen::convert::VectorFromWasmAbi; use wasm_bindgen::prelude::*; use std::collections::HashMap; use std::fs::File; use std::io::{Cursor, Read, Write}; use std::str::FromStr; use std::sync::{Mutex, MutexGuard, OnceLock}; use sdk_common::device::Device; use sdk_common::sp_client::bitcoin::secp256k1::constants::SECRET_KEY_SIZE; use sdk_common::sp_client::silentpayments::bitcoin_hashes::sha256; use sdk_common::sp_client::silentpayments::utils::{Network as SpNetwork, SilentPaymentAddress}; use sdk_common::sp_client::spclient::{OutputList, SpWallet, SpendKey}; use crate::peers::Peer; use crate::wallet::generate_sp_wallet; use crate::MutexExt; use sdk_common::crypto::{AeadCore, Aes256Gcm, KeyInit}; pub static LOCAL_DEVICE: OnceLock> = OnceLock::new(); pub fn set_new_device(sp_wallet: SpWallet) -> Result { let mut device = Device::new(sp_wallet); let mut local_device = lock_local_device()?; if *local_device.get_wallet().get_client() != SpClient::default() { return Err(Error::msg("Device already initialized".to_owned())); } else { *local_device = device; } let our_address = local_device .get_wallet() .get_client() .get_receiving_address(); Ok(our_address) } pub fn lock_local_device() -> Result> { LOCAL_DEVICE .get_or_init(|| Mutex::new(Device::default())) .lock_anyhow() }