Skip to content

Commit

Permalink
client-worker: replace in-memory wallet storage with localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
Twey committed Aug 16, 2024
1 parent 89db1be commit 752b90e
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions client-worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ use linera_core::node::{
use wasm_bindgen::prelude::*;
use web_sys::*;

use std::sync::RwLock;

use linera_client::{chain_listener::ClientContext as _, client_options::ClientOptions, wallet::Wallet};

// TODO convert to IndexedDbStore once we refactor Context
Expand Down Expand Up @@ -81,24 +79,17 @@ pub const OPTIONS: ClientOptions = ClientOptions {
tokio_threads: Some(1),
};

pub async fn get_client_context(wallet: &Wallet) -> Result<ClientContext, JsError> {
pub async fn get_client_context() -> Result<ClientContext, JsError> {
let wallet = linera_client::config::WalletState::read_from_local_storage("linera-wallet")?;
let mut storage = get_storage().await?;
wallet.genesis_config().initialize_storage(&mut storage).await?;
Ok(ClientContext::new(get_storage().await?, OPTIONS, OPTIONS.wallet()?))
Ok(ClientContext::new(get_storage().await?, OPTIONS, wallet))
}

#[wasm_bindgen]
pub async fn dapp_query_validators() -> Result<(), JsError> {
let wallet = WALLET.read()?;
let wallet = wallet.as_ref().ok_or(JsError::new("no wallet set"))?;

let mut storage = get_storage().await?;

wallet.genesis_config().initialize_storage(&mut storage).await?;

let chain_id = wallet.default_chain().expect("No default chain");

let mut client_context: ClientContext = get_client_context(wallet).await?;
let mut client_context: ClientContext = get_client_context().await?;
let chain_id = client_context.wallet().default_chain().expect("No default chain");

let mut chain_client = client_context.make_chain_client(chain_id);
log::info!(
Expand Down Expand Up @@ -133,11 +124,9 @@ pub async fn dapp_query_validators() -> Result<(), JsError> {
Ok(())
}

static WALLET: RwLock<Option<Wallet>> = RwLock::new(None);

#[wasm_bindgen]
pub async fn set_wallet(wallet: &str) -> Result<(), wasm_bindgen::JsError> {
*WALLET.write()? = serde_json::from_str(wallet)?;
linera_client::config::WalletState::create_from_local_storage("linera-wallet", serde_json::from_str(wallet)?)?;
Ok(())
}

Expand Down

0 comments on commit 752b90e

Please sign in to comment.