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

Check model exists on sozo auth #1644

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
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
38 changes: 26 additions & 12 deletions bin/sozo/src/ops/auth.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use anyhow::{Context, Result};
use dojo_world::contracts::world::WorldContract;
use dojo_world::contracts::WorldContractReader;
use dojo_world::metadata::Environment;
use dojo_world::utils::TransactionWaiter;
use starknet::accounts::Account;
use starknet::core::types::{BlockId, BlockTag};
use starknet::core::utils::parse_cairo_short_string;

use super::get_contract_address;
use crate::commands::auth::{AuthCommand, AuthKind, ResourceType};
Expand All @@ -16,25 +19,36 @@

let account = account.account(&provider, env_metadata.as_ref()).await?;
let world = WorldContract::new(world_address, &account);
let world_reader = WorldContractReader::new(world_address, &provider)
.with_block(BlockId::Tag(BlockTag::Pending));

Check warning on line 23 in bin/sozo/src/ops/auth.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/auth.rs#L22-L23

Added lines #L22 - L23 were not covered by tests

let mut calls = Vec::new();

for mc in models_contracts {
let contract = get_contract_address(&world, mc.contract).await?;
calls.push(world.grant_writer_getcall(&mc.model, &contract.into()));
let model_name = parse_cairo_short_string(&mc.model)?;

Check warning on line 28 in bin/sozo/src/ops/auth.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/auth.rs#L28

Added line #L28 was not covered by tests

if world_reader.model_reader(&model_name).await.is_ok() {
let contract = get_contract_address(&world, mc.contract).await?;
calls.push(world.grant_writer_getcall(&mc.model, &contract.into()));
} else {
println!("Unknown model '{}' => IGNORED", model_name);
}

Check warning on line 35 in bin/sozo/src/ops/auth.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/auth.rs#L30-L35

Added lines #L30 - L35 were not covered by tests
}

let res = account
.execute(calls)
.send()
.await
.with_context(|| "Failed to send transaction")?;
if !calls.is_empty() {
let res = account
.execute(calls)
.send()
.await
.with_context(|| "Failed to send transaction")?;

Check warning on line 43 in bin/sozo/src/ops/auth.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/auth.rs#L38-L43

Added lines #L38 - L43 were not covered by tests

if transaction.wait {
let receipt = TransactionWaiter::new(res.transaction_hash, &provider).await?;
println!("{}", serde_json::to_string_pretty(&receipt)?);
} else {
println!("Transaction hash: {:#x}", res.transaction_hash);
if transaction.wait {
let receipt =
TransactionWaiter::new(res.transaction_hash, &provider).await?;
println!("{}", serde_json::to_string_pretty(&receipt)?);
} else {
println!("Transaction hash: {:#x}", res.transaction_hash);
}

Check warning on line 51 in bin/sozo/src/ops/auth.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/ops/auth.rs#L45-L51

Added lines #L45 - L51 were not covered by tests
}
}
AuthKind::Owner { owners_resources } => {
Expand Down
8 changes: 7 additions & 1 deletion crates/dojo-world/src/contracts/model.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use abigen::model::ModelContractReader;
use async_trait::async_trait;
use cainome::cairo_serde::Error as CainomeError;
use cainome::cairo_serde::{ContractAddress, Error as CainomeError};
use dojo_types::packing::{parse_ty, unpack, PackingError, ParseError};
use dojo_types::primitive::PrimitiveError;
use dojo_types::schema::Ty;
Expand Down Expand Up @@ -86,6 +86,12 @@
err => err.into(),
})?;

// World Cairo contract won't raise an error in case of unknown/unregistered
// model so raise an error here in case of zero address.
if contract_address == ContractAddress(FieldElement::ZERO) {
return Err(ModelError::ModelNotFound);

Check warning on line 92 in crates/dojo-world/src/contracts/model.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-world/src/contracts/model.rs#L92

Added line #L92 was not covered by tests
}

let model_reader = ModelContractReader::new(contract_address.into(), world.provider());

Ok(Self {
Expand Down
Loading