Skip to content

Commit

Permalink
♻️ Ease the fetch of the domain separator during test
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Jan 30, 2024
1 parent fcd8781 commit b3ed062
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 33 deletions.
8 changes: 2 additions & 6 deletions src/utils/ERC4337Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,9 @@ library ERC4337Utils {
}

/// @dev Returns the EIP-712 domain separator for the given kernel account.
function getDomainSeparator(Kernel kernel)
internal
view
returns (bytes32 domainSeparator)
{
function getDomainSeparator(Kernel kernel) internal view returns (bytes32 domainSeparator) {
// Extract a few infos from the kernel
(,string memory name, string memory version,,,,) = kernel.eip712Domain();
(, string memory name, string memory version,,,,) = kernel.eip712Domain();
// Build the domain separator
domainSeparator = _buildDomainSeparator(name, version, address(kernel));
}
Expand Down
9 changes: 3 additions & 6 deletions src/utils/KernelTestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {TestERC721} from "../mock/TestERC721.sol";
import {TestERC1155} from "../mock/TestERC1155.sol";

using ERC4337Utils for IEntryPoint;
using ERC4337Utils for Kernel;

abstract contract KernelTestBase is Test {
// to support 0.8.19
Expand Down Expand Up @@ -197,11 +198,7 @@ abstract contract KernelTestBase is Test {
function test_validate_signature() external virtual {
Kernel kernel2 = Kernel(payable(factory.createAccount(address(kernelImpl), getInitializeData(), 3)));
bytes32 hash = keccak256(abi.encodePacked("hello world"));
bytes32 digest = keccak256(
abi.encodePacked(
"\x19\x01", ERC4337Utils._buildDomainSeparator(KERNEL_NAME, KERNEL_VERSION, address(kernel)), hash
)
);
bytes32 digest = keccak256(abi.encodePacked("\x19\x01", kernel.getDomainSeparator(), hash));
bytes memory sig = signHash(digest);
assertEq(kernel.isValidSignature(hash, sig), Kernel.isValidSignature.selector);
assertEq(kernel2.isValidSignature(hash, sig), bytes4(0xffffffff));
Expand Down Expand Up @@ -453,7 +450,7 @@ abstract contract KernelTestBase is Test {
return keccak256(
abi.encodePacked(
"\x19\x01",
ERC4337Utils._buildDomainSeparator(KERNEL_NAME, KERNEL_VERSION, address(kernel)),
kernel.getDomainSeparator(),
ERC4337Utils.getStructHash(sig, validUntil, validAfter, validator, executor, enableData)
)
);
Expand Down
8 changes: 2 additions & 6 deletions test/benchmark/BaseValidatorBenchmark.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ abstract contract BaseValidatorBenchmark is Test {
_addToGlobalJson("signature");

// Write the json output
if(_isWriteEnabled) {
if (_isWriteEnabled) {
string memory fileName = string.concat("./benchmarks/validator/", validatorName, ".json");
vm.writeJson(_jsonOutput, fileName);
}
Expand Down Expand Up @@ -219,11 +219,7 @@ abstract contract BaseValidatorBenchmark is Test {
// Get a few data for the domain separator
bytes32 domainSeparator = _kernel.getDomainSeparator();
// Should create a digest of the hash
bytes32 _digest = keccak256(
abi.encodePacked(
"\x19\x01", domainSeparator, _hash
)
);
bytes32 _digest = keccak256(abi.encodePacked("\x19\x01", domainSeparator, _hash));
bytes memory signature = _generateHashSignature(_digest);

// Perform the validator signature check directly
Expand Down
19 changes: 4 additions & 15 deletions test/foundry/validator/WebAuthnFclValidator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {Base64Url} from "FreshCryptoLib/utils/Base64Url.sol";
import {IKernel} from "src/interfaces/IKernel.sol";

using ERC4337Utils for IEntryPoint;
using ERC4337Utils for Kernel;

contract WebAuthnFclValidatorTest is KernelTestBase {
WebAuthnFclValidator private webAuthNValidator;
Expand Down Expand Up @@ -127,11 +128,7 @@ contract WebAuthnFclValidatorTest is KernelTestBase {
function test_validate_signature() external override {
bytes32 _hash = keccak256(abi.encodePacked("hello world"));

bytes32 digest = keccak256(
abi.encodePacked(
"\x19\x01", ERC4337Utils._buildDomainSeparator(KERNEL_NAME, KERNEL_VERSION, address(kernel)), _hash
)
);
bytes32 digest = keccak256(abi.encodePacked("\x19\x01", kernel.getDomainSeparator(), _hash));

bytes memory signature = signHash(digest);

Expand All @@ -141,11 +138,7 @@ contract WebAuthnFclValidatorTest is KernelTestBase {
function test_fail_validate_wrongsignature() external override {
// Prepare the hash to sign
bytes32 _hash = keccak256(abi.encodePacked("hello world"));
bytes32 digest = keccak256(
abi.encodePacked(
"\x19\x01", ERC4337Utils._buildDomainSeparator(KERNEL_NAME, KERNEL_VERSION, address(kernel)), _hash
)
);
bytes32 digest = keccak256(abi.encodePacked("\x19\x01", kernel.getDomainSeparator(), _hash));

// Sign it (via a wrong signer)
bytes memory sig = getWrongSignature(digest);
Expand All @@ -155,11 +148,7 @@ contract WebAuthnFclValidatorTest is KernelTestBase {
function test_fail_validate_InvalidWebAuthnData() external {
// Prepare the data to sign
bytes32 _hash = keccak256(abi.encodePacked("hello world"));
bytes32 digest = keccak256(
abi.encodePacked(
"\x19\x01", ERC4337Utils._buildDomainSeparator(KERNEL_NAME, KERNEL_VERSION, address(kernel)), _hash
)
);
bytes32 digest = keccak256(abi.encodePacked("\x19\x01", kernel.getDomainSeparator(), _hash));

bytes32 _wrongHash = keccak256(abi.encodePacked("bye world"));

Expand Down

0 comments on commit b3ed062

Please sign in to comment.