Skip to content

Commit

Permalink
chore: avoid mut rng not send issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dav1do committed May 13, 2024
1 parent 380c752 commit d3e2ebf
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ pub mod api;
mod model_definition;
mod query;

use ceramic_event::{Base64String, Cid, event_builder::*, EventBytes, Jws, MultiBase36String, SignedEvent, Signer, StreamId, StreamIdType};
use ceramic_event::{
event_builder::*, Base64String, Cid, EventBytes, Jws, MultiBase36String, SignedEvent, Signer,
StreamId, StreamIdType,
};
use serde::Serialize;
use std::str::FromStr;
use rand::Fill;

use crate::api::{ModelData, PARENT_STREAM_ID, SEP};
pub use ceramic_event;
Expand Down Expand Up @@ -94,7 +96,8 @@ impl<S: Signer + Sync> CeramicHttpClient<S> {
.with_additional(SEP.to_string(), parent.into())
.init()
.with_data(&model)
.build().await?;
.build()
.await?;
let commit = SignedEvent::new(commit.into(), &self.signer).await?;
let controllers: Vec<_> = vec![controller];
let data = Base64String::from(commit.linked_block.as_ref());
Expand Down Expand Up @@ -177,6 +180,16 @@ impl<S: Signer + Sync> CeramicHttpClient<S> {
})
}

fn gen_rand_bytes<const SIZE: usize>() -> [u8; SIZE] {
// can't take &mut rng cause of Send even if we drop it
let mut rng = rand::thread_rng();
let mut arr = [0; SIZE];
for x in &mut arr {
*x = rand::Rng::gen_range(&mut rng, 0..=255);
}
arr
}

/// Create a serde compatible request for a list instance per account creation of a model
pub async fn create_list_instance_request<T: Serialize>(
&self,
Expand All @@ -189,9 +202,7 @@ impl<S: Signer + Sync> CeramicHttpClient<S> {
let model_vec = model_id.to_vec()?;
let model = Base64String::from(model_vec.as_slice());
let model_bytes = EventBytes::from(model_vec);
let mut rng = rand::thread_rng();
let mut unique = [0u8; 12];
unique.try_fill(&mut rng)?;
let unique = Self::gen_rand_bytes::<12>();
let unique: EventBytes = unique.to_vec().into();
let controller = self.signer.id().id.clone();
let commit = Builder::default()
Expand All @@ -201,7 +212,8 @@ impl<S: Signer + Sync> CeramicHttpClient<S> {
.with_additional("unique".to_string(), unique.into())
.init()
.with_data(data)
.build().await?;
.build()
.await?;
let commit = SignedEvent::new(commit.into(), &self.signer).await?;
let controllers: Vec<_> = vec![controller];
let data = Base64String::from(commit.linked_block.as_ref());
Expand Down Expand Up @@ -239,7 +251,8 @@ impl<S: Signer + Sync> CeramicHttpClient<S> {
let model = Base64String::from(model_vec.as_slice());
let commit = Builder::default()
.data(get.stream_id.cid, tip, patch)
.build().await?;
.build()
.await?;
let commit = SignedEvent::new(commit.into(), &self.signer).await?;
let controllers: Vec<_> = vec![controller];
let data = Base64String::from(commit.linked_block.as_ref());
Expand Down

0 comments on commit d3e2ebf

Please sign in to comment.