From 07014db161b542e969f7e702c8a80547bfbc58b8 Mon Sep 17 00:00:00 2001 From: vimageDE Date: Mon, 4 Nov 2024 15:59:41 +0100 Subject: [PATCH] forge fmt --- src/examples/allocator/ServerAllocator.sol | 155 ++----- src/interfaces/IAllocator.sol | 10 +- src/test/AlwaysOKAllocator.sol | 17 +- src/test/ERC20Mock.sol | 5 +- src/test/TheCompactMock.sol | 60 +-- test/ServerAllocator.t.sol | 485 +++++---------------- 6 files changed, 154 insertions(+), 578 deletions(-) diff --git a/src/examples/allocator/ServerAllocator.sol b/src/examples/allocator/ServerAllocator.sol index f775bfc..444f990 100644 --- a/src/examples/allocator/ServerAllocator.sol +++ b/src/examples/allocator/ServerAllocator.sol @@ -2,13 +2,13 @@ pragma solidity ^0.8.27; -import {Compact} from "src/types/EIP712Types.sol"; -import {ITheCompact} from "src/interfaces/ITheCompact.sol"; -import {IAllocator} from "src/interfaces/IAllocator.sol"; -import {Ownable, Ownable2Step} from "lib/openzeppelin-contracts/contracts/access/Ownable2Step.sol"; -import {ECDSA} from "lib/openzeppelin-contracts/contracts/utils/cryptography/ECDSA.sol"; -import {EIP712} from "lib/openzeppelin-contracts/contracts/utils/cryptography/EIP712.sol"; -import {IERC1271} from "lib/openzeppelin-contracts/contracts/interfaces/IERC1271.sol"; +import { Compact } from "src/types/EIP712Types.sol"; +import { ITheCompact } from "src/interfaces/ITheCompact.sol"; +import { IAllocator } from "src/interfaces/IAllocator.sol"; +import { Ownable, Ownable2Step } from "lib/openzeppelin-contracts/contracts/access/Ownable2Step.sol"; +import { ECDSA } from "lib/openzeppelin-contracts/contracts/utils/cryptography/ECDSA.sol"; +import { EIP712 } from "lib/openzeppelin-contracts/contracts/utils/cryptography/EIP712.sol"; +import { IERC1271 } from "lib/openzeppelin-contracts/contracts/interfaces/IERC1271.sol"; contract ServerAllocator is Ownable2Step, EIP712, IAllocator { using ECDSA for bytes32; @@ -30,16 +30,13 @@ contract ServerAllocator is Ownable2Step, EIP712, IAllocator { bytes4 private constant _ATTEST_SELECTOR = 0x1a808f91; // keccak256("RegisterAttest(address signer,bytes32 attestHash,uint256 expiration,uint256 nonce)") - bytes32 private constant _ATTEST_TYPE_HASH = - 0xaf2dfd3fe08723f490d203be627da2725f4ad38681e455221da2fc1a633bbb18; + bytes32 private constant _ATTEST_TYPE_HASH = 0xaf2dfd3fe08723f490d203be627da2725f4ad38681e455221da2fc1a633bbb18; // keccak256("Allocator(bytes32 hash)") - bytes32 private constant _ALLOCATOR_TYPE_HASH = - 0xcdf324dc7c3490a07fbbb105911393dcbc0676ac7c6c1c32c786721de6179e70; + bytes32 private constant _ALLOCATOR_TYPE_HASH = 0xcdf324dc7c3490a07fbbb105911393dcbc0676ac7c6c1c32c786721de6179e70; // keccak256("NonceConsumption(address signer,uint256[] nonces,bytes32[] attests)") - bytes32 private constant _NONCE_CONSUMPTION_TYPE_HASH = - 0xb06793f900067653959d9bc53299ebf6b5aa5cf5f6c1a463305891a3db695f3c; + bytes32 private constant _NONCE_CONSUMPTION_TYPE_HASH = 0xb06793f900067653959d9bc53299ebf6b5aa5cf5f6c1a463305891a3db695f3c; address private immutable _COMPACT_CONTRACT; @@ -72,10 +69,7 @@ contract ServerAllocator is Ownable2Step, EIP712, IAllocator { _; } - constructor( - address owner_, - address compactContract_ - ) Ownable(owner_) EIP712("Allocator", "1") { + constructor(address owner_, address compactContract_) Ownable(owner_) EIP712("Allocator", "1") { _COMPACT_CONTRACT = compactContract_; } @@ -105,31 +99,17 @@ contract ServerAllocator is Ownable2Step, EIP712, IAllocator { } /// @dev There is no way to uniquely identify a transfer, so the contract relies on its own accounting of registered attests. - function registerAttest( - bytes32 attest_, - uint256 expiration_ - ) external isSigner(msg.sender) { + function registerAttest(bytes32 attest_, uint256 expiration_) external isSigner(msg.sender) { _registerAttest(attest_, expiration_); } /// @dev Nonce management in the RegisterAttest is only required for multiple registers of the same attest with the same expiration. - function registerAttestViaSignature( - RegisterAttest calldata attest_, - bytes calldata signature_ - ) external { - bytes32 _attestWithNonce = keccak256( - abi.encode(attest_.attestHash, attest_.expiration, attest_.nonce) - ); + function registerAttestViaSignature(RegisterAttest calldata attest_, bytes calldata signature_) external { + bytes32 _attestWithNonce = keccak256(abi.encode(attest_.attestHash, attest_.expiration, attest_.nonce)); if (_attestSignatures[_attestWithNonce]) { revert AlreadyUsedSig(attest_.attestHash, attest_.nonce); } - address signer = _validateSignedAttest( - attest_.signer, - attest_.attestHash, - attest_.expiration, - attest_.nonce, - signature_ - ); + address signer = _validateSignedAttest(attest_.signer, attest_.attestHash, attest_.expiration, attest_.nonce, signature_); if (signer != attest_.signer || !_containsSigner(signer)) { revert InvalidSignature(signature_, signer); } @@ -165,12 +145,8 @@ contract ServerAllocator is Ownable2Step, EIP712, IAllocator { delete _attestExpirations[countedAttest]; } else { // Shift attest and delete from the end - bytes32 lastAttest = keccak256( - abi.encode(registeredAttest, count) - ); - _attestExpirations[countedAttest] = _attestExpirations[ - lastAttest - ]; + bytes32 lastAttest = keccak256(abi.encode(registeredAttest, count)); + _attestExpirations[countedAttest] = _attestExpirations[lastAttest]; delete _attestExpirations[lastAttest]; } _attestCounts[registeredAttest] = --count; @@ -196,10 +172,7 @@ contract ServerAllocator is Ownable2Step, EIP712, IAllocator { _consumeNonces(nonces_, attests_); } - function consumeViaSignature( - NonceConsumption calldata data_, - bytes calldata signature_ - ) external { + function consumeViaSignature(NonceConsumption calldata data_, bytes calldata signature_) external { if (data_.attests.length != data_.nonces.length) { revert InvalidInput(); } @@ -213,10 +186,7 @@ contract ServerAllocator is Ownable2Step, EIP712, IAllocator { /// @dev A registered attest will be a fallback if no valid signature was provided. // TODO: https://github.com/Uniswap/permit2/blob/cc56ad0f3439c502c246fc5cfcc3db92bb8b7219/src/interfaces/IERC1271.sol - function isValidSignature( - bytes32 hash_, - bytes calldata signature_ - ) external view returns (bytes4 magicValue) { + function isValidSignature(bytes32 hash_, bytes calldata signature_) external view returns (bytes4 magicValue) { address signer = _validateSignedHash(hash_, signature_); if (!_containsSigner(signer)) { revert InvalidSignature(signature_, signer); @@ -232,21 +202,12 @@ contract ServerAllocator is Ownable2Step, EIP712, IAllocator { return _activeSigners; } - function checkAttestExpirations( - bytes32 attest_ - ) external view returns (uint256[] memory) { + function checkAttestExpirations(bytes32 attest_) external view returns (uint256[] memory) { return _checkAttestExpirations(attest_); } - function checkAttestExpirations( - address sponsor_, - uint256 id_, - uint256 amount_ - ) external view returns (uint256[] memory) { - return - _checkAttestExpirations( - keccak256(abi.encode(sponsor_, id_, amount_)) - ); + function checkAttestExpirations(address sponsor_, uint256 id_, uint256 amount_) external view returns (uint256[] memory) { + return _checkAttestExpirations(keccak256(abi.encode(sponsor_, id_, amount_))); } function getCompactContract() external view returns (address) { @@ -266,10 +227,7 @@ contract ServerAllocator is Ownable2Step, EIP712, IAllocator { } /// Todo: This will lead to always the last registered hash being consumed. - function _consumeNonces( - uint256[] calldata nonces_, - bytes32[] calldata attests_ - ) internal { + function _consumeNonces(uint256[] calldata nonces_, bytes32[] calldata attests_) internal { ITheCompact(_COMPACT_CONTRACT).consume(nonces_); uint256 nonceLength = attests_.length; for (uint256 i = 0; i < nonceLength; ++i) { @@ -288,92 +246,45 @@ contract ServerAllocator is Ownable2Step, EIP712, IAllocator { emit NoncesConsumed(nonces_); } - function _validateSignedAttest( - address signer_, - bytes32 hash_, - uint256 expiration_, - uint256 nonce, - bytes calldata signature_ - ) internal view returns (address) { + function _validateSignedAttest(address signer_, bytes32 hash_, uint256 expiration_, uint256 nonce, bytes calldata signature_) internal view returns (address) { bytes32 message = _hashAttest(signer_, hash_, expiration_, nonce); return message.recover(signature_); } - function _hashAttest( - address signer_, - bytes32 hash_, - uint256 expiration_, - uint256 nonce_ - ) internal view returns (bytes32) { - return - _hashTypedDataV4( - keccak256( - abi.encode( - _ATTEST_TYPE_HASH, - signer_, - hash_, - expiration_, - nonce_ - ) - ) - ); + function _hashAttest(address signer_, bytes32 hash_, uint256 expiration_, uint256 nonce_) internal view returns (bytes32) { + return _hashTypedDataV4(keccak256(abi.encode(_ATTEST_TYPE_HASH, signer_, hash_, expiration_, nonce_))); } - function _validateSignedHash( - bytes32 hash_, - bytes calldata signature_ - ) internal view returns (address) { + function _validateSignedHash(bytes32 hash_, bytes calldata signature_) internal view returns (address) { bytes32 message = _hashMessage(hash_); return message.recover(signature_); } function _hashMessage(bytes32 data_) internal view returns (bytes32) { - return - _hashTypedDataV4( - keccak256(abi.encode(_ALLOCATOR_TYPE_HASH, data_)) - ); + return _hashTypedDataV4(keccak256(abi.encode(_ALLOCATOR_TYPE_HASH, data_))); } - function _validateNonceConsumption( - NonceConsumption calldata data_, - bytes calldata signature_ - ) internal view returns (address) { + function _validateNonceConsumption(NonceConsumption calldata data_, bytes calldata signature_) internal view returns (address) { bytes32 message = _hashNonceConsumption(data_); return message.recover(signature_); } - function _hashNonceConsumption( - NonceConsumption calldata data_ - ) internal view returns (bytes32) { - return - _hashTypedDataV4( - keccak256( - abi.encode( - _NONCE_CONSUMPTION_TYPE_HASH, - data_.signer, - data_.nonces, - data_.attests - ) - ) - ); + function _hashNonceConsumption(NonceConsumption calldata data_) internal view returns (bytes32) { + return _hashTypedDataV4(keccak256(abi.encode(_NONCE_CONSUMPTION_TYPE_HASH, data_.signer, data_.nonces, data_.attests))); } function _containsSigner(address signer_) internal view returns (bool) { return _signers[signer_] != 0; } - function _checkAttestExpirations( - bytes32 attest_ - ) internal view returns (uint256[] memory) { + function _checkAttestExpirations(bytes32 attest_) internal view returns (uint256[] memory) { uint256 count = _attestCounts[attest_]; if (count == 0) { revert UnregisteredAttest(attest_); } uint256[] memory expirations = new uint256[](count); for (uint256 i = count; i > 0; --i) { - expirations[i - 1] = _attestExpirations[ - keccak256(abi.encode(attest_, i)) - ]; + expirations[i - 1] = _attestExpirations[keccak256(abi.encode(attest_, i))]; } return expirations; } diff --git a/src/interfaces/IAllocator.sol b/src/interfaces/IAllocator.sol index 5c8d0dc..bf48872 100644 --- a/src/interfaces/IAllocator.sol +++ b/src/interfaces/IAllocator.sol @@ -1,17 +1,11 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.27; -import {IERC1271} from "lib/openzeppelin-contracts/contracts/interfaces/IERC1271.sol"; +import { IERC1271 } from "lib/openzeppelin-contracts/contracts/interfaces/IERC1271.sol"; interface IAllocator is IERC1271 { // Called on standard transfers; must return this function selector (0x1a808f91). - function attest( - address operator, - address from, - address to, - uint256 id, - uint256 amount - ) external returns (bytes4); + function attest(address operator, address from, address to, uint256 id, uint256 amount) external returns (bytes4); // isValidSignature of IERC1271 will be called during a claim and must verify the signature of the allocation. } diff --git a/src/test/AlwaysOKAllocator.sol b/src/test/AlwaysOKAllocator.sol index 758b5c2..d40c058 100644 --- a/src/test/AlwaysOKAllocator.sol +++ b/src/test/AlwaysOKAllocator.sol @@ -1,24 +1,15 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.27; -import {IAllocator} from "../interfaces/IAllocator.sol"; -import {IERC1271} from "permit2/src/interfaces/IERC1271.sol"; +import { IAllocator } from "../interfaces/IAllocator.sol"; +import { IERC1271 } from "permit2/src/interfaces/IERC1271.sol"; contract AlwaysOKAllocator is IAllocator { - function attest( - address, - address, - address, - uint256, - uint256 - ) external pure returns (bytes4) { + function attest(address, address, address, uint256, uint256) external pure returns (bytes4) { return IAllocator.attest.selector; } - function isValidSignature( - bytes32, - bytes calldata - ) external pure returns (bytes4) { + function isValidSignature(bytes32, bytes calldata) external pure returns (bytes4) { return IERC1271.isValidSignature.selector; } } diff --git a/src/test/ERC20Mock.sol b/src/test/ERC20Mock.sol index 5504925..7cbeb5d 100644 --- a/src/test/ERC20Mock.sol +++ b/src/test/ERC20Mock.sol @@ -4,10 +4,7 @@ pragma solidity ^0.8.27; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; contract ERC20Mock is ERC20 { - constructor( - string memory name_, - string memory symbol_ - ) ERC20(name_, symbol_) {} + constructor(string memory name_, string memory symbol_) ERC20(name_, symbol_) { } function mint(address to, uint256 amount) external { _mint(to, amount); diff --git a/src/test/TheCompactMock.sol b/src/test/TheCompactMock.sol index a91ac71..474eae1 100644 --- a/src/test/TheCompactMock.sol +++ b/src/test/TheCompactMock.sol @@ -2,10 +2,10 @@ pragma solidity ^0.8.27; -import {IAllocator} from "src/interfaces/IAllocator.sol"; -import {ERC6909} from "solady/tokens/ERC6909.sol"; -import {ERC20} from "lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol"; -import {IdLib} from "src/lib/IdLib.sol"; +import { IAllocator } from "src/interfaces/IAllocator.sol"; +import { ERC6909 } from "solady/tokens/ERC6909.sol"; +import { ERC20 } from "lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol"; +import { IdLib } from "src/lib/IdLib.sol"; contract TheCompactMock is ERC6909 { using IdLib for uint96; @@ -14,57 +14,27 @@ contract TheCompactMock is ERC6909 { mapping(uint256 nonce => bool consumed) public consumedNonces; - function deposit( - address token, - uint256 amount, - address allocator - ) external { + function deposit(address token, uint256 amount, address allocator) external { ERC20(token).transferFrom(msg.sender, address(this), amount); uint256 id = _getTokenId(token, allocator); _mint(msg.sender, id, amount); } - function transfer( - address from, - address to, - uint256 amount, - address token, - address allocator - ) external { + function transfer(address from, address to, uint256 amount, address token, address allocator) external { uint256 id = _getTokenId(token, allocator); IAllocator(allocator).attest(msg.sender, from, to, id, amount); _transfer(msg.sender, from, to, id, amount); } - function claim( - address from, - address to, - address token, - uint256 amount, - address allocator, - bytes calldata signature - ) external { + function claim(address from, address to, address token, uint256 amount, address allocator, bytes calldata signature) external { uint256 id = _getTokenId(token, allocator); - IAllocator(allocator).isValidSignature( - keccak256(abi.encode(from, id, amount)), - signature - ); + IAllocator(allocator).isValidSignature(keccak256(abi.encode(from, id, amount)), signature); _transfer(msg.sender, from, to, id, amount); } - function withdraw( - address token, - uint256 amount, - address allocator - ) external { + function withdraw(address token, uint256 amount, address allocator) external { uint256 id = _getTokenId(token, allocator); - IAllocator(allocator).attest( - msg.sender, - msg.sender, - msg.sender, - id, - amount - ); + IAllocator(allocator).attest(msg.sender, msg.sender, msg.sender, id, amount); ERC20(token).transferFrom(address(this), msg.sender, amount); _burn(msg.sender, id, amount); } @@ -76,10 +46,7 @@ contract TheCompactMock is ERC6909 { return true; } - function getTokenId( - address token, - address allocator - ) external pure returns (uint256) { + function getTokenId(address token, address allocator) external pure returns (uint256) { return _getTokenId(token, allocator); } @@ -101,10 +68,7 @@ contract TheCompactMock is ERC6909 { return ""; } - function _getTokenId( - address token, - address allocator - ) internal pure returns (uint256) { + function _getTokenId(address token, address allocator) internal pure returns (uint256) { return uint256(keccak256(abi.encode(token, allocator))); } } diff --git a/test/ServerAllocator.t.sol b/test/ServerAllocator.t.sol index 761d9fb..6107967 100644 --- a/test/ServerAllocator.t.sol +++ b/test/ServerAllocator.t.sol @@ -2,13 +2,13 @@ pragma solidity ^0.8.27; -import {Test} from "forge-std/Test.sol"; -import {Ownable} from "lib/openzeppelin-contracts/contracts/access/Ownable.sol"; -import {ServerAllocator} from "src/examples/allocator/ServerAllocator.sol"; -import {TheCompactMock} from "src/test/TheCompactMock.sol"; -import {ERC20Mock} from "src/test/ERC20Mock.sol"; -import {console} from "forge-std/console.sol"; -import {IERC1271} from "lib/permit2/src/interfaces/IERC1271.sol"; +import { Test } from "forge-std/Test.sol"; +import { Ownable } from "lib/openzeppelin-contracts/contracts/access/Ownable.sol"; +import { ServerAllocator } from "src/examples/allocator/ServerAllocator.sol"; +import { TheCompactMock } from "src/test/TheCompactMock.sol"; +import { ERC20Mock } from "src/test/ERC20Mock.sol"; +import { console } from "forge-std/console.sol"; +import { IERC1271 } from "lib/permit2/src/interfaces/IERC1271.sol"; abstract contract MocksSetup is Test { address owner = makeAddr("owner"); @@ -25,10 +25,7 @@ abstract contract MocksSetup is Test { usdc = new ERC20Mock("USDC", "USDC"); compactContract = new TheCompactMock(); serverAllocator = new ServerAllocator(owner, address(compactContract)); - usdcId = compactContract.getTokenId( - address(usdc), - address(serverAllocator) - ); + usdcId = compactContract.getTokenId(address(usdc), address(serverAllocator)); (signer, signerPK) = makeAddrAndKey("signer"); (attacker, attackerPK) = makeAddrAndKey("attacker"); } @@ -37,11 +34,7 @@ abstract contract MocksSetup is Test { abstract contract AttestSetup { bytes4 internal constant _ATTEST_SELECTOR = 0x1a808f91; - function createAttest( - address from_, - uint256 id_, - uint256 amount_ - ) internal pure returns (bytes32) { + function createAttest(address from_, uint256 id_, uint256 amount_) internal pure returns (bytes32) { return keccak256(abi.encode(from_, id_, amount_)); } } @@ -52,97 +45,51 @@ abstract contract CreateHash is Test { } // stringified types - string EIP712_DOMAIN_TYPE = - "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"; // Hashed inside the funcion + string EIP712_DOMAIN_TYPE = "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"; // Hashed inside the funcion string ALLOCATOR_TYPE = "Allocator(bytes32 hash)"; // Hashed inside the funcion - string REGISTER_ATTEST_TYPE = - "RegisterAttest(address signer,bytes32 attestHash,uint256 expiration,uint256 nonce)"; // Hashed inside the funcion - string NONCE_CONSUMPTION_TYPE = - "NonceConsumption(address signer,uint256[] nonces,bytes32[] attests)"; // Hashed inside the funcion + string REGISTER_ATTEST_TYPE = "RegisterAttest(address signer,bytes32 attestHash,uint256 expiration,uint256 nonce)"; // Hashed inside the funcion + string NONCE_CONSUMPTION_TYPE = "NonceConsumption(address signer,uint256[] nonces,bytes32[] attests)"; // Hashed inside the funcion // EIP712 domain type string name = "Allocator"; string version = "1"; - function _hashAllocator( - Allocator memory data, - address verifyingContract - ) internal view returns (bytes32) { + function _hashAllocator(Allocator memory data, address verifyingContract) internal view returns (bytes32) { // hash typed data - return - keccak256( - abi.encodePacked( - "\x19\x01", // backslash is needed to escape the character - _domainSeperator(verifyingContract), - keccak256( - abi.encode(keccak256(bytes(ALLOCATOR_TYPE)), data.hash) - ) - ) - ); - } - - function _hashRegisterAttest( - ServerAllocator.RegisterAttest memory data, - address verifyingContract - ) internal view returns (bytes32) { - return - keccak256( - abi.encodePacked( - "\x19\x01", // backslash is needed to escape the character - _domainSeperator(verifyingContract), - keccak256( - abi.encode( - keccak256(bytes(REGISTER_ATTEST_TYPE)), - data.signer, - data.attestHash, - data.expiration, - data.nonce - ) - ) - ) - ); - } - - function _hashNonceConsumption( - ServerAllocator.NonceConsumption memory data, - address verifyingContract - ) internal view returns (bytes32) { + return keccak256( + abi.encodePacked( + "\x19\x01", // backslash is needed to escape the character + _domainSeperator(verifyingContract), + keccak256(abi.encode(keccak256(bytes(ALLOCATOR_TYPE)), data.hash)) + ) + ); + } + + function _hashRegisterAttest(ServerAllocator.RegisterAttest memory data, address verifyingContract) internal view returns (bytes32) { + return keccak256( + abi.encodePacked( + "\x19\x01", // backslash is needed to escape the character + _domainSeperator(verifyingContract), + keccak256(abi.encode(keccak256(bytes(REGISTER_ATTEST_TYPE)), data.signer, data.attestHash, data.expiration, data.nonce)) + ) + ); + } + + function _hashNonceConsumption(ServerAllocator.NonceConsumption memory data, address verifyingContract) internal view returns (bytes32) { // hash typed data - return - keccak256( - abi.encodePacked( - "\x19\x01", // backslash is needed to escape the character - _domainSeperator(verifyingContract), - keccak256( - abi.encode( - keccak256(bytes(NONCE_CONSUMPTION_TYPE)), - data.signer, - data.nonces, - data.attests - ) - ) - ) - ); - } - - function _domainSeperator( - address verifyingContract - ) internal view returns (bytes32) { - return - keccak256( - abi.encode( - keccak256(bytes(EIP712_DOMAIN_TYPE)), - keccak256(bytes(name)), - keccak256(bytes(version)), - block.chainid, - verifyingContract - ) - ); - } - - function _signMessage( - bytes32 hash_, - uint256 signerPK_ - ) internal pure returns (bytes memory) { + return keccak256( + abi.encodePacked( + "\x19\x01", // backslash is needed to escape the character + _domainSeperator(verifyingContract), + keccak256(abi.encode(keccak256(bytes(NONCE_CONSUMPTION_TYPE)), data.signer, data.nonces, data.attests)) + ) + ); + } + + function _domainSeperator(address verifyingContract) internal view returns (bytes32) { + return keccak256(abi.encode(keccak256(bytes(EIP712_DOMAIN_TYPE)), keccak256(bytes(name)), keccak256(bytes(version)), block.chainid, verifyingContract)); + } + + function _signMessage(bytes32 hash_, uint256 signerPK_) internal pure returns (bytes memory) { (uint8 v, bytes32 r, bytes32 s) = vm.sign(signerPK_, hash_); return abi.encodePacked(r, s, v); } @@ -169,12 +116,7 @@ contract ServerAllocator_ManageSigners is MocksSetup, CreateHash { function test_fuzz_onlyOwnerCanAddSigner(address attacker_) public { vm.assume(attacker_ != owner); - vm.expectRevert( - abi.encodeWithSelector( - Ownable.OwnableUnauthorizedAccount.selector, - attacker_ - ) - ); + vm.expectRevert(abi.encodeWithSelector(Ownable.OwnableUnauthorizedAccount.selector, attacker_)); vm.prank(attacker_); serverAllocator.addSigner(signer); } @@ -223,21 +165,11 @@ contract ServerAllocator_ManageSigners is MocksSetup, CreateHash { vm.startPrank(signer); // try to add another signer - vm.expectRevert( - abi.encodeWithSelector( - Ownable.OwnableUnauthorizedAccount.selector, - signer - ) - ); + vm.expectRevert(abi.encodeWithSelector(Ownable.OwnableUnauthorizedAccount.selector, signer)); serverAllocator.addSigner(attacker); // try to remove a signer - vm.expectRevert( - abi.encodeWithSelector( - Ownable.OwnableUnauthorizedAccount.selector, - signer - ) - ); + vm.expectRevert(abi.encodeWithSelector(Ownable.OwnableUnauthorizedAccount.selector, signer)); serverAllocator.removeSigner(signer); } @@ -259,35 +191,16 @@ contract ServerAllocator_Attest is SignerSet { vm.assume(attacker_ != signer); vm.prank(attacker_); - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.InvalidSigner.selector, - attacker_ - ) - ); - serverAllocator.registerAttest( - createAttest(attacker_, usdcId, 100), - vm.getBlockTimestamp() + 1 days - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.InvalidSigner.selector, attacker_)); + serverAllocator.registerAttest(createAttest(attacker_, usdcId, 100), vm.getBlockTimestamp() + 1 days); } - function test_fuzz_registerAttest_attestExpired( - uint256 expiration_ - ) public { + function test_fuzz_registerAttest_attestExpired(uint256 expiration_) public { vm.assume(expiration_ < vm.getBlockTimestamp()); vm.prank(signer); - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.Expired.selector, - expiration_, - vm.getBlockTimestamp() - ) - ); - serverAllocator.registerAttest( - createAttest(signer, usdcId, 100), - expiration_ - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.Expired.selector, expiration_, vm.getBlockTimestamp())); + serverAllocator.registerAttest(createAttest(signer, usdcId, 100), expiration_); } function test_registerAttest_successful() public { @@ -305,22 +218,12 @@ contract ServerAllocator_Attest is SignerSet { bytes32 attest = createAttest(signer, usdcId, 100); uint256 expiration = vm.getBlockTimestamp() + 1 days; - ServerAllocator.RegisterAttest memory attestData = ServerAllocator - .RegisterAttest(signer, attest, expiration, 0); - bytes32 message = _hashRegisterAttest( - attestData, - address(serverAllocator) - ); + ServerAllocator.RegisterAttest memory attestData = ServerAllocator.RegisterAttest(signer, attest, expiration, 0); + bytes32 message = _hashRegisterAttest(attestData, address(serverAllocator)); bytes memory signature = _signMessage(message, attackerPK); vm.prank(attacker); - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.InvalidSignature.selector, - signature, - attacker - ) - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.InvalidSignature.selector, signature, attacker)); serverAllocator.registerAttestViaSignature(attestData, signature); } @@ -328,12 +231,8 @@ contract ServerAllocator_Attest is SignerSet { bytes32 attest = createAttest(signer, usdcId, 100); uint256 expiration = vm.getBlockTimestamp() + 1 days; - ServerAllocator.RegisterAttest memory attestData = ServerAllocator - .RegisterAttest(signer, attest, expiration, 0); - bytes32 message = _hashRegisterAttest( - attestData, - address(serverAllocator) - ); + ServerAllocator.RegisterAttest memory attestData = ServerAllocator.RegisterAttest(signer, attest, expiration, 0); + bytes32 message = _hashRegisterAttest(attestData, address(serverAllocator)); bytes memory signature = _signMessage(message, signerPK); vm.prank(attacker); @@ -346,12 +245,8 @@ contract ServerAllocator_Attest is SignerSet { bytes32 attest = createAttest(signer, usdcId, 100); uint256 expiration = vm.getBlockTimestamp() + 1 days; - ServerAllocator.RegisterAttest memory attestData = ServerAllocator - .RegisterAttest(signer, attest, expiration, 0); - bytes32 message = _hashRegisterAttest( - attestData, - address(serverAllocator) - ); + ServerAllocator.RegisterAttest memory attestData = ServerAllocator.RegisterAttest(signer, attest, expiration, 0); + bytes32 message = _hashRegisterAttest(attestData, address(serverAllocator)); bytes memory signature = _signMessage(message, signerPK); vm.prank(attacker); @@ -360,13 +255,7 @@ contract ServerAllocator_Attest is SignerSet { serverAllocator.registerAttestViaSignature(attestData, signature); vm.prank(attacker); - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.AlreadyUsedSig.selector, - attest, - 0 - ) - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.AlreadyUsedSig.selector, attest, 0)); serverAllocator.registerAttestViaSignature(attestData, signature); } @@ -381,54 +270,28 @@ contract ServerAllocator_Attest is SignerSet { emit ServerAllocator.AttestRegistered(attest, expiration1); serverAllocator.registerAttest(attest, expiration1); - assertEq( - serverAllocator.checkAttestExpirations(attest)[0], - expiration1 - ); + assertEq(serverAllocator.checkAttestExpirations(attest)[0], expiration1); // second attest with different expiration vm.expectEmit(address(serverAllocator)); emit ServerAllocator.AttestRegistered(attest, expiration2); serverAllocator.registerAttest(attest, expiration2); - assertEq( - serverAllocator.checkAttestExpirations(attest)[0], - expiration1 - ); - assertEq( - serverAllocator.checkAttestExpirations(attest)[1], - expiration2 - ); + assertEq(serverAllocator.checkAttestExpirations(attest)[0], expiration1); + assertEq(serverAllocator.checkAttestExpirations(attest)[1], expiration2); } function test_fuzz_attest_callerMustBeCompact(address caller_) public { vm.assume(caller_ != address(compactContract)); vm.prank(caller_); - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.InvalidCaller.selector, - caller_, - address(compactContract) - ) - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.InvalidCaller.selector, caller_, address(compactContract))); serverAllocator.attest(caller_, signer, attacker, usdcId, 100); } - function test_fuzz_attest_notRegistered( - address operator_, - address from_, - address to_, - uint256 id_, - uint256 amount_ - ) public { + function test_fuzz_attest_notRegistered(address operator_, address from_, address to_, uint256 id_, uint256 amount_) public { vm.prank(address(compactContract)); - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.UnregisteredAttest.selector, - keccak256(abi.encode(from_, id_, amount_)) - ) - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.UnregisteredAttest.selector, keccak256(abi.encode(from_, id_, amount_)))); serverAllocator.attest(operator_, from_, to_, id_, amount_); } @@ -446,28 +309,11 @@ contract ServerAllocator_Attest is SignerSet { // check attest vm.prank(address(compactContract)); - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.ExpiredAttests.selector, - attest - ) - ); - serverAllocator.attest( - signer, - attacker, - makeAddr("to"), - usdcId, - amount_ - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.ExpiredAttests.selector, attest)); + serverAllocator.attest(signer, attacker, makeAddr("to"), usdcId, amount_); } - function test_fuzz_attest_successful( - address operator_, - address from_, - address to_, - uint256 id_, - uint256 amount_ - ) public { + function test_fuzz_attest_successful(address operator_, address from_, address to_, uint256 id_, uint256 amount_) public { bytes32 attest = createAttest(from_, id_, amount_); uint256 expiration = vm.getBlockTimestamp(); @@ -482,22 +328,11 @@ contract ServerAllocator_Attest is SignerSet { vm.prank(address(compactContract)); vm.expectEmit(address(serverAllocator)); emit ServerAllocator.Attested(from_, id_, amount_); - bytes4 attestSelector = serverAllocator.attest( - operator_, - from_, - to_, - id_, - amount_ - ); + bytes4 attestSelector = serverAllocator.attest(operator_, from_, to_, id_, amount_); assertEq(attestSelector, _ATTEST_SELECTOR); // check attest was consumed - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.UnregisteredAttest.selector, - attest - ) - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.UnregisteredAttest.selector, attest)); serverAllocator.checkAttestExpirations(attest); } } @@ -505,20 +340,13 @@ contract ServerAllocator_Attest is SignerSet { contract ServerAllocator_Consume is SignerSet { function test_consume_onlySignerCanConsume() public { vm.prank(attacker); - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.InvalidSigner.selector, - attacker - ) - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.InvalidSigner.selector, attacker)); serverAllocator.consume(new uint256[](0), new bytes32[](0)); } function test_consume_requiresNoncesAndAttestsToBeOfSameLength() public { vm.prank(signer); - vm.expectRevert( - abi.encodeWithSelector(ServerAllocator.InvalidInput.selector) - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.InvalidInput.selector)); serverAllocator.consume(new uint256[](0), new bytes32[](1)); } @@ -558,18 +386,9 @@ contract ServerAllocator_Consume is SignerSet { serverAllocator.registerAttest(attests[1], vm.getBlockTimestamp()); serverAllocator.registerAttest(attests[2], vm.getBlockTimestamp()); - assertEq( - serverAllocator.checkAttestExpirations(attests[0])[0], - vm.getBlockTimestamp() - ); - assertEq( - serverAllocator.checkAttestExpirations(attests[1])[0], - vm.getBlockTimestamp() - ); - assertEq( - serverAllocator.checkAttestExpirations(attests[2])[0], - vm.getBlockTimestamp() - ); + assertEq(serverAllocator.checkAttestExpirations(attests[0])[0], vm.getBlockTimestamp()); + assertEq(serverAllocator.checkAttestExpirations(attests[1])[0], vm.getBlockTimestamp()); + assertEq(serverAllocator.checkAttestExpirations(attests[2])[0], vm.getBlockTimestamp()); vm.expectEmit(address(serverAllocator)); emit ServerAllocator.NoncesConsumed(nonces); @@ -581,82 +400,30 @@ contract ServerAllocator_Consume is SignerSet { } // check attests were consumed - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.UnregisteredAttest.selector, - attests[0] - ) - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.UnregisteredAttest.selector, attests[0])); serverAllocator.checkAttestExpirations(attests[0]); - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.UnregisteredAttest.selector, - attests[1] - ) - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.UnregisteredAttest.selector, attests[1])); serverAllocator.checkAttestExpirations(attests[1]); - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.UnregisteredAttest.selector, - attests[2] - ) - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.UnregisteredAttest.selector, attests[2])); serverAllocator.checkAttestExpirations(attests[2]); } - function test_consumeViaSignature_requiresNoncesAndAttestsToBeOfSameLength() - public - { - bytes32 message = _hashNonceConsumption( - ServerAllocator.NonceConsumption( - signer, - new uint256[](0), - new bytes32[](1) - ), - address(serverAllocator) - ); + function test_consumeViaSignature_requiresNoncesAndAttestsToBeOfSameLength() public { + bytes32 message = _hashNonceConsumption(ServerAllocator.NonceConsumption(signer, new uint256[](0), new bytes32[](1)), address(serverAllocator)); bytes memory signature = _signMessage(message, signerPK); vm.prank(signer); - vm.expectRevert( - abi.encodeWithSelector(ServerAllocator.InvalidInput.selector) - ); - serverAllocator.consumeViaSignature( - ServerAllocator.NonceConsumption( - signer, - new uint256[](0), - new bytes32[](1) - ), - signature - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.InvalidInput.selector)); + serverAllocator.consumeViaSignature(ServerAllocator.NonceConsumption(signer, new uint256[](0), new bytes32[](1)), signature); } function test_consumeViaSignature_requireValidSignature() public { - bytes32 message = _hashNonceConsumption( - ServerAllocator.NonceConsumption( - signer, - new uint256[](1), - new bytes32[](1) - ), - address(serverAllocator) - ); + bytes32 message = _hashNonceConsumption(ServerAllocator.NonceConsumption(signer, new uint256[](1), new bytes32[](1)), address(serverAllocator)); bytes memory signature = _signMessage(message, attackerPK); vm.prank(signer); - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.InvalidSigner.selector, - attacker - ) - ); - serverAllocator.consumeViaSignature( - ServerAllocator.NonceConsumption( - signer, - new uint256[](1), - new bytes32[](1) - ), - signature - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.InvalidSigner.selector, attacker)); + serverAllocator.consumeViaSignature(ServerAllocator.NonceConsumption(signer, new uint256[](1), new bytes32[](1)), signature); } function test_consumeViaSignature_successfulWithoutAttests() public { @@ -665,19 +432,13 @@ contract ServerAllocator_Consume is SignerSet { nonces[1] = 2; nonces[2] = 3; - bytes32 message = _hashNonceConsumption( - ServerAllocator.NonceConsumption(signer, nonces, new bytes32[](3)), - address(serverAllocator) - ); + bytes32 message = _hashNonceConsumption(ServerAllocator.NonceConsumption(signer, nonces, new bytes32[](3)), address(serverAllocator)); bytes memory signature = _signMessage(message, signerPK); vm.prank(attacker); vm.expectEmit(address(serverAllocator)); emit ServerAllocator.NoncesConsumed(nonces); - serverAllocator.consumeViaSignature( - ServerAllocator.NonceConsumption(signer, nonces, new bytes32[](3)), - signature - ); + serverAllocator.consumeViaSignature(ServerAllocator.NonceConsumption(signer, nonces, new bytes32[](3)), signature); } function test_consumeViaSignature_successfulWithAttests() public { @@ -698,32 +459,17 @@ contract ServerAllocator_Consume is SignerSet { serverAllocator.registerAttest(attests[2], vm.getBlockTimestamp()); vm.stopPrank(); - assertEq( - serverAllocator.checkAttestExpirations(attests[0])[0], - vm.getBlockTimestamp() - ); - assertEq( - serverAllocator.checkAttestExpirations(attests[1])[0], - vm.getBlockTimestamp() - ); - assertEq( - serverAllocator.checkAttestExpirations(attests[2])[0], - vm.getBlockTimestamp() - ); + assertEq(serverAllocator.checkAttestExpirations(attests[0])[0], vm.getBlockTimestamp()); + assertEq(serverAllocator.checkAttestExpirations(attests[1])[0], vm.getBlockTimestamp()); + assertEq(serverAllocator.checkAttestExpirations(attests[2])[0], vm.getBlockTimestamp()); - bytes32 message = _hashNonceConsumption( - ServerAllocator.NonceConsumption(signer, nonces, attests), - address(serverAllocator) - ); + bytes32 message = _hashNonceConsumption(ServerAllocator.NonceConsumption(signer, nonces, attests), address(serverAllocator)); bytes memory signature = _signMessage(message, signerPK); vm.prank(attacker); vm.expectEmit(address(serverAllocator)); emit ServerAllocator.NoncesConsumed(nonces); - serverAllocator.consumeViaSignature( - ServerAllocator.NonceConsumption(signer, nonces, attests), - signature - ); + serverAllocator.consumeViaSignature(ServerAllocator.NonceConsumption(signer, nonces, attests), signature); assertEq(compactContract.consumedNonces(0), false); for (uint256 i = 0; i < nonces.length; ++i) { @@ -731,26 +477,11 @@ contract ServerAllocator_Consume is SignerSet { } // check attests were consumed - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.UnregisteredAttest.selector, - attests[0] - ) - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.UnregisteredAttest.selector, attests[0])); serverAllocator.checkAttestExpirations(attests[0]); - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.UnregisteredAttest.selector, - attests[1] - ) - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.UnregisteredAttest.selector, attests[1])); serverAllocator.checkAttestExpirations(attests[1]); - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.UnregisteredAttest.selector, - attests[2] - ) - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.UnregisteredAttest.selector, attests[2])); serverAllocator.checkAttestExpirations(attests[2]); } } @@ -758,29 +489,17 @@ contract ServerAllocator_Consume is SignerSet { contract ServerAllocator_isValidSignature is SignerSet { function test_isValidSignature_revertInvalidSig() public { bytes32 attest = createAttest(signer, usdcId, 100); - bytes32 message = _hashAllocator( - Allocator(attest), - address(serverAllocator) - ); + bytes32 message = _hashAllocator(Allocator(attest), address(serverAllocator)); bytes memory signature = _signMessage(message, attackerPK); vm.prank(attacker); - vm.expectRevert( - abi.encodeWithSelector( - ServerAllocator.InvalidSignature.selector, - signature, - attacker - ) - ); + vm.expectRevert(abi.encodeWithSelector(ServerAllocator.InvalidSignature.selector, signature, attacker)); serverAllocator.isValidSignature(attest, signature); } function test_isValidSignature_successful() public { bytes32 attest = createAttest(signer, usdcId, 100); - bytes32 message = _hashAllocator( - Allocator(attest), - address(serverAllocator) - ); + bytes32 message = _hashAllocator(Allocator(attest), address(serverAllocator)); bytes memory signature = _signMessage(message, signerPK); vm.prank(attacker);