-
Notifications
You must be signed in to change notification settings - Fork 24
Add ERC7579 Executor modules #121
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
Merged
Merged
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 0717223
Fix pragma
ernestognw 1a2fad4
Fix pragma 2
ernestognw 666ab9b
Add missing tests and fix issue in ERC7913Utils
ernestognw 0e69b4c
Remove .only
ernestognw 0e6eeec
nit
ernestognw 19d2f1e
Update CHANGELOG.md
ernestognw 2875b7c
Merge branch 'master' into feature/signer-multi-erc7913
ernestognw e812992
Update naming
ernestognw dd213b4
Fix tests
ernestognw 28c2a99
Update docs/modules/ROOT/pages/multisig-account.adoc
ernestognw da1f4ff
Update docs/modules/ROOT/pages/multisig-account.adoc
ernestognw 0d53def
Rename errors
ernestognw 26e7e11
Update docs/modules/ROOT/pages/multisig-account.adoc
ernestognw 1ca0f1e
Update docs/modules/ROOT/pages/multisig-account.adoc
ernestognw 2728bff
Implement review suggestions
ernestognw 2ba9a11
Unskip tests
ernestognw 96c6405
Address review comments
ernestognw cdafc94
up
ernestognw 7c6fbd4
Nits
ernestognw 9634d0f
Fix tests
ernestognw 33ea2c7
fix unreachable threshold bug, minor variable renamings
gonzaotc b41d3f3
fixed just introduced arithmetic underflow
gonzaotc e8b1557
Improve consistency of error and event naming
ernestognw cb22d0f
Update contracts/utils/cryptography/MultiSignerERC7913Weighted.sol
ernestognw 8a424a3
Add extra isSigner function
ernestognw a8372f6
Pack threshold and totalWeight
ernestognw 30bb001
Update docs
ernestognw be2e94f
Updates and moar test
ernestognw 19f2912
Add ERC7579 Executor modules
ernestognw 6cfa407
Simplify MultiSignerERC7913Weighted and reorder SignerERC7913
ernestognw 1551e61
Move isValidNSignatures to ERC7913Utils
ernestognw b63009a
add tests
ernestognw 4ec4be3
Update contracts/utils/cryptography/MultiSignerERC7913Weighted.sol
ernestognw 81e6e26
Add ERC7579SignatureValidator
ernestognw d316450
Fix tests add docs
ernestognw e0361ca
Avoid using private _signersSet
ernestognw aec67c3
Fix tests
ernestognw 822c678
Improve tests
ernestognw 4377963
Up
ernestognw 3a5ca9b
Merge branch 'master' into feature/erc7579-executors
ernestognw ffd8d23
Rename
ernestognw 03007fa
Nits
ernestognw df9974e
up
ernestognw 7be01b6
Nit
ernestognw 2061c0c
Merge branch 'chore/erc7913-is-valid-n-signatures' into chore/multisi…
ernestognw 174075e
Merge branch 'chore/multisig-nits' into feature/erc7579-signature-val…
ernestognw 14eca65
Merge branch 'feature/erc7579-signature-validator' into feature/erc75…
ernestognw c1e0400
Iteration
ernestognw 97a639c
up
ernestognw 7315301
Merge branch 'master' into feature/erc7579-executors
ernestognw 32051db
Remove unnecessary fil
ernestognw 6b6bb41
Iterate
ernestognw a620544
Nit
ernestognw fc3e500
Nit
ernestognw 636da55
add util function `isModuleInstalled` to avoid repeating
gonzaotc d3f8701
inline `isModuleInstalled` call
gonzaotc 3a6c8bd
Apply suggestions from code review
ernestognw f8eb662
Apply review recommendations
ernestognw 6ae1440
Fix test
ernestognw 8a3903c
lint
ernestognw 5f0b16d
Add missing note
ernestognw 086b3c2
up
ernestognw 4a8cab5
Add ERC7579MultisigConfirmation
ernestognw 7c6a28a
Update contracts/account/modules/ERC7579MultisigConfirmation.sol
ernestognw baa1974
Add ERC7579Executor tests
ernestognw ea52273
Moar tests
ernestognw 563dfe5
Add expiration to ERC7579MultisigConfirmation
ernestognw 76b4b63
Update ERC7579DelayedExecutor
ernestognw aae8d44
More tests for ERC7579DelayedExecutor
ernestognw 8e7f2ae
100% coverage delayed executor
ernestognw e312d0b
Moar tests
ernestognw da9ff1f
Moar tests
ernestognw 5ec34e0
Moar tests
ernestognw 94cb88c
nits
ernestognw a7389a0
Merge branch 'master' into feature/erc7579-executors
ernestognw 4422833
Add executors to docs
ernestognw 3e05f71
Apply suggestions from code review
ernestognw 7c89c89
fix
ernestognw fdf0a50
Reverse order of errors in schedule, execute and cancel
ernestognw 35324f2
Improve docs
ernestognw 0065af1
Fix title levels in ERC7579DelayedExecutor
ernestognw 9ea3743
Change _checkMultisignature for _validateMultisignature
ernestognw 03f3fe5
Replace can* functions with internal _validate* in executors
ernestognw 85047df
Reorder
ernestognw 5557f70
Fix tests
ernestognw ab3c0ee
Simplify types
ernestognw c251f69
Use data to pack extra information on execution
ernestognw ec0ae9e
Change order of arguments to leave bytes data at the end
ernestognw 700558d
Make schedule and cancel noops when not authorized
ernestognw 23c683b
Revert "Make schedule and cancel noops when not authorized"
ernestognw 24dad81
Revert on schedule if the module is not installed
ernestognw 42a9fcb
Self-review
ernestognw 0e1db84
Add more tests. Fix off by 1
ernestognw 0968b48
Missing update
ernestognw aa8c0c2
reset libs
ernestognw ba3aae4
Codespell
ernestognw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.