Skip to content

Commit

Permalink
feat: sig call buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
adamegyed committed Oct 12, 2024
1 parent 1aef4f5 commit 1a83dd3
Show file tree
Hide file tree
Showing 20 changed files with 456 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
257468
257405
Original file line number Diff line number Diff line change
@@ -1 +1 @@
89286
89189
Original file line number Diff line number Diff line change
@@ -1 +1 @@
74791
74706
Original file line number Diff line number Diff line change
@@ -1 +1 @@
422778
422693
Original file line number Diff line number Diff line change
@@ -1 +1 @@
50950
50865
Original file line number Diff line number Diff line change
@@ -1 +1 @@
80029
79969
Original file line number Diff line number Diff line change
@@ -1 +1 @@
113587
113527
Original file line number Diff line number Diff line change
@@ -1 +1 @@
193755
193634
Original file line number Diff line number Diff line change
@@ -1 +1 @@
180839
180730
Original file line number Diff line number Diff line change
@@ -1 +1 @@
529934
529825
Original file line number Diff line number Diff line change
@@ -1 +1 @@
157114
157005
Original file line number Diff line number Diff line change
@@ -1 +1 @@
196336
196156
Original file line number Diff line number Diff line change
@@ -1 +1 @@
227472
227292
Original file line number Diff line number Diff line change
@@ -1 +1 @@
254088
254023
44 changes: 23 additions & 21 deletions src/account/ModularAccountBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
ModuleEntity,
ValidationConfig
} from "@erc6900/reference-implementation/interfaces/IModularAccount.sol";
import {IValidationHookModule} from "@erc6900/reference-implementation/interfaces/IValidationHookModule.sol";
import {IValidationModule} from "@erc6900/reference-implementation/interfaces/IValidationModule.sol";
import {HookConfig, HookConfigLib} from "@erc6900/reference-implementation/libraries/HookConfigLib.sol";
import {ModuleEntityLib} from "@erc6900/reference-implementation/libraries/ModuleEntityLib.sol";
import {ValidationConfigLib} from "@erc6900/reference-implementation/libraries/ValidationConfigLib.sol";
Expand All @@ -32,10 +30,11 @@ import {
ExecutionLib,
PHCallBuffer,
RTCallBuffer,
SigCallBuffer,
UOCallBuffer
} from "../libraries/ExecutionLib.sol";
import {LinkedListSet, LinkedListSetLib} from "../libraries/LinkedListSetLib.sol";
import {MemManagementLib} from "../libraries/MemManagementLib.sol";
import {MemManagementLib, MemSnapshot} from "../libraries/MemManagementLib.sol";
import {SparseCalldataSegmentLib} from "../libraries/SparseCalldataSegmentLib.sol";
import {AccountStorage, getAccountStorage, toSetValue} from "./AccountStorage.sol";
import {AccountStorageInitializable} from "./AccountStorageInitializable.sol";
Expand Down Expand Up @@ -539,9 +538,12 @@ abstract contract ModularAccountBase is
uoValidation
);

// Clear the memory after performing signature validation
MemSnapshot memSnapshot = MemManagementLib.freezeFMP();
if (_isValidSignature(sigValidation, typedDataHash, sig) != _1271_MAGIC_VALUE) {
revert DeferredActionSignatureInvalid();
}
MemManagementLib.restoreFMP(memSnapshot);

return (deadline, uoValidation);
}
Expand Down Expand Up @@ -751,45 +753,45 @@ abstract contract ModularAccountBase is
HookConfig[] memory preSignatureValidationHooks =
MemManagementLib.loadValidationHooks(getAccountStorage().validationData[sigValidation]);

SigCallBuffer sigCallBuffer;
if (!_validationIsNative(sigValidation) || preSignatureValidationHooks.length > 0) {
sigCallBuffer = ExecutionLib.allocateSigCallBuffer(hash, signature);
}
for (uint256 i = preSignatureValidationHooks.length; i > 0;) {
// Decrement here, instead of in the loop body, to convert from length to an index.
unchecked {
--i;
}

(address hookModule, uint32 hookEntityId) = preSignatureValidationHooks[i].moduleEntity().unpack();

bytes memory currentSignatureSegment;
bytes calldata currentSignatureSegment;

(currentSignatureSegment, signature) =
signature.advanceSegmentIfAtIndex(uint8(preSignatureValidationHooks.length - i - 1));

// If this reverts, bubble up revert reason.
IValidationHookModule(hookModule).preSignatureValidationHook(
hookEntityId, msg.sender, hash, currentSignatureSegment
ExecutionLib.invokePreSignatureValidationHook(
sigCallBuffer, preSignatureValidationHooks[i], currentSignatureSegment
);
}
signature = signature.getFinalSegment();
return _exec1271Validation(sigValidation, hash, signature);

return _exec1271Validation(sigCallBuffer, hash, sigValidation, signature);
}

function _exec1271Validation(ModuleEntity sigValidation, bytes32 hash, bytes calldata signature)
internal
view
virtual
returns (bytes4)
{
function _exec1271Validation(
SigCallBuffer buffer,
bytes32 hash,
ModuleEntity sigValidation,
bytes calldata signatureSegment
) internal view virtual returns (bytes4) {
(hash); // unused
AccountStorage storage _storage = getAccountStorage();

(address module, uint32 entityId) = sigValidation.unpack();
if (!_storage.validationData[sigValidation].isSignatureValidation) {
(address module, uint32 entityId) = sigValidation.unpack();
revert SignatureValidationInvalid(module, entityId);
}

if (
IValidationModule(module).validateSignature(address(this), entityId, msg.sender, hash, signature)
== _1271_MAGIC_VALUE
) {
if (ExecutionLib.invokeSignatureValidation(buffer, sigValidation, signatureSegment) == _1271_MAGIC_VALUE) {
return _1271_MAGIC_VALUE;
}
return _1271_INVALID;
Expand Down
16 changes: 8 additions & 8 deletions src/account/SemiModularAccountBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {SignatureChecker} from "@openzeppelin/contracts/utils/cryptography/Signa

import {DIRECT_CALL_VALIDATION_ENTITYID, FALLBACK_VALIDATION} from "../helpers/Constants.sol";
import {SignatureType} from "../helpers/SignatureType.sol";
import {RTCallBuffer, UOCallBuffer} from "../libraries/ExecutionLib.sol";
import {RTCallBuffer, SigCallBuffer, UOCallBuffer} from "../libraries/ExecutionLib.sol";
import {SemiModularKnownSelectorsLib} from "../libraries/SemiModularKnownSelectorsLib.sol";
import {ModularAccountBase} from "./ModularAccountBase.sol";

Expand Down Expand Up @@ -134,12 +134,12 @@ abstract contract SemiModularAccountBase is ModularAccountBase {
}
}

function _exec1271Validation(ModuleEntity sigValidation, bytes32 hash, bytes calldata signature)
internal
view
override
returns (bytes4)
{
function _exec1271Validation(
SigCallBuffer buffer,
bytes32 hash,
ModuleEntity sigValidation,
bytes calldata signature
) internal view override returns (bytes4) {
if (sigValidation.eq(FALLBACK_VALIDATION)) {
address fallbackSigner = _getFallbackSigner();

Expand All @@ -148,7 +148,7 @@ abstract contract SemiModularAccountBase is ModularAccountBase {
}
return _1271_INVALID;
}
return super._exec1271Validation(sigValidation, hash, signature);
return super._exec1271Validation(buffer, hash, sigValidation, signature);
}

function _checkSignature(address owner, bytes32 digest, bytes calldata sig) internal view returns (bool) {
Expand Down
Loading

0 comments on commit 1a83dd3

Please sign in to comment.