Skip to content

Commit

Permalink
refactor(torii-libp2p): always use is_valid_signature (dojoengine#2776)
Browse files Browse the repository at this point in the history
* refactor(torii-libp2p): always use is_valid_signature

* fmt
  • Loading branch information
Larkooo committed Dec 9, 2024
1 parent 69b2d2d commit 329c953
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 56 deletions.
61 changes: 17 additions & 44 deletions crates/torii/libp2p/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use crate::errors::Error;
mod events;

use crate::server::events::ServerEvent;
use crate::typed_data::{encode_type, parse_value_to_ty, PrimitiveType, TypedData};
use crate::types::{Message, Signature};
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 @@ -414,51 +414,24 @@ async fn validate_signature<P: Provider + Sync>(
provider: &P,
entity_identity: Felt,
message: &TypedData,
signature: &Signature,
signature: &[Felt],
) -> Result<bool, Error> {
let message_hash = message.encode(entity_identity)?;

match signature {
Signature::Account(signature) => {
let mut calldata = vec![message_hash, Felt::from(signature.len())];
calldata.extend(signature);
provider
.call(
FunctionCall {
contract_address: entity_identity,
entry_point_selector: get_selector_from_name("is_valid_signature").unwrap(),
calldata,
},
BlockId::Tag(BlockTag::Pending),
)
.await
.map_err(Error::ProviderError)
.map(|res| res[0] != Felt::ZERO)
}
Signature::Session(signature) => {
let mut calldata = vec![
Felt::ONE,
get_selector_from_name(&encode_type(&message.primary_type, &message.types)?)
.map_err(|e| Error::InvalidMessageError(e.to_string()))?,
message_hash,
signature.len().into(),
];
calldata.extend(signature);
provider
.call(
FunctionCall {
contract_address: entity_identity,
entry_point_selector: get_selector_from_name("is_session_sigature_valid")
.unwrap(),
calldata,
},
BlockId::Tag(BlockTag::Pending),
)
.await
.map_err(Error::ProviderError)
.map(|res| res[0] != Felt::ZERO)
}
}
let mut calldata = vec![message_hash, Felt::from(signature.len())];
calldata.extend(signature);
provider
.call(
FunctionCall {
contract_address: entity_identity,
entry_point_selector: get_selector_from_name("is_valid_signature").unwrap(),
calldata,
},
BlockId::Tag(BlockTag::Pending),
)
.await
.map_err(Error::ProviderError)
.map(|res| res[0] != Felt::ZERO)
}

fn ty_keys(ty: &Ty) -> Result<Vec<Felt>, Error> {
Expand Down
7 changes: 2 additions & 5 deletions crates/torii/libp2p/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ mod test {

use crate::server::Relay;
use crate::typed_data::{Domain, Field, SimpleField, TypedData};
use crate::types::{Message, Signature};
use crate::types::Message;

let _ = tracing_subscriber::fmt()
.with_env_filter("torii::relay::client=debug,torii::relay::server=debug")
Expand Down Expand Up @@ -693,10 +693,7 @@ mod test {

client
.command_sender
.publish(Message {
message: typed_data,
signature: Signature::Account(vec![signature.r, signature.s]),
})
.publish(Message { message: typed_data, signature: vec![signature.r, signature.s] })
.await?;

sleep(std::time::Duration::from_secs(2)).await;
Expand Down
8 changes: 1 addition & 7 deletions crates/torii/libp2p/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@ use starknet::core::types::Felt;

use crate::typed_data::TypedData;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Signature {
Account(Vec<Felt>),
Session(Vec<Felt>),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
pub message: TypedData,
pub signature: Signature,
pub signature: Vec<Felt>,
}

0 comments on commit 329c953

Please sign in to comment.