Skip to content
Merged
Show file tree
Hide file tree
Changes from 76 commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
4711f42
Add SignerMultiERC7913 and SignerMultiERC7913Weighted
ernestognw Apr 21, 2025
0717223
Fix pragma
ernestognw Apr 21, 2025
1a2fad4
Fix pragma 2
ernestognw Apr 21, 2025
666ab9b
Add missing tests and fix issue in ERC7913Utils
ernestognw Apr 21, 2025
0e69b4c
Remove .only
ernestognw Apr 21, 2025
0e6eeec
nit
ernestognw Apr 21, 2025
19d2f1e
Update CHANGELOG.md
ernestognw Apr 21, 2025
2875b7c
Merge branch 'master' into feature/signer-multi-erc7913
ernestognw Apr 23, 2025
e812992
Update naming
ernestognw Apr 23, 2025
dd213b4
Fix tests
ernestognw Apr 23, 2025
28c2a99
Update docs/modules/ROOT/pages/multisig-account.adoc
ernestognw Apr 23, 2025
da1f4ff
Update docs/modules/ROOT/pages/multisig-account.adoc
ernestognw Apr 23, 2025
0d53def
Rename errors
ernestognw Apr 23, 2025
26e7e11
Update docs/modules/ROOT/pages/multisig-account.adoc
ernestognw Apr 23, 2025
1ca0f1e
Update docs/modules/ROOT/pages/multisig-account.adoc
ernestognw Apr 23, 2025
2728bff
Implement review suggestions
ernestognw Apr 23, 2025
2ba9a11
Unskip tests
ernestognw Apr 23, 2025
96c6405
Address review comments
ernestognw Apr 24, 2025
cdafc94
up
ernestognw Apr 24, 2025
7c6fbd4
Nits
ernestognw Apr 24, 2025
9634d0f
Fix tests
ernestognw Apr 24, 2025
33ea2c7
fix unreachable threshold bug, minor variable renamings
gonzaotc Apr 24, 2025
b41d3f3
fixed just introduced arithmetic underflow
gonzaotc Apr 24, 2025
e8b1557
Improve consistency of error and event naming
ernestognw Apr 24, 2025
cb22d0f
Update contracts/utils/cryptography/MultiSignerERC7913Weighted.sol
ernestognw Apr 24, 2025
8a424a3
Add extra isSigner function
ernestognw Apr 24, 2025
a8372f6
Pack threshold and totalWeight
ernestognw Apr 24, 2025
30bb001
Update docs
ernestognw Apr 24, 2025
be2e94f
Updates and moar test
ernestognw Apr 25, 2025
19f2912
Add ERC7579 Executor modules
ernestognw Apr 25, 2025
6cfa407
Simplify MultiSignerERC7913Weighted and reorder SignerERC7913
ernestognw Apr 26, 2025
1551e61
Move isValidNSignatures to ERC7913Utils
ernestognw Apr 26, 2025
b63009a
add tests
ernestognw Apr 26, 2025
4ec4be3
Update contracts/utils/cryptography/MultiSignerERC7913Weighted.sol
ernestognw Apr 26, 2025
81e6e26
Add ERC7579SignatureValidator
ernestognw Apr 26, 2025
d316450
Fix tests add docs
ernestognw Apr 26, 2025
e0361ca
Avoid using private _signersSet
ernestognw Apr 26, 2025
aec67c3
Fix tests
ernestognw Apr 26, 2025
822c678
Improve tests
ernestognw Apr 26, 2025
4377963
Up
ernestognw Apr 26, 2025
3a5ca9b
Merge branch 'master' into feature/erc7579-executors
ernestognw Apr 26, 2025
ffd8d23
Rename
ernestognw Apr 27, 2025
03007fa
Nits
ernestognw Apr 27, 2025
df9974e
up
ernestognw Apr 27, 2025
7be01b6
Nit
ernestognw Apr 27, 2025
2061c0c
Merge branch 'chore/erc7913-is-valid-n-signatures' into chore/multisi…
ernestognw Apr 27, 2025
174075e
Merge branch 'chore/multisig-nits' into feature/erc7579-signature-val…
ernestognw Apr 27, 2025
14eca65
Merge branch 'feature/erc7579-signature-validator' into feature/erc75…
ernestognw Apr 27, 2025
c1e0400
Iteration
ernestognw Apr 27, 2025
97a639c
up
ernestognw Apr 27, 2025
7315301
Merge branch 'master' into feature/erc7579-executors
ernestognw May 8, 2025
32051db
Remove unnecessary fil
ernestognw May 8, 2025
6b6bb41
Iterate
ernestognw May 8, 2025
a620544
Nit
ernestognw May 8, 2025
fc3e500
Nit
ernestognw May 8, 2025
636da55
add util function `isModuleInstalled` to avoid repeating
gonzaotc May 8, 2025
d3f8701
inline `isModuleInstalled` call
gonzaotc May 8, 2025
3a6c8bd
Apply suggestions from code review
ernestognw May 9, 2025
f8eb662
Apply review recommendations
ernestognw May 9, 2025
6ae1440
Fix test
ernestognw May 9, 2025
8a3903c
lint
ernestognw May 9, 2025
5f0b16d
Add missing note
ernestognw May 9, 2025
086b3c2
up
ernestognw May 9, 2025
4a8cab5
Add ERC7579MultisigConfirmation
ernestognw May 9, 2025
7c6a28a
Update contracts/account/modules/ERC7579MultisigConfirmation.sol
ernestognw May 9, 2025
baa1974
Add ERC7579Executor tests
ernestognw May 10, 2025
ea52273
Moar tests
ernestognw May 12, 2025
563dfe5
Add expiration to ERC7579MultisigConfirmation
ernestognw May 12, 2025
76b4b63
Update ERC7579DelayedExecutor
ernestognw May 13, 2025
aae8d44
More tests for ERC7579DelayedExecutor
ernestognw May 13, 2025
8e7f2ae
100% coverage delayed executor
ernestognw May 13, 2025
e312d0b
Moar tests
ernestognw May 13, 2025
da9ff1f
Moar tests
ernestognw May 13, 2025
5ec34e0
Moar tests
ernestognw May 13, 2025
94cb88c
nits
ernestognw May 13, 2025
a7389a0
Merge branch 'master' into feature/erc7579-executors
ernestognw May 13, 2025
4422833
Add executors to docs
ernestognw May 13, 2025
3e05f71
Apply suggestions from code review
ernestognw May 13, 2025
7c89c89
fix
ernestognw May 13, 2025
fdf0a50
Reverse order of errors in schedule, execute and cancel
ernestognw May 14, 2025
35324f2
Improve docs
ernestognw May 14, 2025
0065af1
Fix title levels in ERC7579DelayedExecutor
ernestognw May 14, 2025
9ea3743
Change _checkMultisignature for _validateMultisignature
ernestognw May 15, 2025
03f3fe5
Replace can* functions with internal _validate* in executors
ernestognw May 15, 2025
85047df
Reorder
ernestognw May 15, 2025
5557f70
Fix tests
ernestognw May 15, 2025
ab3c0ee
Simplify types
ernestognw May 15, 2025
c251f69
Use data to pack extra information on execution
ernestognw May 15, 2025
ec0ae9e
Change order of arguments to leave bytes data at the end
ernestognw May 15, 2025
700558d
Make schedule and cancel noops when not authorized
ernestognw May 16, 2025
23c683b
Revert "Make schedule and cancel noops when not authorized"
ernestognw May 16, 2025
24dad81
Revert on schedule if the module is not installed
ernestognw May 16, 2025
42a9fcb
Self-review
ernestognw May 16, 2025
0e1db84
Add more tests. Fix off by 1
ernestognw May 16, 2025
0968b48
Missing update
ernestognw May 16, 2025
aa8c0c2
reset libs
ernestognw May 16, 2025
ba3aae4
Codespell
ernestognw May 16, 2025
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
445 changes: 445 additions & 0 deletions contracts/account/modules/ERC7579DelayedExecutor.sol

Large diffs are not rendered by default.

91 changes: 91 additions & 0 deletions contracts/account/modules/ERC7579Executor.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;

import {IERC7579Module, MODULE_TYPE_EXECUTOR, IERC7579Execution} from "@openzeppelin/contracts/interfaces/draft-IERC7579.sol";
import {Mode} from "@openzeppelin/contracts/account/utils/draft-ERC7579Utils.sol";

/**
* @dev Basic implementation for ERC-7579 executor modules that provides execution functionality
* for smart accounts.
*
* The module enables accounts to execute arbitrary operations, leveraging the execution
* capabilities defined in the ERC-7579 standard. By default, the executor is restricted to
* operations initiated by the account itself, but this can be customized in derived contracts
* by overriding the {canExecute} function.
*
* TIP: This is a simplified executor that directly executes operations without delay or expiration
* mechanisms. For a more advanced implementation with time-delayed execution patterns and
* security features, see {ERC7579DelayedExecutor}.
*/
abstract contract ERC7579Executor is IERC7579Module {
/// @dev Emitted when an operation is executed.
event ERC7579ExecutorOperationExecuted(address indexed account, Mode mode, bytes callData, bytes32 salt);

/// @dev Thrown when the executor is uninstalled.
error ERC7579UnauthorizedExecution();

/// @inheritdoc IERC7579Module
function isModuleType(uint256 moduleTypeId) public pure virtual returns (bool) {
return moduleTypeId == MODULE_TYPE_EXECUTOR;
}

/**
* @dev Check if the caller is authorized to execute operations.
* By default, this checks if the caller is the account itself. Derived contracts can
* override this to implement custom authorization logic.
*
* Example extension:
*
* ```
* function canExecute(
* address account,
* Mode mode,
* bytes calldata executionCalldata,
* bytes32 salt
* ) public view virtual returns (bool) {
* bool isAuthorized = ...; // custom logic to check authorization
* return isAuthorized || super.canExecute(account, mode, executionCalldata, salt);
* }
*```
*/
function canExecute(
address account,
Mode /* mode */,
bytes calldata /* executionCalldata */,
bytes32 /* salt */
) public view virtual returns (bool) {
return msg.sender == account;
}

/**
* @dev Executes an operation and returns the result data from the executed operation.
* Restricted to the account itself by default. See {_execute} for requirements and
* {canExecute} for authorization checks.
*/
function execute(
address account,
Mode mode,
bytes calldata executionCalldata,
bytes32 salt
) public virtual returns (bytes[] memory returnData) {
require(canExecute(account, mode, executionCalldata, salt), ERC7579UnauthorizedExecution());
return _execute(account, mode, executionCalldata, salt);
}

/**
* @dev Internal version of {execute}. Emits {ERC7579ExecutorOperationExecuted} event.
*
* Requirements:
*
* * The `account` must implement the {IERC7579Execution-executeFromExecutor} function.
*/
function _execute(
address account,
Mode mode,
bytes calldata executionCalldata,
bytes32 salt
) internal virtual returns (bytes[] memory returnData) {
emit ERC7579ExecutorOperationExecuted(account, mode, executionCalldata, salt);
return IERC7579Execution(account).executeFromExecutor(Mode.unwrap(mode), executionCalldata);
}
}
293 changes: 293 additions & 0 deletions contracts/account/modules/ERC7579Multisig.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;

import {ERC7913Utils} from "../../utils/cryptography/ERC7913Utils.sol";
import {EnumerableSetExtended} from "../../utils/structs/EnumerableSetExtended.sol";
import {IERC7579Module} from "@openzeppelin/contracts/interfaces/draft-IERC7579.sol";
import {Mode} from "@openzeppelin/contracts/account/utils/draft-ERC7579Utils.sol";

/**
* @dev Implementation of an {IERC7579Module} that uses ERC-7913 signers for multisignature
* validation.
*
* This module provides a base implementation for multisignature validation that can be
* attached to any function through the {_checkMultiSignature} internal function. The signers
* are represented using the ERC-7913 format, which concatenates a verifier address and
* a key: `verifier || key`.
*
* Example implementation:
*
* ```solidity
* function execute(
* address account,
* Mode mode,
* bytes calldata executionCalldata,
* bytes32 salt,
* bytes calldata signature
* ) public virtual {
* _checkMultiSignature(account, hash, signature);
* // ... rest of execute logic
* }
* ```
*
* Example use case:
*
* A smart account with this module installed can require multiple signers to approve
* operations before they are executed, such as requiring 3-of-5 guardians to approve
* a social recovery operation.
*/
abstract contract ERC7579Multisig is IERC7579Module {
using EnumerableSetExtended for EnumerableSetExtended.BytesSet;
using ERC7913Utils for bytes32;
using ERC7913Utils for bytes;

/// @dev Emitted when signers are added.
event ERC7913SignersAdded(address indexed account, bytes[] signers);

/// @dev Emitted when signers are removed.
event ERC7913SignersRemoved(address indexed account, bytes[] signers);

/// @dev Emitted when the threshold is updated.
event ERC7913ThresholdSet(address indexed account, uint256 threshold);

/// @dev The `signer` already exists.
error ERC7579MultisigAlreadyExists(bytes signer);

/// @dev The `signer` does not exist.
error ERC7579MultisigNonexistentSigner(bytes signer);

/// @dev The `signer` is less than 20 bytes long.
error ERC7579MultisigInvalidSigner(bytes signer);

/// @dev The `threshold` is unreachable given the number of `signers`.
error ERC7579MultisigUnreachableThreshold(uint256 signers, uint256 threshold);

/// @dev The signatures are invalid.
error ERC7579MultisigInvalidSignatures();

mapping(address account => EnumerableSetExtended.BytesSet) private _signersSetByAccount;
mapping(address account => uint256) private _thresholdByAccount;

/**
* @dev Sets up the module's initial configuration when installed by an account.
* See {ERC7579DelayedExecutor-onInstall}. Besides the delay setup, the `initdata` can
* include `signers` and `threshold`.
*
* The initData should be encoded as:
* `abi.encode(bytes[] signers, uint256 threshold)`
*
* If no signers or threshold are provided, the multisignature functionality will be
* disabled until they are added later.
*
* NOTE: An account can only call onInstall once. If called directly by the account,
* the signer will be set to the provided data. Future installations will behave as a no-op.
*/
function onInstall(bytes calldata initData) public virtual {
if (initData.length > 32 && _signers(msg.sender).length() == 0) {
// More than just delay parameter
(bytes[] memory signers_, uint256 threshold_) = abi.decode(initData, (bytes[], uint256));
_addSigners(msg.sender, signers_);
_setThreshold(msg.sender, threshold_);
}
}

/**
* @dev Cleans up module's configuration when uninstalled from an account.
* Clears all signers and resets the threshold.
*
* See {ERC7579DelayedExecutor-onUninstall}.
*
* WARNING: This function has unbounded gas costs and may become uncallable if the set grows too large.
* See {EnumerableSetExtended-clear}.
*/
function onUninstall(bytes calldata /* data */) public virtual {
_signersSetByAccount[msg.sender].clear();
delete _thresholdByAccount[msg.sender];
}

/**
* @dev Returns the set of authorized signers for the specified account.
*
* WARNING: This operation copies the entire signers set to memory, which
* can be expensive or may result in unbounded computation.
*/
function signers(address account) public view virtual returns (bytes[] memory) {
return _signers(account).values();
}

/// @dev Returns whether the `signer` is an authorized signer for the specified account.
function isSigner(address account, bytes memory signer) public view virtual returns (bool) {
return _signers(account).contains(signer);
}

/// @dev Returns the set of authorized signers for the specified account.
function _signers(address account) internal view virtual returns (EnumerableSetExtended.BytesSet storage) {
return _signersSetByAccount[account];
}

/**
* @dev Returns the minimum number of signers required to approve a multisignature operation
* for the specified account.
*/
function threshold(address account) public view virtual returns (uint256) {
return _thresholdByAccount[account];
}

/**
* @dev Adds new signers to the authorized set for the calling account.
* Can only be called by the account itself.
*
* Requirements:
*
* * Each of `newSigners` must be at least 20 bytes long.
* * Each of `newSigners` must not be already authorized.
*/
function addSigners(bytes[] memory newSigners) public virtual {
_addSigners(msg.sender, newSigners);
}

/**
* @dev Removes signers from the authorized set for the calling account.
* Can only be called by the account itself.
*
* Requirements:
*
* * Each of `oldSigners` must be authorized.
* * After removal, the threshold must still be reachable.
*/
function removeSigners(bytes[] memory oldSigners) public virtual {
_removeSigners(msg.sender, oldSigners);
}

/**
* @dev Sets the threshold for the calling account.
* Can only be called by the account itself.
*
* Requirements:
*
* * The threshold must be reachable with the current number of signers.
*/
function setThreshold(uint256 newThreshold) public virtual {
_setThreshold(msg.sender, newThreshold);
}

/**
* @dev Checks whether the number of valid signatures meets or exceeds the
* threshold set for the target account. Reverts with {ERC7579MultisigInvalidSignatures}
* if the multisignature is not valid.
*
* The signature should be encoded as:
* `abi.encode(bytes[] signingSigners, bytes[] signatures)`
*
* Where `signingSigners` are the authorized signers and signatures are their corresponding
* signatures of the operation `hash`.
*/
function _checkMultiSignature(address account, bytes32 hash, bytes calldata signature) internal view virtual {
(bytes[] memory signingSigners, bytes[] memory signatures) = abi.decode(signature, (bytes[], bytes[]));
require(
_validateThreshold(account, signingSigners) &&
_validateSignatures(account, hash, signingSigners, signatures),
ERC7579MultisigInvalidSignatures()
);
}

/**
* @dev Adds the `newSigners` to those allowed to sign on behalf of the account.
*
* Requirements:
*
* * Each of `newSigners` must be at least 20 bytes long. Reverts with {ERC7579MultisigInvalidSigner} if not.
* * Each of `newSigners` must not be authorized. Reverts with {ERC7579MultisigAlreadyExists} if it already exists.
*/
function _addSigners(address account, bytes[] memory newSigners) internal virtual {
uint256 newSignersLength = newSigners.length;
for (uint256 i = 0; i < newSignersLength; i++) {
bytes memory signer = newSigners[i];
require(signer.length >= 20, ERC7579MultisigInvalidSigner(signer));
require(_signers(account).add(signer), ERC7579MultisigAlreadyExists(signer));
}
emit ERC7913SignersAdded(account, newSigners);
}

/**
* @dev Removes the `oldSigners` from the authorized signers for the account.
*
* Requirements:
*
* * Each of `oldSigners` must be authorized. Reverts with {ERC7579MultisigNonexistentSigner} if not.
* * The threshold must remain reachable after removal. See {_validateReachableThreshold} for details.
*/
function _removeSigners(address account, bytes[] memory oldSigners) internal virtual {
uint256 oldSignersLength = oldSigners.length;
for (uint256 i = 0; i < oldSignersLength; i++) {
bytes memory signer = oldSigners[i];
require(_signers(account).remove(signer), ERC7579MultisigNonexistentSigner(signer));
}
_validateReachableThreshold(account);
emit ERC7913SignersRemoved(account, oldSigners);
}

/**
* @dev Sets the signatures `threshold` required to approve a multisignature operation.
*
* Requirements:
*
* * The threshold must be reachable with the current number of signers. See {_validateReachableThreshold} for details.
*/
function _setThreshold(address account, uint256 newThreshold) internal virtual {
_thresholdByAccount[account] = newThreshold;
_validateReachableThreshold(account);
emit ERC7913ThresholdSet(account, newThreshold);
}

/**
* @dev Validates the current threshold is reachable with the number of {signers}.
*
* Requirements:
*
* * The number of signers must be >= the threshold. Reverts with {ERC7579MultisigUnreachableThreshold} if not.
*/
function _validateReachableThreshold(address account) internal view virtual {
uint256 totalSigners = _signers(account).length();
uint256 currentThreshold = threshold(account);
require(totalSigners >= currentThreshold, ERC7579MultisigUnreachableThreshold(totalSigners, currentThreshold));
}

/**
* @dev Validates the signatures using the signers and their corresponding signatures.
* Returns whether the signers are authorized and the signatures are valid for the given hash.
*
* The signers must be ordered by their `keccak256` hash to prevent duplications and to optimize
* the verification process. The function will return `false` if any signer is not authorized or
* if the signatures are invalid for the given hash.
*
* Requirements:
*
* * The `signatures` array must be at least the `signers` array's length.
*/
function _validateSignatures(
address account,
bytes32 hash,
bytes[] memory signingSigners,
bytes[] memory signatures
) internal view virtual returns (bool valid) {
uint256 signersLength = signingSigners.length;
for (uint256 i = 0; i < signersLength; i++) {
if (!isSigner(account, signingSigners[i])) {
return false;
}
}
return hash.areValidSignaturesNow(signingSigners, signatures);
}

/**
* @dev Validates that the number of signers meets the {threshold} requirement.
* Assumes the signers were already validated. See {_validateSignatures} for more details.
*/
function _validateThreshold(
address account,
bytes[] memory validatingSigners
) internal view virtual returns (bool) {
return validatingSigners.length >= threshold(account);
}
}
Loading