From 4f3a54d123c140bec17507be0167ddcd5026b1fa Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Thu, 19 Sep 2024 15:29:34 +0100 Subject: [PATCH 1/6] permission account can execute --- tests/test_permissionable_component.cairo | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/test_permissionable_component.cairo b/tests/test_permissionable_component.cairo index 6b8f421..b143a4e 100644 --- a/tests/test_permissionable_component.cairo +++ b/tests/test_permissionable_component.cairo @@ -212,3 +212,48 @@ fn test_set_permission_emits_event() { ); } + +#[test] +fn test_permissioned_accounts_can_execute() { + let (contract_address, _) = __setup__(); + let acct_dispatcher = IAccountDispatcher { contract_address: contract_address }; + let safe_dispatcher = IExecutableDispatcher { contract_address }; + let owner = acct_dispatcher.owner(); + + let mut permission_addresses = ArrayTrait::new(); + permission_addresses.append(ACCOUNT2.try_into().unwrap()); + permission_addresses.append(ACCOUNT3.try_into().unwrap()); + permission_addresses.append(ACCOUNT4.try_into().unwrap()); + + let mut permissions = ArrayTrait::new(); + permissions.append(true); + permissions.append(true); + permissions.append(false); + + start_cheat_caller_address(contract_address, owner); + + let permissionable_dispatcher = IPermissionableDispatcher { contract_address }; + permissionable_dispatcher.set_permission(permission_addresses, permissions); + + let has_permission2 = permissionable_dispatcher + .has_permission(owner, ACCOUNT2.try_into().unwrap()); + assert(has_permission2 == true, 'Account: permitted'); + + // deploy `HelloStarknet` contract for testing + let test_contract = declare("HelloStarknet").unwrap(); + let (test_address, _) = test_contract.deploy(@array![]).unwrap(); + + // craft calldata for call array + let mut calldata = array![100].span(); + let call = Call { + to: test_address, + selector: 1530486729947006463063166157847785599120665941190480211966374137237989315360, + calldata: calldata + }; + + start_cheat_caller_address(contract_address, ACCOUNT2.try_into().unwrap()); + safe_dispatcher.execute(array![call]); + + stop_cheat_caller_address(contract_address); +} + From 03f635bf48197097eee03771d6a684a5e8fae940 Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Thu, 19 Sep 2024 16:04:41 +0100 Subject: [PATCH 2/6] permission account can upgrade --- tests/test_permissionable_component.cairo | 42 +++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/test_permissionable_component.cairo b/tests/test_permissionable_component.cairo index b143a4e..3bcb3d3 100644 --- a/tests/test_permissionable_component.cairo +++ b/tests/test_permissionable_component.cairo @@ -18,6 +18,10 @@ use token_bound_accounts::interfaces::IPermissionable::{ use token_bound_accounts::interfaces::IExecutable::{ IExecutableDispatcher, IExecutableDispatcherTrait }; + +use token_bound_accounts::interfaces::IUpgradeable::{ + IUpgradeableDispatcher, IUpgradeableDispatcherTrait +}; use token_bound_accounts::interfaces::IERC721::{IERC721Dispatcher, IERC721DispatcherTrait}; use token_bound_accounts::components::presets::account_preset::AccountPreset; use token_bound_accounts::components::permissionable::permissionable::PermissionableComponent; @@ -257,3 +261,41 @@ fn test_permissioned_accounts_can_execute() { stop_cheat_caller_address(contract_address); } +#[test] +fn test_permissioned_accounts_can_upgrade() { + let (contract_address, erc721_contract_address) = __setup__(); + let acct_dispatcher = IAccountDispatcher { contract_address: contract_address }; + let owner = acct_dispatcher.owner(); + + // get token owner + let token_dispatcher = IERC721Dispatcher { contract_address: erc721_contract_address }; + let token_owner = token_dispatcher.ownerOf(1.try_into().unwrap()); + + let mut permission_addresses = ArrayTrait::new(); + permission_addresses.append(ACCOUNT2.try_into().unwrap()); + permission_addresses.append(token_owner); + + let mut permissions = ArrayTrait::new(); + permissions.append(true); + permissions.append(true); + + start_cheat_caller_address(contract_address, owner); + + let permissionable_dispatcher = IPermissionableDispatcher { contract_address }; + permissionable_dispatcher.set_permission(permission_addresses, permissions); + + let has_permission2 = permissionable_dispatcher.has_permission(owner, token_owner); + assert(has_permission2 == true, 'Account: permitted'); + + stop_cheat_caller_address(contract_address); + + let new_class_hash = declare("UpgradedAccount").unwrap().class_hash; + + // call the upgrade function + let dispatcher = IUpgradeableDispatcher { contract_address }; + + start_cheat_caller_address(contract_address, token_owner); + dispatcher.upgrade(new_class_hash); + stop_cheat_caller_address(contract_address); +} + From af67426cc60db7427dc437cf5f9474ec93ea5d8b Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Thu, 19 Sep 2024 18:35:16 +0100 Subject: [PATCH 3/6] test owner and permission address is valid signer --- tests/test_account_v3.cairo | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/test_account_v3.cairo b/tests/test_account_v3.cairo index ee306b9..19f72ac 100644 --- a/tests/test_account_v3.cairo +++ b/tests/test_account_v3.cairo @@ -13,6 +13,11 @@ use token_bound_accounts::interfaces::IRegistry::{IRegistryDispatcherTrait, IReg use token_bound_accounts::interfaces::IERC721::{ IERC721Dispatcher, IERC721DispatcherTrait, IERC721SafeDispatcher, IERC721SafeDispatcherTrait }; +use token_bound_accounts::interfaces::IAccount::{IAccountDispatcher, IAccountDispatcherTrait}; +use token_bound_accounts::interfaces::IPermissionable::{ + IPermissionableDispatcher, IPermissionableDispatcherTrait +}; +use token_bound_accounts::interfaces::ISignatory::{ISignatoryDispatcher, ISignatoryDispatcherTrait}; use token_bound_accounts::interfaces::IAccountV3::{IAccountV3Dispatcher, IAccountV3DispatcherTrait}; use token_bound_accounts::test_helper::{ hello_starknet::{IHelloStarknetDispatcher, IHelloStarknetDispatcherTrait, HelloStarknet}, @@ -22,6 +27,7 @@ use token_bound_accounts::test_helper::{ const ACCOUNT: felt252 = 1234; +const ACCOUNT1: felt252 = 5739; const ACCOUNT2: felt252 = 5729; const SALT: felt252 = 123; @@ -124,3 +130,55 @@ fn test_context() { assert(_implementation == implementation, 'invalid implementation'); assert(_salt == 20, 'invalid salt'); } + + +#[test] +fn test_owner_and_permissioned_accounts_is_valid_signer() { + let (erc721_contract_address, _, account_v3_contract_address, _, _,) = __setup__(); + let acct_dispatcher = IAccountDispatcher { contract_address: account_v3_contract_address }; + + let signatory_dispatcher = ISignatoryDispatcher { + contract_address: account_v3_contract_address + }; + + let owner = acct_dispatcher.owner(); + + // get token owner + let token_dispatcher = IERC721Dispatcher { contract_address: erc721_contract_address }; + let token_owner = token_dispatcher.ownerOf(1.try_into().unwrap()); + + let mut permission_addresses = ArrayTrait::new(); + permission_addresses.append(ACCOUNT2.try_into().unwrap()); + permission_addresses.append(token_owner); + + let mut permissions = ArrayTrait::new(); + permissions.append(true); + permissions.append(true); + + start_cheat_caller_address(account_v3_contract_address, owner); + + let permissionable_dispatcher = IPermissionableDispatcher { + contract_address: account_v3_contract_address + }; + permissionable_dispatcher.set_permission(permission_addresses, permissions); + + let has_permission2 = permissionable_dispatcher.has_permission(owner, token_owner); + assert(has_permission2 == true, 'Account: permitted'); + + stop_cheat_caller_address(account_v3_contract_address); + + // check owner is a valid signer + start_cheat_caller_address(account_v3_contract_address, owner); + let is_valid_signer = signatory_dispatcher.is_valid_signer(owner); + assert(is_valid_signer == true, 'should be a valid signer'); + + stop_cheat_caller_address(account_v3_contract_address); + + // check permission address is a valid signer + start_cheat_caller_address(account_v3_contract_address, token_owner); + let is_valid_signer = signatory_dispatcher.is_valid_signer(token_owner); + assert(is_valid_signer == true, 'should be a valid signer'); + + stop_cheat_caller_address(account_v3_contract_address); +} + From b5175c17a64737f5327f5d2b1afd7a02b0150f85 Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Fri, 20 Sep 2024 10:25:47 +0100 Subject: [PATCH 4/6] three set of test --- tests/test_account_v3.cairo | 119 ++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/tests/test_account_v3.cairo b/tests/test_account_v3.cairo index 19f72ac..6ce4a1e 100644 --- a/tests/test_account_v3.cairo +++ b/tests/test_account_v3.cairo @@ -13,6 +13,13 @@ use token_bound_accounts::interfaces::IRegistry::{IRegistryDispatcherTrait, IReg use token_bound_accounts::interfaces::IERC721::{ IERC721Dispatcher, IERC721DispatcherTrait, IERC721SafeDispatcher, IERC721SafeDispatcherTrait }; +use token_bound_accounts::interfaces::IExecutable::{ + IExecutableDispatcher, IExecutableDispatcherTrait +}; +use token_bound_accounts::interfaces::IUpgradeable::{ + IUpgradeableDispatcher, IUpgradeableDispatcherTrait +}; +use token_bound_accounts::interfaces::ILockable::{ILockableDispatcher, ILockableDispatcherTrait}; use token_bound_accounts::interfaces::IAccount::{IAccountDispatcher, IAccountDispatcherTrait}; use token_bound_accounts::interfaces::IPermissionable::{ IPermissionableDispatcher, IPermissionableDispatcherTrait @@ -22,6 +29,7 @@ use token_bound_accounts::interfaces::IAccountV3::{IAccountV3Dispatcher, IAccoun use token_bound_accounts::test_helper::{ hello_starknet::{IHelloStarknetDispatcher, IHelloStarknetDispatcherTrait, HelloStarknet}, simple_account::{ISimpleAccountDispatcher, ISimpleAccountDispatcherTrait, SimpleAccount}, + account_upgrade::{IUpgradedAccountDispatcher, IUpgradedAccountDispatcherTrait, UpgradedAccount}, erc721_helper::ERC721 }; @@ -29,6 +37,8 @@ use token_bound_accounts::test_helper::{ const ACCOUNT: felt252 = 1234; const ACCOUNT1: felt252 = 5739; const ACCOUNT2: felt252 = 5729; +const ACCOUNT3: felt252 = 6908; +const ACCOUNT4: felt252 = 4697; const SALT: felt252 = 123; @@ -182,3 +192,112 @@ fn test_owner_and_permissioned_accounts_is_valid_signer() { stop_cheat_caller_address(account_v3_contract_address); } +#[test] +fn test_owner_and_any_permissioned_accounts_can_execute() { + // let (contract_address, _) = __setup__(); + let (erc721_contract_address, _, account_v3_contract_address, _, _,) = __setup__(); + let acct_dispatcher = IAccountDispatcher { contract_address: account_v3_contract_address }; + let safe_dispatcher = IExecutableDispatcher { contract_address: account_v3_contract_address }; + let owner = acct_dispatcher.owner(); + + let mut permission_addresses = ArrayTrait::new(); + permission_addresses.append(ACCOUNT2.try_into().unwrap()); + permission_addresses.append(ACCOUNT3.try_into().unwrap()); + permission_addresses.append(ACCOUNT4.try_into().unwrap()); + + let mut permissions = ArrayTrait::new(); + permissions.append(true); + permissions.append(true); + permissions.append(false); + + start_cheat_caller_address(account_v3_contract_address, owner); + + let permissionable_dispatcher = IPermissionableDispatcher { + contract_address: account_v3_contract_address + }; + permissionable_dispatcher.set_permission(permission_addresses, permissions); + + let has_permission2 = permissionable_dispatcher + .has_permission(owner, ACCOUNT2.try_into().unwrap()); + assert(has_permission2 == true, 'Account: permitted'); + + // deploy `HelloStarknet` contract for testing + let test_contract = declare("HelloStarknet").unwrap(); + let (test_address, _) = test_contract.deploy(@array![]).unwrap(); + + // craft calldata for call array + let mut calldata = array![100].span(); + let call = Call { + to: test_address, + selector: 1530486729947006463063166157847785599120665941190480211966374137237989315360, + calldata: calldata + }; + + start_cheat_caller_address(account_v3_contract_address, owner); + safe_dispatcher.execute(array![call]); + + stop_cheat_caller_address(account_v3_contract_address); + + start_cheat_caller_address(account_v3_contract_address, ACCOUNT2.try_into().unwrap()); + safe_dispatcher.execute(array![call]); + + stop_cheat_caller_address(account_v3_contract_address); +} + +#[test] +#[should_panic(expected: ('Account: locked',))] +fn test_locked_account_cannot_execute() { + // let (contract_address, _) = __setup__(); + let (erc721_contract_address, _, account_v3_contract_address, _, _,) = __setup__(); + let acct_dispatcher = IAccountDispatcher { contract_address: account_v3_contract_address }; + let safe_dispatcher = IExecutableDispatcher { contract_address: account_v3_contract_address }; + + let owner = acct_dispatcher.owner(); + let lock_duration = 30_u64; + + let lockable_dispatcher = ILockableDispatcher { contract_address: account_v3_contract_address }; + + start_cheat_caller_address(account_v3_contract_address, owner); + lockable_dispatcher.lock(lock_duration); + + stop_cheat_caller_address(account_v3_contract_address); + + // deploy `HelloStarknet` contract for testing + let test_contract = declare("HelloStarknet").unwrap(); + let (test_address, _) = test_contract.deploy(@array![]).unwrap(); + + // craft calldata for call array + let mut calldata = array![100].span(); + let call = Call { + to: test_address, + selector: 1530486729947006463063166157847785599120665941190480211966374137237989315360, + calldata: calldata + }; + + start_cheat_caller_address(account_v3_contract_address, owner); + safe_dispatcher.execute(array![call]); +} + +#[test] +fn test_owner_can_upgrade() { + // let (contract_address, erc721_contract_address) = __setup__(); + let (erc721_contract_address, _, account_v3_contract_address, _, _,) = __setup__(); + let new_class_hash = declare("UpgradedAccount").unwrap().class_hash; + + // get token owner + let token_dispatcher = IERC721Dispatcher { contract_address: erc721_contract_address }; + let token_owner = token_dispatcher.ownerOf(1.try_into().unwrap()); + + // call the upgrade function + let dispatcher = IUpgradeableDispatcher { contract_address: account_v3_contract_address }; + start_cheat_caller_address(account_v3_contract_address, token_owner); + dispatcher.upgrade(new_class_hash); + + // try to call the version function + let upgraded_dispatcher = IUpgradedAccountDispatcher { + contract_address: account_v3_contract_address + }; + let version = upgraded_dispatcher.version(); + assert(version == 1_u8, 'upgrade unsuccessful'); + stop_cheat_caller_address(account_v3_contract_address); +} From a295213bdb60098dda09a9623557992bc34c653d Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Fri, 20 Sep 2024 10:38:15 +0100 Subject: [PATCH 5/6] permission account cannot upgrade --- tests/test_account_v3.cairo | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/test_account_v3.cairo b/tests/test_account_v3.cairo index 6ce4a1e..50a8678 100644 --- a/tests/test_account_v3.cairo +++ b/tests/test_account_v3.cairo @@ -301,3 +301,41 @@ fn test_owner_can_upgrade() { assert(version == 1_u8, 'upgrade unsuccessful'); stop_cheat_caller_address(account_v3_contract_address); } + +#[test] +#[should_panic(expected: ('Account: unauthorized',))] +fn test_permissioned_accounts_can_not_upgrade() { + // let (contract_address, _) = __setup__(); + let (erc721_contract_address, _, account_v3_contract_address, _, _,) = __setup__(); + let acct_dispatcher = IAccountDispatcher { contract_address: account_v3_contract_address }; + let safe_dispatcher = IExecutableDispatcher { contract_address: account_v3_contract_address }; + let new_class_hash = declare("UpgradedAccount").unwrap().class_hash; + let owner = acct_dispatcher.owner(); + + let mut permission_addresses = ArrayTrait::new(); + permission_addresses.append(ACCOUNT2.try_into().unwrap()); + permission_addresses.append(ACCOUNT3.try_into().unwrap()); + permission_addresses.append(ACCOUNT4.try_into().unwrap()); + + let mut permissions = ArrayTrait::new(); + permissions.append(true); + permissions.append(true); + permissions.append(false); + + start_cheat_caller_address(account_v3_contract_address, owner); + + let permissionable_dispatcher = IPermissionableDispatcher { + contract_address: account_v3_contract_address + }; + permissionable_dispatcher.set_permission(permission_addresses, permissions); + + let has_permission2 = permissionable_dispatcher + .has_permission(owner, ACCOUNT2.try_into().unwrap()); + assert(has_permission2 == true, 'Account: permitted'); + + // call the upgrade function + let dispatcher = IUpgradeableDispatcher { contract_address: account_v3_contract_address }; + start_cheat_caller_address(account_v3_contract_address, ACCOUNT2.try_into().unwrap()); + dispatcher.upgrade(new_class_hash); +} + From f887cb103d2a870d5baac1a00b5bc0135031e1ce Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Fri, 20 Sep 2024 12:16:38 +0100 Subject: [PATCH 6/6] owner can set permission --- tests/test_account_v3.cairo | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/tests/test_account_v3.cairo b/tests/test_account_v3.cairo index 50a8678..a33f296 100644 --- a/tests/test_account_v3.cairo +++ b/tests/test_account_v3.cairo @@ -4,7 +4,7 @@ use snforge_std::{ start_cheat_account_contract_address, stop_cheat_account_contract_address, start_cheat_transaction_hash, start_cheat_nonce, spy_events, EventSpyAssertionsTrait, ContractClassTrait, ContractClass, start_cheat_chain_id, stop_cheat_chain_id, - start_cheat_chain_id_global, stop_cheat_chain_id_global + start_cheat_chain_id_global, stop_cheat_chain_id_global, }; use core::hash::HashStateTrait; use core::pedersen::PedersenTrait; @@ -194,7 +194,6 @@ fn test_owner_and_permissioned_accounts_is_valid_signer() { #[test] fn test_owner_and_any_permissioned_accounts_can_execute() { - // let (contract_address, _) = __setup__(); let (erc721_contract_address, _, account_v3_contract_address, _, _,) = __setup__(); let acct_dispatcher = IAccountDispatcher { contract_address: account_v3_contract_address }; let safe_dispatcher = IExecutableDispatcher { contract_address: account_v3_contract_address }; @@ -247,7 +246,6 @@ fn test_owner_and_any_permissioned_accounts_can_execute() { #[test] #[should_panic(expected: ('Account: locked',))] fn test_locked_account_cannot_execute() { - // let (contract_address, _) = __setup__(); let (erc721_contract_address, _, account_v3_contract_address, _, _,) = __setup__(); let acct_dispatcher = IAccountDispatcher { contract_address: account_v3_contract_address }; let safe_dispatcher = IExecutableDispatcher { contract_address: account_v3_contract_address }; @@ -280,7 +278,6 @@ fn test_locked_account_cannot_execute() { #[test] fn test_owner_can_upgrade() { - // let (contract_address, erc721_contract_address) = __setup__(); let (erc721_contract_address, _, account_v3_contract_address, _, _,) = __setup__(); let new_class_hash = declare("UpgradedAccount").unwrap().class_hash; @@ -305,7 +302,6 @@ fn test_owner_can_upgrade() { #[test] #[should_panic(expected: ('Account: unauthorized',))] fn test_permissioned_accounts_can_not_upgrade() { - // let (contract_address, _) = __setup__(); let (erc721_contract_address, _, account_v3_contract_address, _, _,) = __setup__(); let acct_dispatcher = IAccountDispatcher { contract_address: account_v3_contract_address }; let safe_dispatcher = IExecutableDispatcher { contract_address: account_v3_contract_address }; @@ -339,3 +335,33 @@ fn test_permissioned_accounts_can_not_upgrade() { dispatcher.upgrade(new_class_hash); } +#[test] +fn test_only_owner_can_set_permissions() { + let (erc721_contract_address, _, account_v3_contract_address, _, _,) = __setup__(); + let acct_dispatcher = IAccountDispatcher { contract_address: account_v3_contract_address }; + + let owner = acct_dispatcher.owner(); + + let mut permission_addresses = ArrayTrait::new(); + permission_addresses.append(ACCOUNT2.try_into().unwrap()); + permission_addresses.append(ACCOUNT3.try_into().unwrap()); + permission_addresses.append(ACCOUNT4.try_into().unwrap()); + + let mut permissions = ArrayTrait::new(); + permissions.append(true); + permissions.append(true); + permissions.append(true); + + start_cheat_caller_address(account_v3_contract_address, owner); + + let permissionable_dispatcher = IPermissionableDispatcher { + contract_address: account_v3_contract_address + }; + permissionable_dispatcher.set_permission(permission_addresses, permissions); + + let has_permission = permissionable_dispatcher + .has_permission(owner, ACCOUNT2.try_into().unwrap()); + + assert(has_permission == true, 'Account: not permitted'); + stop_cheat_caller_address(account_v3_contract_address); +}