Skip to content

Commit

Permalink
[sozo][auth]: revoke logic added (starkware-libs#1712)
Browse files Browse the repository at this point in the history
* [sozo][auth]: revoke logic added

* test added for auth revoke

* fix: remove world reader as argument

WorldContractReader can be initialized from the inside
of the ops. Hence, we avoid passing it as an argument
and it is initialized inside the ops function.

tests added for the auth revoke

* test added for auth revoke

* fix: refacto testing functions

* fix: add missing spaces

---------

Co-authored-by: glihm <[email protected]>
  • Loading branch information
Mohiiit and glihm authored Mar 28, 2024
1 parent f84d207 commit 0b35400
Show file tree
Hide file tree
Showing 9 changed files with 351 additions and 78 deletions.
23 changes: 22 additions & 1 deletion bin/sozo/src/commands/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ pub async fn grant(
}
}

pub async fn revoke(
world: WorldOptions,
account: AccountOptions,
starknet: StarknetOptions,
env_metadata: Option<Environment>,
kind: AuthKind,
transaction: TransactionOptions,
) -> Result<()> {
let world =
utils::world_from_env_metadata(world, account, starknet, &env_metadata).await.unwrap();
match kind {
AuthKind::Writer { models_contracts } => {
auth::revoke_writer(&world, models_contracts, transaction.into()).await
}
AuthKind::Owner { owners_resources } => {
auth::revoke_owner(&world, owners_resources, transaction.into()).await
}
}
}
#[derive(Debug, Subcommand)]
pub enum AuthCommand {
#[command(about = "Grant an auth role.")]
Expand Down Expand Up @@ -108,7 +127,9 @@ impl AuthArgs {
AuthCommand::Grant { kind, world, starknet, account, transaction } => config
.tokio_handle()
.block_on(grant(world, account, starknet, env_metadata, kind, transaction)),
_ => todo!(),
AuthCommand::Revoke { kind, world, starknet, account, transaction } => config
.tokio_handle()
.block_on(revoke(world, account, starknet, env_metadata, kind, transaction)),
}
}
}
Expand Down
97 changes: 91 additions & 6 deletions crates/sozo/ops/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use starknet::core::types::{BlockId, BlockTag};
use starknet::core::utils::parse_cairo_short_string;
use starknet_crypto::FieldElement;

use super::get_contract_address;
use crate::utils::handle_transaction_result;
use crate::utils;

#[derive(Debug, Clone, PartialEq)]
pub enum ResourceType {
Expand Down Expand Up @@ -104,7 +103,7 @@ where
let model_name = parse_cairo_short_string(&mc.model)?;
match world_reader.model_reader(&model_name).await {
Ok(_) => {
let contract = get_contract_address(world, mc.contract).await?;
let contract = utils::get_contract_address(world, mc.contract).await?;
calls.push(world.grant_writer_getcall(&mc.model, &contract.into()));
}

Expand All @@ -126,7 +125,7 @@ where
.await
.with_context(|| "Failed to send transaction")?;

handle_transaction_result(
utils::handle_transaction_result(
&world.account.provider(),
res,
transaction.wait,
Expand All @@ -152,7 +151,7 @@ where
let resource = match &or.resource {
ResourceType::Model(name) => *name,
ResourceType::Contract(name_or_address) => {
get_contract_address(world, name_or_address.clone()).await?
utils::get_contract_address(world, name_or_address.clone()).await?
}
};

Expand All @@ -162,7 +161,93 @@ where
let res =
world.account.execute(calls).send().await.with_context(|| "Failed to send transaction")?;

handle_transaction_result(
utils::handle_transaction_result(
&world.account.provider(),
res,
transaction.wait,
transaction.receipt,
)
.await?;

Ok(())
}

pub async fn revoke_writer<A>(
world: &WorldContract<A>,
models_contracts: Vec<ModelContract>,
transaction: TxConfig,
) -> Result<()>
where
A: ConnectedAccount + Sync + Send + 'static,
{
let mut calls = Vec::new();

let world_reader = WorldContractReader::new(world.address, world.account.provider())
.with_block(BlockId::Tag(BlockTag::Pending));

for mc in models_contracts {
let model_name = parse_cairo_short_string(&mc.model)?;
match world_reader.model_reader(&model_name).await {
Ok(_) => {
let contract = utils::get_contract_address(world, mc.contract).await?;
calls.push(world.revoke_writer_getcall(&mc.model, &contract.into()));
}

Err(ModelError::ModelNotFound) => {
println!("Unknown model '{}' => IGNORED", model_name);
}

Err(err) => {
return Err(err.into());
}
}
}

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

utils::handle_transaction_result(
&world.account.provider(),
res,
transaction.wait,
transaction.receipt,
)
.await?;
}

Ok(())
}

pub async fn revoke_owner<A>(
world: &WorldContract<A>,
owners_resources: Vec<OwnerResource>,
transaction: TxConfig,
) -> Result<()>
where
A: ConnectedAccount + Sync + Send + 'static,
{
let mut calls = Vec::new();

for or in owners_resources {
let resource = match &or.resource {
ResourceType::Model(name) => *name,
ResourceType::Contract(name_or_address) => {
utils::get_contract_address(world, name_or_address.clone()).await?
}
};

calls.push(world.revoke_owner_getcall(&or.owner.into(), &resource));
}

let res =
world.account.execute(calls).send().await.with_context(|| "Failed to send transaction")?;

utils::handle_transaction_result(
&world.account.provider(),
res,
transaction.wait,
Expand Down
14 changes: 9 additions & 5 deletions crates/sozo/ops/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use starknet::accounts::{Call, ConnectedAccount};
use starknet::core::types::FieldElement;
use starknet::core::utils::get_selector_from_name;

use super::get_contract_address;
use crate::utils::handle_transaction_result;
use crate::utils;

pub async fn execute<A>(
contract: String,
Expand All @@ -18,7 +17,7 @@ pub async fn execute<A>(
where
A: ConnectedAccount + Sync + Send + 'static,
{
let contract_address = get_contract_address(world, contract).await?;
let contract_address = utils::get_contract_address(world, contract).await?;
let res = world
.account
.execute(vec![Call {
Expand All @@ -30,6 +29,11 @@ where
.await
.with_context(|| "Failed to send transaction")?;

handle_transaction_result(&world.account.provider(), res, transaction.wait, transaction.receipt)
.await
utils::handle_transaction_result(
&world.account.provider(),
res,
transaction.wait,
transaction.receipt,
)
.await
}
23 changes: 0 additions & 23 deletions crates/sozo/ops/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
use anyhow::Result;
use dojo_world::contracts::world::WorldContract;
use dojo_world::migration::strategy::generate_salt;
use starknet::accounts::ConnectedAccount;
use starknet::core::types::FieldElement;

pub mod auth;
pub mod events;
pub mod execute;
Expand All @@ -14,20 +8,3 @@ pub mod utils;

#[cfg(test)]
pub mod tests;

pub async fn get_contract_address<A: ConnectedAccount + Sync>(
world: &WorldContract<A>,
name_or_address: String,
) -> Result<FieldElement> {
if name_or_address.starts_with("0x") {
FieldElement::from_hex_be(&name_or_address).map_err(anyhow::Error::from)
} else {
let contract_class_hash = world.base().call().await?;
Ok(starknet::core::utils::get_contract_address(
generate_salt(&name_or_address),
contract_class_hash.into(),
&[],
world.address,
))
}
}
Loading

0 comments on commit 0b35400

Please sign in to comment.