-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store decompressed BlsPublicKeys in IndexedDB
- Loading branch information
Showing
5 changed files
with
167 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Compress}; | ||
use idb::{ | ||
Database, DatabaseEvent, Error, Factory, IndexParams, KeyPath, ObjectStore, ObjectStoreParams, | ||
TransactionMode, | ||
}; | ||
use nimiq_bls::{G2Projective, LazyPublicKey, PublicKey}; | ||
use nimiq_serde::{Deserialize, Serialize}; | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
|
||
/// Caches decompressed BlsPublicKeys in an IndexedDB | ||
pub(crate) struct BlsCache { | ||
db: Option<Database>, | ||
keys: Vec<LazyPublicKey>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
struct BlsKeyEntry { | ||
compressed_key: String, | ||
public_key: String, | ||
} | ||
|
||
impl BlsCache { | ||
pub async fn new() -> Self { | ||
let db = match Database::builder("nimiq_client_cache") | ||
.version(1) | ||
.add_object_store( | ||
ObjectStore::builder("bls_keys") | ||
.key_path(Some(KeyPath::new_single("compressed_key"))), | ||
) | ||
.build() | ||
.await | ||
{ | ||
Ok(db) => Some(db), | ||
Err(err) => { | ||
log::warn!("idb: Couldn't create database {}", err); | ||
None | ||
} | ||
}; | ||
|
||
BlsCache { db, keys: vec![] } | ||
} | ||
|
||
/// Add the given keys into IndexedDB | ||
pub async fn add_keys(&self, keys: Vec<LazyPublicKey>) -> Result<(), Error> { | ||
if let Some(db) = &self.db { | ||
let transaction = db.transaction(&["bls_keys"], TransactionMode::ReadWrite)?; | ||
let bls_keys_store = transaction.object_store("bls_keys")?; | ||
|
||
for key in keys { | ||
let mut result = Vec::new(); | ||
key.uncompress() | ||
.unwrap() | ||
.public_key | ||
.serialize_with_mode(&mut result, Compress::No) | ||
.unwrap(); | ||
let public_key = hex::encode(&result); | ||
let compressed_key = hex::encode(key.compressed().serialize_to_vec()); | ||
|
||
let entry = BlsKeyEntry { | ||
compressed_key, | ||
public_key, | ||
}; | ||
let entry_js_value = serde_wasm_bindgen::to_value(&entry).unwrap(); | ||
bls_keys_store.put(&entry_js_value, None)?.await?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Fetches all bls keys from the IndexedDB and stores them, which makes the decompressed keys | ||
/// available in other places. | ||
pub async fn init(&mut self) -> Result<(), Error> { | ||
if let Some(db) = &self.db { | ||
let transaction = db.transaction(&["bls_keys"], TransactionMode::ReadOnly)?; | ||
let bls_keys_store = transaction.object_store("bls_keys")?; | ||
|
||
let js_keys = bls_keys_store.get_all(None, None)?.await?; | ||
|
||
for js_key in &js_keys { | ||
let value: BlsKeyEntry = serde_wasm_bindgen::from_value(js_key.clone()).unwrap(); | ||
let public_key = PublicKey::new( | ||
G2Projective::deserialize_uncompressed_unchecked( | ||
&*hex::decode(value.public_key.clone()).unwrap(), | ||
) | ||
.unwrap(), | ||
); | ||
self.keys.push(LazyPublicKey::from(public_key)); | ||
} | ||
transaction.await?; | ||
} | ||
Ok(()) | ||
} | ||
} |
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,4 +1,5 @@ | ||
pub mod account; | ||
pub mod block; | ||
mod bls_cache; | ||
pub mod lib; | ||
pub mod peer_info; |