Skip to content

Commit

Permalink
Add IntoKey for Datastore
Browse files Browse the repository at this point in the history
  • Loading branch information
ryardley committed Oct 14, 2024
1 parent 5fe148d commit 164c983
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 8 deletions.
112 changes: 108 additions & 4 deletions packages/ciphernode/data/src/data_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,61 @@ use anyhow::Result;

use crate::InMemDataStore;

pub trait IntoKey {
fn into_key(self) -> Vec<u8>;
}

impl IntoKey for Vec<String> {
fn into_key(self) -> Vec<u8> {
self.join("/").into_bytes()
}
}

impl<'a> IntoKey for Vec<&'a str> {
fn into_key(self) -> Vec<u8> {
self.join("/").into_bytes()
}
}

impl IntoKey for String {
fn into_key(self) -> Vec<u8> {
self.into_bytes()
}
}

impl<'a> IntoKey for &'a str {
fn into_key(self) -> Vec<u8> {
self.as_bytes().to_vec()
}
}

pub trait WithPrefix: Sized {
fn prefix(self, prefix: &str) -> Self;
fn base(self, key: &str) -> Self;
}

impl WithPrefix for Vec<u8> {
fn prefix(self, prefix: &str) -> Self {
let Ok(encoded) = String::from_utf8(self.clone()) else {
// If this is not encoded as utf8 do nothing
return self;
};
vec![prefix.to_string(), encoded].join("/").into_bytes()
}

fn base(self, key: &str) -> Self {
key.to_string().into_bytes()
}
}

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash)]
#[rtype(result = "()")]
pub struct Insert(pub Vec<u8>, pub Vec<u8>);
impl Insert {
pub fn new<K: IntoKey>(key: K, value: Vec<u8>) -> Self {
Self(key.into_key(), value)
}

pub fn key(&self) -> Vec<u8> {
self.0.clone()
}
Expand All @@ -16,29 +67,61 @@ impl Insert {
}
}

impl WithPrefix for Insert {
fn prefix(self, prefix: &str) -> Self {
Insert(self.0.prefix(prefix), self.1)
}

fn base(self, key: &str) -> Self {
Insert(self.0.base(key), self.1)
}
}

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash)]
#[rtype(result = "Option<Vec<u8>>")]
pub struct Get(pub Vec<u8>);
impl Get {
pub fn new<K: IntoKey>(key: K) -> Self {
Self(key.into_key())
}

pub fn key(&self) -> Vec<u8> {
self.0.clone()
}
}

impl WithPrefix for Get {
fn prefix(self, prefix: &str) -> Self {
Get(self.0.prefix(prefix))
}
fn base(self, key: &str) -> Self {
Get(self.0.base(key))
}
}

#[derive(Clone)]
pub struct DataStore(Recipient<Get>, Recipient<Insert>);
pub struct DataStore {
prefix: Option<String>,
get: Recipient<Get>,
insert: Recipient<Insert>,
}

impl DataStore {
pub async fn read(&self, msg: Get) -> Result<Option<Vec<u8>>> {
Ok(self.0.send(msg).await?)
Ok(self.get.send(msg).await?)
}

pub fn write(&self, msg: Insert) {
self.1.do_send(msg)
self.insert.do_send(msg)
}

// use this for testing
pub fn from_in_mem(addr: Addr<InMemDataStore>) -> Self {
Self(addr.clone().recipient(), addr.clone().recipient())
Self {
get: addr.clone().recipient(),
insert: addr.clone().recipient(),
prefix: None,
}
}

// // use this for production
Expand All @@ -47,3 +130,24 @@ impl DataStore {
// Self(d.recipient(),d.recipient())
// }
}

impl WithPrefix for DataStore {
fn prefix(self, prefix: &str) -> Self {
Self {
get: self.get,
insert: self.insert,
prefix: self.prefix.map_or_else(
|| Some(prefix.to_string()),
|p| Some(vec![prefix.to_string(), p].join("/")),
),
}
}

fn base(self, key: &str) -> Self {
Self {
get: self.get,
insert: self.insert,
prefix: Some(key.to_string()),
}
}
}
4 changes: 2 additions & 2 deletions packages/ciphernode/data/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod in_mem;
mod data_store;
pub use in_mem::*;
mod in_mem;
pub use data_store::*;
pub use in_mem::*;
3 changes: 1 addition & 2 deletions packages/ciphernode/keyshare/src/keyshare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ impl Handler<CiphernodeSelected> for Keyshare {
// reencrypt secretkey locally with env var - this is so we don't have to serialize a secret
// best practice would be as you boot up a node you enter in a configured password from
// which we derive a kdf which gets used to generate this key
self.data
.write(Insert(format!("{}/sk", e3_id).into(), sk));
self.data.write(Insert(format!("{}/sk", e3_id).into(), sk));

// save public key against e3_id/pk
self.data
Expand Down

0 comments on commit 164c983

Please sign in to comment.