diff --git a/src/lib.rs b/src/lib.rs index 7aba7d6..98157b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -94,7 +96,8 @@ impl CeramicHttpClient { .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()); @@ -177,6 +180,16 @@ impl CeramicHttpClient { }) } + fn gen_rand_bytes() -> [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( &self, @@ -189,9 +202,7 @@ impl CeramicHttpClient { 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() @@ -201,7 +212,8 @@ impl CeramicHttpClient { .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()); @@ -239,7 +251,8 @@ impl CeramicHttpClient { 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());