-
Notifications
You must be signed in to change notification settings - Fork 25
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
feat: [v0.8-develop, experimental] default validation #63
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[profile.default] | ||
solc = '0.8.25' | ||
solc = '0.8.26' | ||
via_ir = false | ||
src = 'src' | ||
test = 'test' | ||
|
This file contains 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
This file contains 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,67 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.25; | ||
|
||
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; | ||
|
||
import {IPlugin} from "../interfaces/IPlugin.sol"; | ||
import {FunctionReference} from "../interfaces/IPluginManager.sol"; | ||
import {FunctionReferenceLib} from "../helpers/FunctionReferenceLib.sol"; | ||
import {AccountStorage, getAccountStorage, toSetValue} from "./AccountStorage.sol"; | ||
|
||
abstract contract PluginManager2 { | ||
huaweigu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
using EnumerableSet for EnumerableSet.Bytes32Set; | ||
|
||
error DefaultValidationAlreadySet(address plugin, uint8 functionId); | ||
error ValidationAlreadySet(bytes4 selector, address plugin, uint8 functionId); | ||
error ValidationNotSet(bytes4 selector, address plugin, uint8 functionId); | ||
|
||
function _installValidation( | ||
address plugin, | ||
huaweigu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uint8 functionId, | ||
bool shared, | ||
bytes4[] memory selectors, | ||
bytes calldata installData | ||
) internal { | ||
FunctionReference validationFunction = FunctionReferenceLib.pack(plugin, functionId); | ||
|
||
AccountStorage storage _storage = getAccountStorage(); | ||
|
||
if (shared) { | ||
if (!_storage.defaultValidations.add(toSetValue(validationFunction))) { | ||
revert DefaultValidationAlreadySet(plugin, functionId); | ||
} | ||
} | ||
|
||
for (uint256 i = 0; i < selectors.length; ++i) { | ||
bytes4 selector = selectors[i]; | ||
if (!_storage.selectorData[selector].validations.add(toSetValue(validationFunction))) { | ||
revert ValidationAlreadySet(selector, plugin, functionId); | ||
} | ||
} | ||
|
||
IPlugin(plugin).onInstall(installData); | ||
} | ||
|
||
function _uninstallValidation( | ||
howydev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
address plugin, | ||
uint8 functionId, | ||
bytes4[] calldata selectors, | ||
bytes calldata uninstallData | ||
) internal { | ||
FunctionReference validationFunction = FunctionReferenceLib.pack(plugin, functionId); | ||
|
||
AccountStorage storage _storage = getAccountStorage(); | ||
|
||
// Ignore return value - remove if present, do nothing otherwise. | ||
_storage.defaultValidations.remove(toSetValue(validationFunction)); | ||
adamegyed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for (uint256 i = 0; i < selectors.length; ++i) { | ||
bytes4 selector = selectors[i]; | ||
if (!_storage.selectorData[selector].validations.remove(toSetValue(validationFunction))) { | ||
revert ValidationNotSet(selector, plugin, functionId); | ||
} | ||
} | ||
|
||
IPlugin(plugin).onUninstall(uninstallData); | ||
} | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -30,6 +30,7 @@ import { | |
} from "./AccountStorage.sol"; | ||
import {AccountStorageInitializable} from "./AccountStorageInitializable.sol"; | ||
import {PluginManagerInternals} from "./PluginManagerInternals.sol"; | ||
import {PluginManager2} from "./PluginManager2.sol"; | ||
|
||
contract UpgradeableModularAccount is | ||
AccountExecutor, | ||
|
@@ -41,6 +42,7 @@ contract UpgradeableModularAccount is | |
IPluginExecutor, | ||
IStandardExecutor, | ||
PluginManagerInternals, | ||
PluginManager2, | ||
UUPSUpgradeable | ||
{ | ||
using EnumerableSet for EnumerableSet.Bytes32Set; | ||
|
@@ -77,6 +79,7 @@ contract UpgradeableModularAccount is | |
error UnexpectedAggregator(address plugin, uint8 functionId, address aggregator); | ||
error UnrecognizedFunction(bytes4 selector); | ||
error UserOpValidationFunctionMissing(bytes4 selector); | ||
error ValidationDoesNotApply(bytes4 selector, address plugin, uint8 functionId, bool shared); | ||
|
||
// Wraps execution of a native function with runtime validation and hooks | ||
// Used for upgradeTo, upgradeToAndCall, execute, executeBatch, installPlugin, uninstallPlugin | ||
|
@@ -279,11 +282,12 @@ contract UpgradeableModularAccount is | |
if (_storage.selectorData[execSelector].denyExecutionCount > 0) { | ||
revert AlwaysDenyRule(); | ||
} | ||
if (!_storage.selectorData[execSelector].validations.contains(toSetValue(runtimeValidationFunction))) { | ||
revert RuntimeValidationFunctionMissing(execSelector); | ||
} | ||
|
||
_doRuntimeValidation(runtimeValidationFunction, data, authorization[21:]); | ||
// Check if the runtime validation function is allowed to be called | ||
bool isSharedValidation = uint8(authorization[21]) == 1; | ||
_checkIfValidationApplies(execSelector, runtimeValidationFunction, isSharedValidation); | ||
|
||
_doRuntimeValidation(runtimeValidationFunction, data, authorization[22:]); | ||
|
||
// If runtime validation passes, execute the call | ||
|
||
|
@@ -325,6 +329,40 @@ contract UpgradeableModularAccount is | |
_uninstallPlugin(plugin, manifest, pluginUninstallData); | ||
} | ||
|
||
/// @notice Initializes the account with a validation function added to the default pool. | ||
/// TODO: remove and merge with regular initialization, after we figure out a better install/uninstall workflow | ||
/// with user install configs. | ||
/// @dev This function is only callable once, and only by the EntryPoint. | ||
|
||
function initializeDefaultValidation(address plugin, uint8 functionId, bytes calldata installData) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
external | ||
initializer | ||
{ | ||
_installValidation(plugin, functionId, true, new bytes4[](0), installData); | ||
emit ModularAccountInitialized(_ENTRY_POINT); | ||
} | ||
|
||
/// @inheritdoc IPluginManager | ||
function installValidation( | ||
adamegyed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
address plugin, | ||
uint8 functionId, | ||
bool shared, | ||
bytes4[] calldata selectors, | ||
bytes calldata installData | ||
) external wrapNativeFunction { | ||
_installValidation(plugin, functionId, shared, selectors, installData); | ||
} | ||
|
||
/// @inheritdoc IPluginManager | ||
function uninstallValidation( | ||
address plugin, | ||
uint8 functionId, | ||
bytes4[] calldata selectors, | ||
bytes calldata uninstallData | ||
) external wrapNativeFunction { | ||
_uninstallValidation(plugin, functionId, selectors, uninstallData); | ||
} | ||
|
||
/// @notice ERC165 introspection | ||
/// @dev returns true for `IERC165.interfaceId` and false for `0xFFFFFFFF` | ||
/// @param interfaceId interface id to check against | ||
|
@@ -398,14 +436,12 @@ contract UpgradeableModularAccount is | |
|
||
// Revert if the provided `authorization` less than 21 bytes long, rather than right-padding. | ||
FunctionReference userOpValidationFunction = FunctionReference.wrap(bytes21(userOp.signature[:21])); | ||
bool isSharedValidation = uint8(userOp.signature[21]) == 1; | ||
|
||
if (!getAccountStorage().selectorData[selector].validations.contains(toSetValue(userOpValidationFunction))) | ||
{ | ||
revert UserOpValidationFunctionMissing(selector); | ||
} | ||
_checkIfValidationApplies(selector, userOpValidationFunction, isSharedValidation); | ||
|
||
validationData = | ||
_doUserOpValidation(selector, userOpValidationFunction, userOp, userOp.signature[21:], userOpHash); | ||
_doUserOpValidation(selector, userOpValidationFunction, userOp, userOp.signature[22:], userOpHash); | ||
} | ||
|
||
// To support gas estimation, we don't fail early when the failure is caused by a signature failure | ||
|
@@ -573,6 +609,41 @@ contract UpgradeableModularAccount is | |
// solhint-disable-next-line no-empty-blocks | ||
function _authorizeUpgrade(address newImplementation) internal override {} | ||
|
||
function _checkIfValidationApplies(bytes4 selector, FunctionReference validationFunction, bool shared) | ||
huaweigu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
internal | ||
view | ||
{ | ||
AccountStorage storage _storage = getAccountStorage(); | ||
|
||
// Check that the provided validation function is applicable to the selector | ||
if (shared) { | ||
howydev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( | ||
!_sharedValidationAllowed(selector) | ||
|| !_storage.defaultValidations.contains(toSetValue(validationFunction)) | ||
) { | ||
revert UserOpValidationFunctionMissing(selector); | ||
} | ||
} else { | ||
// Not shared validation, but per-selector | ||
if (!getAccountStorage().selectorData[selector].validations.contains(toSetValue(validationFunction))) { | ||
revert UserOpValidationFunctionMissing(selector); | ||
} | ||
} | ||
} | ||
|
||
function _sharedValidationAllowed(bytes4 selector) internal view returns (bool) { | ||
if ( | ||
adamegyed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
selector == this.execute.selector || selector == this.executeBatch.selector | ||
|| selector == this.installPlugin.selector || selector == this.uninstallPlugin.selector | ||
|| selector == this.installValidation.selector || selector == this.uninstallValidation.selector | ||
|| selector == this.upgradeToAndCall.selector | ||
) { | ||
return true; | ||
} | ||
|
||
return getAccountStorage().selectorData[selector].allowSharedValidation; | ||
} | ||
|
||
function _checkPermittedCallerIfNotFromEP() internal view { | ||
AccountStorage storage _storage = getAccountStorage(); | ||
|
||
|
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This field being
true
also indicates the function is not state-change, right? Might be worth adding it to the docs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically, it can still be state-changing, it's just that usually this will only be used for
view
functions. See this PR comment for more context: #61