use anyhow::{Context, Result};
use bsv_sdk::primitives::PrivateKey;
use bsv_wallet_toolbox::{Chain, Services, StorageSqlx, Wallet, WalletStorageWriter};
use crate::cli::Cli;
use crate::services_env;
pub struct WalletContext {
pub wallet: Wallet<StorageSqlx, Services>,
pub identity_key: String,
pub root_key: PrivateKey,
pub chain: Chain,
pub json_output: bool,
pub db_path: String,
}
impl WalletContext {
pub async fn load(cli: &Cli) -> Result<Self> {
let root_key_hex =
std::env::var("ROOT_KEY").context("ROOT_KEY not set. Run `bsv-wallet init` first.")?;
Self::load_with(&cli.db, &root_key_hex, cli.testnet, cli.json).await
}
pub async fn load_with(
db: &str,
root_key_hex: &str,
testnet: bool,
json: bool,
) -> Result<Self> {
let root_key = PrivateKey::from_hex(root_key_hex)?;
let identity_key = root_key.public_key().to_hex();
let chain = if testnet { Chain::Test } else { Chain::Main };
let storage = StorageSqlx::open(db).await?;
storage.make_available().await?;
let services = {
let opts = services_env::services_options_from_env(chain, db)?;
Services::with_options(chain, opts)?
};
if let Some(ref ct) = services.chaintracks {
storage.set_chain_tracker(ct.clone()).await;
}
let wallet = Wallet::new(Some(root_key.clone()), storage, services).await?;
Ok(Self {
wallet,
identity_key,
root_key,
chain,
json_output: json,
db_path: db.to_string(),
})
}
}