From 32715224e20e0ab8aaac23b0ba635e65989075f7 Mon Sep 17 00:00:00 2001 From: 0age <37939117+0age@users.noreply.github.com> Date: Tue, 15 Oct 2024 19:51:43 -0700 Subject: [PATCH] put the function casts in HashLib in FunctionCastLib too --- src/lib/FunctionCastLib.sol | 169 ++++++++++++++++++++++++++- src/lib/HashLib.sol | 225 ++++++++---------------------------- 2 files changed, 216 insertions(+), 178 deletions(-) diff --git a/src/lib/FunctionCastLib.sol b/src/lib/FunctionCastLib.sol index cfef686..f1fd1f4 100644 --- a/src/lib/FunctionCastLib.sol +++ b/src/lib/FunctionCastLib.sol @@ -27,7 +27,12 @@ import { QualifiedSplitBatchClaimWithWitness } from "../types/BatchClaims.sol"; -import { TransferComponent, SplitByIdComponent } from "../types/Components.sol"; +import { + TransferComponent, + SplitByIdComponent, + BatchClaimComponent, + SplitBatchClaimComponent +} from "../types/Components.sol"; library FunctionCastLib { function usingSplitTransfer( @@ -66,6 +71,168 @@ library FunctionCastLib { } } + function usingSplitBatchTransfer( + function(BatchTransfer calldata, bytes32) internal view returns (bytes32) fnIn + ) + internal + pure + returns ( + function(SplitBatchTransfer calldata, bytes32) internal view returns (bytes32) fnOut + ) + { + assembly { + fnOut := fnIn + } + } + + function usingQualifiedSplitClaim( + function(QualifiedClaim calldata) internal view returns (bytes32, bytes32) fnIn + ) + internal + pure + returns ( + function(QualifiedSplitClaim calldata) internal view returns (bytes32, bytes32) fnOut + ) + { + assembly { + fnOut := fnIn + } + } + + function usingQualifiedClaimWithWitness( + function(ClaimWithWitness calldata, uint256) internal view returns (bytes32) fnIn + ) + internal + pure + returns ( + function(QualifiedClaimWithWitness calldata, uint256) internal view returns (bytes32) fnOut + ) + { + assembly { + fnOut := fnIn + } + } + + function usingQualifiedClaimWithWitness( + function(QualifiedClaim calldata, bytes32, uint256) internal pure returns (bytes32) fnIn + ) + internal + pure + returns ( + function(QualifiedClaimWithWitness calldata, bytes32, uint256) + internal + pure + returns (bytes32) fnOut + ) + { + assembly { + fnOut := fnIn + } + } + + function usingQualifiedSplitClaimWithWitness( + function (QualifiedClaimWithWitness calldata) internal view returns (bytes32, bytes32) fnIn + ) + internal + pure + returns ( + function (QualifiedSplitClaimWithWitness calldata) internal view returns (bytes32, bytes32) + fnOut + ) + { + assembly { + fnOut := fnIn + } + } + + function usingQualifiedSplitBatchClaim( + function (SplitBatchClaim calldata, SplitBatchClaimComponent[] calldata) internal view returns (bytes32) + fnIn + ) + internal + pure + returns ( + function (QualifiedSplitBatchClaim calldata, SplitBatchClaimComponent[] calldata) internal view returns (bytes32) + fnOut + ) + { + assembly { + fnOut := fnIn + } + } + + function usingQualifiedSplitBatchClaimWithWitness( + function (SplitBatchClaimWithWitness calldata, SplitBatchClaimComponent[] calldata) internal view returns (bytes32) + fnIn + ) + internal + pure + returns ( + function (QualifiedSplitBatchClaimWithWitness calldata, SplitBatchClaimComponent[] calldata) internal view returns (bytes32) + fnOut + ) + { + assembly { + fnOut := fnIn + } + } + + function usingQualifiedBatchClaim( + function(BatchClaim calldata, BatchClaimComponent[] calldata) internal view returns (bytes32) + fnIn + ) + internal + pure + returns ( + function(QualifiedBatchClaim calldata, BatchClaimComponent[] calldata) internal view returns (bytes32) + fnOut + ) + { + assembly { + fnOut := fnIn + } + } + + function usingQualifiedBatchClaimWithWitness( + function(BatchClaimWithWitness calldata, BatchClaimComponent[] calldata) internal view returns (bytes32) + fnIn + ) + internal + pure + returns ( + function(QualifiedBatchClaimWithWitness calldata, BatchClaimComponent[] calldata) internal view returns (bytes32) + fnOut + ) + { + assembly { + fnOut := fnIn + } + } + + function usingSplitClaim(function (Claim calldata) internal view returns (bytes32) fnIn) + internal + pure + returns (function (SplitClaim calldata) internal view returns (bytes32) fnOut) + { + assembly { + fnOut := fnIn + } + } + + function usingSplitClaimWithWitness( + function (ClaimWithWitness calldata, uint256) internal view returns (bytes32) fnIn + ) + internal + pure + returns ( + function (SplitClaimWithWitness calldata, uint256) internal view returns (bytes32) fnOut + ) + { + assembly { + fnOut := fnIn + } + } + // NOTE: the id field needs to be at the exact same struct offset for this to work! function usingSplitByIdComponent( function (TransferComponent[] memory, uint256) internal returns (address) fnIn diff --git a/src/lib/HashLib.sol b/src/lib/HashLib.sol index ebacee3..513024d 100644 --- a/src/lib/HashLib.sol +++ b/src/lib/HashLib.sol @@ -76,9 +76,45 @@ import { BatchClaimComponent, SplitBatchClaimComponent } from "../types/Componen import { ResetPeriod } from "../types/ResetPeriod.sol"; import { Scope } from "../types/Scope.sol"; +import { FunctionCastLib } from "./FunctionCastLib.sol"; + // TODO: make calldata versions of these where useful library HashLib { - error Debug(bytes data); + using + FunctionCastLib + for function(BatchTransfer calldata, bytes32) internal view returns (bytes32); + using + FunctionCastLib + for function(QualifiedClaim calldata) internal view returns (bytes32, bytes32); + using + FunctionCastLib + for function(ClaimWithWitness calldata, uint256) internal view returns (bytes32); + using + FunctionCastLib + for function(QualifiedClaim calldata, bytes32, uint256) internal pure returns (bytes32); + using + FunctionCastLib + for function(QualifiedClaimWithWitness calldata) internal view returns (bytes32, bytes32); + using + FunctionCastLib + for + function(SplitBatchClaim calldata, SplitBatchClaimComponent[] calldata) internal view returns (bytes32); + using + FunctionCastLib + for + function(SplitBatchClaimWithWitness calldata, SplitBatchClaimComponent[] calldata) internal view returns (bytes32); + using + FunctionCastLib + for + function(BatchClaim calldata, BatchClaimComponent[] calldata) internal view returns (bytes32); + using + FunctionCastLib + for + function(BatchClaimWithWitness calldata, BatchClaimComponent[] calldata) internal view returns (bytes32); + using FunctionCastLib for function(Claim calldata) internal view returns (bytes32); + using + FunctionCastLib + for function(ClaimWithWitness calldata, uint256) internal view returns (bytes32); /// @dev `keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")`. bytes32 internal constant _DOMAIN_TYPEHASH = @@ -173,26 +209,12 @@ library HashLib { return toQualifiedClaimMessageHash(claim); } - function usingQualifiedSplitClaim( - function(QualifiedClaim calldata) internal view returns (bytes32, bytes32) fnIn - ) - internal - pure - returns ( - function(QualifiedSplitClaim calldata) internal view returns (bytes32, bytes32) fnOut - ) - { - assembly { - fnOut := fnIn - } - } - function toMessageHash(QualifiedSplitClaim calldata claim) internal view returns (bytes32 messageHash, bytes32 qualificationMessageHash) { - return usingQualifiedSplitClaim(toQualifiedClaimMessageHash)(claim); + return toQualifiedClaimMessageHash.usingQualifiedSplitClaim()(claim); } function toQualificationMessageHash( @@ -251,63 +273,14 @@ library HashLib { } } - function usingQualifiedClaimWithWitness( - function(ClaimWithWitness calldata, uint256) internal view returns (bytes32) fnIn - ) - internal - pure - returns ( - function(QualifiedClaimWithWitness calldata, uint256) internal view returns (bytes32) fnOut - ) - { - assembly { - fnOut := fnIn - } - } - - function usingQualifiedClaimWithWitness( - function(QualifiedClaim calldata, bytes32, uint256) - internal - pure - returns (bytes32) fnIn - ) - internal - pure - returns ( - function(QualifiedClaimWithWitness calldata, bytes32, uint256) - internal - pure - returns (bytes32) fnOut - ) - { - assembly { - fnOut := fnIn - } - } - function toQualifiedClaimWithWitnessMessageHash(QualifiedClaimWithWitness calldata claim) internal view returns (bytes32 messageHash, bytes32 qualificationMessageHash) { - messageHash = usingQualifiedClaimWithWitness(toMessageHashWithWitness)(claim, 0x40); + messageHash = toMessageHashWithWitness.usingQualifiedClaimWithWitness()(claim, 0x40); qualificationMessageHash = - usingQualifiedClaimWithWitness(toQualificationMessageHash)(claim, messageHash, 0x40); - } - - function _usingQualifiedSplitClaimWithWitness( - function (QualifiedClaimWithWitness calldata) internal view returns (bytes32, bytes32) fnIn - ) - internal - pure - returns ( - function (QualifiedSplitClaimWithWitness calldata) internal view returns (bytes32, bytes32) - fnOut - ) - { - assembly { - fnOut := fnIn - } + toQualificationMessageHash.usingQualifiedClaimWithWitness()(claim, messageHash, 0x40); } function toMessageHash(QualifiedClaimWithWitness calldata claim) @@ -323,7 +296,7 @@ library HashLib { view returns (bytes32 messageHash, bytes32 qualificationMessageHash) { - return _usingQualifiedSplitClaimWithWitness(toQualifiedClaimWithWitnessMessageHash)(claim); + return toQualifiedClaimWithWitnessMessageHash.usingQualifiedSplitClaimWithWitness()(claim); } function toMessageHash(BatchTransfer calldata transfer) @@ -358,20 +331,6 @@ library HashLib { } } - function _usingSplitBatchTransfer( - function(BatchTransfer calldata, bytes32) internal view returns (bytes32) fnIn - ) - internal - pure - returns ( - function(SplitBatchTransfer calldata, bytes32) internal view returns (bytes32) fnOut - ) - { - assembly { - fnOut := fnIn - } - } - function toMessageHash(SplitBatchTransfer calldata transfer) internal view @@ -389,7 +348,7 @@ library HashLib { bytes32 idsAndAmountsHash = keccak256(abi.encodePacked(idsAndAmounts)); messageHash = - _usingSplitBatchTransfer(_deriveBatchCompactMessageHash)(transfer, idsAndAmountsHash); + _deriveBatchCompactMessageHash.usingSplitBatchTransfer()(transfer, idsAndAmountsHash); } function toIdsAndAmountsHash(BatchClaimComponent[] calldata claims) @@ -455,38 +414,6 @@ library HashLib { return _toSplitBatchMessageHashWithWitness(claim, claim.claims); } - function _usingQualifiedSplitBatchClaim( - function (SplitBatchClaim calldata, SplitBatchClaimComponent[] calldata) internal view returns (bytes32) - fnIn - ) - internal - pure - returns ( - function (QualifiedSplitBatchClaim calldata, SplitBatchClaimComponent[] calldata) internal view returns (bytes32) - fnOut - ) - { - assembly { - fnOut := fnIn - } - } - - function _usingQualifiedSplitBatchClaimWithWitness( - function (SplitBatchClaimWithWitness calldata, SplitBatchClaimComponent[] calldata) internal view returns (bytes32) - fnIn - ) - internal - pure - returns ( - function (QualifiedSplitBatchClaimWithWitness calldata, SplitBatchClaimComponent[] calldata) internal view returns (bytes32) - fnOut - ) - { - assembly { - fnOut := fnIn - } - } - function _toSplitBatchMessageHash( SplitBatchClaim calldata claim, SplitBatchClaimComponent[] calldata claims @@ -556,44 +483,12 @@ library HashLib { } } - function _usingQualifiedBatchClaim( - function(BatchClaim calldata, BatchClaimComponent[] calldata) internal view returns (bytes32) - fnIn - ) - internal - pure - returns ( - function(QualifiedBatchClaim calldata, BatchClaimComponent[] calldata) internal view returns (bytes32) - fnOut - ) - { - assembly { - fnOut := fnIn - } - } - - function _usingQualifiedBatchClaimWithWitness( - function(BatchClaimWithWitness calldata, BatchClaimComponent[] calldata) internal view returns (bytes32) - fnIn - ) - internal - pure - returns ( - function(QualifiedBatchClaimWithWitness calldata, BatchClaimComponent[] calldata) internal view returns (bytes32) - fnOut - ) - { - assembly { - fnOut := fnIn - } - } - function toMessageHash(QualifiedBatchClaim calldata claim) internal view returns (bytes32 messageHash, bytes32 qualificationMessageHash) { - messageHash = _usingQualifiedBatchClaim(_deriveBatchMessageHash)(claim, claim.claims); + messageHash = _deriveBatchMessageHash.usingQualifiedBatchClaim()(claim, claim.claims); // TODO: optimize once we're using calldata qualificationMessageHash = keccak256( @@ -614,7 +509,7 @@ library HashLib { view returns (bytes32 messageHash, bytes32 qualificationMessageHash) { - messageHash = _usingQualifiedBatchClaimWithWitness(_toBatchMessageHashWithWitness)( + messageHash = _toBatchMessageHashWithWitness.usingQualifiedBatchClaimWithWitness()( claim, claim.claims ); @@ -624,32 +519,8 @@ library HashLib { ); } - function _usingSplitClaim(function (Claim calldata) internal view returns (bytes32) fnIn) - internal - pure - returns (function (SplitClaim calldata) internal view returns (bytes32) fnOut) - { - assembly { - fnOut := fnIn - } - } - function toMessageHash(SplitClaim calldata claim) internal view returns (bytes32 messageHash) { - return _usingSplitClaim(toClaimMessageHash)(claim); - } - - function _usingSplitClaimWithWitness( - function (ClaimWithWitness calldata, uint256) internal view returns (bytes32) fnIn - ) - internal - pure - returns ( - function (SplitClaimWithWitness calldata, uint256) internal view returns (bytes32) fnOut - ) - { - assembly { - fnOut := fnIn - } + return toClaimMessageHash.usingSplitClaim()(claim); } function toMessageHash(SplitClaimWithWitness calldata claim) @@ -657,7 +528,7 @@ library HashLib { view returns (bytes32 messageHash) { - return _usingSplitClaimWithWitness(toMessageHashWithWitness)(claim, 0); + return toMessageHashWithWitness.usingSplitClaimWithWitness()(claim, 0); } function toMessageHash(QualifiedSplitBatchClaim calldata claim) @@ -665,7 +536,7 @@ library HashLib { view returns (bytes32 messageHash, bytes32 qualificationMessageHash) { - messageHash = _usingQualifiedSplitBatchClaim(_toSplitBatchMessageHash)(claim, claim.claims); + messageHash = _toSplitBatchMessageHash.usingQualifiedSplitBatchClaim()(claim, claim.claims); // TODO: optimize once we're using calldata qualificationMessageHash = keccak256( @@ -678,7 +549,7 @@ library HashLib { view returns (bytes32 messageHash, bytes32 qualificationMessageHash) { - messageHash = _usingQualifiedSplitBatchClaimWithWitness(_toSplitBatchMessageHashWithWitness)( + messageHash = _toSplitBatchMessageHashWithWitness.usingQualifiedSplitBatchClaimWithWitness()( claim, claim.claims );