-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
245 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2024 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use crate::error::Error; | ||
use crate::store::WalletStore; | ||
use indexed_db_futures::prelude::*; | ||
use js_sys::Uint8Array; | ||
use wasm_bindgen::JsValue; | ||
|
||
static DB_NAME: &str = "test_db"; | ||
static KV_STORE: &str = "kv_store"; | ||
/// A simple IndexedDb store for wallet data | ||
pub struct IdbStore { | ||
pub inner: indexed_db_futures::IdbDatabase, | ||
} | ||
|
||
impl WalletStore for IdbStore { | ||
async fn update(&mut self, key: &str, value: &[u8]) -> Result<(), Error> { | ||
let tx = self | ||
.inner | ||
.transaction_on_one_with_mode(KV_STORE, IdbTransactionMode::Readwrite)?; | ||
let store = tx.object_store(KV_STORE)?; | ||
|
||
store.put_key_val_owned(JsValue::from_str(key), &Uint8Array::from(value))?; | ||
tx.await.into_result()?; | ||
Ok(()) | ||
} | ||
|
||
async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, Error> { | ||
let tx = self | ||
.inner | ||
.transaction_on_one_with_mode(KV_STORE, IdbTransactionMode::Readonly)?; | ||
let store = tx.object_store(KV_STORE)?; | ||
match store.get(&JsValue::from_str(key))?.await? { | ||
Some(v) => { | ||
let v = Uint8Array::from(v); | ||
Ok(Some(v.to_vec())) | ||
} | ||
None => Ok(None), | ||
} | ||
} | ||
|
||
async fn clear(&mut self, key: &str) -> Result<(), Error> { | ||
let tx = self | ||
.inner | ||
.transaction_on_one_with_mode(KV_STORE, IdbTransactionMode::Readwrite)?; | ||
let store = tx.object_store(KV_STORE)?; | ||
store.delete_owned(&JsValue::from_str(key))?; | ||
tx.await.into_result()?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl IdbStore { | ||
pub async fn new() -> Result<Self, Error> { | ||
let mut db_req = IdbDatabase::open_u32(DB_NAME, 1)?; | ||
// let db = open.await?; | ||
db_req.set_on_upgrade_needed(Some(|evt: &IdbVersionChangeEvent| -> Result<(), JsValue> { | ||
let create_store_if_needed = | ||
|evt: &IdbVersionChangeEvent, store_key: &'static str| -> Result<(), JsValue> { | ||
if let None = evt.db().object_store_names().find(|n| n == store_key) { | ||
evt.db().create_object_store(store_key)?; | ||
} | ||
Ok(()) | ||
}; | ||
create_store_if_needed(evt, &KV_STORE)?; | ||
Ok(()) | ||
})); | ||
Ok(Self { | ||
inner: db_req.await?, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod indexed_db_store; | ||
mod injected_store; | ||
mod memory_store; | ||
mod wallet_store; | ||
|
||
pub use indexed_db_store::IdbStore; | ||
pub use injected_store::InjectedStore; | ||
pub use memory_store::MemoryStore; | ||
pub use wallet_store::WalletStore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use wasm_bindgen_test::*; | ||
use web_sys::console; | ||
wasm_bindgen_test_configure!(run_in_browser); | ||
use webz_core::store::{self, WalletStore as _}; | ||
#[wasm_bindgen_test] | ||
fn pass() { | ||
assert_eq!(1, 1); | ||
} | ||
|
||
#[wasm_bindgen_test] | ||
async fn idb() { | ||
let k = "key"; | ||
let v = vec![1, 2, 3]; | ||
let v2 = vec![1, 2, 3, 4]; | ||
// Add to store | ||
let mut store = store::IdbStore::new().await.unwrap(); | ||
store.update(k, &v).await.unwrap(); | ||
|
||
// Get from store | ||
assert_eq!(store.get(k).await.unwrap().unwrap(), v); | ||
|
||
// Update Key | ||
store.update(k, &v2).await.unwrap(); | ||
assert_eq!(store.get(k).await.unwrap().unwrap(), v2); | ||
|
||
// Clear Key | ||
store.clear(k).await.unwrap(); | ||
assert_eq!(store.get(k).await.unwrap(), None); | ||
} |