Skip to content

Commit

Permalink
fix(libp2p): fix typed data from model (#2357)
Browse files Browse the repository at this point in the history
* fix(typeddata): fix from model

* chore: add types

* chore: shortstirng

* wip

* feat: update torii server

* fix: tests
  • Loading branch information
Larkooo authored Aug 29, 2024
1 parent 1298382 commit fc1f894
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 62 deletions.
38 changes: 9 additions & 29 deletions crates/torii/libp2p/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,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;
Expand All @@ -36,7 +35,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";
Expand Down Expand Up @@ -202,7 +201,7 @@ impl<P: Provider + Sync> Relay<P> {
}
};

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!(
Expand Down Expand Up @@ -455,37 +454,18 @@ fn ty_keys(ty: &Ty) -> Result<Vec<Felt>, Error> {

// Validates the message model
// and returns the identity and signature
async fn validate_message(
db: &Sql,
message: &IndexMap<String, PrimitiveType>,
) -> Result<Ty, Error> {
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<Ty, Error> {
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)
}
Expand Down
40 changes: 7 additions & 33 deletions crates/torii/libp2p/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,20 +604,7 @@ mod test {
let mut typed_data = TypedData::new(
IndexMap::from_iter(vec![
(
"OffchainMessage".to_string(),
vec![
Field::SimpleType(SimpleField {
name: "model".to_string(),
r#type: "shortstring".to_string(),
}),
Field::SimpleType(SimpleField {
name: "types_test-Message".to_string(),
r#type: "Model".to_string(),
}),
],
),
(
"Model".to_string(),
"types_test-Message".to_string(),
vec![
Field::SimpleType(SimpleField {
name: "identity".to_string(),
Expand Down Expand Up @@ -651,31 +638,18 @@ mod test {
],
),
]),
"OffchainMessage",
"types_test-Message",
Domain::new("types_test-Message", "1", "0x0", Some("1")),
IndexMap::new(),
);

typed_data.message.insert(
"model".to_string(),
crate::typed_data::PrimitiveType::String("types_test-Message".to_string()),
"identity".to_string(),
crate::typed_data::PrimitiveType::String(account.address.to_string()),
);

typed_data.message.insert(
"types_test-Message".to_string(),
crate::typed_data::PrimitiveType::Object(
vec![
(
"identity".to_string(),
crate::typed_data::PrimitiveType::String(account.address.to_string()),
),
(
"message".to_string(),
crate::typed_data::PrimitiveType::String("mimi".to_string()),
),
]
.into_iter()
.collect(),
),
"message".to_string(),
crate::typed_data::PrimitiveType::String("mimi".to_string()),
);

let message_hash = typed_data.encode(account.address).unwrap();
Expand Down

0 comments on commit fc1f894

Please sign in to comment.