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

Add EthAccount #853

Merged
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
cd13579
feat: add eth_account main logic
ericnordelo Dec 12, 2023
19ce07d
Merge branch 'main' of github.com:OpenZeppelin/cairo-contracts into f…
ericnordelo Dec 12, 2023
463d663
feat: add tests
ericnordelo Dec 15, 2023
6ce5a08
fix: workflow
ericnordelo Dec 15, 2023
e2ee218
feat: change EthPublicKey to Secp256k1Point
ericnordelo Dec 19, 2023
3fdaf15
feat: add more tests
ericnordelo Dec 20, 2023
6cdbac9
Merge branch 'main' of github.com:OpenZeppelin/cairo-contracts into f…
ericnordelo Dec 20, 2023
16427fc
feat: add entry to CHANGELOG
ericnordelo Dec 20, 2023
135e8f1
feat: add tests for preset
ericnordelo Dec 20, 2023
f9c811a
docs: add API Reference entries
ericnordelo Dec 20, 2023
b883c6b
Update src/account/eth_account/eth_account.cairo
ericnordelo Dec 21, 2023
3c396de
feat: apply review updates
ericnordelo Dec 21, 2023
ba60238
Merge branch 'feat/migrate-eth-account-#573' of github.com:ericnordel…
ericnordelo Dec 21, 2023
34e3010
refactor: test
ericnordelo Dec 21, 2023
b40970b
feat: apply review updates
ericnordelo Dec 28, 2023
e960a4a
feat: update error messages
ericnordelo Jan 3, 2024
79c64a2
feat: apply review updates
ericnordelo Jan 5, 2024
facf8ab
Update docs/modules/ROOT/pages/api/account.adoc
ericnordelo Jan 17, 2024
ebc2e9d
feat: add tests for upgrade
ericnordelo Jan 17, 2024
1ef85b9
feat: add tests for signature
ericnordelo Jan 17, 2024
9c99ffb
Merge branch 'feat/migrate-eth-account-#573' of github.com:ericnordel…
ericnordelo Jan 17, 2024
a723d53
refactor: drop X.Y.Z in favor of last released version
ericnordelo Jan 17, 2024
f17f151
Merge branch 'main' of github.com:OpenZeppelin/cairo-contracts into f…
ericnordelo Jan 22, 2024
ef4128c
feat: updated class hashes
ericnordelo Jan 22, 2024
de0c40c
Merge branch 'main' of github.com:OpenZeppelin/cairo-contracts into f…
ericnordelo Jan 29, 2024
73144d5
Merge branch 'main' of github.com:OpenZeppelin/cairo-contracts into f…
ericnordelo Jan 29, 2024
42abd67
fix: casm target
ericnordelo Jan 29, 2024
26651b3
fix: versions
ericnordelo Jan 30, 2024
97151fd
refactor: account directory
ericnordelo Jan 30, 2024
c9d50e4
Merge branch 'main' of github.com:OpenZeppelin/cairo-contracts into f…
ericnordelo Jan 31, 2024
8a3939b
fix: comments
ericnordelo Jan 31, 2024
0f9b41c
fix: class hashes
ericnordelo Jan 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v3
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.3.1"
scarb-version: "2.4.2"
- name: Markdown lint
uses: DavidAnson/markdownlint-cli2-action@5b7c9f74fec47e6b15667b2cc23c63dff11e449e # v9
with:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Usage docs (#823)
- Utilities documentation (#825)
- Documentation for presets (#832)
- EthAccount component and preset (#853)

### Changed

- Use ComponentState in tests (#836)
- Docsite navbar (#838)
- Account events indexed keys (#853)
5 changes: 3 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "openzeppelin"
version = "0.8.0"
cairo-version = "2.3.1"
edition = "2023_01"
cairo-version = "2.4.2"
authors = ["OpenZeppelin Community <maintainers@openzeppelin.org>"]
description = "OpenZeppelin Contracts written in Cairo for StarkNet, a decentralized ZK Rollup"
documentation = "https://docs.openzeppelin.com/contracts-cairo"
Expand All @@ -11,7 +12,7 @@ license-file = "LICENSE"
keywords = ["openzeppelin", "starknet", "cairo", "contracts", "security", "standards"]

[dependencies]
starknet = "2.3.1"
starknet = "2.4.2"

[lib]

Expand Down
276 changes: 275 additions & 1 deletion docs/modules/ROOT/pages/api/account.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ Returns the short string `'VALID'` if valid, otherwise it reverts.

[.contract]
[[AccountComponent]]
=== `++AccountComponent++` link:https://github.com/OpenZeppelin/cairo-contracts/blob/release-v0.8.0/src/account/account.cairo#L27[{github-icon},role=heading-link]
=== `++AccountComponent++` link:https://github.com/OpenZeppelin/cairo-contracts/blob/release-vX.Y.Z/src/account/account/account.cairo[{github-icon},role=heading-link]
martriay marked this conversation as resolved.
Show resolved Hide resolved

:OwnerAdded: xref:AccountComponent-OwnerAdded[OwnerAdded]
:OwnerRemoved: xref:AccountComponent-OwnerRemoved[OwnerRemoved]
:starknet-curve: https://docs.starknet.io/documentation/architecture_and_concepts/Cryptography/stark-curve/[Starknet curve]

```javascript
use openzeppelin::account::AccountComponent;
Expand All @@ -75,6 +76,8 @@ Account component implementing xref:ISRC6[`ISRC6`].

NOTE: Implementing xref:api/introspection.adoc#SRC5Component[SRC5Component] is a requirement for this component to be implemented.

NOTE: Every function involving signature verification verifies with the account's current public key using the {starknet-curve}.
ericnordelo marked this conversation as resolved.
Show resolved Hide resolved

[.contract-index#AccountComponent-Embeddable-Impls]
.Embeddable Implementations
--
Expand Down Expand Up @@ -260,6 +263,210 @@ Emitted when a `public_key` is added.

Emitted when a `public_key` is removed.

[.contract]
[[EthAccountComponent]]
=== `++EthAccountComponent++` link:https://github.com/OpenZeppelin/cairo-contracts/blob/release-vX.Y.Z/src/account/eth_account/eth_account.cairo[{github-icon},role=heading-link]

:OwnerAdded: xref:EthAccountComponent-OwnerAdded[OwnerAdded]
:OwnerRemoved: xref:EthAccountComponent-OwnerRemoved[OwnerRemoved]
:secp256k1-curve: https://en.bitcoin.it/wiki/Secp256k1[Secp256k1 curve]

```javascript
use openzeppelin::account::eth_account::EthAccountComponent;
```
Account component implementing xref:ISRC6[`ISRC6`], but leveraging public key and signatures over the {secp256k1-curve}.
ericnordelo marked this conversation as resolved.
Show resolved Hide resolved

NOTE: Implementing xref:api/introspection.adoc#SRC5Component[SRC5Component] is a requirement for this component to be implemented.

NOTE: The `EthPublicKey` type is an alias for `starknet::secp256k1::Secp256k1Point`.

[.contract-index#EthAccountComponent-Embeddable-Impls]
.Embeddable Implementations
--
.SRC6Impl

* xref:#EthAccountComponent-\\__execute__[`++__execute__(self, calls)++`]
* xref:#EthAccountComponent-\\__validate__[`++__validate__(self, calls)++`]
* xref:#EthAccountComponent-is_valid_signature[`++is_valid_signature(self, hash, signature)++`]

.DeclarerImpl

* xref:#EthAccountComponent-\\__validate_declare__[`++__validate_declare__(self, class_hash)++`]

.DeployableImpl

* xref:#EthAccountComponent-\\__validate_deploy__[`++__validate_deploy__(self, hash, signature)++`]

.PublicKeyImpl

* xref:#EthAccountComponent-get_public_key[`++get_public_key(self)++`]
* xref:#EthAccountComponent-set_public_key[`++set_public_key(self, new_public_key)++`]
--

[.contract-index#EthAccountComponent-Embeddable-Impls-camelCase]
.Embeddable Implementations (camelCase)
--
.SRC6CamelOnlyImpl

* xref:#EthAccountComponent-isValidSignature[`++isValidSignature(self, hash, signature)++`]

.PublicKeyCamelImpl

* xref:#EthAccountComponent-getPublicKey[`++getPublicKey(self)++`]
* xref:#EthAccountComponent-setPublicKey[`++setPublicKey(self, newPublicKey)++`]
--

[.contract-index]
.Internal Implementations
--
.InternalImpl

* xref:#EthAccountComponent-initializer[`++initializer(self, public_key)++`]
* xref:#EthAccountComponent-assert_only_self[`++assert_only_self(self)++`]
* xref:#EthAccountComponent-validate_transaction[`++validate_transaction(self)++`]
* xref:#EthAccountComponent-_set_public_key[`++_set_public_key(self, new_public_key)++`]
* xref:#EthAccountComponent-_is_valid_signature[`++_is_valid_signature(self, hash, signature)++`]
--

[.contract-index]
.Events
--
* xref:#EthAccountComponent-OwnerAdded[`++OwnerAdded(new_owner_guid)++`]
* xref:#EthAccountComponent-OwnerRemoved[`++OwnerRemoved(removed_owner_guid)++`]
--

[#EthAccountComponent-Embeddable-Functions]
==== Embeddable Functions

[.contract-item]
[[EthAccountComponent-__execute__]]
==== `[.contract-item-name]#++__execute__++#++(self: @ContractState, calls: Array<Call>) → Array<Span<felt252>>++` [.item-kind]#external#

See xref:ISRC6-\\__execute__[ISRC6::\\__execute__].

[.contract-item]
[[EthAccountComponent-__validate__]]
==== `[.contract-item-name]#++__validate__++#++(self: @ContractState, calls: Array<Call>) → felt252++` [.item-kind]#external#

See xref:ISRC6-\\__validate__[ISRC6::\\__validate__].

[.contract-item]
[[EthAccountComponent-is_valid_signature]]
==== `[.contract-item-name]#++is_valid_signature++#++(self: @ContractState, hash: felt252, signature: Array<felt252>) → felt252++` [.item-kind]#external#

See xref:ISRC6-is_valid_signature[ISRC6::is_valid_signature].

[.contract-item]
[[EthAccountComponent-__validate_declare__]]
==== `[.contract-item-name]#++__validate_declare__++#++(self: @ContractState, class_hash: felt252) → felt252++` [.item-kind]#external#

Validates a https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/Blocks/transactions/#declare-transaction[`Declare` transaction].

Returns the short string `'VALID'` if valid, otherwise it reverts.

[.contract-item]
[[EthAccountComponent-__validate_deploy__]]
==== `[.contract-item-name]#++__validate_deploy__++#++(self: @ContractState, class_hash: felt252, contract_address_salt: felt252, public_key: EthPublicKey) → felt252++` [.item-kind]#external#

Validates a https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/Blocks/transactions/#deploy_account_transaction[`DeployAccount` transaction].
See xref:/guides/deployment.adoc[Counterfactual Deployments].

Returns the short string `'VALID'` if valid, otherwise it reverts.

[.contract-item]
[[EthAccountComponent-get_public_key]]
==== `[.contract-item-name]#++get_public_key++#++(self: @ContractState)++ → EthPublicKey` [.item-kind]#external#

Returns the current public key of the account.

[.contract-item]
[[EthAccountComponent-set_public_key]]
==== `[.contract-item-name]#++set_public_key++#++(ref self: ContractState, new_public_key: EthPublicKey)++` [.item-kind]#external#

Sets a new public key for the account. Only accesible by the account calling itself through `\\__execute__`.

Emits both an {OwnerRemoved} and an {OwnerAdded} event.

[#EthAccountComponent-camelCase-Support]
==== camelCase Support

[.contract-item]
[[EthAccountComponent-isValidSignature]]
==== `[.contract-item-name]#++isValidSignature++#++(self: @ContractState, hash: felt252, signature: Array<felt252>) → felt252++` [.item-kind]#external#

See xref:ISRC6-is_valid_signature[ISRC6::is_valid_signature].

[.contract-item]
[[EthAccountComponent-getPublicKey]]
==== `[.contract-item-name]#++getPublicKey++#++(self: @ContractState)++ → EthPublicKey` [.item-kind]#external#

See xref:EthAccountComponent-get_public_key[get_public_key].

[.contract-item]
[[EthAccountComponent-setPublicKey]]
==== `[.contract-item-name]#++setPublicKey++#++(ref self: ContractState, newPublicKey: EthPublicKey)++` [.item-kind]#external#

See xref:EthAccountComponent-set_public_key[set_public_key].

[#EthAccountComponent-Internal-Functions]
==== Internal Functions

[.contract-item]
[[EthAccountComponent-initializer]]
==== `[.contract-item-name]#++initializer++#++(ref self: ComponentState, public_key: EthPublicKey)++` [.item-kind]#internal#

Initializes the account with the given public key, and registers the ISRC6 interface ID.

Emits an {OwnerAdded} event.

[.contract-item]
[[EthAccountComponent-assert_only_self]]
==== `[.contract-item-name]#++assert_only_self++#++(self: @ComponentState)++` [.item-kind]#internal#

Validates that the caller is the account itself. Otherwise it reverts.

[.contract-item]
[[EthAccountComponent-validate_transaction]]
==== `[.contract-item-name]#++validate_transaction++#++(self: @ComponentState)++ → felt252` [.item-kind]#internal#

Validates a transaction signature from the
https://github.com/starkware-libs/cairo/blob/main/corelib/src/starknet/info.cairo#L61[global context].

Returns the short string `'VALID'` if valid, otherwise it reverts.

[.contract-item]
[[EthAccountComponent-_set_public_key]]
==== `[.contract-item-name]#++_set_public_key++#++(ref self: ComponentState, new_public_key: EthPublicKey)++` [.item-kind]#internal#

Set the public key without validating the caller.

Emits an {OwnerAdded} event.

CAUTION: The usage of this method outside the `set_public_key` function is discouraged.

[.contract-item]
[[EthAccountComponent-_is_valid_signature]]
==== `[.contract-item-name]#++_is_valid_signature++#++(self: @ComponentState, hash: felt252, signature: Span<felt252>)++ → bool` [.item-kind]#internal#

Validates the provided `signature` for the `hash`, using the account's current public key.

[#EthAccountComponent-Events]
==== Events

NOTE: The `guid` is computed as the hash of the public key, using the poseidon hash function.

[.contract-item]
[[EthAccountComponent-OwnerAdded]]
==== `[.contract-item-name]#++OwnerAdded++#++(new_owner_guid: felt252)++` [.item-kind]#event#

Emitted when a `public_key` is added.

[.contract-item]
[[EthAccountComponent-OwnerRemoved]]
==== `[.contract-item-name]#++OwnerRemoved++#++(removed_owner_guid: felt252)++` [.item-kind]#event#

Emitted when a `public_key` is removed.

== Presets

[.contract]
Expand Down Expand Up @@ -311,3 +518,70 @@ include::../utils/_class_hashes.adoc[]
==== `[.contract-item-name]#++constructor++#++(ref self: ContractState, public_key: felt252)++` [.item-kind]#constructor#

Sets the account `public_key` and registers the interfaces the contract supports.

[.contract]
[[EthAccountUpgradeable]]
=== `++EthAccountUpgradeable++` link:https://github.com/OpenZeppelin/cairo-contracts/blob/release-vX.Y.Z/src/presets/eth_account.cairo[{github-icon},role=heading-link]

```javascript
use openzeppelin::presets::EthAccountUpgradeable;
```

Account contract leveraging xref:#EthAccountComponent[EthAccountComponent].

NOTE: The `EthPublicKey` type is an alias for `starknet::secp256k1::Secp256k1Point`.

include::../utils/_class_hashes.adoc[]

[.contract-index]
.{presets-page}
--
{eth-account-upgradeable-class-hash}
--

[.contract-index]
.Constructor
--
* xref:#EthAccountUpgradeable-constructor[`++constructor(self, public_key)++`]
--

[.contract-index]
.Embedded Implementations
--
.EthAccountComponent

* xref:#EthAccountComponent-Embeddable-Impls[`++SRC6Impl++`]
* xref:#EthAccountComponent-Embeddable-Impls[`++PublicKeyImpl++`]
* xref:#EthAccountComponent-Embeddable-Impls[`++DeclarerImpl++`]
* xref:#EthAccountComponent-Embeddable-Impls[`++DeployableImpl++`]
* xref:#EthAccountComponent-Embeddable-Impls-camelCase[`++SRC6CamelOnlyImpl++`]
* xref:#EthAccountComponent-Embeddable-Impls-camelCase[`++PublicKeyCamelImpl++`]

.SRC5Component

* xref:api/introspection.adoc#SRC5Component-Embeddable-Impls[`++SRC5Impl++`]
--

[.contract-index]
.External Functions
--
* xref:#EthAccountUpgradeable-upgrade[`++upgrade(self, new_class_hash)++`]
--

[#EthAccountUpgradeable-constructor-section]
==== Constructor

[.contract-item]
[[EthAccountUpgradeable-constructor]]
==== `[.contract-item-name]#++constructor++#++(ref self: ContractState, public_key: EthPublicKey)++` [.item-kind]#constructor#

Sets the account `public_key` and registers the interfaces the contract supports.

[#EthAccountUpgradeable-external-functions]
==== External Functions

[.contract-item]
[[EthAccountUpgradeable-upgrade]]
==== `[.contract-item-name]#++upgrade++#++(ref self: ContractState, new_class_hash: ClassHash)++` [.item-kind]#external#

Upgrades the contract to a new implementation given by `new_class_hash`.
10 changes: 7 additions & 3 deletions docs/modules/ROOT/pages/presets.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ include::utils/_class_hashes.adoc[]

List of "ready-to-deploy" presets available and their corresponding class hashes.

:account: xref:/api/account.adoc#Account[Account.cairo]
:erc20: xref:/api/erc20.adoc#ERC20[ERC20.cairo]
:erc721: xref:/api/erc721.adoc#ERC721[ERC721.cairo]
:account: xref:/api/account.adoc#Account[Account]
:eth-account-upgradeable: xref:/api/account.adoc#EthAccountUpgradeable[EthAccountUpgradeable]
:erc20: xref:/api/erc20.adoc#ERC20[ERC20]
:erc721: xref:/api/erc721.adoc#ERC721[ERC721]
:cairo-and-sierra: https://docs.starknet.io/documentation/architecture_and_concepts/Smart_Contracts/cairo-and-sierra/[Cairo and Sierra]

NOTE: Class hashes were computed using {class-hash-cairo-version}.
Expand All @@ -19,6 +20,9 @@ NOTE: Class hashes were computed using {class-hash-cairo-version}.
| `{account}`
| `{account-class-hash}`

| `{eth-account-upgradeable}`
| `{eth-account-upgradeable-class-hash}`

| `{erc20}`
| `{erc20-class-hash}`

Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/utils/_class_hashes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

// Class Hashes
:account-class-hash: 0x016c6081eb34ad1e0c5513234ed0c025b3c7f305902d291bad534cd6474c85bc
:eth-account-upgradeable-class-hash: TBD
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment to remind us to complete this once we have it

:erc20-class-hash: 0x01d7085cee580ba542cdb5709bda80ebfe8bdede664261a3e6af92994d9a1de7
:erc721-class-hash: 0x04376782cbcd20a05f8e742e96150757383dab737ab3e791b8505ad856756907

Expand Down
8 changes: 4 additions & 4 deletions src/account.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod account;
mod dual_account;
mod interface;
mod eth_account;
mod utils;

use account::AccountComponent;
use interface::AccountABIDispatcher;
use interface::AccountABIDispatcherTrait;
use account::dual_account;
use account::interface;