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

Eth account support #361

Merged
merged 66 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
8561dc7
Create separate execute function
JulissaDantes Jun 10, 2022
7676f32
Add is_valid_eth_signature to account library
JulissaDantes Jun 10, 2022
99446d5
Add eth_execute to account library
JulissaDantes Jun 10, 2022
1e2f521
Create eth account mock and test
JulissaDantes Jun 10, 2022
2b7b91a
Add missing dependencies
JulissaDantes Jun 10, 2022
c72c4cd
Create TestEthSigner
JulissaDantes Jun 10, 2022
b6e4701
Update used private key
JulissaDantes Jun 10, 2022
2f737db
Update implicit parameters
JulissaDantes Jun 10, 2022
a92e1de
Update execute parameters
JulissaDantes Jun 14, 2022
e507109
Update all implicit arguments
JulissaDantes Jun 14, 2022
8e0c04d
Update signature values and hash
JulissaDantes Jun 14, 2022
199d272
Update variable name
JulissaDantes Jun 14, 2022
f9cd939
Merge branch 'main' into eth-account
JulissaDantes Jun 14, 2022
2601297
Update documentation
JulissaDantes Jun 15, 2022
75f75c5
Fix merge error
JulissaDantes Jun 15, 2022
4092021
Improve format
JulissaDantes Jun 15, 2022
2e7e85a
Update tests/utils.py
JulissaDantes Jun 17, 2022
6219d02
Update docs/Account.md
JulissaDantes Jun 17, 2022
121f87d
Update docs/Account.md
JulissaDantes Jun 17, 2022
12fbf29
Rename test and fix documentation
JulissaDantes Jun 17, 2022
a88b9bd
Add documentation
JulissaDantes Jun 21, 2022
0394607
Update src/openzeppelin/account/library.cairo
JulissaDantes Jun 21, 2022
b10dbaf
Update src/openzeppelin/account/library.cairo
JulissaDantes Jun 21, 2022
e5a2f97
Update src/openzeppelin/account/library.cairo
JulissaDantes Jun 21, 2022
e62486d
Update src/openzeppelin/account/library.cairo
JulissaDantes Jun 21, 2022
2880487
Update tests/utils.py
JulissaDantes Jun 21, 2022
9d47a8c
Update tests/utils.py
JulissaDantes Jun 21, 2022
c7c685f
Update tests/mocks/eth_account.cairo
JulissaDantes Jun 21, 2022
dd74a2b
Create eth account preset
JulissaDantes Jun 21, 2022
09faeb0
Merge branch 'eth-account' of https://github.com/JulissaDantes/cairo-…
JulissaDantes Jun 21, 2022
3b71400
Create signers module
JulissaDantes Jun 22, 2022
e868715
use assert_revert to test nonce
JulissaDantes Jun 22, 2022
69ed42b
Add test for valid signature
JulissaDantes Jun 23, 2022
586ba4f
use internal hash
JulissaDantes Jun 23, 2022
43dea0f
Update validity test
JulissaDantes Jun 23, 2022
7204bf6
Update docs/Account.md
JulissaDantes Jun 24, 2022
3572442
Update docs/Account.md
JulissaDantes Jun 24, 2022
c7b2f73
Update tests/signers.py
JulissaDantes Jun 24, 2022
f357988
Update src/openzeppelin/account/library.cairo
JulissaDantes Jun 24, 2022
e002076
Merge branch 'main' into eth-account
JulissaDantes Jun 24, 2022
0123dae
Fix after merge
JulissaDantes Jun 24, 2022
397756c
Improve tests
JulissaDantes Jun 24, 2022
042c4fd
Update account library
JulissaDantes Jun 24, 2022
0171e96
Update Account.md
JulissaDantes Jun 24, 2022
d9ab11e
update format
JulissaDantes Jun 24, 2022
3e8e82d
Update src/openzeppelin/account/library.cairo
JulissaDantes Jun 27, 2022
656d460
Update tests/access/test_Ownable.py
JulissaDantes Jun 27, 2022
a195736
Update src/openzeppelin/account/library.cairo
JulissaDantes Jun 27, 2022
8cc4308
Update eth test
JulissaDantes Jun 27, 2022
866254a
Update Account.md
JulissaDantes Jun 27, 2022
cfa1713
Update test
JulissaDantes Jun 27, 2022
9dc3c31
Update tests/signers.py
JulissaDantes Jun 27, 2022
7c9291b
Fix typo
JulissaDantes Jun 27, 2022
860f756
Merge branch 'eth-account' of https://github.com/JulissaDantes/cairo-…
JulissaDantes Jun 27, 2022
b73307a
Update signers
JulissaDantes Jun 27, 2022
0d0bc1a
Update test
JulissaDantes Jun 28, 2022
47b4785
Update docs/Account.md
JulissaDantes Jun 29, 2022
346cd79
Update docs/Account.md
JulissaDantes Jun 29, 2022
650e578
Update tests/signers.py
JulissaDantes Jun 29, 2022
fd745fa
Update docs/Account.md
JulissaDantes Jun 29, 2022
78a7b7b
Merge branch 'main' into eth-account
JulissaDantes Jun 29, 2022
668a25f
Merge branch 'eth-account' of https://github.com/JulissaDantes/cairo-…
JulissaDantes Jun 29, 2022
7d78282
update test
JulissaDantes Jun 29, 2022
4e583e5
Update documenation for Account
JulissaDantes Jun 29, 2022
4302136
Update docs/Account.md
martriay Jun 29, 2022
2c121ed
Update docs/Account.md
JulissaDantes Jun 29, 2022
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
13 changes: 8 additions & 5 deletions docs/Account.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,10 @@ is_valid: felt
This is the only external entrypoint to interact with the Account contract. It:

1. Validates the transaction signature matches the message (including the nonce)
2. Calls _unsafe_execute.
2. Validates the transaction signature matches the message (including the nonce)
martriay marked this conversation as resolved.
Show resolved Hide resolved
3. Increments the nonce
4. Calls the target contract with the intended function selector and calldata parameters
5. Forwards the contract call response data as return value

Parameters:

Expand Down Expand Up @@ -441,7 +444,7 @@ response: felt*

### `_unsafe_execute`

Is the internal method that performs the following task inside the `execute` and `eth_execute` functions:
It's an internal method that performs the following tasks:

1. Increments the nonce.
2. Takes the input and builds a `Call` for each iterated message. See [Multicall transactions](#multicall-transactions) for more information.
Expand All @@ -454,11 +457,11 @@ The following contract presets are ready to deploy and can be used as-is for qui

### Account

The [`Account`](../src/openzeppelin/account/Account.cairo) preset offers an basic implementation of the library.
The [`Account`](../src/openzeppelin/account/Account.cairo) preset uses StarkNet keys to validate transactions.

### Eth Account

The [`EthAccount`](../src/openzeppelin/account/EthAccount.cairo) preset offers an account that supports ethereum addresses, meaning the key pair uses the 256k1 curve.
The [`EthAccount`](../src/openzeppelin/account/EthAccount.cairo) preset supports Ethereum addresses, validating transactions with secp256k1 keys.

## Account differentiation with ERC165

Expand All @@ -474,7 +477,7 @@ To implement custom account contracts, a pair of `validate` and `execute` functi

Account contract developers are encouraged to implement the [standard Account interface](https://github.com/OpenZeppelin/cairo-contracts/discussions/41) and incorporate the custom logic thereafter.

To implement alternative `execute` functions, make sure to check their corresponding `validate` function before calling the `_unsafe_execute` building block. Do not expose `_unsafe_execute` directly.
To implement alternative `execute` functions, make sure to check their corresponding `validate` function before calling the `_unsafe_execute` building block, as each of the current presets is doing. Do not expose `_unsafe_execute` directly.

Some other validation schemes to look out for in the future:

Expand Down
4 changes: 1 addition & 3 deletions tests/account/test_Account.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import pytest
from starkware.starknet.testing.starknet import Starknet
from starkware.starkware_utils.error_handling import StarkException
from starkware.starknet.definitions.error_codes import StarknetErrorCode
from signers import MockSigner
from utils import assert_revert, get_contract_def, cached_contract, TRUE
from utils import assert_revert, get_contract_class, cached_contract, TRUE


signer = MockSigner(123456789987654321)
Expand Down
3 changes: 1 addition & 2 deletions tests/account/test_AddressRegistry.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import pytest
from starkware.starknet.testing.starknet import Starknet
from signers import MockSigner
from utils import contract_path

from utils import get_contract_class, cached_contract


signer = MockSigner(123456789987654321)
Expand Down
44 changes: 21 additions & 23 deletions tests/account/test_EthAccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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 import assert_revert, get_contract_def, cached_contract, TRUE, FALSE
from utils import assert_revert, get_contract_class, cached_contract, TRUE, FALSE
from signers import MockEthSigner

private_key = b'\x01' * 32
Expand All @@ -14,36 +14,36 @@

@pytest.fixture(scope='module')
def contract_defs():
account_def = get_contract_def('openzeppelin/account/EthAccount.cairo')
init_def = get_contract_def("tests/mocks/Initializable.cairo")
attacker_def = get_contract_def("tests/mocks/account_reentrancy.cairo")
account_cls = get_contract_class('openzeppelin/account/EthAccount.cairo')
init_cls = get_contract_class("tests/mocks/Initializable.cairo")
attacker_cls = get_contract_class("tests/mocks/account_reentrancy.cairo")

return account_def, init_def, attacker_def
return account_cls, init_cls, attacker_cls


@pytest.fixture(scope='module')
async def account_init(contract_defs):
account_def, init_def, attacker_def = contract_defs
account_cls, init_cls, attacker_cls = contract_defs
starknet = await Starknet.empty()

account1 = await starknet.deploy(
contract_def=account_def,
contract_class=account_cls,
constructor_calldata=[signer.eth_address]
)
account2 = await starknet.deploy(
contract_def=account_def,
contract_class=account_cls,
constructor_calldata=[signer.eth_address]
)
initializable1 = await starknet.deploy(
contract_def=init_def,
contract_class=init_cls,
constructor_calldata=[],
)
initializable2 = await starknet.deploy(
contract_def=init_def,
contract_class=init_cls,
constructor_calldata=[],
)
attacker = await starknet.deploy(
contract_def=attacker_def,
contract_class=attacker_cls,
constructor_calldata=[],
)

Expand All @@ -52,14 +52,14 @@ async def account_init(contract_defs):

@pytest.fixture
def account_factory(contract_defs, account_init):
account_def, init_def, attacker_def = contract_defs
account_cls, init_cls, attacker_cls = contract_defs
state, account1, account2, initializable1, initializable2, attacker = account_init
_state = state.copy()
account1 = cached_contract(_state, account_def, account1)
account2 = cached_contract(_state, account_def, account2)
initializable1 = cached_contract(_state, init_def, initializable1)
initializable2 = cached_contract(_state, init_def, initializable2)
attacker = cached_contract(_state, attacker_def, attacker)
account1 = cached_contract(_state, account_cls, account1)
account2 = cached_contract(_state, account_cls, account2)
initializable1 = cached_contract(_state, init_cls, initializable1)
initializable2 = cached_contract(_state, init_cls, initializable2)
attacker = cached_contract(_state, attacker_cls, attacker)

return account1, account2, initializable1, initializable2, attacker

Expand Down Expand Up @@ -91,12 +91,10 @@ async def test_execute(account_factory):
assert execution_info.result == (TRUE,)

# should revert if signature is not correct
try:
await signer.send_transactions(account, [(account.contract_address, 'is_valid_signature', [hash-1, len(signature), *signature])])
assert False
except StarkException as err:
_, error = err.args
assert error['code'] == StarknetErrorCode.TRANSACTION_FAILED
await assert_revert(
signer.send_transactions(account, [(account.contract_address, 'is_valid_signature', [hash-1, len(signature), *signature])]),
reverted_with="Invalid signature"
)


@pytest.mark.asyncio
Expand Down
2 changes: 1 addition & 1 deletion tests/security/test_pausable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from signers import MockSigner
from utils import (
TRUE, FALSE, assert_revert, assert_event_emitted,
get_contract_def, cached_contract
get_contract_class, cached_contract
)


Expand Down
2 changes: 1 addition & 1 deletion tests/signers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async def send_transactions(self, account, calls, nonce=None, max_fee=0):
sig_r = to_uint(signature.r)
sig_s = to_uint(signature.s)

# the hash and signature are returned for other tests to use them
# the hash and signature are returned for other tests to use
return await account.__execute__(call_array, calldata, nonce).invoke(
signature=[signature.v, *sig_r, *sig_s]
), message_hash, [signature.v, *sig_r, *sig_s]
2 changes: 1 addition & 1 deletion tests/token/erc20/test_ERC20.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from signers import MockSigner
from utils import (
to_uint, add_uint, sub_uint, str_to_felt, MAX_UINT256,
ZERO_ADDRESS, INVALID_UINT256, TRUE, get_contract_def, cached_contract,
ZERO_ADDRESS, INVALID_UINT256, TRUE, get_contract_class, cached_contract,
assert_revert, assert_event_emitted, contract_path
)

Expand Down
2 changes: 1 addition & 1 deletion tests/token/erc20/test_ERC20_Burnable_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from signers import MockSigner
from utils import (
to_uint, add_uint, sub_uint, str_to_felt, ZERO_ADDRESS, INVALID_UINT256,
get_contract_def, cached_contract, assert_revert, assert_event_emitted,
get_contract_class, cached_contract, assert_revert, assert_event_emitted
)


Expand Down
2 changes: 1 addition & 1 deletion tests/token/erc20/test_ERC20_Mintable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from signers import MockSigner
from utils import (
to_uint, add_uint, sub_uint, str_to_felt,
MAX_UINT256, ZERO_ADDRESS, INVALID_UINT256, get_contract_def,
MAX_UINT256, ZERO_ADDRESS, INVALID_UINT256, get_contract_class,
cached_contract, assert_revert, assert_event_emitted
)

Expand Down
2 changes: 1 addition & 1 deletion tests/token/erc20/test_ERC20_Pausable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from signers import MockSigner
from utils import (
TRUE, FALSE, to_uint, str_to_felt, assert_revert,
get_contract_def, cached_contract
get_contract_class, cached_contract
)


Expand Down
2 changes: 1 addition & 1 deletion tests/token/erc20/test_ERC20_Upgradeable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from signers import MockSigner
from utils import (
to_uint, sub_uint, str_to_felt, assert_revert,
get_contract_def, cached_contract
get_contract_class, cached_contract
)


Expand Down
2 changes: 1 addition & 1 deletion tests/token/erc721/test_ERC721_Mintable_Burnable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from signers import MockSigner
from utils import (
str_to_felt, ZERO_ADDRESS, TRUE, FALSE, assert_revert, INVALID_UINT256,
assert_event_emitted, get_contract_def, cached_contract, to_uint, sub_uint, add_uint
assert_event_emitted, get_contract_class, cached_contract, to_uint, sub_uint, add_uint
)


Expand Down
2 changes: 1 addition & 1 deletion tests/token/erc721/test_ERC721_Mintable_Pausable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from starkware.starknet.testing.starknet import Starknet
from signers import MockSigner
from utils import (
str_to_felt, TRUE, FALSE, get_contract_def, cached_contract,
str_to_felt, TRUE, FALSE, get_contract_class, cached_contract,
assert_revert, to_uint
)

Expand Down
2 changes: 1 addition & 1 deletion tests/token/erc721/test_ERC721_SafeMintable_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from signers import MockSigner
from utils import (
str_to_felt, ZERO_ADDRESS, INVALID_UINT256, assert_revert,
assert_event_emitted, get_contract_def, cached_contract, to_uint
assert_event_emitted, get_contract_class, cached_contract, to_uint
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from starkware.starknet.testing.starknet import Starknet
from signers import MockSigner
from utils import (
str_to_felt, MAX_UINT256, get_contract_def, cached_contract,
str_to_felt, MAX_UINT256, get_contract_class, cached_contract,
TRUE, assert_revert, to_uint, sub_uint, add_uint
)

Expand Down
6 changes: 5 additions & 1 deletion tests/upgrades/test_Proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
from starkware.starknet.testing.starknet import Starknet
from signers import MockSigner
from utils import (
assert_revert, get_contract_def, cached_contract
assert_revert,
get_contract_class,
cached_contract,
assert_event_emitted,
assert_revert_entry_point
)

# random value
Expand Down
6 changes: 5 additions & 1 deletion tests/upgrades/test_upgrades.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
from starkware.starknet.testing.starknet import Starknet
from signers import MockSigner
from utils import (
assert_revert, assert_event_emitted, get_contract_def, cached_contract
assert_revert,
assert_revert_entry_point,
assert_event_emitted,
get_contract_class,
cached_contract
)


Expand Down