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

update permission api #2224

Merged
merged 1 commit into from
Jul 30, 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
42 changes: 21 additions & 21 deletions crates/dojo-core/src/tests/world.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ fn test_register_namespace() {

world.register_namespace(namespace);

assert(world.is_owner(caller, hash), 'namespace not registered');
assert(world.is_owner(hash, caller), 'namespace not registered');

assert_eq!(
starknet::testing::pop_log(world.contract_address),
Expand All @@ -507,7 +507,7 @@ fn test_register_namespace_already_registered_same_caller() {

world.register_namespace("namespace");

assert(world.is_owner(caller, hash), 'namespace not registered');
assert(world.is_owner(hash, caller), 'namespace not registered');

let event = starknet::testing::pop_log_raw(world.contract_address);
assert(event.is_none(), 'unexpected event');
Expand Down Expand Up @@ -650,7 +650,7 @@ fn test_metadata_update_owner_only() {
let bob = starknet::contract_address_const::<0xb0b>();
starknet::testing::set_contract_address(bob);

world.grant_owner(bob, bytearray_hash(@"dojo"));
world.grant_owner(bytearray_hash(@"dojo"), bob);

starknet::testing::set_account_contract_address(bob);

Expand All @@ -667,20 +667,20 @@ fn test_owner() {
let alice = starknet::contract_address_const::<0x1337>();
let bob = starknet::contract_address_const::<0x1338>();

assert(!world.is_owner(alice, 0), 'should not be owner');
assert(!world.is_owner(bob, foo_selector), 'should not be owner');
assert(!world.is_owner(0, alice), 'should not be owner');
assert(!world.is_owner(foo_selector, bob), 'should not be owner');

world.grant_owner(alice, 0);
assert(world.is_owner(alice, 0), 'should be owner');
world.grant_owner(0, alice);
assert(world.is_owner(0, alice), 'should be owner');

world.grant_owner(bob, foo_selector);
assert(world.is_owner(bob, foo_selector), 'should be owner');
world.grant_owner(foo_selector, bob);
assert(world.is_owner(foo_selector, bob), 'should be owner');

world.revoke_owner(alice, 0);
assert(!world.is_owner(alice, 0), 'should not be owner');
world.revoke_owner(0, alice);
assert(!world.is_owner(0, alice), 'should not be owner');

world.revoke_owner(bob, foo_selector);
assert(!world.is_owner(bob, foo_selector), 'should not be owner');
world.revoke_owner(foo_selector, bob);
assert(!world.is_owner(foo_selector, bob), 'should not be owner');
}

#[test]
Expand All @@ -692,10 +692,10 @@ fn test_set_owner_fails_for_non_owner() {
let alice = starknet::contract_address_const::<0x1337>();
starknet::testing::set_account_contract_address(alice);

world.revoke_owner(alice, 0);
assert(!world.is_owner(alice, 0), 'should not be owner');
world.revoke_owner(0, alice);
assert(!world.is_owner(0, alice), 'should not be owner');

world.grant_owner(alice, 0);
world.grant_owner(0, alice);
}

#[test]
Expand Down Expand Up @@ -767,7 +767,7 @@ fn test_set_writer_fails_for_non_owner() {
let alice = starknet::contract_address_const::<0x1337>();
starknet::testing::set_contract_address(alice);

assert(!world.is_owner(alice, 0), 'should not be owner');
assert(!world.is_owner(0, alice), 'should not be owner');

world.grant_writer(42, 69.try_into().unwrap());
}
Expand Down Expand Up @@ -1546,7 +1546,7 @@ fn test_write_model_for_namespace_owner() {
let contract = starknet::contract_address_const::<0xdeadbeef>();

// the caller account is a model namespace owner
world.grant_owner(account, Model::<Foo>::namespace_hash());
world.grant_owner(Model::<Foo>::namespace_hash(), account);
starknet::testing::set_account_contract_address(account);
starknet::testing::set_contract_address(contract);

Expand All @@ -1562,7 +1562,7 @@ fn test_write_model_for_model_owner() {
let account = starknet::contract_address_const::<0xb0b>();
let contract = starknet::contract_address_const::<0xdeadbeef>();

world.grant_owner(account, Model::<Foo>::selector());
world.grant_owner(Model::<Foo>::selector(), account);
starknet::testing::set_account_contract_address(account);
starknet::testing::set_contract_address(contract);

Expand Down Expand Up @@ -1610,7 +1610,7 @@ fn test_write_namespace_for_namespace_owner() {
let account = starknet::contract_address_const::<0xb0b>();
let contract = starknet::contract_address_const::<0xdeadbeef>();

world.grant_owner(account, Model::<Foo>::namespace_hash());
world.grant_owner(Model::<Foo>::namespace_hash(), account);

// the account owns the Foo model namespace so it should be able to deploy
// and register the model.
Expand Down Expand Up @@ -1666,7 +1666,7 @@ fn test_deploy_contract_for_namespace_owner() {
let world = deploy_world();

let account = starknet::contract_address_const::<0xb0b>();
world.grant_owner(account, bytearray_hash(@"dojo"));
world.grant_owner(bytearray_hash(@"dojo"), account);

// the account owns the 'test_contract' namespace so it should be able to deploy
// and register the model.
Expand Down
22 changes: 11 additions & 11 deletions crates/dojo-core/src/world/world_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ pub trait IWorld<T> {
/// In Dojo, there are 2 levels of authorization: `owner` and `writer`.
/// Only accounts can own a resource while any contract can write to a resource,
/// as soon as it has granted the write access from an owner of the resource.
fn is_owner(self: @T, address: ContractAddress, resource: felt252) -> bool;
fn grant_owner(ref self: T, address: ContractAddress, resource: felt252);
fn revoke_owner(ref self: T, address: ContractAddress, resource: felt252);
fn is_owner(self: @T, resource: felt252, address: ContractAddress) -> bool;
fn grant_owner(ref self: T, resource: felt252, address: ContractAddress);
fn revoke_owner(ref self: T, resource: felt252, address: ContractAddress);

fn is_writer(self: @T, resource: felt252, contract: ContractAddress) -> bool;
fn grant_writer(ref self: T, resource: felt252, contract: ContractAddress);
Expand Down Expand Up @@ -343,13 +343,13 @@ pub mod world {
///
/// # Arguments
///
/// * `address` - The contract address.
/// * `resource` - The resource.
/// * `address` - The contract address.
///
/// # Returns
///
/// * `bool` - True if the address is an owner of the resource, false otherwise.
fn is_owner(self: @ContractState, address: ContractAddress, resource: felt252) -> bool {
fn is_owner(self: @ContractState, resource: felt252, address: ContractAddress) -> bool {
self.owners.read((resource, address))
}

Expand All @@ -360,9 +360,9 @@ pub mod world {
///
/// # Arguments
///
/// * `address` - The contract address.
/// * `resource` - The resource.
fn grant_owner(ref self: ContractState, address: ContractAddress, resource: felt252) {
/// * `address` - The contract address.
fn grant_owner(ref self: ContractState, resource: felt252, address: ContractAddress) {
assert(!self.resources.read(resource).is_none(), Errors::NOT_REGISTERED);
assert(self.is_account_owner(resource), Errors::NOT_OWNER);

Expand All @@ -378,9 +378,9 @@ pub mod world {
///
/// # Arguments
///
/// * `address` - The contract address.
/// * `resource` - The resource.
fn revoke_owner(ref self: ContractState, address: ContractAddress, resource: felt252) {
/// * `address` - The contract address.
fn revoke_owner(ref self: ContractState, resource: felt252, address: ContractAddress) {
assert(!self.resources.read(resource).is_none(), Errors::NOT_REGISTERED);
assert(self.is_account_owner(resource), Errors::NOT_OWNER);

Expand Down Expand Up @@ -978,7 +978,7 @@ pub mod world {
/// false otherwise.
#[inline(always)]
fn is_account_owner(self: @ContractState, resource: felt252) -> bool {
IWorld::is_owner(self, self.get_account_address(), resource)
IWorld::is_owner(self, resource, self.get_account_address())
|| self.is_account_world_owner()
}

Expand All @@ -1004,7 +1004,7 @@ pub mod world {
/// * `bool` - True if the calling account is the world owner, false otherwise.
#[inline(always)]
fn is_account_world_owner(self: @ContractState) -> bool {
IWorld::is_owner(self, self.get_account_address(), WORLD)
IWorld::is_owner(self, WORLD, self.get_account_address())
}

/// Indicates if the provided namespace is already registered
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,13 @@
"type": "function",
"name": "is_owner",
"inputs": [
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
},
{
"name": "resource",
"type": "core::felt252"
},
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
}
],
"outputs": [
Expand All @@ -414,13 +414,13 @@
"type": "function",
"name": "grant_owner",
"inputs": [
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
},
{
"name": "resource",
"type": "core::felt252"
},
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
}
],
"outputs": [],
Expand All @@ -430,13 +430,13 @@
"type": "function",
"name": "revoke_owner",
"inputs": [
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
},
{
"name": "resource",
"type": "core::felt252"
},
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
}
],
"outputs": [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kind = "Class"
class_hash = "0x32fb65ebfe6d91bb4b7ba0640650722c43ca0c917f5fc0f649ee2ecf720cde"
original_class_hash = "0x32fb65ebfe6d91bb4b7ba0640650722c43ca0c917f5fc0f649ee2ecf720cde"
class_hash = "0x4c90da98d2bad157dec1d7b9b9c8c5861828a7ec1b323425d84fa6c3071303f"
original_class_hash = "0x4c90da98d2bad157dec1d7b9b9c8c5861828a7ec1b323425d84fa6c3071303f"
abi = "manifests/dev/base/abis/dojo-world.json"
tag = "dojo-world"
manifest_name = "dojo-world"
24 changes: 12 additions & 12 deletions crates/dojo-world/src/contracts/abi/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,13 @@ abigen!(
"type": "function",
"name": "is_owner",
"inputs": [
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
},
{
"name": "resource",
"type": "core::felt252"
},
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
}
],
"outputs": [
Expand All @@ -420,13 +420,13 @@ abigen!(
"type": "function",
"name": "grant_owner",
"inputs": [
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
},
{
"name": "resource",
"type": "core::felt252"
},
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
}
],
"outputs": [],
Expand All @@ -436,13 +436,13 @@ abigen!(
"type": "function",
"name": "revoke_owner",
"inputs": [
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
},
{
"name": "resource",
"type": "core::felt252"
},
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
}
],
"outputs": [],
Expand Down
4 changes: 2 additions & 2 deletions crates/sozo/ops/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ where
for new_owner in new_owners {
let resource_selector =
get_resource_selector(ui, world, &new_owner.resource, default_namespace).await?;
calls.push(world.grant_owner_getcall(&new_owner.owner.into(), &resource_selector));
calls.push(world.grant_owner_getcall(&resource_selector, &new_owner.owner.into()));
}

let res = world
Expand Down Expand Up @@ -233,7 +233,7 @@ where
for new_owner in new_owners {
let resource_selector =
get_resource_selector(ui, world, &new_owner.resource, default_namespace).await?;
calls.push(world.revoke_owner_getcall(&new_owner.owner.into(), &resource_selector));
calls.push(world.revoke_owner_getcall(&resource_selector, &new_owner.owner.into()));
}

let res = world
Expand Down
2 changes: 1 addition & 1 deletion crates/sozo/ops/src/tests/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn test_model_ops() {
)
.await
.unwrap(),
Felt::from_hex("0x6dd9f573496d64b1c026c68cbfe31a7724c7bcd4a142099666e8e22b82e0688")
Felt::from_hex("0x2b14bbb22e6a21cc949e06a187436c96aeab0e0290b3a8d91fb357ed2e6d973")
.unwrap()
);

Expand Down
2 changes: 1 addition & 1 deletion examples/spawn-and-move/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ rpc_url = "http://localhost:5050/"
# Default account for katana with seed = 0
account_address = "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03"
private_key = "0x1800000000300000180000000000030000000000003006001800006600"
world_address = "0x64525f120f0c0bfaa610e38fad3ace40ff8c696e0c498697d73e0f58fd5ad1"
world_address = "0x74c73d35df54ddc53bcf34aab5e0dbb09c447e99e01f4d69535441253c9571a"

[profile.release.tool.dojo]
# for more info on how `merge-strategy` works see:
Expand Down
24 changes: 12 additions & 12 deletions examples/spawn-and-move/manifests/dev/base/abis/dojo-world.json
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,13 @@
"type": "function",
"name": "is_owner",
"inputs": [
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
},
{
"name": "resource",
"type": "core::felt252"
},
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
}
],
"outputs": [
Expand All @@ -414,13 +414,13 @@
"type": "function",
"name": "grant_owner",
"inputs": [
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
},
{
"name": "resource",
"type": "core::felt252"
},
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
}
],
"outputs": [],
Expand All @@ -430,13 +430,13 @@
"type": "function",
"name": "revoke_owner",
"inputs": [
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
},
{
"name": "resource",
"type": "core::felt252"
},
{
"name": "address",
"type": "core::starknet::contract_address::ContractAddress"
}
],
"outputs": [],
Expand Down
Loading
Loading