Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
juniset committed Apr 20, 2022
2 parents 983c3c3 + 0410c1c commit 7225b83
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 190 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/contracts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.9
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.9
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
144 changes: 13 additions & 131 deletions contracts/ArgentAccount.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ end
# CONSTANTS
####################

const VERSION = '0.2.0' # '0.2.0' = 30 2E 32 2E 30 = 0x302E322E30 = 206933470768
const VERSION = '0.2.1'

const CHANGE_SIGNER_SELECTOR = 1540130945889430637313403138889853410180247761946478946165786566748520529557
const CHANGE_GUARDIAN_SELECTOR = 1374386526556551464817815908276843861478960435557596145330240747921847320237
Expand All @@ -40,13 +40,11 @@ const ESCAPE_SECURITY_PERIOD = 7*24*60*60 # set to e.g. 7 days in prod
const ESCAPE_TYPE_GUARDIAN = 0
const ESCAPE_TYPE_SIGNER = 1

const ERC156_ACCOUNT_INTERFACE = 0xf10dbd44
const ERC165_ACCOUNT_INTERFACE = 0xf10dbd44

const TRUE = 1
const FALSE = 0

const PREFIX_TRANSACTION = 'StarkNet Transaction'

####################
# STRUCTS
####################
Expand Down Expand Up @@ -169,6 +167,7 @@ func initialize{
end

@external
@raw_output
func __execute__{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
Expand All @@ -181,8 +180,8 @@ func __execute__{
calldata: felt*,
nonce: felt
) -> (
response_len: felt,
response: felt*
retdata_size: felt,
retdata: felt*
):
alloc_locals

Expand All @@ -199,21 +198,18 @@ func __execute__{
# get the tx info
let (tx_info) = get_tx_info()

# compute message hash
let (hash) = hash_multicall(tx_info.account_contract_address, calls_len, calls, nonce, tx_info.max_fee, tx_info.version)

if calls_len == 1:
if calls[0].to == tx_info.account_contract_address:
tempvar signer_condition = (calls[0].selector - ESCAPE_GUARDIAN_SELECTOR) * (calls[0].selector - TRIGGER_ESCAPE_GUARDIAN_SELECTOR)
tempvar guardian_condition = (calls[0].selector - ESCAPE_SIGNER_SELECTOR) * (calls[0].selector - TRIGGER_ESCAPE_SIGNER_SELECTOR)
if signer_condition == 0:
# validate signer signature
validate_signer_signature(hash, tx_info.signature, tx_info.signature_len)
validate_signer_signature(tx_info.transaction_hash, tx_info.signature, tx_info.signature_len)
jmp do_execute
end
if guardian_condition == 0:
# validate guardian signature
validate_guardian_signature(hash, tx_info.signature, tx_info.signature_len)
validate_guardian_signature(tx_info.transaction_hash, tx_info.signature, tx_info.signature_len)
jmp do_execute
end
end
Expand All @@ -222,8 +218,8 @@ func __execute__{
assert_no_self_call(tx_info.account_contract_address, calls_len, calls)
end
# validate signer and guardian signatures
validate_signer_signature(hash, tx_info.signature, tx_info.signature_len)
validate_guardian_signature(hash, tx_info.signature + 2, tx_info.signature_len - 2)
validate_signer_signature(tx_info.transaction_hash, tx_info.signature, tx_info.signature_len)
validate_guardian_signature(tx_info.transaction_hash, tx_info.signature + 2, tx_info.signature_len - 2)

# execute calls
do_execute:
Expand All @@ -234,8 +230,8 @@ func __execute__{
let (response : felt*) = alloc()
let (response_len) = execute_list(calls_len, calls, response)
# emit event
transaction_executed.emit(hash=hash, response_len=response_len, response=response)
return (response_len=response_len, response=response)
transaction_executed.emit(hash=tx_info.transaction_hash, response_len=response_len, response=response)
return (retdata_size=response_len, retdata=response)
end

@external
Expand All @@ -250,7 +246,7 @@ func upgrade{
assert_only_self()
# make sure the target is an account
with_attr error_message("implementation invalid"):
let (success) = IAccount.supportsInterface(contract_address=implementation, interfaceId=ERC156_ACCOUNT_INTERFACE)
let (success) = IAccount.supportsInterface(contract_address=implementation, interfaceId=ERC165_ACCOUNT_INTERFACE)
assert success = TRUE
end
# change implementation
Expand Down Expand Up @@ -506,7 +502,7 @@ func supportsInterface{
return (TRUE)
end
# IAccount
if interfaceId == ERC156_ACCOUNT_INTERFACE:
if interfaceId == ERC165_ACCOUNT_INTERFACE:
return (TRUE)
end
return (FALSE)
Expand Down Expand Up @@ -725,120 +721,6 @@ func execute_list{
return (response_len + res.retdata_size)
end

# @notice Computes the hash of a multicall to the `execute` method.
# @param calls_len The legnth of the array of `Call`
# @param calls A pointer to the array of `Call`
# @param nonce The nonce for the multicall transaction
# @param max_fee The max fee the user is willing to pay for the multicall
# @param version The version of transaction in the Cairo OS. Always set to 0.
# @return res The hash of the multicall
func hash_multicall{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*
} (
account: felt,
calls_len: felt,
calls: Call*,
nonce: felt,
max_fee: felt,
version: felt
) -> (res: felt):
alloc_locals
let (calls_hash) = hash_call_array(calls_len, calls)
let hash_ptr = pedersen_ptr
with hash_ptr:
let (hash_state_ptr) = hash_init()
let (hash_state_ptr) = hash_update_single(hash_state_ptr, PREFIX_TRANSACTION)
let (hash_state_ptr) = hash_update_single(hash_state_ptr, account)
let (hash_state_ptr) = hash_update_single(hash_state_ptr, calls_hash)
let (hash_state_ptr) = hash_update_single(hash_state_ptr, nonce)
let (hash_state_ptr) = hash_update_single(hash_state_ptr, max_fee)
let (hash_state_ptr) = hash_update_single(hash_state_ptr, version)
let (res) = hash_finalize(hash_state_ptr)
let pedersen_ptr = hash_ptr
return (res=res)
end
end

# @notice Computes the hash of an array of `Call`
# @param calls_len The legnth of the array of `Call`
# @param calls A pointer to the array of `Call`
# @return res The hash of the array of `Call`
func hash_call_array{
pedersen_ptr: HashBuiltin*
}(
calls_len: felt,
calls: Call*
) -> (
res: felt
):
alloc_locals

let (hash_array : felt*) = alloc()
hash_call_loop(calls_len, calls, hash_array)

let hash_ptr = pedersen_ptr
with hash_ptr:
let (hash_state_ptr) = hash_init()
let (hash_state_ptr) = hash_update(hash_state_ptr, hash_array, calls_len)
let (res) = hash_finalize(hash_state_ptr)
let pedersen_ptr = hash_ptr
return (res=res)
end
end

# @notice Turns an array of `Call` into an array of `hash(Call)`
# @param calls_len The legnth of the array of `Call`
# @param calls A pointer to the array of `Call`
# @param hash_array A pointer to the array of `hash(Call)`
func hash_call_loop{
pedersen_ptr: HashBuiltin*
} (
calls_len: felt,
calls: Call*,
hash_array: felt*
):
if calls_len == 0:
return ()
end
let this_call = [calls]
let (calldata_hash) = hash_calldata(this_call.calldata_len, this_call.calldata)
let hash_ptr = pedersen_ptr
with hash_ptr:
let (hash_state_ptr) = hash_init()
let (hash_state_ptr) = hash_update_single(hash_state_ptr, this_call.to)
let (hash_state_ptr) = hash_update_single(hash_state_ptr, this_call.selector)
let (hash_state_ptr) = hash_update_single(hash_state_ptr, calldata_hash)
let (res) = hash_finalize(hash_state_ptr)
let pedersen_ptr = hash_ptr
assert [hash_array] = res
end
hash_call_loop(calls_len - 1, calls + Call.SIZE, hash_array + 1)
return()
end

# @notice Computes the hash of calldata as an array of felt
# @param calldata_len The length of the calldata array
# @param calldata A pointer to the calldata array
# @return the hash of the calldata
func hash_calldata{
pedersen_ptr: HashBuiltin*
} (
calldata_len: felt,
calldata: felt*,
) -> (
res: felt
):
let hash_ptr = pedersen_ptr
with hash_ptr:
let (hash_state_ptr) = hash_init()
let (hash_state_ptr) = hash_update(hash_state_ptr, calldata, calldata_len)
let (res) = hash_finalize(hash_state_ptr)
let pedersen_ptr = hash_ptr
return (res=res)
end
end

func from_call_array_to_call{
syscall_ptr: felt*
} (
Expand Down
22 changes: 0 additions & 22 deletions contracts/utils/array.cairo

This file was deleted.

4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cairo-lang>=0.7.1
cairo-nile
cairo-lang>=0.8.1
cairo-nile>=0.5.0
pytest
pytest-asyncio
9 changes: 5 additions & 4 deletions test/address_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import asyncio
from starkware.starknet.testing.starknet import Starknet
from utils.Signer import Signer
from utils.deploy import deploy
from utils.utilities import deploy, str_to_felt
from utils.TransactionSender import TransactionSender

signer = Signer(123456789987654321)
guardian = Signer(456789987654321123)

VERSION = 206933405232 # '0.1.0' = 30 2E 31 2E 30 = 0x302E312E30 = 206933405232
VERSION = str_to_felt('0.2.1')
L1_ADDRESS = 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984

@pytest.fixture(scope='module')
Expand All @@ -23,7 +23,8 @@ async def get_starknet():
@pytest.fixture
async def account_factory(get_starknet):
starknet = get_starknet
account = await deploy(starknet, "contracts/ArgentAccount.cairo", [signer.public_key, guardian.public_key])
account = await deploy(starknet, "contracts/ArgentAccount.cairo")
await account.initialize(signer.public_key, guardian.public_key).invoke()
return account

@pytest.fixture
Expand All @@ -47,6 +48,6 @@ async def test_setup_registry(account_factory, registry_factory):

assert (await registry.get_L1_address(account.contract_address).call()).result.res == 0

await sender.send_transaction(registry.contract_address, 'set_L1_address', [L1_ADDRESS], [signer, guardian])
await sender.send_transaction([(registry.contract_address, 'set_L1_address', [L1_ADDRESS])], [signer, guardian])

assert (await registry.get_L1_address(account.contract_address).call()).result.res == L1_ADDRESS
9 changes: 3 additions & 6 deletions test/argent_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
import asyncio
import logging
from starkware.starknet.testing.starknet import Starknet
from starkware.starkware_utils.error_handling import StarkException
from starkware.starknet.definitions.error_codes import StarknetErrorCode
from starkware.starknet.business_logic.state import BlockInfo
from starkware.starknet.compiler.compile import get_selector_from_name
from starkware.starknet.business_logic.state.state import BlockInfo
from utils.Signer import Signer
from utils.utilities import deploy, assert_revert, str_to_felt, assert_event_emmited
from utils.TransactionSender import TransactionSender
Expand All @@ -22,7 +19,7 @@
DEFAULT_TIMESTAMP = 1640991600
ESCAPE_SECURITY_PERIOD = 24*7*60*60

VERSION = str_to_felt('0.2.0')
VERSION = str_to_felt('0.2.1')

IACCOUNT_ID = 0xf10dbd44

Expand All @@ -39,7 +36,7 @@ async def get_starknet():
return starknet

def update_starknet_block(starknet, block_number=1, block_timestamp=DEFAULT_TIMESTAMP):
starknet.state.state.block_info = BlockInfo(block_number=block_number, block_timestamp=block_timestamp)
starknet.state.state.block_info = BlockInfo(block_number=block_number, block_timestamp=block_timestamp, gas_price=0)

def reset_starknet_block(starknet):
update_starknet_block(starknet=starknet)
Expand Down
2 changes: 1 addition & 1 deletion test/multicall.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import asyncio
from starkware.starknet.testing.starknet import Starknet
from starkware.starknet.public.abi import get_selector_from_name
from utils.utilities import deploy, str_to_felt, uint
from utils.utilities import deploy, str_to_felt

user1 = 0x69221ff9023c4d7ba9123f0f9c32634c23fc5776d86657f464ecb51fd811445
user2 = 0x72648c3b1953572d2c4395a610f18b83cca14fa4d1ba10fc4484431fd463e5c
Expand Down
3 changes: 1 addition & 2 deletions test/proxy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest
import asyncio
from starkware.starknet.testing.starknet import Starknet
from starkware.starkware_utils.error_handling import StarkException
from starkware.starknet.definitions.error_codes import StarknetErrorCode
from utils.Signer import Signer
from utils.utilities import deploy, deploy_proxy, assert_revert, str_to_felt, assert_event_emmited
Expand All @@ -12,7 +11,7 @@
wrong_signer = Signer(666666666666666666)
wrong_guardian = Signer(6767676767)

VERSION = str_to_felt('0.2.0')
VERSION = str_to_felt('0.2.1')

@pytest.fixture(scope='module')
def event_loop():
Expand Down
2 changes: 1 addition & 1 deletion test/struct_hash.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
import asyncio
from starkware.starknet.testing.starknet import Starknet
from utils.deploy import deploy
from utils.utilities import deploy

user1 = 0x69221ff9023c4d7ba9123f0f9c32634c23fc5776d86657f464ecb51fd811445
user2 = 0x72648c3b1953572d2c4395a610f18b83cca14fa4d1ba10fc4484431fd463e5c
Expand Down
Loading

0 comments on commit 7225b83

Please sign in to comment.