Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(torii): offchain messages signature validation on first set #2390

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 40 additions & 38 deletions crates/torii/libp2p/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,44 +267,29 @@
}
};

if entity_identity.is_none() {
// we can set the entity without checking identity
if let Err(e) = self
.db
.set_entity(
ty,
&message_id.to_string(),
Utc::now().timestamp() as u64,
)
.await
{
info!(
target: LOG_TARGET,
error = %e,
"Setting message."
);
continue;
} else {
info!(
target: LOG_TARGET,
message_id = %message_id,
peer_id = %peer_id,
"Message set."
);
continue;
}
}

let entity_identity = match Felt::from_str(&entity_identity.unwrap()) {
Ok(identity) => identity,
Err(e) => {
warn!(
target: LOG_TARGET,
error = %e,
"Parsing identity."
);
continue;
}
let entity_identity = match entity_identity {
Some(identity) => match Felt::from_str(&identity) {
Ok(identity) => identity,
Err(e) => {
warn!(

Check warning on line 274 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L271-L274

Added lines #L271 - L274 were not covered by tests
target: LOG_TARGET,
error = %e,
"Parsing identity."

Check warning on line 277 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L277

Added line #L277 was not covered by tests
);
continue;

Check warning on line 279 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L279

Added line #L279 was not covered by tests
}
},
None => match get_identity_from_ty(&ty) {
Ok(identity) => identity,
Err(e) => {
warn!(

Check warning on line 285 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L284-L285

Added lines #L284 - L285 were not covered by tests
target: LOG_TARGET,
error = %e,
"Getting identity from message."

Check warning on line 288 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L288

Added line #L288 was not covered by tests
);
continue;

Check warning on line 290 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L290

Added line #L290 was not covered by tests
}
},
glihm marked this conversation as resolved.
Show resolved Hide resolved
};

// TODO: have a nonce in model to check
Expand All @@ -324,6 +309,8 @@
};

let mut calldata = vec![message_hash];
calldata.push(Felt::from(data.signature.len()));

calldata.extend(data.signature);
if !match self
.provider
Expand Down Expand Up @@ -505,6 +492,21 @@
Ok(cert)
}

fn get_identity_from_ty(ty: &Ty) -> Result<Felt, Error> {
let identity = ty
.as_struct()
.ok_or_else(|| Error::InvalidMessageError("Message is not a struct".to_string()))?
.get("identity")
.ok_or_else(|| Error::InvalidMessageError("No field identity".to_string()))?
.as_primitive()
.ok_or_else(|| Error::InvalidMessageError("Identity is not a primitive".to_string()))?
.as_contract_address()
.ok_or_else(|| {
Error::InvalidMessageError("Identity is not a contract address".to_string())

Check warning on line 505 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L505

Added line #L505 was not covered by tests
})?;
Ok(identity)
}

#[cfg(test)]
mod tests {
use tempfile::tempdir;
Expand Down
Loading