Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestognw committed Sep 24, 2024
1 parent 62d8164 commit 9f25076
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 74 deletions.
8 changes: 6 additions & 2 deletions contracts/account/AccountBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ abstract contract AccountBase is IAccount, IAccountExecute, ERC1155Holder, ERC72
}

/// @inheritdoc IAccountExecute
function executeUserOp(PackedUserOperation calldata userOp, bytes32 /*userOpHash*/) public virtual onlyEntryPoint {
Address.functionDelegateCall(address(this), userOp.callData[4:]);
function executeUserOp(
PackedUserOperation calldata userOp,
bytes32 /*userOpHash*/
) public virtual onlyEntryPointOrSelf {
(address target, uint256 value, bytes memory data) = abi.decode(userOp.callData[4:], (address, uint256, bytes));
Address.functionCallWithValue(target, data, value);
}

function _validateUserOp(
Expand Down
30 changes: 18 additions & 12 deletions contracts/account/AccountERC7579.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ abstract contract AccountERC7579 is

/// @inheritdoc IERC1271
function isValidSignature(bytes32 hash, bytes calldata signature) public view virtual override returns (bytes4) {
address module = address(bytes20(signature[0:20]));
address module = abi.decode(signature[0:20], (address));
_checkModule(MODULE_TYPE_VALIDATOR, module);
return IERC7579Validator(module).isValidSignatureWithSender(msg.sender, hash, signature);
}
Expand Down Expand Up @@ -85,7 +85,7 @@ abstract contract AccountERC7579 is
function installModule(
uint256 moduleTypeId,
address module,
bytes calldata initData
bytes memory initData
) public virtual onlyEntryPointOrSelf {
_installModule(moduleTypeId, module, initData);
}
Expand All @@ -94,7 +94,7 @@ abstract contract AccountERC7579 is
function uninstallModule(
uint256 moduleTypeId,
address module,
bytes calldata deInitData
bytes memory deInitData
) public virtual onlyEntryPointOrSelf {
_uninstallModule(moduleTypeId, module, deInitData);
}
Expand All @@ -104,7 +104,7 @@ abstract contract AccountERC7579 is
bytes32 userOpHash
) internal virtual override returns (address signer, uint256 validationData) {
PackedUserOperation memory userOpCopy = userOp;
address module = address(bytes20(userOp.signature[0:20]));
address module = abi.decode(userOp.signature[0:20], (address));
userOpCopy.signature = userOp.signature[20:];
return
isModuleInstalled(MODULE_TYPE_EXECUTOR, module, userOp.signature[0:0])
Expand Down Expand Up @@ -145,30 +145,36 @@ abstract contract AccountERC7579 is
return false;
}

function _installModule(uint256 moduleTypeId, address module, bytes calldata initData) internal virtual {
function _installModule(uint256 moduleTypeId, address module, bytes memory initData) internal virtual {
if (!_supportsModule(moduleTypeId)) revert ERC7579UnsupportedModuleType(moduleTypeId);
if (!IERC7579Module(module).isModuleType(moduleTypeId))
revert ERC7579MismatchedModuleTypeId(moduleTypeId, module);
if (
(moduleTypeId == MODULE_TYPE_VALIDATOR && !_validators.add(module)) ||
(moduleTypeId == MODULE_TYPE_EXECUTOR && !_executors.add(module)) ||
(moduleTypeId == MODULE_TYPE_FALLBACK && !_installFallback(module, bytes4(initData[0:4])))
(moduleTypeId == MODULE_TYPE_EXECUTOR && !_executors.add(module))
) revert ERC7579AlreadyInstalledModule(moduleTypeId, module);

if (moduleTypeId == MODULE_TYPE_FALLBACK) initData = initData[4:];
if (moduleTypeId == MODULE_TYPE_FALLBACK) {
bytes4 selector;
(selector, initData) = abi.decode(initData, (bytes4, bytes));
if (!_installFallback(module, selector)) revert ERC7579AlreadyInstalledModule(moduleTypeId, module);
}

IERC7579Module(module).onInstall(initData);
emit ModuleInstalled(moduleTypeId, module);
}

function _uninstallModule(uint256 moduleTypeId, address module, bytes calldata deInitData) internal virtual {
function _uninstallModule(uint256 moduleTypeId, address module, bytes memory deInitData) internal virtual {
if (
(moduleTypeId == MODULE_TYPE_VALIDATOR && !_validators.remove(module)) ||
(moduleTypeId == MODULE_TYPE_EXECUTOR && !_executors.remove(module)) ||
(moduleTypeId == MODULE_TYPE_FALLBACK && !_uninstallFallback(module, bytes4(deInitData[0:4])))
(moduleTypeId == MODULE_TYPE_EXECUTOR && !_executors.remove(module))
) revert ERC7579UninstalledModule(moduleTypeId, module);

if (moduleTypeId == MODULE_TYPE_FALLBACK) deInitData = deInitData[4:];
if (moduleTypeId == MODULE_TYPE_FALLBACK) {
bytes4 selector;
(selector, deInitData) = abi.decode(deInitData, (bytes4, bytes));
if (!_uninstallFallback(module, selector)) revert ERC7579UninstalledModule(moduleTypeId, module);
}

IERC7579Module(module).onUninstall(deInitData);
emit ModuleUninstalled(moduleTypeId, module);
Expand Down
2 changes: 2 additions & 0 deletions contracts/account/ERC1271TypedSigner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {ShortStrings} from "../utils/ShortStrings.sol";
abstract contract ERC1271TypedSigner is EIP712, IERC1271 {
error MismatchedTypedData();

constructor(string memory name, string memory version) EIP712(name, version) {}

function isValidSignature(bytes32 hash, bytes calldata signature) public view virtual returns (bytes4 result) {
return _isValidSignature(hash, signature) ? IERC1271.isValidSignature.selector : bytes4(0xffffffff);
}
Expand Down
26 changes: 16 additions & 10 deletions contracts/account/modules/MultisigValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ abstract contract MultisigValidator is IERC7579Validator, IERC1271 {

/// @inheritdoc IERC1271
function isValidSignature(bytes32 hash, bytes calldata signature) public view virtual returns (bytes4) {
address account = address(bytes20(signature[0:20]));
address account = abi.decode(signature[0:20], (address));
bytes calldata sig = signature[20:];
return _isValidSignature(account, hash, sig) ? IERC1271.isValidSignature.selector : bytes4(0xffffffff);
}
Expand Down Expand Up @@ -137,24 +137,30 @@ abstract contract MultisigValidator is IERC7579Validator, IERC1271 {
bytes calldata signature
) internal view virtual returns (bool) {
(address[] calldata signers, bytes[] calldata signatures) = _decodePackedSignatures(signature);
if (signers.length != signatures.length)
revert MultisigMismatchedSignaturesLength(account, signers.length, signatures.length);
if (signers.length != signatures.length) return false;
return _validateNSignatures(account, hash, signers, signatures);
}

uint256 count = 0;
function _validateNSignatures(
address account,
bytes32 hash,
address[] calldata signers,
bytes[] calldata signatures
) private view returns (bool) {
address currentSigner = address(0);

for (uint256 i = 0; i < signers.length; i++) {
uint256 signersLength = signers.length;
for (uint256 i = 0; i < signersLength; i++) {
// Signers must be in order to ensure no duplicates
address signer = signers[i];
if (currentSigner >= signer) revert MultisigUnorderedSigners(account, currentSigner, signer);
if (currentSigner >= signer) return false;
currentSigner = signer;

bool canSign = _associatedSigners[account].contains(signer);
bool isValid = signer.isValidSignatureNow(hash, signatures[i]);
if (canSign && isValid) count++;
if (!_associatedSigners[account].contains(signer) || !signer.isValidSignatureNow(hash, signatures[i]))
return false;
}

return count >= _associatedThreshold[account];
return signersLength >= _associatedThreshold[account];
}

function _decodePackedSignatures(
Expand Down
6 changes: 3 additions & 3 deletions contracts/account/utils/ERC7579Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ library ERC7579Utils {
function decodeSingle(
bytes calldata executionCalldata
) internal pure returns (address target, uint256 value, bytes calldata callData) {
target = address(bytes20(executionCalldata[0:20]));
value = uint256(bytes32(executionCalldata[20:52]));
target = abi.decode(executionCalldata[0:20], (address));
value = abi.decode(executionCalldata[20:52], (uint256));
callData = executionCalldata[52:];
}

Expand All @@ -143,7 +143,7 @@ library ERC7579Utils {
function decodeDelegate(
bytes calldata executionCalldata
) internal pure returns (address target, bytes calldata callData) {
target = address(bytes20(executionCalldata[0:20]));
target = abi.decode(executionCalldata[0:20], (address));
callData = executionCalldata[20:];
}

Expand Down
Loading

0 comments on commit 9f25076

Please sign in to comment.