-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create dummy identity with in memory keystore
- Loading branch information
1 parent
4639280
commit 0abf678
Showing
7 changed files
with
151 additions
and
7 deletions.
There are no files selected for viewing
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,4 @@ | ||
use openmls_traits::types::Ciphersuite; | ||
|
||
// TODO confirm ciphersuite choice | ||
pub const CIPHERSUITE: Ciphersuite = Ciphersuite::MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519; |
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,59 @@ | ||
use openmls_traits::key_store::{MlsEntity, OpenMlsKeyStore}; | ||
use std::{collections::HashMap, sync::RwLock}; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct InMemoryKeyStore { | ||
values: RwLock<HashMap<Vec<u8>, Vec<u8>>>, | ||
} | ||
|
||
impl OpenMlsKeyStore for InMemoryKeyStore { | ||
/// The error type returned by the [`OpenMlsKeyStore`]. | ||
type Error = InMemoryKeyStoreError; | ||
|
||
/// Store a value `v` that implements the [`ToKeyStoreValue`] trait for | ||
/// serialization for ID `k`. | ||
/// | ||
/// Returns an error if storing fails. | ||
fn store<V: MlsEntity>(&self, k: &[u8], v: &V) -> Result<(), Self::Error> { | ||
let value = serde_json::to_vec(v).map_err(|_| InMemoryKeyStoreError::SerializationError)?; | ||
// We unwrap here, because this is the only function claiming a write | ||
// lock on `credential_bundles`. It only holds the lock very briefly and | ||
// should not panic during that period. | ||
let mut values = self.values.write().unwrap(); | ||
values.insert(k.to_vec(), value); | ||
Ok(()) | ||
} | ||
|
||
/// Read and return a value stored for ID `k` that implements the | ||
/// [`FromKeyStoreValue`] trait for deserialization. | ||
/// | ||
/// Returns [`None`] if no value is stored for `k` or reading fails. | ||
fn read<V: MlsEntity>(&self, k: &[u8]) -> Option<V> { | ||
// We unwrap here, because the two functions claiming a write lock on | ||
// `init_key_package_bundles` (this one and `generate_key_package_bundle`) only | ||
// hold the lock very briefly and should not panic during that period. | ||
let values = self.values.read().unwrap(); | ||
if let Some(value) = values.get(k) { | ||
serde_json::from_slice(value).ok() | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Delete a value stored for ID `k`. | ||
/// | ||
/// Returns an error if storing fails. | ||
fn delete<V: MlsEntity>(&self, k: &[u8]) -> Result<(), Self::Error> { | ||
// We just delete both ... | ||
let mut values = self.values.write().unwrap(); | ||
values.remove(k); | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Errors thrown by the key store. | ||
#[derive(thiserror::Error, Debug, Copy, Clone, PartialEq, Eq)] | ||
pub enum InMemoryKeyStoreError { | ||
#[error("Error serializing value.")] | ||
SerializationError, | ||
} |
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,5 +1,6 @@ | ||
mod encrypted_store; | ||
mod errors; | ||
pub mod in_memory_key_store; | ||
|
||
pub use encrypted_store::{DbConnection, EncryptedMessageStore, EncryptionKey, StorageOption}; | ||
pub use errors::StorageError; |
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,28 @@ | ||
use openmls_rust_crypto::RustCrypto; | ||
use openmls_traits::OpenMlsProvider; | ||
|
||
use crate::storage::in_memory_key_store::InMemoryKeyStore; | ||
|
||
#[derive(Default, Debug)] | ||
pub struct XmtpOpenMlsProvider { | ||
crypto: RustCrypto, | ||
key_store: InMemoryKeyStore, | ||
} | ||
|
||
impl OpenMlsProvider for XmtpOpenMlsProvider { | ||
type CryptoProvider = RustCrypto; | ||
type RandProvider = RustCrypto; | ||
type KeyStoreProvider = InMemoryKeyStore; | ||
|
||
fn crypto(&self) -> &Self::CryptoProvider { | ||
&self.crypto | ||
} | ||
|
||
fn rand(&self) -> &Self::RandProvider { | ||
&self.crypto | ||
} | ||
|
||
fn key_store(&self) -> &Self::KeyStoreProvider { | ||
&self.key_store | ||
} | ||
} |