fix(sled): Don't derive a StoreCipher twice if we're reusing the db

This commit is contained in:
Damir Jelić
2022-04-26 17:47:39 +02:00
parent 39ef1d550d
commit e1baa25713
3 changed files with 18 additions and 18 deletions

View File

@@ -221,13 +221,23 @@ impl SledStore {
.open()
.map_err(|e| CryptoStoreError::Backend(anyhow!(e)))?;
SledStore::open_helper(db, Some(path), passphrase)
let store_cipher = if let Some(passphrase) = passphrase {
Some(Self::get_or_create_store_cipher(passphrase, &db)?)
} else {
None
}
.into();
SledStore::open_helper(db, Some(path), store_cipher)
}
/// Create a sled based cryptostore using the given sled database.
/// The given passphrase will be used to encrypt private data.
pub fn open_with_database(db: Db, passphrase: Option<&str>) -> Result<Self, OpenStoreError> {
SledStore::open_helper(db, None, passphrase)
pub fn open_with_database(
db: Db,
store_cipher: Arc<Option<StoreCipher>>,
) -> Result<Self, OpenStoreError> {
SledStore::open_helper(db, None, store_cipher)
}
fn get_account_info(&self) -> Option<AccountInfo> {
@@ -347,7 +357,7 @@ impl SledStore {
fn open_helper(
db: Db,
path: Option<PathBuf>,
passphrase: Option<&str>,
store_cipher: Arc<Option<StoreCipher>>,
) -> Result<Self, OpenStoreError> {
let account = db.open_tree("account")?;
let private_identity = db.open_tree("private_identity")?;
@@ -369,13 +379,6 @@ impl SledStore {
let session_cache = SessionStore::new();
let store_cipher = if let Some(passphrase) = passphrase {
Some(Self::get_or_create_store_cipher(passphrase, &db)?)
} else {
None
}
.into();
let database = Self {
account_info: RwLock::new(None).into(),
path,

View File

@@ -295,11 +295,8 @@ impl SledStore {
///
/// The given passphrase will be used to encrypt private data.
#[cfg(feature = "crypto-store")]
pub fn open_crypto_store(
&self,
passphrase: Option<&str>,
) -> Result<CryptoStore, OpenStoreError> {
CryptoStore::open_with_database(self.inner.clone(), passphrase)
pub fn open_crypto_store(&self) -> Result<CryptoStore, OpenStoreError> {
CryptoStore::open_with_database(self.inner.clone(), self.store_cipher.clone())
}
fn serialize_event(&self, event: &impl Serialize) -> Result<Vec<u8>, SledStoreError> {

View File

@@ -80,11 +80,11 @@ fn open_stores_with_path(
) -> Result<(Box<StateStore>, Box<CryptoStore>), OpenStoreError> {
if let Some(passphrase) = passphrase {
let state_store = StateStore::open_with_passphrase(path, passphrase)?;
let crypto_store = state_store.open_crypto_store(Some(passphrase))?;
let crypto_store = state_store.open_crypto_store()?;
Ok((Box::new(state_store), Box::new(crypto_store)))
} else {
let state_store = StateStore::open_with_path(path)?;
let crypto_store = state_store.open_crypto_store(None)?;
let crypto_store = state_store.open_crypto_store()?;
Ok((Box::new(state_store), Box::new(crypto_store)))
}
}