Skip to content

Commit

Permalink
chore: move CID operations to dwn-rs-core
Browse files Browse the repository at this point in the history
  • Loading branch information
enmand committed Nov 19, 2024
1 parent 5fd5d5c commit 327e926
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 12 deletions.
9 changes: 8 additions & 1 deletion crates/dwn-rs-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ serde_json = "1.0.113"
tracing = "0.1.40"
tracing-test = { version = "0.2.5", features = ["no-env-filter"] }
bytes = "1.8.0"
rand = "0.8.5"
secp256k1 = { version = "0.30", features = ["rand"] }
partially = { version = "0.2.1", features = ["derive"] }
derive_builder = "0.20.2"
multicodec = { git = "https://github.com/cryptidtech/rust-multicodec.git" } # Use moden fork, see gnunicorn/rust-multicodec#1
multihash = { version = "0.19.1", features = ["serde"] }
multihash-codetable = { version = "0.1.4", features = ["serde", "sha2"] }
dwn-rs-message-derive = { path = "../dwn-rs-message-derive" }

[dev-dependencies]
serde_json = "1.0.113"
dwn-rs-message-derive = { path = "../dwn-rs-message-derive" }
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use bytes::Bytes;
use futures_util::TryStreamExt;
use std::collections::TryReserveError;

use cid::Cid;
use futures_util::TryStream;
use multihash_codetable::Code;
use multihash_codetable::MultihashDigest;
use serde_ipld_dagcbor::EncodeError;
Expand All @@ -16,6 +19,33 @@ where
Ok(cid)
}

pub async fn generate_cid_from_serialized<T: serde::Serialize>(
data: T,
) -> Result<Cid, EncodeError<TryReserveError>> {
let serialized = serde_ipld_dagcbor::to_vec(&data)?;
generate_cid(serialized)
}

pub async fn generate_cid_from_stream<S: TryStream<Ok = Bytes> + Unpin>(
stream: S,
) -> Result<Cid, EncodeError<TryReserveError>>
where
S::Error: Into<EncodeError<TryReserveError>>,
{
let mut buf = Vec::new();
let _ = stream
.try_for_each(|chunk| {
buf.extend_from_slice(&chunk);
async { Ok(()) }
})
.await;

let mh = Code::Sha2_256.digest(&buf);
let cid = Cid::new_v1(multicodec::Codec::DagCbor.code(), mh);

Ok(cid)
}

pub async fn generate_cid_from_asyncreader<R>(
reader: R,
) -> Result<Cid, EncodeError<TryReserveError>>
Expand Down
66 changes: 66 additions & 0 deletions crates/dwn-rs-core/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
pub mod cid;

use partially::Partial;
use rand::{distributions::Alphanumeric, Rng};
use secp256k1::{Keypair, Secp256k1};
use ssi_dids_core::DIDBuf;
use std::str::FromStr;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum PersonaError {
#[error("DID error: {0}")]
DIDError(#[from] ssi_dids_core::InvalidDID<String>),
}

#[derive(Partial, Debug)]
#[partially(derive(Default))]
pub struct Persona {
pub did: DIDBuf,
pub key_id: String,
keypair: secp256k1::Keypair,
}

impl Persona {
pub fn generate(
PartialPersona {
did,
key_id,
keypair,
}: PartialPersona,
) -> Result<Self, PersonaError> {
let did = did.unwrap_or_else(|| {
let suffix = generate_random_string(32);
DIDBuf::from_str(&format!("did:example:{}", suffix)).unwrap()
});

let keypair = keypair.unwrap_or_else(|| {
let secp = Secp256k1::new();

Keypair::new(&secp, &mut rand::thread_rng())
});

let key_id = key_id.unwrap_or_else(|| {
let suffix = generate_random_string(16);
format!("{}#{}", did, suffix)
});

Ok(Self {
did,
key_id,
keypair,
})
}

pub fn public_key(&self) -> secp256k1::PublicKey {
self.keypair.public_key()
}
}

pub fn generate_random_string(len: usize) -> String {
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(len)
.map(char::from)
.collect()
}
4 changes: 0 additions & 4 deletions crates/dwn-rs-stores/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ no-std = []
futures-util = "0.3.30"
chrono = { version = "0.4.37", features = ["serde", "wasmbind"] }
cid = { version = "0.11.1", features = ["serde"] }
ipld-core = { version = "0.4.1", features = ["serde"] }
multicodec = { git = "https://github.com/cryptidtech/rust-multicodec.git" } # Use moden fork, see gnunicorn/rust-multicodec#1
multihash = { version = "0.19.1", features = ["serde"] }
multihash-codetable = { version = "0.1.2", features = ["serde", "sha2"] }
thiserror = "1.0.63"
time = "0.3.36"
tokio = { version = "1.39.2", features = ["io-util", "rt", "macros"] }
Expand Down
3 changes: 0 additions & 3 deletions crates/dwn-rs-stores/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
pub mod cid;
pub use cid::*;

#[cfg(feature = "surrealdb")]
pub mod surrealdb;
#[cfg(feature = "surrealdb")]
Expand Down
3 changes: 2 additions & 1 deletion crates/dwn-rs-stores/src/surrealdb/message_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use cid::Cid;
use serde::{de::DeserializeOwned, Serialize};

use super::core::SurrealDB;
use crate::{generate_cid, SurrealQuery};
use crate::SurrealQuery;
use dwn_rs_core::utils::cid::generate_cid;
use dwn_rs_core::{
descriptors::MessageDescriptor,
errors::{MessageStoreError, StoreError},
Expand Down
2 changes: 1 addition & 1 deletion crates/dwn-rs-stores/src/surrealdb/models.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::str::FromStr;

use cid::Cid;
use dwn_rs_core::{
filters::{MessageSort, MessageWatermark, NoSort},
value::{MapValue, Value},
};
use ipld_core::cid::Cid;
use serde::{Deserialize, Serialize};
use surrealdb::{
sql::{Datetime, Value as SurrealValue},
Expand Down
4 changes: 2 additions & 2 deletions crates/dwn-rs-stores/src/surrealdb/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
ops::{Bound, RangeBounds},
};

use cid::Cid;
use serde::de::DeserializeOwned;
use surrealdb::sql::{value as surreal_value, Cond, Function, Idiom, Subquery};
use surrealdb::{
Expand Down Expand Up @@ -64,7 +65,7 @@ where
}

pub trait CursorValue<T> {
fn cid(&self) -> ipld_core::cid::Cid;
fn cid(&self) -> Cid;
fn cursor_value(&self, sort: T) -> dwn_rs_core::value::Value;
}

Expand Down Expand Up @@ -243,7 +244,6 @@ where
None => stmt.cond,
};


let mut q = self
.db
.query(stmt.clone())
Expand Down

0 comments on commit 327e926

Please sign in to comment.