Skip to content

Commit 6caff9c

Browse files
committed
Add wasm compatible database using gloo_storage::LocalStorage
1 parent 5d5b2fb commit 6caff9c

4 files changed

Lines changed: 504 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ rocksdb = { version = "0.14", default-features = false, features = ["snappy"], o
3232
cc = { version = ">=1.0.64", optional = true }
3333
socks = { version = "0.3", optional = true }
3434
hwi = { version = "0.3.0", optional = true }
35+
gloo-storage = { version = "0.2.2", optional = true }
3536

3637
bip39 = { version = "1.0.1", optional = true }
3738
bitcoinconsensus = { version = "0.19.0-3", optional = true }
@@ -61,6 +62,7 @@ all-keys = ["keys-bip39"]
6162
keys-bip39 = ["bip39"]
6263
rpc = ["bitcoincore-rpc"]
6364
hardware-signer = ["hwi"]
65+
wasm-db = ["gloo-storage"]
6466

6567
# We currently provide mulitple implementations of `Blockchain`, all are
6668
# blocking except for the `EsploraBlockchain` which can be either async or

src/database/any.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ macro_rules! impl_inner_method {
6868
$enum_name::Sled(inner) => inner.$name( $($args, )* ),
6969
#[cfg(feature = "sqlite")]
7070
$enum_name::Sqlite(inner) => inner.$name( $($args, )* ),
71+
#[cfg(feature = "wasm-db")]
72+
$enum_name::LocalStorage(inner) => inner.$name( $($args, )* ),
7173
}
7274
}
7375
}
@@ -89,11 +91,16 @@ pub enum AnyDatabase {
8991
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
9092
/// Sqlite embedded database using [`rusqlite`]
9193
Sqlite(sqlite::SqliteDatabase),
94+
#[cfg(feature = "wasm-db")]
95+
#[cfg_attr(docsrs, doc(cfg(feature = "wasm-db")))]
96+
/// Browser LocalStorage database based on [`gloo_storage`]
97+
LocalStorage(localstorage::LocalStorageDatabase),
9298
}
9399

94100
impl_from!(memory::MemoryDatabase, AnyDatabase, Memory,);
95101
impl_from!(sled::Tree, AnyDatabase, Sled, #[cfg(feature = "key-value-db")]);
96102
impl_from!(sqlite::SqliteDatabase, AnyDatabase, Sqlite, #[cfg(feature = "sqlite")]);
103+
impl_from!(localstorage::LocalStorageDatabase, AnyDatabase, LocalStorage, #[cfg(feature = "wasm-db")]);
97104

98105
/// Type that contains any of the [`BatchDatabase::Batch`] types defined by the library
99106
pub enum AnyBatch {
@@ -107,6 +114,10 @@ pub enum AnyBatch {
107114
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
108115
/// Sqlite embedded database using [`rusqlite`]
109116
Sqlite(<sqlite::SqliteDatabase as BatchDatabase>::Batch),
117+
#[cfg(feature = "wasm-db")]
118+
#[cfg_attr(docsrs, doc(cfg(feature = "wasm-db")))]
119+
/// Browser LocalStorage database based on [`gloo_storage`]
120+
LocalStorage(<gloo_storage::LocalStorage as BatchDatabase>::Batch),
110121
}
111122

112123
impl_from!(
@@ -116,6 +127,7 @@ impl_from!(
116127
);
117128
impl_from!(<sled::Tree as BatchDatabase>::Batch, AnyBatch, Sled, #[cfg(feature = "key-value-db")]);
118129
impl_from!(<sqlite::SqliteDatabase as BatchDatabase>::Batch, AnyBatch, Sqlite, #[cfg(feature = "sqlite")]);
130+
impl_from!(<localstorage::LocalStorageDatabase as BatchDatabase>::Batch, AnyBatch, LocalStorage, #[cfg(feature = "wasm-db")]);
119131

120132
impl BatchOperations for AnyDatabase {
121133
fn set_script_pubkey(
@@ -326,6 +338,8 @@ impl BatchDatabase for AnyDatabase {
326338
AnyDatabase::Sled(inner) => inner.begin_batch().into(),
327339
#[cfg(feature = "sqlite")]
328340
AnyDatabase::Sqlite(inner) => inner.begin_batch().into(),
341+
#[cfg(feature = "wasm-db")]
342+
AnyDatabase::LocalStorage(inner) => inner.begin_batch().into(),
329343
}
330344
}
331345
fn commit_batch(&mut self, batch: Self::Batch) -> Result<(), Error> {
@@ -345,6 +359,11 @@ impl BatchDatabase for AnyDatabase {
345359
AnyBatch::Sqlite(batch) => db.commit_batch(batch),
346360
_ => unimplemented!("Other batch shouldn't be used with Sqlite db."),
347361
},
362+
#[cfg(feature = "wasm-db")]
363+
AnyDatabase::LocalStorage(db) => match batch {
364+
AnyBatch::LocalStorage(batch) => db.commit_batch(batch),
365+
_ => unimplemented!("Other batch shouldn't be used with LocalStorage db."),
366+
},
348367
}
349368
}
350369
}
@@ -402,6 +421,10 @@ pub enum AnyDatabaseConfig {
402421
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
403422
/// Sqlite embedded database using [`rusqlite`]
404423
Sqlite(SqliteDbConfiguration),
424+
#[cfg(feature = "wasm-db")]
425+
#[cfg_attr(docsrs, doc(cfg(feature = "wasm-db")))]
426+
/// Browser LocalStorage database based on [`gloo_storage`]
427+
LocalStorage(()),
405428
}
406429

407430
impl ConfigurableDatabase for AnyDatabase {
@@ -418,10 +441,15 @@ impl ConfigurableDatabase for AnyDatabase {
418441
AnyDatabaseConfig::Sqlite(inner) => {
419442
AnyDatabase::Sqlite(sqlite::SqliteDatabase::from_config(inner)?)
420443
}
444+
#[cfg(feature = "wasm-db")]
445+
AnyDatabaseConfig::LocalStorage(inner) => {
446+
AnyDatabase::LocalStorage(localstorage::LocalStorageDatabase::from_config(inner)?)
447+
}
421448
})
422449
}
423450
}
424451

425452
impl_from!((), AnyDatabaseConfig, Memory,);
426453
impl_from!(SledDbConfiguration, AnyDatabaseConfig, Sled, #[cfg(feature = "key-value-db")]);
427454
impl_from!(SqliteDbConfiguration, AnyDatabaseConfig, Sqlite, #[cfg(feature = "sqlite")]);
455+
impl_from!((), AnyDatabaseConfig, LocalStorage, #[cfg(feature = "wasm-db")]);

0 commit comments

Comments
 (0)