Skip to content
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

Fix/eip712 #72

Merged
merged 6 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Compiler files
cache/
out/
out-optimized/
.DS_Store
docs/

Expand Down
69 changes: 57 additions & 12 deletions src/lib/AttestationLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ import { SSTORE2 } from "solady/utils/SSTORE2.sol";

library AttestationLib {
// The hash of the data type used to relay calls to the attest function. It's the value of
bytes32 internal constant ATTEST_REQUEST_TYPEHASH = keccak256("AttestationRequest(address,uint48,bytes,uint256[])");
bytes32 internal constant ATTEST_TYPEHASH =
keccak256("SignedAttestationRequest(AttestationRequest,uint256 nonce)AttestationRequest(address,uint48,bytes,uint256[])");
bytes32 internal constant ATTEST_ARRAY_TYPEHASH =
keccak256("SignedAttestationRequests(AttestationRequest[],uint256 nonce)AttestationRequest(address,uint48,bytes,uint256[])");
bytes32 internal constant ATTEST_REQUEST_TYPEHASH =
keccak256("AttestationRequest(address moduleAddress,uint48 expirationTime,bytes data,uint256[] moduleTypes)");
bytes32 internal constant ATTEST_TYPEHASH = keccak256(
"SignedAttestationRequest(AttestationRequest,uint256 nonce)AttestationRequest(address moduleAddress,uint48 expirationTime,bytes data,uint256[] moduleTypes)"
);
bytes32 internal constant ATTEST_ARRAY_TYPEHASH = keccak256(
"SignedAttestationRequests(AttestationRequest[],uint256 nonce)AttestationRequest(address moduleAddress,uint48 expirationTimme,bytes data,uint256[] moduleTypes)"
);

// The hash of the data type used to relay calls to the revoke function. It's the value of
bytes32 internal constant REVOKE_REQUEST_TYPEHASH = keccak256("RevocationRequest(address)");
bytes32 internal constant REVOKE_REQUEST_TYPEHASH = keccak256("RevocationRequest(address moduleAddress)");
bytes32 internal constant REVOKE_TYPEHASH =
keccak256("SignedRevocationRequest(RevocationRequest,uint256 nonce)RevocationRequest(address)");
keccak256("SignedRevocationRequest(RevocationRequest,uint256 nonce)RevocationRequest(address moduleAddress)");
bytes32 internal constant REVOKE_ARRAY_TYPEHASH =
keccak256("SignedRevocationRequests(RevocationRequest[],uint256 nonce)RevocationRequest(address)");
keccak256("SignedRevocationRequests(RevocationRequest[],uint256 nonce)RevocationRequest(address moduleAddressq)");

/**
* Helper function to SSTORE2 read an attestation
Expand Down Expand Up @@ -64,7 +67,21 @@ library AttestationLib {
* @return _hash the hash
*/
function hash(AttestationRequest calldata request, uint256 nonce) internal pure returns (bytes32 _hash) {
_hash = keccak256(abi.encode(ATTEST_TYPEHASH, keccak256(abi.encode(request)), nonce));
_hash = keccak256(
abi.encode(
ATTEST_TYPEHASH,
keccak256(
abi.encode(
ATTEST_REQUEST_TYPEHASH,
request.moduleAddr,
request.expirationTime,
keccak256(request.data),
keccak256(abi.encodePacked(request.moduleTypes))
)
),
nonce
)
);
}

/**
Expand All @@ -74,7 +91,25 @@ library AttestationLib {
* @return _hash the hash
*/
function hash(AttestationRequest[] calldata requests, uint256 nonce) internal pure returns (bytes32 _hash) {
_hash = keccak256(abi.encode(ATTEST_ARRAY_TYPEHASH, keccak256(abi.encode(requests)), nonce));
bytes memory concatinatedAttestations;

uint256 length = requests.length;
for (uint256 i; i < length; i++) {
concatinatedAttestations = abi.encodePacked(
concatinatedAttestations, // concat previous
keccak256(
abi.encode(
ATTEST_REQUEST_TYPEHASH,
requests[i].moduleAddr,
requests[i].expirationTime,
keccak256(requests[i].data),
keccak256(abi.encodePacked(requests[i].moduleTypes))
)
)
);
}

_hash = keccak256(abi.encode(ATTEST_ARRAY_TYPEHASH, keccak256(concatinatedAttestations), nonce));
}

/**
Expand All @@ -84,7 +119,7 @@ library AttestationLib {
* @return _hash the hash
*/
function hash(RevocationRequest calldata request, uint256 nonce) internal pure returns (bytes32 _hash) {
_hash = keccak256(abi.encode(REVOKE_TYPEHASH, keccak256(abi.encode(request)), nonce));
_hash = keccak256(abi.encode(REVOKE_TYPEHASH, keccak256(abi.encode(REVOKE_REQUEST_TYPEHASH, request.moduleAddr)), nonce));
}

/**
Expand All @@ -94,6 +129,16 @@ library AttestationLib {
* @return _hash the hash
*/
function hash(RevocationRequest[] calldata requests, uint256 nonce) internal pure returns (bytes32 _hash) {
_hash = keccak256(abi.encode(REVOKE_ARRAY_TYPEHASH, keccak256(abi.encode(requests)), nonce));
bytes memory concatinatedAttestations;

uint256 length = requests.length;
for (uint256 i; i < length; i++) {
concatinatedAttestations = abi.encodePacked(
concatinatedAttestations, // concat previous
keccak256(abi.encode(REVOKE_REQUEST_TYPEHASH, requests[i].moduleAddr))
);
}

_hash = keccak256(abi.encode(REVOKE_ARRAY_TYPEHASH, keccak256(concatinatedAttestations), nonce));
}
}
8 changes: 3 additions & 5 deletions test/ModuleRegistration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ contract ModuleRegistrationTest is BaseTest {
ResolverUID invalidUID = ResolverUID.wrap(hex"00");

vm.expectRevert(abi.encodeWithSelector(IRegistry.InvalidResolverUID.selector, invalidUID));
registry.registerModule(invalidUID, address(newModule), "");
registry.registerModule(invalidUID, address(newModule), "", "");

invalidUID = ResolverUID.wrap("1");
vm.expectRevert(abi.encodeWithSelector(IRegistry.InvalidResolverUID.selector, invalidUID));
registry.registerModule(invalidUID, address(newModule), "");

registry.registerModule(invalidUID, address(newModule), "", "");
}

function test_WhenRegisteringAModuleOnAValidResolverUID() external prankWithAccount(moduleDev1) {
Expand All @@ -66,8 +65,7 @@ contract ModuleRegistrationTest is BaseTest {
MockModule newModule = new MockModule();

vm.expectRevert(abi.encodeWithSelector(IRegistry.InvalidResolverUID.selector, ResolverUID.wrap(bytes32("foobar"))));
registry.registerModule(ResolverUID.wrap(bytes32("foobar")), address(newModule), "");

registry.registerModule(ResolverUID.wrap(bytes32("foobar")), address(newModule), "", "");
}

function test_WhenRegisteringTwoModulesWithTheSameBytecode() external prankWithAccount(moduleDev1) {
Expand Down
Loading