From 5cf3054c69c70e72f1ce704424b170474d30aff4 Mon Sep 17 00:00:00 2001 From: LucasLvy Date: Tue, 17 Oct 2023 16:47:31 +0200 Subject: [PATCH] add cairo 1 contracts --- .gitignore | 2 + scripts/setup-tests.sh | 12 ++- tests/contracts/cairo1/.gitignore | 1 + tests/contracts/cairo1/Scarb.toml | 12 +++ tests/contracts/cairo1/src/counter.cairo | 19 +++++ tests/contracts/cairo1/src/lib.cairo | 4 + .../cairo1/src/no_validate_account.cairo | 80 +++++++++++++++++++ 7 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 tests/contracts/cairo1/.gitignore create mode 100644 tests/contracts/cairo1/Scarb.toml create mode 100644 tests/contracts/cairo1/src/counter.cairo create mode 100644 tests/contracts/cairo1/src/lib.cairo create mode 100644 tests/contracts/cairo1/src/no_validate_account.cairo diff --git a/.gitignore b/.gitignore index 85dc355f..c4743098 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ cairo-lang venv # Hint Dependency symlink starkware + +.DS_Store diff --git a/scripts/setup-tests.sh b/scripts/setup-tests.sh index 9bd6dc58..6d518dca 100755 --- a/scripts/setup-tests.sh +++ b/scripts/setup-tests.sh @@ -14,6 +14,13 @@ then exit 1 fi +if ! command -v starknet-compile > /dev/null +then + echo "please install scarb https://docs.swmansion.com/scarb/download.html#install-via-installation-script" + echo "curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh" + exit 1 +fi + echo -e "\ninitializing cairo-lang($CAIRO_VER)...\n" git submodule update --init @@ -40,7 +47,7 @@ cp tests/dependencies/initializable.cairo cairo-lang/src/starkware/starknet/std_ # compile cairo programs -echo -e "compmiling cairo programs...\n" +echo -e "compiling cairo programs...\n" mkdir -p build/programs cairo-format -i tests/programs/* cairo-compile tests/programs/bad_output.cairo --output build/programs/bad_output.json @@ -51,7 +58,7 @@ cairo-compile tests/programs/load_deprecated_class.cairo --output build/programs cairo-compile cairo-lang/src/starkware/starknet/core/os/os.cairo --output build/os_debug.json --cairo_path cairo-lang/src # compile starknet contract -echo -e "compiling starknet contrarcts...\n" +echo -e "compiling starknet contracts...\n" mkdir -p build/contracts ln -s cairo-lang/src/starkware starkware starknet-compile-deprecated --no_debug_info tests/contracts/token_for_testing.cairo --output build/contracts/token_for_testing.json --cairo_path cairo-lang/src --account_contract @@ -60,3 +67,4 @@ starknet-compile-deprecated --no_debug_info tests/contracts/dummy_token.cairo -- starknet-compile-deprecated --no_debug_info tests/contracts/delegate_proxy.cairo --output build/contracts/delegate_proxy.json --cairo_path cairo-lang/src starknet-compile-deprecated --no_debug_info tests/contracts/test_contract.cairo --output build/contracts/test_contract.json --cairo_path cairo-lang/src starknet-compile-deprecated --no_debug_info tests/contracts/test_contract2.cairo --output build/contracts/test_contract2.json --cairo_path cairo-lang/src +cd ./tests/contracts/cairo1/ && scarb build && mv target/dev/* ../../../ \ No newline at end of file diff --git a/tests/contracts/cairo1/.gitignore b/tests/contracts/cairo1/.gitignore new file mode 100644 index 00000000..eb5a316c --- /dev/null +++ b/tests/contracts/cairo1/.gitignore @@ -0,0 +1 @@ +target diff --git a/tests/contracts/cairo1/Scarb.toml b/tests/contracts/cairo1/Scarb.toml new file mode 100644 index 00000000..9b52735c --- /dev/null +++ b/tests/contracts/cairo1/Scarb.toml @@ -0,0 +1,12 @@ +[package] +name = "cairo1" +version = "0.1.0" + +# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html + +[dependencies] +starknet = "2.2.0" + +[[target.starknet-contract]] +sierra = true +casm = true \ No newline at end of file diff --git a/tests/contracts/cairo1/src/counter.cairo b/tests/contracts/cairo1/src/counter.cairo new file mode 100644 index 00000000..1a9e1120 --- /dev/null +++ b/tests/contracts/cairo1/src/counter.cairo @@ -0,0 +1,19 @@ +#[starknet::contract] +mod HelloStarknet { + #[storage] + struct Storage { + balance: felt252, + } + + // Increases the balance by the given amount. + #[external(v0)] + fn increase_balance(ref self: ContractState, amount: felt252) { + self.balance.write(self.balance.read() + amount); + } + + // Returns the current balance. + #[external(v0)] + fn get_balance(self: @ContractState) -> felt252 { + self.balance.read() + } +} diff --git a/tests/contracts/cairo1/src/lib.cairo b/tests/contracts/cairo1/src/lib.cairo new file mode 100644 index 00000000..df542c4c --- /dev/null +++ b/tests/contracts/cairo1/src/lib.cairo @@ -0,0 +1,4 @@ +mod no_validate_account; +mod counter; +use no_validate_account::Account; +use counter::HelloStarknet; diff --git a/tests/contracts/cairo1/src/no_validate_account.cairo b/tests/contracts/cairo1/src/no_validate_account.cairo new file mode 100644 index 00000000..18b2743c --- /dev/null +++ b/tests/contracts/cairo1/src/no_validate_account.cairo @@ -0,0 +1,80 @@ +#[starknet::contract] +mod Account { + use array::{ArrayTrait, SpanTrait}; + use box::BoxTrait; + use ecdsa::check_ecdsa_signature; + use option::OptionTrait; + use starknet::account::Call; + use starknet::{ContractAddress, call_contract_syscall}; + use zeroable::Zeroable; + use array::ArraySerde; + + #[storage] + struct Storage { + public_key: felt252 + } + + #[constructor] + fn constructor(ref self: ContractState, public_key_: felt252) { + self.public_key.write(public_key_); + } + + trait StorageTrait { + fn validate_transaction(self: @ContractState) -> felt252; + } + impl StorageImpl of StorageTrait { + fn validate_transaction(self: @ContractState) -> felt252 { + starknet::VALIDATED + } + } + + + #[external(v0)] + fn __validate_deploy__( + self: @ContractState, + class_hash: felt252, + contract_address_salt: felt252, + public_key_: felt252 + ) -> felt252 { + self.validate_transaction() + } + + #[external(v0)] + impl AccountContractImpl of starknet::account::AccountContract { + fn __validate_declare__(self: @ContractState, class_hash: felt252) -> felt252 { + self.validate_transaction() + } + + fn __validate__(ref self: ContractState, calls: Array) -> felt252 { + self.validate_transaction() + } + + fn __execute__(ref self: ContractState, mut calls: Array) -> Array> { + // Validate caller. + assert(starknet::get_caller_address().is_zero(), 'INVALID_CALLER'); + + // Check the tx version here, since version 0 transaction skip the __validate__ function. + let tx_info = starknet::get_tx_info().unbox(); + assert(tx_info.version != 0, 'INVALID_TX_VERSION'); + + let mut result = ArrayTrait::new(); + loop { + match calls.pop_front() { + Option::Some(call) => { + let mut res = call_contract_syscall( + address: call.to, + entry_point_selector: call.selector, + calldata: call.calldata.span() + ) + .unwrap(); + result.append(res); + }, + Option::None(()) => { + break; // Can't break result; because of 'variable was previously moved' + }, + }; + }; + result + } + } +}