-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// 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"; | ||
|
||
// Temporary additional functions for a user-controlled install flow for validation functions. | ||
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 isDefault, | ||
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. Is this necessary? Is the goal to separate validation installation from other plugin installations? Would it be easier to only separate default validation installation from all other plugin installations (include non default validation installations). 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. These are not final interfaces, they're side doors to the install path because we haven't fully implemented user-supplied install configs yet. More detail in |
||
bytes4[] memory selectors, | ||
bytes calldata installData | ||
) internal { | ||
FunctionReference validationFunction = FunctionReferenceLib.pack(plugin, functionId); | ||
|
||
AccountStorage storage _storage = getAccountStorage(); | ||
|
||
if (isDefault) { | ||
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); | ||
} | ||
} |
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 isDefault); | ||
|
||
// Wraps execution of a native function with runtime validation and hooks | ||
// Used for upgradeTo, upgradeToAndCall, execute, executeBatch, installPlugin, uninstallPlugin | ||
|
@@ -155,6 +158,7 @@ contract UpgradeableModularAccount is | |
} | ||
|
||
/// @inheritdoc IStandardExecutor | ||
/// @notice May be validated by a default validation. | ||
function execute(address target, uint256 value, bytes calldata data) | ||
external | ||
payable | ||
|
@@ -166,6 +170,7 @@ contract UpgradeableModularAccount is | |
} | ||
|
||
/// @inheritdoc IStandardExecutor | ||
/// @notice May be validated by a default validation function. | ||
function executeBatch(Call[] calldata calls) | ||
external | ||
payable | ||
|
@@ -279,11 +284,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 isDefaultValidation = uint8(authorization[21]) == 1; | ||
_checkIfValidationApplies(execSelector, runtimeValidationFunction, isDefaultValidation); | ||
|
||
_doRuntimeValidation(runtimeValidationFunction, data, authorization[22:]); | ||
|
||
// If runtime validation passes, execute the call | ||
|
||
|
@@ -299,6 +305,7 @@ contract UpgradeableModularAccount is | |
} | ||
|
||
/// @inheritdoc IPluginManager | ||
/// @notice May be validated by a default validation. | ||
function installPlugin( | ||
address plugin, | ||
bytes32 manifestHash, | ||
|
@@ -309,6 +316,7 @@ contract UpgradeableModularAccount is | |
} | ||
|
||
/// @inheritdoc IPluginManager | ||
/// @notice May be validated by a default validation. | ||
function uninstallPlugin(address plugin, bytes calldata config, bytes calldata pluginUninstallData) | ||
external | ||
override | ||
|
@@ -325,6 +333,42 @@ 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 | ||
/// @notice May be validated by a default validation. | ||
function installValidation( | ||
adamegyed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
address plugin, | ||
uint8 functionId, | ||
bool isDefault, | ||
bytes4[] calldata selectors, | ||
bytes calldata installData | ||
) external wrapNativeFunction { | ||
_installValidation(plugin, functionId, isDefault, selectors, installData); | ||
} | ||
|
||
/// @inheritdoc IPluginManager | ||
/// @notice May be validated by a default validation. | ||
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 | ||
|
@@ -341,6 +385,7 @@ contract UpgradeableModularAccount is | |
} | ||
|
||
/// @inheritdoc UUPSUpgradeable | ||
/// @notice May be validated by a default validation. | ||
function upgradeToAndCall(address newImplementation, bytes memory data) | ||
public | ||
payable | ||
|
@@ -398,14 +443,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 isDefaultValidation = uint8(userOp.signature[21]) == 1; | ||
|
||
if (!getAccountStorage().selectorData[selector].validations.contains(toSetValue(userOpValidationFunction))) | ||
{ | ||
revert UserOpValidationFunctionMissing(selector); | ||
} | ||
_checkIfValidationApplies(selector, userOpValidationFunction, isDefaultValidation); | ||
|
||
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 +616,41 @@ contract UpgradeableModularAccount is | |
// solhint-disable-next-line no-empty-blocks | ||
function _authorizeUpgrade(address newImplementation) internal override {} | ||
|
||
function _checkIfValidationApplies(bytes4 selector, FunctionReference validationFunction, bool isDefault) | ||
internal | ||
view | ||
{ | ||
AccountStorage storage _storage = getAccountStorage(); | ||
|
||
// Check that the provided validation function is applicable to the selector | ||
if (isDefault) { | ||
if ( | ||
!_defaultValidationAllowed(selector) | ||
|| !_storage.defaultValidations.contains(toSetValue(validationFunction)) | ||
) { | ||
revert UserOpValidationFunctionMissing(selector); | ||
} | ||
} else { | ||
// Not default validation, but per-selector | ||
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. Minor, during the installation, it seems like we also install the default validation
should we check 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. That path in the installation is used if the user wishes to install a validation both as a default validation and for some specific selectors. If the user manually adds selectors that are already usable from default validation (e.g. 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. I see, that makes sense! |
||
if (!getAccountStorage().selectorData[selector].validations.contains(toSetValue(validationFunction))) { | ||
revert UserOpValidationFunctionMissing(selector); | ||
} | ||
} | ||
} | ||
|
||
function _defaultValidationAllowed(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].allowDefaultValidation; | ||
} | ||
|
||
function _checkPermittedCallerIfNotFromEP() internal view { | ||
AccountStorage storage _storage = getAccountStorage(); | ||
|
||
|
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