Skip to content

Commit

Permalink
Add src9 component to account presets (#1201)
Browse files Browse the repository at this point in the history
* feat: add src9 component to account presets

* feat: apply review update

* feat: update CHANGELOG
  • Loading branch information
ericnordelo authored Nov 8, 2024
1 parent e1c0952 commit 6e3d8aa
Show file tree
Hide file tree
Showing 10 changed files with 505 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- SRC9 (Outside Execution) integration to account presets (#1201)

## 0.19.0 (2024-11-08)

### Added
Expand Down
14 changes: 10 additions & 4 deletions docs/modules/ROOT/pages/api/account.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,8 @@ Initializes the account by registering the `ISRC9_V2` interface Id.
use openzeppelin_presets::AccountUpgradeable;
```

Upgradeable account contract leveraging xref:#AccountComponent[AccountComponent].
Upgradeable account which can change its public key and declare, deploy, or call
contracts. Supports outside execution by implementing xref:#SRC9Component[SRC9].

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

Expand All @@ -724,8 +725,10 @@ include::../utils/_class_hashes.adoc[]
.Embedded Implementations
--
.AccountComponent

* xref:#AccountComponent-Embeddable-Mixin-Impl[`++AccountMixinImpl++`]

.SRC9Component
* xref:#SRC9Component-Embeddable-Impls-OutsideExecutionV2Impl[`++OutsideExecutionV2Impl++`]
--

[.contract-index]
Expand Down Expand Up @@ -765,7 +768,8 @@ Requirements:
use openzeppelin_presets::EthAccountUpgradeable;
```

Upgradeable account contract leveraging xref:#EthAccountComponent[EthAccountComponent].
Upgradeable account which can change its public key and declare, deploy, or call contracts, using Ethereum
signing keys. Supports outside execution by implementing xref:#SRC9Component[SRC9].

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

Expand All @@ -787,8 +791,10 @@ include::../utils/_class_hashes.adoc[]
.Embedded Implementations
--
.EthAccountComponent

* xref:#EthAccountComponent-Embeddable-Mixin-Impl[`++EthAccountMixinImpl++`]

.SRC9Component
* xref:#SRC9Component-Embeddable-Impls-OutsideExecutionV2Impl[`++OutsideExecutionV2Impl++`]
--

[.contract-index]
Expand Down
1 change: 1 addition & 0 deletions packages/presets/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ build-external-contracts = [
"openzeppelin_test_common::mocks::account::DualCaseAccountMock",
"openzeppelin_test_common::mocks::account::SnakeAccountMock",
"openzeppelin_test_common::mocks::account::SnakeEthAccountMock",
"openzeppelin_test_common::mocks::simple::SimpleMock",
"openzeppelin_test_common::mocks::erc20::DualCaseERC20Mock",
"openzeppelin_test_common::mocks::erc20::SnakeERC20Mock",
"openzeppelin_test_common::mocks::erc721::SnakeERC721Mock",
Expand Down
15 changes: 14 additions & 1 deletion packages/presets/src/account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
/// # Account Preset
///
/// OpenZeppelin's upgradeable account which can change its public key and declare, deploy, or call
/// contracts.
/// contracts. Supports outside execution by implementing SRC9.
#[starknet::contract(account)]
pub mod AccountUpgradeable {
use openzeppelin_account::AccountComponent;
use openzeppelin_account::extensions::SRC9Component;
use openzeppelin_introspection::src5::SRC5Component;
use openzeppelin_upgrades::UpgradeableComponent;
use openzeppelin_upgrades::interface::IUpgradeable;
use starknet::ClassHash;

component!(path: AccountComponent, storage: account, event: AccountEvent);
component!(path: SRC5Component, storage: src5, event: SRC5Event);
component!(path: SRC9Component, storage: src9, event: SRC9Event);
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);

// Account Mixin
Expand All @@ -23,6 +25,12 @@ pub mod AccountUpgradeable {
AccountComponent::AccountMixinImpl<ContractState>;
impl AccountInternalImpl = AccountComponent::InternalImpl<ContractState>;

// SRC9
#[abi(embed_v0)]
impl OutsideExecutionV2Impl =
SRC9Component::OutsideExecutionV2Impl<ContractState>;
impl OutsideExecutionInternalImpl = SRC9Component::InternalImpl<ContractState>;

// Upgradeable
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

Expand All @@ -33,6 +41,8 @@ pub mod AccountUpgradeable {
#[substorage(v0)]
pub src5: SRC5Component::Storage,
#[substorage(v0)]
pub src9: SRC9Component::Storage,
#[substorage(v0)]
pub upgradeable: UpgradeableComponent::Storage
}

Expand All @@ -44,12 +54,15 @@ pub mod AccountUpgradeable {
#[flat]
SRC5Event: SRC5Component::Event,
#[flat]
SRC9Event: SRC9Component::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event
}

#[constructor]
pub fn constructor(ref self: ContractState, public_key: felt252) {
self.account.initializer(public_key);
self.src9.initializer();
}

#[abi(embed_v0)]
Expand Down
16 changes: 15 additions & 1 deletion packages/presets/src/eth_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
/// # EthAccount Preset
///
/// OpenZeppelin's upgradeable account which can change its public key and declare,
/// deploy, or call contracts, using Ethereum signing keys.
/// deploy, or call contracts, using Ethereum signing keys. Supports outside execution by
/// implementing SRC9.
#[starknet::contract(account)]
pub(crate) mod EthAccountUpgradeable {
use openzeppelin_account::EthAccountComponent;
use openzeppelin_account::extensions::SRC9Component;
use openzeppelin_account::interface::EthPublicKey;
use openzeppelin_introspection::src5::SRC5Component;
use openzeppelin_upgrades::UpgradeableComponent;
Expand All @@ -16,6 +18,7 @@ pub(crate) mod EthAccountUpgradeable {

component!(path: EthAccountComponent, storage: eth_account, event: EthAccountEvent);
component!(path: SRC5Component, storage: src5, event: SRC5Event);
component!(path: SRC9Component, storage: src9, event: SRC9Event);
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);

// EthAccount Mixin
Expand All @@ -24,6 +27,12 @@ pub(crate) mod EthAccountUpgradeable {
EthAccountComponent::EthAccountMixinImpl<ContractState>;
impl EthAccountInternalImpl = EthAccountComponent::InternalImpl<ContractState>;

// SRC9
#[abi(embed_v0)]
impl OutsideExecutionV2Impl =
SRC9Component::OutsideExecutionV2Impl<ContractState>;
impl OutsideExecutionInternalImpl = SRC9Component::InternalImpl<ContractState>;

// Upgradeable
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

Expand All @@ -34,6 +43,8 @@ pub(crate) mod EthAccountUpgradeable {
#[substorage(v0)]
pub src5: SRC5Component::Storage,
#[substorage(v0)]
pub src9: SRC9Component::Storage,
#[substorage(v0)]
pub upgradeable: UpgradeableComponent::Storage
}

Expand All @@ -45,12 +56,15 @@ pub(crate) mod EthAccountUpgradeable {
#[flat]
SRC5Event: SRC5Component::Event,
#[flat]
SRC9Event: SRC9Component::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event
}

#[constructor]
pub(crate) fn constructor(ref self: ContractState, public_key: EthPublicKey) {
self.eth_account.initializer(public_key);
self.src9.initializer();
}

#[abi(embed_v0)]
Expand Down
7 changes: 7 additions & 0 deletions packages/presets/src/interfaces/account.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use openzeppelin_account::extensions::src9::interface::OutsideExecution;
use starknet::ClassHash;
use starknet::account::Call;

Expand All @@ -11,6 +12,12 @@ pub trait AccountUpgradeableABI<TState> {
// ISRC5
fn supports_interface(self: @TState, interface_id: felt252) -> bool;

// ISRC9
fn execute_from_outside_v2(
ref self: TState, outside_execution: OutsideExecution, signature: Span<felt252>,
) -> Array<Span<felt252>>;
fn is_valid_outside_execution_nonce(self: @TState, nonce: felt252) -> bool;

// IDeclarer
fn __validate_declare__(self: @TState, class_hash: felt252) -> felt252;

Expand Down
7 changes: 7 additions & 0 deletions packages/presets/src/interfaces/eth_account.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use openzeppelin_account::extensions::src9::interface::OutsideExecution;
use openzeppelin_account::interface::EthPublicKey;
use starknet::ClassHash;
use starknet::account::Call;
Expand All @@ -12,6 +13,12 @@ pub trait EthAccountUpgradeableABI<TState> {
// ISRC5
fn supports_interface(self: @TState, interface_id: felt252) -> bool;

// ISRC9
fn execute_from_outside_v2(
ref self: TState, outside_execution: OutsideExecution, signature: Span<felt252>,
) -> Array<Span<felt252>>;
fn is_valid_outside_execution_nonce(self: @TState, nonce: felt252) -> bool;

// IDeclarer
fn __validate_declare__(self: @TState, class_hash: felt252) -> felt252;

Expand Down
Loading

0 comments on commit 6e3d8aa

Please sign in to comment.