Account

这个 crate 提供了实现可以用于与网络交互的账户合约的组件。

Core

ISRC6

use openzeppelin_account::interface::ISRC6;

SNIP-6 中定义的 SRC6 标准账户的接口。

0x2ceccef7f994940b3962a6c67e0ba4fcd37df7d131417c604f91e03caecc1cd

函数

__execute__(calls: Array<Call>) external

在验证后,将调用列表作为交易执行。

Call 结构体定义在 corelib 中。

__validate__(calls: Array<Call>) → felt252 external

在执行前验证一个交易。

如果有效,则返回短字符串 'VALID',否则恢复。

is_valid_signature(hash: felt252, signature: Array<felt252>) → felt252 external

验证给定消息哈希的签名是否有效。

如果有效,则返回短字符串 'VALID',否则恢复。

AccountComponent

use openzeppelin_account::AccountComponent;

实现 Starknet 曲线 上签名的 ISRC6 的账户组件。

实施 SRC5Component 是本组件需要实现的要求。

可嵌入的函数

__execute__(self: @ContractState, calls: Array<Call>) external

参见 ISRC6::__execute__

__validate__(self: @ContractState, calls: Array<Call>) → felt252 external

is_valid_signature(self: @ContractState, hash: felt252, signature: Array<felt252>) → felt252 external

__validate_declare__(self: @ContractState, class_hash: felt252) → felt252 external

验证一个 Declare 交易

如果有效,则返回短字符串 'VALID',否则恢复。

__validate_deploy__(self: @ContractState, class_hash: felt252, contract_address_salt: felt252, public_key: felt252) → felt252 external

如果有效,则返回短字符串 'VALID',否则恢复。

get_public_key(self: @ContractState) → felt252 external

返回账户当前的公钥。

set_public_key(ref self: ContractState, new_public_key: felt252, signature: Span<felt252>) external

为账户设置一个新的公钥。只能通过 __execute__ 由账户本身调用。

要求:

  • 调用者必须是合约本身。

  • 签名对于新所有者必须有效。

发出 OwnerRemovedOwnerAdded 事件。

要在 Cairo 中签名的消息计算如下:

let message_hash = PoseidonTrait::new()
        .update_with('StarkNet Message')
        .update_with('accept_ownership')
        .update_with(get_contract_address())
        .update_with(current_owner)
        .finalize();

isValidSignature(self: @ContractState, hash: felt252, signature: Array<felt252>) → felt252 external

getPublicKey(self: @ContractState) → felt252 external

参见 get_public_key

setPublicKey(ref self: ContractState, newPublicKey: felt252, signature: Span<felt252>) external

参见 set_public_key

内部函数

initializer(ref self: ComponentState, public_key: felt252) internal

使用给定的公钥初始化账户,并注册 ISRC6 接口 ID。

发出一个 OwnerAdded 事件。

assert_only_self(self: @ComponentState) internal

验证调用者是否是账户本身。否则会恢复。

assert_valid_new_owner(self: @ComponentState, current_owner: felt252, new_owner: felt252, signature: Span<felt252>) internal

验证 new_owner 是否通过签名接受了合约的所有权。

要求:

  • 对于新所有者,signature 必须有效。

此函数假定 current_owner 是合约的当前所有者,并且不会验证此假设。

validate_transaction(self: @ComponentState) → felt252 internal

验证来自 全局上下文的交易签名。

如果有效,则返回短字符串 'VALID',否则恢复。

_set_public_key(ref self: ComponentState, new_public_key: felt252) internal

设置公钥而不验证调用者。

发出一个 OwnerAdded 事件。

不鼓励在 set_public_key 函数之外使用此方法。

_is_valid_signature(self: @ComponentState, hash: felt252, signature: Span<felt252>) → bool internal

使用账户当前的公钥验证提供的 signature 是否对 hash 有效。

事件

OwnerAdded(new_owner_guid: felt252) event

添加 public_key 时发出。

OwnerRemoved(removed_owner_guid: felt252) event

删除 public_key 时发出。

EthAccountComponent

use openzeppelin_account::eth_account::EthAccountComponent;

实现 Secp256k1 曲线 上签名的 ISRC6 的账户组件。

实施 SRC5Component 是本组件需要实现的要求。
EthPublicKey 类型是 starknet::secp256k1::Secp256k1Point 的别名。

可嵌入的函数

__execute__(self: @ContractState, calls: Array<Call>) external

参见 ISRC6::__execute__

__validate__(self: @ContractState, calls: Array<Call>) → felt252 external

is_valid_signature(self: @ContractState, hash: felt252, signature: Array<felt252>) → felt252 external

__validate_declare__(self: @ContractState, class_hash: felt252) → felt252 external

验证一个 Declare 交易

如果有效,则返回短字符串 'VALID',否则恢复。

__validate_deploy__(self: @ContractState, class_hash: felt252, contract_address_salt: felt252, public_key: EthPublicKey) → felt252 external

如果有效,则返回短字符串 'VALID',否则恢复。

get_public_key(self: @ContractState) → EthPublicKey external

返回账户当前的公钥。

set_public_key(ref self: ContractState, new_public_key: EthPublicKey, signature: Span<felt252>) external

为账户设置一个新的公钥。只能通过 __execute__ 由账户本身调用。

要求:

  • 调用者必须是合约本身。

  • 签名对于新所有者必须有效。

发出 OwnerRemovedOwnerAdded 事件。

要在 Cairo 中签名的消息计算如下:

let message_hash = PoseidonTrait::new()
        .update_with('StarkNet Message')
        .update_with('accept_ownership')
        .update_with(get_contract_address())
        .update_with(current_owner.get_coordinates().unwrap_syscall())
        .finalize();

isValidSignature(self: @ContractState, hash: felt252, signature: Array<felt252>) → felt252 external

getPublicKey(self: @ContractState) → EthPublicKey external

参见 get_public_key

setPublicKey(ref self: ContractState, newPublicKey: EthPublicKey, signature: Span<felt252>) external

参见 set_public_key

内部函数

initializer(ref self: ComponentState, public_key: EthPublicKey) internal

使用给定的公钥初始化账户,并注册 ISRC6 接口 ID。

发出一个 OwnerAdded 事件。

assert_only_self(self: @ComponentState) internal

验证调用者是否是账户本身。否则会恢复。

assert_valid_new_owner(self: @ComponentState, current_owner: EthPublicKey, new_owner: EthPublicKey, signature: Span<felt252>) internal

验证 new_owner 是否通过签名接受了合约的所有权。

要求:

  • 对于 new_ownersignature 必须有效。

此函数假定 current_owner 是合约的当前所有者,并且不会验证此假设。

validate_transaction(self: @ComponentState) → felt252 internal

验证来自 全局上下文的交易签名。

如果有效,则返回短字符串 'VALID',否则恢复。

_set_public_key(ref self: ComponentState, new_public_key: EthPublicKey) internal

设置公钥而不验证调用者。

发出一个 OwnerAdded 事件。

不鼓励在 set_public_key 函数之外使用此方法。

_is_valid_signature(self: @ComponentState, hash: felt252, signature: Span<felt252>) → bool internal

使用账户当前的公钥验证提供的 signature 是否对 hash 有效。

事件

guid 计算为公钥的哈希值,使用poseidon哈希函数。

OwnerAdded(new_owner_guid: felt252) event

添加 public_key 时发出。

OwnerRemoved(removed_owner_guid: felt252) event

删除 public_key 时发出。

Extensions

ISRC9_V2

use openzeppelin_account::extensions::src9::ISRC9_V2;

SNIP-9 中定义的 SRC9 标准的接口。

0x1d1144bb2138366ff28d8e9ab57456b1d332ac42196230c3a602003c89872

函数

execute_from_outside_v2(outside_execution: OutsideExecution, signature: Span<felt252>,) → Array<Span<felt252>> external

只要拥有相关的签名,就允许任何人代表账户提交交易。

此方法允许重入。调用 __execute__execute_from_outside_v2 可以触发另一个嵌套的 execute_from_outside_v2 交易,因此实现必须验证提供的 signatureoutside_execution 的哈希匹配,并且 nonce 尚未被使用。

该实现应期望在域分隔符中将版本设置为 2。

Arguments:

  • outside_execution - 要执行的交易的参数。

  • signature - 在 outside_executionSNIP-12 消息编码上的有效签名。

is_valid_outside_execution_nonce(nonce: felt252) → bool external

获取给定 nonce 的状态。如果 nonce 可用,则为 true

SRC9Component

use openzeppelin_account::extensions::SRC9Component;

实现 ISRC9_V2 的 OutsideExecution 组件。

此组件与签名无关,这意味着它可以集成到任何账户合约中,只要该账户实现了 ISRC6 接口。
内部实现
InternalImpl

可嵌入的函数

execute_from_outside_v2(ref self: ContractState, outside_execution: OutsideExecution, signature: Span<felt252>) → Array<Span<felt252>> external

只要他们拥有相关的签名,就允许任何人代表账户提交交易。

此方法允许重入。调用 __execute__execute_from_outside_v2 可以触发另一个嵌套的 execute_from_outside_v2 交易。此实现验证提供的 signatureoutside_execution 的哈希匹配,并且 nonce 尚未被使用。

Arguments:

  • outside_execution - 要执行的交易的参数。

  • signature - 在 outside_executionSNIP-12 消息编码上的有效签名。

要求:

  • 调用者必须是 outside_execution.caller,除非使用 'ANY_CALLER'。

  • 当前时间必须在 outside_execution.execute_afteroutside_execution.execute_before 跨度内。

  • outside_execution.nonce 之前不能使用过。

  • signature 必须有效。

is_valid_outside_execution_nonce(self: @ContractState, nonce: felt252) → bool external

返回给定 nonce 的状态。如果 nonce 可用,则为 true

内部函数

initializer(ref self: ComponentState) internal

通过注册 ISRC9_V2 接口 ID 初始化账户。

Presets

AccountUpgradeable

use openzeppelin_presets::AccountUpgradeable;

可升级的账户,可以更改其公钥并声明、部署或调用合约。通过实现 .contract-index .{presets-page}

{EthAccountUpgradeable-class-hash}

嵌入式实现
EthAccountComponent
SRC9Component

构造函数

constructor(ref self: ContractState, public_key: EthPublicKey) constructor

设置账户 public_key 并注册合约支持的接口。

外部函数

upgrade(ref self: ContractState, new_class_hash: ClassHash) external

将合约升级到由 new_class_hash 给出的新实现。

要求:

  • 调用者是账户合约本身。

  • new_class_hash 不能为零。