sled: Add method to create CryptoStore from StateStore

Allow to use the same database for both stores
This commit is contained in:
Kévin Commaille
2022-03-08 11:53:17 +01:00
parent 5f8d3dec82
commit 692f95da79

View File

@@ -59,6 +59,9 @@ use sled::{
use tokio::task::spawn_blocking;
use tracing::{info, warn};
#[cfg(feature = "encryption")]
pub use crate::CryptoStore;
#[derive(Debug, Serialize, Deserialize)]
pub enum DatabaseType {
Unencrypted,
@@ -377,6 +380,14 @@ impl SledStore {
SledStore::open_helper(db, Some(path), None)
}
#[cfg(feature = "encryption")]
/// Open a `CryptoStore` that uses the same database as this store.
///
/// The given passphrase will be used to encrypt private data.
pub fn get_crypto_store(&self, passphrase: Option<&str>) -> Result<CryptoStore, anyhow::Error> {
CryptoStore::open_with_database(self.inner.clone(), passphrase)
}
fn serialize_event(&self, event: &impl Serialize) -> Result<Vec<u8>, SledStoreError> {
if let Some(key) = &*self.store_key {
let encrypted = key.encrypt(event)?;