From 779aba4fa13af7e61024618ce6de781faa8c16d8 Mon Sep 17 00:00:00 2001 From: Nasr Date: Wed, 28 Aug 2024 14:08:47 -0400 Subject: [PATCH] feat: update torii server --- crates/torii/libp2p/src/server/mod.rs | 38 +++++++-------------------- crates/torii/libp2p/src/typed_data.rs | 19 ++------------ 2 files changed, 11 insertions(+), 46 deletions(-) diff --git a/crates/torii/libp2p/src/server/mod.rs b/crates/torii/libp2p/src/server/mod.rs index 3b38d8ce53..bc87a1fa4a 100644 --- a/crates/torii/libp2p/src/server/mod.rs +++ b/crates/torii/libp2p/src/server/mod.rs @@ -9,9 +9,8 @@ use std::{fs, io}; use chrono::Utc; use dojo_types::schema::Ty; -use dojo_world::contracts::naming::compute_selector_from_names; +use dojo_world::contracts::naming::compute_selector_from_tag; use futures::StreamExt; -use indexmap::IndexMap; use libp2p::core::multiaddr::Protocol; use libp2p::core::muxing::StreamMuxerBox; use libp2p::core::upgrade::Version; @@ -37,7 +36,7 @@ use crate::errors::Error; mod events; use crate::server::events::ServerEvent; -use crate::typed_data::{parse_value_to_ty, PrimitiveType}; +use crate::typed_data::{parse_value_to_ty, PrimitiveType, TypedData}; use crate::types::Message; pub(crate) const LOG_TARGET: &str = "torii::relay::server"; @@ -211,7 +210,7 @@ impl Relay

{ } }; - let ty = match validate_message(&self.db, &data.message.message).await { + let ty = match validate_message(&self.db, &data.message).await { Ok(parsed_message) => parsed_message, Err(e) => { info!( @@ -464,37 +463,18 @@ fn ty_keys(ty: &Ty) -> Result, Error> { // Validates the message model // and returns the identity and signature -async fn validate_message( - db: &Sql, - message: &IndexMap, -) -> Result { - let (selector, model) = if let Some(model_name) = message.get("model") { - if let PrimitiveType::String(model_name) = model_name { - let (namespace, name) = model_name.split_once('-').ok_or_else(|| { - Error::InvalidMessageError( - "Model name is not in the format namespace-model".to_string(), - ) - })?; - - (compute_selector_from_names(namespace, name), model_name) - } else { - return Err(Error::InvalidMessageError("Model name is not a string".to_string())); - } - } else { - return Err(Error::InvalidMessageError("Model name is missing".to_string())); - }; +async fn validate_message(db: &Sql, message: &TypedData) -> Result { + let selector = compute_selector_from_tag(&message.primary_type); let mut ty = db .model(selector) .await - .map_err(|e| Error::InvalidMessageError(format!("Model {} not found: {}", model, e)))? + .map_err(|e| { + Error::InvalidMessageError(format!("Model {} not found: {}", message.primary_type, e)) + })? .schema; - if let Some(object) = message.get(model) { - parse_value_to_ty(object, &mut ty)?; - } else { - return Err(Error::InvalidMessageError("Model is missing".to_string())); - }; + parse_value_to_ty(&PrimitiveType::Object(message.message.clone()), &mut ty)?; Ok(ty) } diff --git a/crates/torii/libp2p/src/typed_data.rs b/crates/torii/libp2p/src/typed_data.rs index 0a301ee2e1..b15ee88188 100644 --- a/crates/torii/libp2p/src/typed_data.rs +++ b/crates/torii/libp2p/src/typed_data.rs @@ -152,11 +152,7 @@ pub fn encode_type(name: &str, types: &IndexMap>) -> Result>() .join(","); @@ -870,26 +866,15 @@ impl TypedData { ); let mut values = IndexMap::new(); - let mut model_values = IndexMap::new(); let mut fields = Vec::new(); for member in model.children.iter() { let field = map_ty_type(&mut types, &member.name, member.ty.clone()); fields.push(field); - model_values.insert(member.name.clone(), map_ty_to_primitive(&member.ty)?); + values.insert(member.name.clone(), map_ty_to_primitive(&member.ty)?); } - values.insert("model".to_string(), PrimitiveType::String(model.name.clone())); - values.insert(model.name.clone(), PrimitiveType::Object(model_values)); - - types.insert( - "model".to_string(), - vec![Field::SimpleType(SimpleField { - name: "model".to_string(), - r#type: "shortstring".to_string(), - })], - ); types.insert(model.name.clone(), fields); Ok(Self::new(types, model.name.as_str(), domain, values))