diff --git a/snapshots/TheCompactTest.json b/snapshots/TheCompactTest.json index 4ce9a18..c272a96 100644 --- a/snapshots/TheCompactTest.json +++ b/snapshots/TheCompactTest.json @@ -1,42 +1,42 @@ { - "basicTransfer": "56817", - "basicWithdrawal": "59797", - "batchClaim": "111809", - "batchClaimRegisteredWithDeposit": "111809", - "batchClaimRegisteredWithDepositWithWitness": "112571", - "batchClaimWithWitness": "112565", - "batchDepositAndRegisterViaPermit2": "221878", - "batchDepositAndRegisterWithWitnessViaPermit2": "221856", - "batchTransfer": "81517", - "batchWithdrawal": "99932", - "claim": "56956", - "claimAndWithdraw": "73247", - "claimWithWitness": "59421", - "depositAndRegisterViaPermit2": "124248", - "depositBatchSingleERC20": "67846", - "depositBatchSingleNative": "28149", - "depositBatchViaPermit2NativeAndERC20": "129570", - "depositBatchViaPermit2SingleERC20": "104724", - "depositERC20AndURI": "67095", - "depositERC20Basic": "67124", + "basicTransfer": "56861", + "basicWithdrawal": "59819", + "batchClaim": "111831", + "batchClaimRegisteredWithDeposit": "111831", + "batchClaimRegisteredWithDepositWithWitness": "112593", + "batchClaimWithWitness": "112587", + "batchDepositAndRegisterViaPermit2": "221900", + "batchDepositAndRegisterWithWitnessViaPermit2": "221878", + "batchTransfer": "81539", + "batchWithdrawal": "99954", + "claim": "56978", + "claimAndWithdraw": "73269", + "claimWithWitness": "59443", + "depositAndRegisterViaPermit2": "124270", + "depositBatchSingleERC20": "67868", + "depositBatchSingleNative": "28171", + "depositBatchViaPermit2NativeAndERC20": "129592", + "depositBatchViaPermit2SingleERC20": "104746", + "depositERC20AndURI": "67117", + "depositERC20Basic": "67146", "depositERC20ViaPermit2AndURI": "98312", - "depositETHAndURI": "26755", - "depositETHBasic": "28318", - "qualifiedBatchClaim": "113227", - "qualifiedBatchClaimWithWitness": "112691", - "qualifiedClaim": "60215", + "depositETHAndURI": "26777", + "depositETHBasic": "28384", + "qualifiedBatchClaim": "113249", + "qualifiedBatchClaimWithWitness": "112713", + "qualifiedClaim": "60237", "qualifiedClaimWithWitness": "58777", - "qualifiedSplitBatchClaim": "140893", - "qualifiedSplitBatchClaimWithWitness": "140885", - "qualifiedSplitClaim": "86502", - "qualifiedSplitClaimWithWitness": "86846", - "register": "25357", - "splitBatchClaim": "140377", - "splitBatchClaimWithWitness": "140341", - "splitBatchTransfer": "110592", - "splitBatchWithdrawal": "139800", - "splitClaim": "86425", - "splitClaimWithWitness": "85906", - "splitTransfer": "82729", - "splitWithdrawal": "93639" + "qualifiedSplitBatchClaim": "140937", + "qualifiedSplitBatchClaimWithWitness": "140929", + "qualifiedSplitClaim": "86524", + "qualifiedSplitClaimWithWitness": "86890", + "register": "25379", + "splitBatchClaim": "140399", + "splitBatchClaimWithWitness": "140363", + "splitBatchTransfer": "110614", + "splitBatchWithdrawal": "139822", + "splitClaim": "86447", + "splitClaimWithWitness": "85928", + "splitTransfer": "82751", + "splitWithdrawal": "93705" } \ No newline at end of file diff --git a/src/TheCompact.sol b/src/TheCompact.sol index b05cdd1..ffd3f9c 100644 --- a/src/TheCompact.sol +++ b/src/TheCompact.sol @@ -3,35 +3,25 @@ pragma solidity ^0.8.27; import { ITheCompact } from "./interfaces/ITheCompact.sol"; import { ITheCompactClaims } from "./interfaces/ITheCompactClaims.sol"; + +import { BatchTransfer, SplitBatchTransfer } from "./types/BatchClaims.sol"; +import { BasicTransfer, SplitTransfer } from "./types/Claims.sol"; import { CompactCategory } from "./types/CompactCategory.sol"; import { Lock } from "./types/Lock.sol"; import { Scope } from "./types/Scope.sol"; import { ResetPeriod } from "./types/ResetPeriod.sol"; import { ForcedWithdrawalStatus } from "./types/ForcedWithdrawalStatus.sol"; + +import { ClaimProcessor } from "./lib/ClaimProcessor.sol"; import { ConsumerLib } from "./lib/ConsumerLib.sol"; -import { IdLib } from "./lib/IdLib.sol"; import { EfficiencyLib } from "./lib/EfficiencyLib.sol"; +import { Extsload } from "./lib/Extsload.sol"; import { HashLib } from "./lib/HashLib.sol"; +import { IdLib } from "./lib/IdLib.sol"; import { ValidityLib } from "./lib/ValidityLib.sol"; -import { Extsload } from "./lib/Extsload.sol"; + import { ERC6909 } from "solady/tokens/ERC6909.sol"; -import { FixedPointMathLib } from "solady/utils/FixedPointMathLib.sol"; -import { SafeTransferLib } from "solady/utils/SafeTransferLib.sol"; -import { IPermit2 } from "permit2/src/interfaces/IPermit2.sol"; -import { Tstorish } from "tstorish/Tstorish.sol"; import { ISignatureTransfer } from "permit2/src/interfaces/ISignatureTransfer.sol"; -import { BasicTransfer, SplitTransfer } from "./types/Claims.sol"; - -import { BatchTransfer, SplitBatchTransfer } from "./types/BatchClaims.sol"; - -import { COMPACT_TYPEHASH } from "./types/EIP712Types.sol"; - -import { SplitComponent, TransferComponent, SplitByIdComponent, BatchClaimComponent, SplitBatchClaimComponent } from "./types/Components.sol"; - -import { IAllocator } from "./interfaces/IAllocator.sol"; -import { MetadataRenderer } from "./lib/MetadataRenderer.sol"; - -import { ClaimProcessor } from "./lib/ClaimProcessor.sol"; /** * @title The Compact @@ -41,7 +31,7 @@ import { ClaimProcessor } from "./lib/ClaimProcessor.sol"; * formation (and, if necessary, involuntary dissolution) of "resource locks." * This contract has not yet been properly tested, audited, or reviewed. */ -contract TheCompact is ITheCompact, ClaimProcessor, ERC6909 { +contract TheCompact is ITheCompact, ClaimProcessor, ERC6909, Extsload { using HashLib for address; using HashLib for bytes32; using HashLib for uint256; @@ -55,8 +45,6 @@ contract TheCompact is ITheCompact, ClaimProcessor, ERC6909 { using IdLib for Lock; using IdLib for ResetPeriod; using IdLib for CompactCategory; - using SafeTransferLib for address; - using FixedPointMathLib for uint256; using ConsumerLib for uint256; using EfficiencyLib for bool; using EfficiencyLib for uint256; @@ -190,11 +178,9 @@ contract TheCompact is ITheCompact, ClaimProcessor, ERC6909 { Scope, //scope address recipient, bytes calldata signature - ) external payable returns (uint256[] memory ids) { - uint256 totalTokensLessInitialNative; - bool firstUnderlyingTokenIsNative; - uint256[] memory initialTokenBalances; - (totalTokensLessInitialNative, firstUnderlyingTokenIsNative, ids, initialTokenBalances) = _preprocessAndPerformInitialNativeDeposit(permitted, recipient); + ) external payable returns (uint256[] memory) { + (uint256 totalTokensLessInitialNative, bool firstUnderlyingTokenIsNative, uint256[] memory ids, uint256[] memory initialTokenBalances) = + _preprocessAndPerformInitialNativeDeposit(permitted, recipient); bytes32 witness = _deriveCompactDepositWitnessHash(uint256(0x84).asStubborn()); @@ -213,6 +199,8 @@ contract TheCompact is ITheCompact, ClaimProcessor, ERC6909 { _writeSignatureAndPerformPermit2Call(m, uint256(0xc0).asStubborn(), signatureOffsetValue, signature); _verifyBalancesAndPerformDeposits(ids, permitted, initialTokenBalances, recipient, firstUnderlyingTokenIsNative); + + return ids; } function depositAndRegister( diff --git a/src/lib/ClaimProcessor.sol b/src/lib/ClaimProcessor.sol index fb6d7db..f9bb888 100644 --- a/src/lib/ClaimProcessor.sol +++ b/src/lib/ClaimProcessor.sol @@ -2,7 +2,7 @@ pragma solidity ^0.8.27; import { ITheCompactClaims } from "../interfaces/ITheCompactClaims.sol"; -import { InternalLogic } from "./InternalLogic.sol"; +import { ClaimProcessorLogic } from "./ClaimProcessorLogic.sol"; import { BasicClaim, QualifiedClaim, ClaimWithWitness, QualifiedClaimWithWitness, SplitClaim, SplitClaimWithWitness, QualifiedSplitClaim, QualifiedSplitClaimWithWitness } from "../types/Claims.sol"; @@ -55,7 +55,7 @@ import { ExogenousQualifiedSplitBatchMultichainClaimWithWitness } from "../types/BatchMultichainClaims.sol"; -contract ClaimProcessor is ITheCompactClaims, InternalLogic { +contract ClaimProcessor is ITheCompactClaims, ClaimProcessorLogic { function claim(BasicClaim calldata claimPayload) external returns (bool) { return _processBasicClaim(claimPayload, _release); } diff --git a/src/lib/ClaimProcessorLogic.sol b/src/lib/ClaimProcessorLogic.sol new file mode 100644 index 0000000..550268d --- /dev/null +++ b/src/lib/ClaimProcessorLogic.sol @@ -0,0 +1,898 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.27; + +import { BasicClaim, QualifiedClaim, ClaimWithWitness, QualifiedClaimWithWitness, SplitClaim, SplitClaimWithWitness, QualifiedSplitClaim, QualifiedSplitClaimWithWitness } from "../types/Claims.sol"; +import { + BatchClaim, + QualifiedBatchClaim, + BatchClaimWithWitness, + QualifiedBatchClaimWithWitness, + SplitBatchClaim, + SplitBatchClaimWithWitness, + QualifiedSplitBatchClaim, + QualifiedSplitBatchClaimWithWitness +} from "../types/BatchClaims.sol"; +import { + MultichainClaim, + QualifiedMultichainClaim, + MultichainClaimWithWitness, + QualifiedMultichainClaimWithWitness, + SplitMultichainClaim, + SplitMultichainClaimWithWitness, + QualifiedSplitMultichainClaim, + QualifiedSplitMultichainClaimWithWitness, + ExogenousMultichainClaim, + ExogenousQualifiedMultichainClaim, + ExogenousMultichainClaimWithWitness, + ExogenousQualifiedMultichainClaimWithWitness, + ExogenousSplitMultichainClaim, + ExogenousSplitMultichainClaimWithWitness, + ExogenousQualifiedSplitMultichainClaim, + ExogenousQualifiedSplitMultichainClaimWithWitness +} from "../types/MultichainClaims.sol"; +import { + BatchMultichainClaim, + QualifiedBatchMultichainClaim, + BatchMultichainClaimWithWitness, + QualifiedBatchMultichainClaimWithWitness, + SplitBatchMultichainClaim, + SplitBatchMultichainClaimWithWitness, + QualifiedSplitBatchMultichainClaim, + QualifiedSplitBatchMultichainClaimWithWitness, + ExogenousBatchMultichainClaim, + ExogenousQualifiedBatchMultichainClaim, + ExogenousBatchMultichainClaimWithWitness, + ExogenousQualifiedBatchMultichainClaimWithWitness, + ExogenousSplitBatchMultichainClaim, + ExogenousSplitBatchMultichainClaimWithWitness, + ExogenousQualifiedSplitBatchMultichainClaim, + ExogenousQualifiedSplitBatchMultichainClaimWithWitness +} from "../types/BatchMultichainClaims.sol"; +import { COMPACT_TYPEHASH, BATCH_COMPACT_TYPEHASH, MULTICHAIN_COMPACT_TYPEHASH } from "../types/EIP712Types.sol"; +import { SplitComponent, BatchClaimComponent, SplitBatchClaimComponent } from "../types/Components.sol"; +import { Scope } from "../types/Scope.sol"; + +import { EfficiencyLib } from "./EfficiencyLib.sol"; +import { FunctionCastLib } from "./FunctionCastLib.sol"; +import { HashLib } from "./HashLib.sol"; +import { IdLib } from "./IdLib.sol"; +import { InternalLogic } from "./InternalLogic.sol"; +import { ValidityLib } from "./ValidityLib.sol"; + +contract ClaimProcessorLogic is InternalLogic { + using HashLib for address; + using HashLib for bytes32; + using HashLib for uint256; + using HashLib for BasicClaim; + using HashLib for QualifiedClaim; + using HashLib for ClaimWithWitness; + using HashLib for QualifiedClaimWithWitness; + using HashLib for SplitClaim; + using HashLib for SplitClaimWithWitness; + using HashLib for QualifiedSplitClaim; + using HashLib for QualifiedSplitClaimWithWitness; + using HashLib for BatchClaim; + using HashLib for QualifiedBatchClaim; + using HashLib for BatchClaimWithWitness; + using HashLib for QualifiedBatchClaimWithWitness; + using HashLib for SplitBatchClaim; + using HashLib for SplitBatchClaimWithWitness; + using HashLib for QualifiedSplitBatchClaim; + using HashLib for QualifiedSplitBatchClaimWithWitness; + using HashLib for MultichainClaim; + using HashLib for QualifiedMultichainClaim; + using HashLib for MultichainClaimWithWitness; + using HashLib for QualifiedMultichainClaimWithWitness; + using HashLib for SplitMultichainClaim; + using HashLib for SplitMultichainClaimWithWitness; + using HashLib for QualifiedSplitMultichainClaim; + using HashLib for QualifiedSplitMultichainClaimWithWitness; + using HashLib for ExogenousMultichainClaim; + using HashLib for ExogenousQualifiedMultichainClaim; + using HashLib for ExogenousMultichainClaimWithWitness; + using HashLib for ExogenousQualifiedMultichainClaimWithWitness; + using HashLib for ExogenousSplitMultichainClaim; + using HashLib for ExogenousSplitMultichainClaimWithWitness; + using HashLib for ExogenousQualifiedSplitMultichainClaim; + using HashLib for ExogenousQualifiedSplitMultichainClaimWithWitness; + using HashLib for BatchMultichainClaim; + using HashLib for QualifiedBatchMultichainClaim; + using HashLib for BatchMultichainClaimWithWitness; + using HashLib for QualifiedBatchMultichainClaimWithWitness; + using HashLib for SplitBatchMultichainClaim; + using HashLib for SplitBatchMultichainClaimWithWitness; + using HashLib for QualifiedSplitBatchMultichainClaim; + using HashLib for QualifiedSplitBatchMultichainClaimWithWitness; + using HashLib for ExogenousBatchMultichainClaim; + using HashLib for ExogenousQualifiedBatchMultichainClaim; + using HashLib for ExogenousBatchMultichainClaimWithWitness; + using HashLib for ExogenousQualifiedBatchMultichainClaimWithWitness; + using HashLib for ExogenousSplitBatchMultichainClaim; + using HashLib for ExogenousSplitBatchMultichainClaimWithWitness; + using HashLib for ExogenousQualifiedSplitBatchMultichainClaim; + using HashLib for ExogenousQualifiedSplitBatchMultichainClaimWithWitness; + using IdLib for uint96; + using IdLib for uint256; + using IdLib for address; + using EfficiencyLib for bool; + using EfficiencyLib for bytes32; + using EfficiencyLib for uint256; + using ValidityLib for address; + using ValidityLib for uint96; + using ValidityLib for uint256; + using ValidityLib for bytes32; + using FunctionCastLib for function(bytes32, BasicClaim calldata, address) internal view; + using FunctionCastLib for function(bytes32, bytes32, QualifiedClaim calldata, address) internal view; + using FunctionCastLib for function(QualifiedClaim calldata) internal returns (bytes32, address); + using FunctionCastLib for function(QualifiedClaimWithWitness calldata) internal returns (bytes32, address); + using FunctionCastLib for function(bytes32, uint256, uint256, bytes32, function(address, address, uint256, uint256) internal returns (bool)) internal returns (bool); + using FunctionCastLib for function(bytes32, uint256, uint256, bytes32, bytes32, function(address, address, uint256, uint256) internal returns (bool)) internal returns (bool); + using FunctionCastLib for function(bytes32, bytes32, uint256, uint256, bytes32, function(address, address, uint256, uint256) internal returns (bool)) internal returns (bool); + using FunctionCastLib for function(bytes32, bytes32, uint256, uint256, bytes32, bytes32, function(address, address, uint256, uint256) internal returns (bool)) internal returns (bool); + + function _processSimpleClaim(bytes32 messageHash, uint256 calldataPointer, uint256 offsetToId, bytes32 typehash, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + return _processClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); + } + + function _processSimpleSplitClaim( + bytes32 messageHash, + uint256 calldataPointer, + uint256 offsetToId, + bytes32 typehash, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + return _processSplitClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); + } + + function _processSimpleBatchClaim( + bytes32 messageHash, + uint256 calldataPointer, + uint256 offsetToId, + bytes32 typehash, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + return _processBatchClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); + } + + function _processBatchClaimWithQualification( + bytes32 messageHash, + bytes32 qualificationMessageHash, + uint256 calldataPointer, + uint256 offsetToId, + bytes32 typehash, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + return _processBatchClaimWithQualificationAndSponsorDomain(messageHash, qualificationMessageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); + } + + function _processSimpleSplitBatchClaim( + bytes32 messageHash, + uint256 calldataPointer, + uint256 offsetToId, + bytes32 typehash, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + return _processSplitBatchClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); + } + + function _processSplitBatchClaimWithQualification( + bytes32 messageHash, + bytes32 qualificationMessageHash, + uint256 calldataPointer, + uint256 offsetToId, + bytes32 typehash, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + return _processSplitBatchClaimWithQualificationAndSponsorDomain(messageHash, qualificationMessageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); + } + + function _processClaimWithSponsorDomain( + bytes32 messageHash, + uint256 calldataPointer, + uint256 offsetToId, + bytes32 sponsorDomain, + bytes32 typehash, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + return _processClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, sponsorDomain, typehash, operation); + } + + function _processClaimWithQualification( + bytes32 messageHash, + bytes32 qualificationMessageHash, + uint256 calldataPointer, + uint256 offsetToId, + bytes32 typehash, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + return _processClaimWithQualificationAndSponsorDomain(messageHash, qualificationMessageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); + } + + function _processSplitClaimWithQualification( + bytes32 messageHash, + bytes32 qualificationMessageHash, + uint256 calldataPointer, + uint256 offsetToId, + bytes32 typehash, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + return _processSplitClaimWithQualificationAndSponsorDomain(messageHash, qualificationMessageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); + } + + function _processSplitClaimWithSponsorDomain( + bytes32 messageHash, + uint256 calldataPointer, + uint256 offsetToId, + bytes32 sponsorDomain, + bytes32 typehash, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + return _processSplitClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, sponsorDomain, typehash, operation); + } + + function _processBatchClaimWithSponsorDomain( + bytes32 messageHash, + uint256 calldataPointer, + uint256 offsetToId, + bytes32 sponsorDomain, + bytes32 typehash, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + return _processBatchClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, sponsorDomain, typehash, operation); + } + + function _processSplitBatchClaimWithSponsorDomain( + bytes32 messageHash, + uint256 calldataPointer, + uint256 offsetToId, + bytes32 sponsorDomain, + bytes32 typehash, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + return _processSplitBatchClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, sponsorDomain, typehash, operation); + } + + function _processClaimWithQualificationAndSponsorDomain( + bytes32 messageHash, + bytes32 qualificationMessageHash, + uint256 calldataPointer, + uint256 offsetToId, + bytes32 sponsorDomainSeparator, + bytes32 typehash, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + uint256 id; + uint256 allocatedAmount; + address claimant; + uint256 amount; + + assembly ("memory-safe") { + let calldataPointerWithOffset := add(calldataPointer, offsetToId) + id := calldataload(calldataPointerWithOffset) + allocatedAmount := calldataload(add(calldataPointerWithOffset, 0x20)) + claimant := shr(96, shl(96, calldataload(add(calldataPointerWithOffset, 0x40)))) + amount := calldataload(add(calldataPointerWithOffset, 0x60)) + } + + _ensureValidScope(sponsorDomainSeparator, id); + + amount.withinAllocated(allocatedAmount); + + return operation(_validate(id.toAllocatorId(), messageHash, qualificationMessageHash, calldataPointer, sponsorDomainSeparator, typehash), claimant, id, amount); + } + + function _processSplitClaimWithQualificationAndSponsorDomain( + bytes32 messageHash, + bytes32 qualificationMessageHash, + uint256 calldataPointer, + uint256 offsetToId, + bytes32 sponsorDomainSeparator, + bytes32 typehash, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + uint256 id; + uint256 allocatedAmount; + SplitComponent[] calldata claimants; + + assembly ("memory-safe") { + let calldataPointerWithOffset := add(calldataPointer, offsetToId) + id := calldataload(calldataPointerWithOffset) + allocatedAmount := calldataload(add(calldataPointerWithOffset, 0x20)) + + let claimantsPtr := add(calldataPointer, calldataload(add(calldataPointerWithOffset, 0x40))) + claimants.offset := add(0x20, claimantsPtr) + claimants.length := calldataload(claimantsPtr) + } + + address sponsor = _validate(id.toAllocatorId(), messageHash, qualificationMessageHash, calldataPointer, sponsorDomainSeparator, typehash); + + _ensureValidScope(sponsorDomainSeparator, id); + + return _verifyAndProcessSplitComponents(sponsor, id, allocatedAmount, claimants, operation); + } + + function _processBatchClaimWithQualificationAndSponsorDomain( + bytes32 messageHash, + bytes32 qualificationMessageHash, + uint256 calldataPointer, + uint256 offsetToId, + bytes32 sponsorDomainSeparator, + bytes32 typehash, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + BatchClaimComponent[] calldata claims; + address claimant; + assembly ("memory-safe") { + let calldataPointerWithOffset := add(calldataPointer, offsetToId) + let claimsPtr := add(calldataPointer, calldataload(calldataPointerWithOffset)) + claims.offset := add(0x20, claimsPtr) + claims.length := calldataload(claimsPtr) + + claimant := calldataload(add(calldataPointerWithOffset, 0x20)) + } + + uint96 firstAllocatorId = claims[0].id.toAllocatorId(); + + address sponsor = _validate(firstAllocatorId, messageHash, qualificationMessageHash, calldataPointer, sponsorDomainSeparator, typehash); + + uint256 totalClaims = claims.length; + + assembly ("memory-safe") { + if iszero(totalClaims) { + // revert InvalidBatchAllocation() + mstore(0, 0x3a03d3bb) + revert(0x1c, 0x04) + } + } + + // TODO: many of the bounds checks on these array accesses can be skipped as an optimization + BatchClaimComponent calldata component = claims[0]; + uint256 id = component.id; + uint256 amount = component.amount; + uint256 errorBuffer = (component.allocatedAmount < amount).or((sponsorDomainSeparator != bytes32(0)).and(id.toScope() == Scope.ChainSpecific)).asUint256(); + + operation(sponsor, claimant, id, amount); + + unchecked { + for (uint256 i = 1; i < totalClaims; ++i) { + component = claims[i]; + id = component.id; + amount = component.amount; + errorBuffer |= + (id.toAllocatorId() != firstAllocatorId).or(component.allocatedAmount < amount).or((sponsorDomainSeparator != bytes32(0)).and(id.toScope() == Scope.ChainSpecific)).asUint256(); + + operation(sponsor, claimant, id, amount); + } + + if (errorBuffer.asBool()) { + for (uint256 i = 0; i < totalClaims; ++i) { + component = claims[i]; + component.amount.withinAllocated(component.allocatedAmount); + id = component.id; + _ensureValidScope(sponsorDomainSeparator, component.id); + } + + assembly ("memory-safe") { + // revert InvalidBatchAllocation() + mstore(0, 0x3a03d3bb) + revert(0x1c, 0x04) + } + } + } + + return true; + } + + function _processSplitBatchClaimWithQualificationAndSponsorDomain( + bytes32 messageHash, + bytes32 qualificationMessageHash, + uint256 calldataPointer, + uint256 offsetToId, + bytes32 sponsorDomainSeparator, + bytes32 typehash, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + SplitBatchClaimComponent[] calldata claims; + assembly ("memory-safe") { + let claimsPtr := add(calldataPointer, calldataload(add(calldataPointer, offsetToId))) + claims.offset := add(0x20, claimsPtr) + claims.length := calldataload(claimsPtr) + } + + uint96 firstAllocatorId = claims[0].id.toAllocatorId(); + + address sponsor = _validate(firstAllocatorId, messageHash, qualificationMessageHash, calldataPointer, sponsorDomainSeparator, typehash); + + uint256 totalClaims = claims.length; + uint256 errorBuffer = (totalClaims == 0).asUint256(); + uint256 id; + + unchecked { + for (uint256 i = 0; i < totalClaims; ++i) { + SplitBatchClaimComponent calldata claimComponent = claims[i]; + id = claimComponent.id; + errorBuffer |= (id.toAllocatorId() != firstAllocatorId).or((sponsorDomainSeparator != bytes32(0)).and(id.toScope() == Scope.ChainSpecific)).asUint256(); + + _verifyAndProcessSplitComponents(sponsor, id, claimComponent.allocatedAmount, claimComponent.portions, operation); + } + + if (errorBuffer.asBool()) { + for (uint256 i = 0; i < totalClaims; ++i) { + _ensureValidScope(sponsorDomainSeparator, claims[i].id); + } + + assembly ("memory-safe") { + // revert InvalidBatchAllocation() + mstore(0, 0x3a03d3bb) + revert(0x1c, 0x04) + } + } + } + + return true; + } + + function _processBasicClaim(BasicClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + return _processSimpleClaim.usingBasicClaim()(claimPayload.toMessageHash(), claimPayload, uint256(0xa0).asStubborn(), _typehashes(uint256(0).asStubborn()), operation); + } + + function _processQualifiedClaim(QualifiedClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); + return _processClaimWithQualification.usingQualifiedClaim()(messageHash, qualificationMessageHash, claimPayload, 0xe0, _typehashes(uint256(0).asStubborn()), operation); + } + + function _processClaimWithWitness(ClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSimpleClaim.usingClaimWithWitness()(messageHash, claimPayload, 0xe0, typehash, operation); + } + + function _processQualifiedClaimWithWitness(QualifiedClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processClaimWithQualification.usingQualifiedClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x120, typehash, operation); + } + + function _processMultichainClaim(MultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + return _processSimpleClaim.usingMultichainClaim()(claimPayload.toMessageHash(), claimPayload, 0xc0, _typehashes(uint256(2).asStubborn()), operation); + } + + function _processQualifiedMultichainClaim(QualifiedMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); + return _processClaimWithQualification.usingQualifiedMultichainClaim()(messageHash, qualificationMessageHash, claimPayload, 0x100, _typehashes(uint256(2).asStubborn()), operation); + } + + function _processMultichainClaimWithWitness(MultichainClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSimpleClaim.usingMultichainClaimWithWitness()(messageHash, claimPayload, 0x100, typehash, operation); + } + + function _processQualifiedMultichainClaimWithWitness(QualifiedMultichainClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processClaimWithQualification.usingQualifiedMultichainClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x140, typehash, operation); + } + + function _processQualifiedSplitBatchMultichainClaim(QualifiedSplitBatchMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); + return _processSplitBatchClaimWithQualification.usingQualifiedSplitBatchMultichainClaim()( + messageHash, qualificationMessageHash, claimPayload, 0x100, _typehashes(uint256(1).asStubborn()), operation + ); + } + + function _processSplitBatchMultichainClaimWithWitness(SplitBatchMultichainClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSimpleSplitBatchClaim.usingSplitBatchMultichainClaimWithWitness()(messageHash, claimPayload, 0x100, typehash, operation); + } + + function _processQualifiedSplitBatchMultichainClaimWithWitness( + QualifiedSplitBatchMultichainClaimWithWitness calldata claimPayload, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSplitBatchClaimWithQualification.usingQualifiedSplitBatchMultichainClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x140, typehash, operation); + } + + function _processSplitMultichainClaim(SplitMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + return _processSimpleSplitClaim.usingSplitMultichainClaim()(claimPayload.toMessageHash(), claimPayload, 0xc0, _typehashes(uint256(2).asStubborn()), operation); + } + + function _processQualifiedSplitMultichainClaim(QualifiedSplitMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); + return _processSplitClaimWithQualification.usingQualifiedSplitMultichainClaim()(messageHash, qualificationMessageHash, claimPayload, 0x100, _typehashes(uint256(2).asStubborn()), operation); + } + + function _processSplitMultichainClaimWithWitness(SplitMultichainClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSimpleSplitClaim.usingSplitMultichainClaimWithWitness()(messageHash, claimPayload, 0x100, typehash, operation); + } + + function _processQualifiedSplitMultichainClaimWithWitness( + QualifiedSplitMultichainClaimWithWitness calldata claimPayload, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSplitClaimWithQualification.usingQualifiedSplitMultichainClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x140, typehash, operation); + } + + function _processBatchMultichainClaim(BatchMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + return _processSimpleBatchClaim.usingBatchMultichainClaim()(claimPayload.toMessageHash(), claimPayload, 0xc0, _typehashes(uint256(2).asStubborn()), operation); + } + + function _processQualifiedBatchMultichainClaim(QualifiedBatchMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); + return _processBatchClaimWithQualification.usingQualifiedBatchMultichainClaim()(messageHash, qualificationMessageHash, claimPayload, 0x100, _typehashes(uint256(2).asStubborn()), operation); + } + + function _processBatchMultichainClaimWithWitness(BatchMultichainClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSimpleBatchClaim.usingBatchMultichainClaimWithWitness()(messageHash, claimPayload, 0x100, typehash, operation); + } + + function _processQualifiedBatchMultichainClaimWithWitness( + QualifiedBatchMultichainClaimWithWitness calldata claimPayload, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processBatchClaimWithQualification.usingQualifiedBatchMultichainClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x140, typehash, operation); + } + + function _processExogenousQualifiedBatchMultichainClaim( + ExogenousQualifiedBatchMultichainClaim calldata claimPayload, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); + return _processBatchClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedBatchMultichainClaim()( + messageHash, qualificationMessageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation + ); + } + + function _processExogenousBatchMultichainClaimWithWitness( + ExogenousBatchMultichainClaimWithWitness calldata claimPayload, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processBatchClaimWithSponsorDomain.usingExogenousBatchMultichainClaimWithWitness()( + messageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation + ); + } + + function _processExogenousQualifiedBatchMultichainClaimWithWitness( + ExogenousQualifiedBatchMultichainClaimWithWitness calldata claimPayload, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processBatchClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedBatchMultichainClaimWithWitness()( + messageHash, qualificationMessageHash, claimPayload, 0x180, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation + ); + } + + function _processExogenousMultichainClaim(ExogenousMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + return _processClaimWithSponsorDomain.usingExogenousMultichainClaim()( + claimPayload.toMessageHash(), claimPayload, 0x100, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation + ); + } + + function _processSplitBatchMultichainClaim(SplitBatchMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + return _processSimpleSplitBatchClaim.usingSplitBatchMultichainClaim()(claimPayload.toMessageHash(), claimPayload, 0xc0, _typehashes(uint256(2).asStubborn()), operation); + } + + function _processExogenousMultichainClaimWithWitness(ExogenousMultichainClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return + _processClaimWithSponsorDomain.usingExogenousMultichainClaimWithWitness()(messageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation); + } + + function _processExogenousQualifiedMultichainClaim(ExogenousQualifiedMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); + return _processClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedMultichainClaim()( + messageHash, qualificationMessageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation + ); + } + + function _processExogenousQualifiedSplitMultichainClaim( + ExogenousQualifiedSplitMultichainClaim calldata claimPayload, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); + return _processSplitClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedSplitMultichainClaim()( + messageHash, qualificationMessageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation + ); + } + + function _processExogenousQualifiedMultichainClaimWithWitness( + ExogenousQualifiedMultichainClaimWithWitness calldata claimPayload, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedMultichainClaimWithWitness()( + messageHash, qualificationMessageHash, claimPayload, 0x180, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation + ); + } + + function _processExogenousSplitMultichainClaim(ExogenousSplitMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + return _processSplitClaimWithSponsorDomain.usingExogenousSplitMultichainClaim()( + claimPayload.toMessageHash(), claimPayload, 0x100, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation + ); + } + + function _processExogenousSplitMultichainClaimWithWitness( + ExogenousSplitMultichainClaimWithWitness calldata claimPayload, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSplitClaimWithSponsorDomain.usingExogenousSplitMultichainClaimWithWitness()( + messageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation + ); + } + + function _processExogenousQualifiedSplitMultichainClaimWithWitness( + ExogenousQualifiedSplitMultichainClaimWithWitness calldata claimPayload, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSplitClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedSplitMultichainClaimWithWitness()( + messageHash, qualificationMessageHash, claimPayload, 0x180, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation + ); + } + + function _processExogenousBatchMultichainClaim(ExogenousBatchMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + return _processBatchClaimWithSponsorDomain.usingExogenousBatchMultichainClaim()( + claimPayload.toMessageHash(), claimPayload, 0x100, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation + ); + } + + function _processExogenousSplitBatchMultichainClaim(ExogenousSplitBatchMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + return _processSplitBatchClaimWithSponsorDomain.usingExogenousSplitBatchMultichainClaim()( + claimPayload.toMessageHash(), claimPayload, 0x100, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation + ); + } + + function _processExogenousQualifiedSplitBatchMultichainClaim( + ExogenousQualifiedSplitBatchMultichainClaim calldata claimPayload, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); + return _processSplitBatchClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedSplitBatchMultichainClaim()( + messageHash, qualificationMessageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation + ); + } + + function _processExogenousSplitBatchMultichainClaimWithWitness( + ExogenousSplitBatchMultichainClaimWithWitness calldata claimPayload, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSplitBatchClaimWithSponsorDomain.usingExogenousSplitBatchMultichainClaimWithWitness()( + messageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation + ); + } + + function _processExogenousQualifiedSplitBatchMultichainClaimWithWitness( + ExogenousQualifiedSplitBatchMultichainClaimWithWitness calldata claimPayload, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSplitBatchClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedSplitBatchMultichainClaimWithWitness()( + messageHash, qualificationMessageHash, claimPayload, 0x180, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation + ); + } + + function _processSplitClaim(SplitClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + return _processSimpleSplitClaim.usingSplitClaim()(claimPayload.toMessageHash(), claimPayload, 0xa0, _typehashes(uint256(0).asStubborn()), operation); + } + + function _processQualifiedSplitClaim(QualifiedSplitClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); + return _processSplitClaimWithQualification.usingQualifiedSplitClaim()(messageHash, qualificationMessageHash, claimPayload, 0xe0, _typehashes(uint256(0).asStubborn()), operation); + } + + function _processSplitClaimWithWitness(SplitClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSimpleSplitClaim.usingSplitClaimWithWitness()(messageHash, claimPayload, 0xe0, typehash, operation); + } + + function _processQualifiedSplitClaimWithWitness(QualifiedSplitClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSplitClaimWithQualification.usingQualifiedSplitClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x120, typehash, operation); + } + + function _processBatchClaim(BatchClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + return _processSimpleBatchClaim.usingBatchClaim()(claimPayload.toMessageHash(), claimPayload, 0xa0, _typehashes(uint256(1).asStubborn()), operation); + } + + function _processSplitBatchClaim(SplitBatchClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + return _processSimpleSplitBatchClaim.usingSplitBatchClaim()(claimPayload.toMessageHash(), claimPayload, 0xa0, _typehashes(uint256(1).asStubborn()), operation); + } + + function _processQualifiedSplitBatchClaim(QualifiedSplitBatchClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); + return _processSplitBatchClaimWithQualification.usingQualifiedSplitBatchClaim()(messageHash, qualificationMessageHash, claimPayload, 0xe0, _typehashes(uint256(1).asStubborn()), operation); + } + + function _processQualifiedSplitBatchClaimWithWitness(QualifiedSplitBatchClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSplitBatchClaimWithQualification.usingQualifiedSplitBatchClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x120, typehash, operation); + } + + function _processSplitBatchClaimWithWitness(SplitBatchClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSimpleSplitBatchClaim.usingSplitBatchClaimWithWitness()(messageHash, claimPayload, 0xe0, typehash, operation); + } + + function _processQualifiedBatchClaim(QualifiedBatchClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); + return _processBatchClaimWithQualification.usingQualifiedBatchClaim()(messageHash, qualificationMessageHash, claimPayload, 0xe0, _typehashes(uint256(1).asStubborn()), operation); + } + + function _processBatchClaimWithWitness(BatchClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { + (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processSimpleBatchClaim.usingBatchClaimWithWitness()(messageHash, claimPayload, 0xe0, typehash, operation); + } + + function _processQualifiedBatchClaimWithWitness(QualifiedBatchClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) + internal + returns (bool) + { + (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); + return _processBatchClaimWithQualification.usingQualifiedBatchClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x120, typehash, operation); + } + + function _verifyAndProcessSplitComponents( + address sponsor, + uint256 id, + uint256 allocatedAmount, + SplitComponent[] calldata claimants, + function(address, address, uint256, uint256) internal returns (bool) operation + ) internal returns (bool) { + uint256 totalClaims = claimants.length; + uint256 spentAmount = 0; + uint256 errorBuffer = (totalClaims == 0).asUint256(); + + unchecked { + for (uint256 i = 0; i < totalClaims; ++i) { + SplitComponent calldata component = claimants[i]; + uint256 amount = component.amount; + + uint256 updatedSpentAmount = amount + spentAmount; + errorBuffer |= (updatedSpentAmount < spentAmount).asUint256(); + spentAmount = updatedSpentAmount; + + operation(sponsor, component.claimant, id, amount); + } + } + + errorBuffer |= (allocatedAmount < spentAmount).asUint256(); + assembly ("memory-safe") { + if errorBuffer { + // revert AllocatedAmountExceeded(allocatedAmount, amount); + mstore(0, 0x3078b2f6) + mstore(0x20, allocatedAmount) + mstore(0x40, spentAmount) + revert(0x1c, 0x44) + } + } + + return true; + } + + function _validate(uint96 allocatorId, bytes32 messageHash, bytes32 qualificationMessageHash, uint256 calldataPointer, bytes32 sponsorDomainSeparator, bytes32 typehash) + internal + returns (address sponsor) + { + bytes calldata allocatorSignature; + bytes calldata sponsorSignature; + uint256 nonce; + uint256 expires; + + assembly ("memory-safe") { + let allocatorSignaturePtr := add(calldataPointer, calldataload(calldataPointer)) + allocatorSignature.offset := add(0x20, allocatorSignaturePtr) + allocatorSignature.length := calldataload(allocatorSignaturePtr) + + let sponsorSignaturePtr := add(calldataPointer, calldataload(add(calldataPointer, 0x20))) + sponsorSignature.offset := add(0x20, sponsorSignaturePtr) + sponsorSignature.length := calldataload(sponsorSignaturePtr) + + sponsor := shr(96, shl(96, calldataload(add(calldataPointer, 0x40)))) + nonce := calldataload(add(calldataPointer, 0x60)) + expires := calldataload(add(calldataPointer, 0x80)) + } + + expires.later(); + + address allocator = allocatorId.fromRegisteredAllocatorIdWithConsumed(nonce); + + bytes32 domainSeparator = _domainSeparator(); + assembly ("memory-safe") { + sponsorDomainSeparator := add(sponsorDomainSeparator, mul(iszero(sponsorDomainSeparator), domainSeparator)) + } + + if ((sponsorDomainSeparator != domainSeparator).or(sponsorSignature.length != 0) || _hasNoActiveRegistration(sponsor, messageHash, typehash)) { + messageHash.signedBy(sponsor, sponsorSignature, sponsorDomainSeparator); + } + qualificationMessageHash.signedBy(allocator, allocatorSignature, domainSeparator); + + _emitClaim(sponsor, messageHash, allocator); + } + + function _hasNoActiveRegistration(address sponsor, bytes32 claimHash, bytes32 typehash) internal view returns (bool) { + return _getRegistrationStatus(sponsor, claimHash, typehash) <= block.timestamp; + } + + function _ensureValidScope(bytes32 sponsorDomainSeparator, uint256 id) internal pure { + assembly ("memory-safe") { + if iszero(or(iszero(sponsorDomainSeparator), iszero(shr(255, id)))) { + // revert InvalidScope(id) + mstore(0, 0xa06356f5) + mstore(0x20, id) + revert(0x1c, 0x24) + } + } + } + + function _typehashes(uint256 i) internal pure returns (bytes32 typehash) { + assembly ("memory-safe") { + let m := mload(0x40) + mstore(0, COMPACT_TYPEHASH) + mstore(0x20, BATCH_COMPACT_TYPEHASH) + mstore(0x40, MULTICHAIN_COMPACT_TYPEHASH) + typehash := mload(shl(5, i)) + mstore(0x40, m) + } + } +} diff --git a/src/lib/Extsload.sol b/src/lib/Extsload.sol index 1681301..e533d4e 100644 --- a/src/lib/Extsload.sol +++ b/src/lib/Extsload.sol @@ -1,35 +1,18 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.27; -// TODO: add this back in once we have more headroom? contract Extsload { - function extsload(bytes32 slot) external view returns (bytes32) { + function exttload(bytes32 slot) external view returns (bytes32) { assembly ("memory-safe") { - mstore(0, sload(slot)) + mstore(0, tload(slot)) return(0, 0x20) } } - function extsload(bytes32 startSlot, uint256 nSlots) external view returns (bytes32[] memory) { + function extsload(bytes32 slot) external view returns (bytes32) { assembly ("memory-safe") { - let memptr := mload(0x40) - let start := memptr - // A left bit-shift of 5 is equivalent to multiplying by 32 but costs less gas. - let length := shl(5, nSlots) - // The abi offset of dynamic array in the returndata is 32. - mstore(memptr, 0x20) - // Store the length of the array returned - mstore(add(memptr, 0x20), nSlots) - // update memptr to the first location to hold a result - memptr := add(memptr, 0x40) - let end := add(memptr, length) - for { } 1 { } { - mstore(memptr, sload(startSlot)) - memptr := add(memptr, 0x20) - startSlot := add(startSlot, 1) - if iszero(lt(memptr, end)) { break } - } - return(start, sub(end, start)) + mstore(0, sload(slot)) + return(0, 0x20) } } diff --git a/src/lib/InternalLogic.sol b/src/lib/InternalLogic.sol index ee9b8f1..c72e37f 100644 --- a/src/lib/InternalLogic.sol +++ b/src/lib/InternalLogic.sol @@ -1,90 +1,10 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.27; -import { ITheCompact } from "../interfaces/ITheCompact.sol"; -import { ITheCompactClaims } from "../interfaces/ITheCompactClaims.sol"; +import { BatchTransfer, SplitBatchTransfer } from "../types/BatchClaims.sol"; +import { BasicTransfer, SplitTransfer } from "../types/Claims.sol"; import { CompactCategory } from "../types/CompactCategory.sol"; -import { Lock } from "../types/Lock.sol"; -import { Scope } from "../types/Scope.sol"; -import { ResetPeriod } from "../types/ResetPeriod.sol"; -import { ForcedWithdrawalStatus } from "../types/ForcedWithdrawalStatus.sol"; -import { ConsumerLib } from "./ConsumerLib.sol"; -import { IdLib } from "./IdLib.sol"; -import { EfficiencyLib } from "./EfficiencyLib.sol"; -import { FunctionCastLib } from "./FunctionCastLib.sol"; -import { HashLib } from "./HashLib.sol"; -import { ValidityLib } from "./ValidityLib.sol"; -import { Extsload } from "./Extsload.sol"; -import { ERC6909 } from "solady/tokens/ERC6909.sol"; -import { FixedPointMathLib } from "solady/utils/FixedPointMathLib.sol"; -import { SafeTransferLib } from "solady/utils/SafeTransferLib.sol"; -import { IPermit2 } from "permit2/src/interfaces/IPermit2.sol"; -import { Tstorish } from "tstorish/Tstorish.sol"; -import { ISignatureTransfer } from "permit2/src/interfaces/ISignatureTransfer.sol"; -import { - BasicTransfer, - SplitTransfer, - BasicClaim, - QualifiedClaim, - ClaimWithWitness, - QualifiedClaimWithWitness, - SplitClaim, - SplitClaimWithWitness, - QualifiedSplitClaim, - QualifiedSplitClaimWithWitness -} from "../types/Claims.sol"; - -import { - BatchTransfer, - SplitBatchTransfer, - BatchClaim, - QualifiedBatchClaim, - BatchClaimWithWitness, - QualifiedBatchClaimWithWitness, - SplitBatchClaim, - SplitBatchClaimWithWitness, - QualifiedSplitBatchClaim, - QualifiedSplitBatchClaimWithWitness -} from "../types/BatchClaims.sol"; - -import { - MultichainClaim, - QualifiedMultichainClaim, - MultichainClaimWithWitness, - QualifiedMultichainClaimWithWitness, - SplitMultichainClaim, - SplitMultichainClaimWithWitness, - QualifiedSplitMultichainClaim, - QualifiedSplitMultichainClaimWithWitness, - ExogenousMultichainClaim, - ExogenousQualifiedMultichainClaim, - ExogenousMultichainClaimWithWitness, - ExogenousQualifiedMultichainClaimWithWitness, - ExogenousSplitMultichainClaim, - ExogenousSplitMultichainClaimWithWitness, - ExogenousQualifiedSplitMultichainClaim, - ExogenousQualifiedSplitMultichainClaimWithWitness -} from "../types/MultichainClaims.sol"; - -import { - BatchMultichainClaim, - QualifiedBatchMultichainClaim, - BatchMultichainClaimWithWitness, - QualifiedBatchMultichainClaimWithWitness, - SplitBatchMultichainClaim, - SplitBatchMultichainClaimWithWitness, - QualifiedSplitBatchMultichainClaim, - QualifiedSplitBatchMultichainClaimWithWitness, - ExogenousBatchMultichainClaim, - ExogenousQualifiedBatchMultichainClaim, - ExogenousBatchMultichainClaimWithWitness, - ExogenousQualifiedBatchMultichainClaimWithWitness, - ExogenousSplitBatchMultichainClaim, - ExogenousSplitBatchMultichainClaimWithWitness, - ExogenousQualifiedSplitBatchMultichainClaim, - ExogenousQualifiedSplitBatchMultichainClaimWithWitness -} from "../types/BatchMultichainClaims.sol"; - +import { SplitComponent, TransferComponent, SplitByIdComponent } from "../types/Components.sol"; import { COMPACT_TYPEHASH, BATCH_COMPACT_TYPEHASH, @@ -115,86 +35,43 @@ import { PERMIT2_ACTIVATION_MULTICHAIN_COMPACT_TYPESTRING_FRAGMENT_THREE, PERMIT2_ACTIVATION_MULTICHAIN_COMPACT_TYPESTRING_FRAGMENT_FOUR, PERMIT2_ACTIVATION_MULTICHAIN_COMPACT_TYPESTRING_FRAGMENT_FIVE, - PERMIT2_ACTIVATION_MULTICHAIN_COMPACT_TYPESTRING_FRAGMENT_SIX + PERMIT2_ACTIVATION_MULTICHAIN_COMPACT_TYPESTRING_FRAGMENT_SIX, + COMPACT_DEPOSIT_TYPESTRING_FRAGMENT_ONE, + COMPACT_DEPOSIT_TYPESTRING_FRAGMENT_TWO, + COMPACT_DEPOSIT_TYPESTRING_FRAGMENT_THREE, + COMPACT_DEPOSIT_TYPESTRING_FRAGMENT_FOUR, + COMPACT_DEPOSIT_TYPESTRING_FRAGMENT_FIVE } from "../types/EIP712Types.sol"; +import { Lock } from "../types/Lock.sol"; +import { ResetPeriod } from "../types/ResetPeriod.sol"; +import { Scope } from "../types/Scope.sol"; -import { SplitComponent, TransferComponent, SplitByIdComponent, BatchClaimComponent, SplitBatchClaimComponent } from "../types/Components.sol"; - -import { IAllocator } from "../interfaces/IAllocator.sol"; +import { ConsumerLib } from "./ConsumerLib.sol"; +import { EfficiencyLib } from "./EfficiencyLib.sol"; +import { FunctionCastLib } from "./FunctionCastLib.sol"; +import { HashLib } from "./HashLib.sol"; +import { IdLib } from "./IdLib.sol"; import { MetadataRenderer } from "./MetadataRenderer.sol"; +import { ValidityLib } from "./ValidityLib.sol"; + +import { SafeTransferLib } from "solady/utils/SafeTransferLib.sol"; +import { Tstorish } from "tstorish/Tstorish.sol"; +import { ISignatureTransfer } from "permit2/src/interfaces/ISignatureTransfer.sol"; -/** - * @title The Compact - * @custom:version 0 (early-stage proof-of-concept) - * @author 0age (0age.eth) - * @notice The Compact is an ownerless ERC6909 contract that facilitates the voluntary - * formation (and, if necessary, involuntary dissolution) of "resource locks." - * This contract has not yet been properly tested, audited, or reviewed. - */ contract InternalLogic is Tstorish { using HashLib for address; using HashLib for bytes32; using HashLib for uint256; using HashLib for BasicTransfer; using HashLib for SplitTransfer; - using HashLib for BasicClaim; - using HashLib for QualifiedClaim; - using HashLib for ClaimWithWitness; - using HashLib for QualifiedClaimWithWitness; - using HashLib for SplitClaim; - using HashLib for SplitClaimWithWitness; - using HashLib for QualifiedSplitClaim; - using HashLib for QualifiedSplitClaimWithWitness; using HashLib for BatchTransfer; using HashLib for SplitBatchTransfer; - using HashLib for BatchClaim; - using HashLib for QualifiedBatchClaim; - using HashLib for BatchClaimWithWitness; - using HashLib for QualifiedBatchClaimWithWitness; - using HashLib for SplitBatchClaim; - using HashLib for SplitBatchClaimWithWitness; - using HashLib for QualifiedSplitBatchClaim; - using HashLib for QualifiedSplitBatchClaimWithWitness; - using HashLib for MultichainClaim; - using HashLib for QualifiedMultichainClaim; - using HashLib for MultichainClaimWithWitness; - using HashLib for QualifiedMultichainClaimWithWitness; - using HashLib for SplitMultichainClaim; - using HashLib for SplitMultichainClaimWithWitness; - using HashLib for QualifiedSplitMultichainClaim; - using HashLib for QualifiedSplitMultichainClaimWithWitness; - using HashLib for ExogenousMultichainClaim; - using HashLib for ExogenousQualifiedMultichainClaim; - using HashLib for ExogenousMultichainClaimWithWitness; - using HashLib for ExogenousQualifiedMultichainClaimWithWitness; - using HashLib for ExogenousSplitMultichainClaim; - using HashLib for ExogenousSplitMultichainClaimWithWitness; - using HashLib for ExogenousQualifiedSplitMultichainClaim; - using HashLib for ExogenousQualifiedSplitMultichainClaimWithWitness; - using HashLib for BatchMultichainClaim; - using HashLib for QualifiedBatchMultichainClaim; - using HashLib for BatchMultichainClaimWithWitness; - using HashLib for QualifiedBatchMultichainClaimWithWitness; - using HashLib for SplitBatchMultichainClaim; - using HashLib for SplitBatchMultichainClaimWithWitness; - using HashLib for QualifiedSplitBatchMultichainClaim; - using HashLib for QualifiedSplitBatchMultichainClaimWithWitness; - using HashLib for ExogenousBatchMultichainClaim; - using HashLib for ExogenousQualifiedBatchMultichainClaim; - using HashLib for ExogenousBatchMultichainClaimWithWitness; - using HashLib for ExogenousQualifiedBatchMultichainClaimWithWitness; - using HashLib for ExogenousSplitBatchMultichainClaim; - using HashLib for ExogenousSplitBatchMultichainClaimWithWitness; - using HashLib for ExogenousQualifiedSplitBatchMultichainClaim; - using HashLib for ExogenousQualifiedSplitBatchMultichainClaimWithWitness; using IdLib for uint96; using IdLib for uint256; using IdLib for address; using IdLib for Lock; using IdLib for ResetPeriod; - using IdLib for CompactCategory; using SafeTransferLib for address; - using FixedPointMathLib for uint256; using ConsumerLib for uint256; using EfficiencyLib for bool; using EfficiencyLib for bytes32; @@ -204,15 +81,6 @@ contract InternalLogic is Tstorish { using ValidityLib for uint256; using ValidityLib for bytes32; using FunctionCastLib for function(bytes32, address, BasicTransfer calldata) internal; - using FunctionCastLib for function(TransferComponent[] calldata, uint256) internal returns (address); - using FunctionCastLib for function(bytes32, BasicClaim calldata, address) internal view; - using FunctionCastLib for function(bytes32, bytes32, QualifiedClaim calldata, address) internal view; - using FunctionCastLib for function(QualifiedClaim calldata) internal returns (bytes32, address); - using FunctionCastLib for function(QualifiedClaimWithWitness calldata) internal returns (bytes32, address); - using FunctionCastLib for function(bytes32, uint256, uint256, bytes32, function(address, address, uint256, uint256) internal returns (bool)) internal returns (bool); - using FunctionCastLib for function(bytes32, uint256, uint256, bytes32, bytes32, function(address, address, uint256, uint256) internal returns (bool)) internal returns (bool); - using FunctionCastLib for function(bytes32, bytes32, uint256, uint256, bytes32, function(address, address, uint256, uint256) internal returns (bool)) internal returns (bool); - using FunctionCastLib for function(bytes32, bytes32, uint256, uint256, bytes32, bytes32, function(address, address, uint256, uint256) internal returns (bool)) internal returns (bool); address private constant _PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3; @@ -371,302 +239,6 @@ contract InternalLogic is Tstorish { return true; } - function _processSimpleClaim(bytes32 messageHash, uint256 calldataPointer, uint256 offsetToId, bytes32 typehash, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - return _processClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); - } - - function _processSimpleSplitClaim( - bytes32 messageHash, - uint256 calldataPointer, - uint256 offsetToId, - bytes32 typehash, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - return _processSplitClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); - } - - function _processSimpleBatchClaim( - bytes32 messageHash, - uint256 calldataPointer, - uint256 offsetToId, - bytes32 typehash, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - return _processBatchClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); - } - - function _processBatchClaimWithQualification( - bytes32 messageHash, - bytes32 qualificationMessageHash, - uint256 calldataPointer, - uint256 offsetToId, - bytes32 typehash, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - return _processBatchClaimWithQualificationAndSponsorDomain(messageHash, qualificationMessageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); - } - - function _processSimpleSplitBatchClaim( - bytes32 messageHash, - uint256 calldataPointer, - uint256 offsetToId, - bytes32 typehash, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - return _processSplitBatchClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); - } - - function _processSplitBatchClaimWithQualification( - bytes32 messageHash, - bytes32 qualificationMessageHash, - uint256 calldataPointer, - uint256 offsetToId, - bytes32 typehash, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - return _processSplitBatchClaimWithQualificationAndSponsorDomain(messageHash, qualificationMessageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); - } - - function _processClaimWithSponsorDomain( - bytes32 messageHash, - uint256 calldataPointer, - uint256 offsetToId, - bytes32 sponsorDomain, - bytes32 typehash, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - return _processClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, sponsorDomain, typehash, operation); - } - - function _processClaimWithQualification( - bytes32 messageHash, - bytes32 qualificationMessageHash, - uint256 calldataPointer, - uint256 offsetToId, - bytes32 typehash, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - return _processClaimWithQualificationAndSponsorDomain(messageHash, qualificationMessageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); - } - - function _processSplitClaimWithQualification( - bytes32 messageHash, - bytes32 qualificationMessageHash, - uint256 calldataPointer, - uint256 offsetToId, - bytes32 typehash, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - return _processSplitClaimWithQualificationAndSponsorDomain(messageHash, qualificationMessageHash, calldataPointer, offsetToId, bytes32(0).asStubborn(), typehash, operation); - } - - function _validate(uint96 allocatorId, bytes32 messageHash, bytes32 qualificationMessageHash, uint256 calldataPointer, bytes32 sponsorDomainSeparator, bytes32 typehash) - internal - returns (address sponsor) - { - bytes calldata allocatorSignature; - bytes calldata sponsorSignature; - uint256 nonce; - uint256 expires; - - assembly ("memory-safe") { - let allocatorSignaturePtr := add(calldataPointer, calldataload(calldataPointer)) - allocatorSignature.offset := add(0x20, allocatorSignaturePtr) - allocatorSignature.length := calldataload(allocatorSignaturePtr) - - let sponsorSignaturePtr := add(calldataPointer, calldataload(add(calldataPointer, 0x20))) - sponsorSignature.offset := add(0x20, sponsorSignaturePtr) - sponsorSignature.length := calldataload(sponsorSignaturePtr) - - sponsor := shr(96, shl(96, calldataload(add(calldataPointer, 0x40)))) - nonce := calldataload(add(calldataPointer, 0x60)) - expires := calldataload(add(calldataPointer, 0x80)) - } - - expires.later(); - - address allocator = allocatorId.fromRegisteredAllocatorIdWithConsumed(nonce); - - bytes32 domainSeparator = _INITIAL_DOMAIN_SEPARATOR.toLatest(_INITIAL_CHAIN_ID); - assembly ("memory-safe") { - sponsorDomainSeparator := add(sponsorDomainSeparator, mul(iszero(sponsorDomainSeparator), domainSeparator)) - } - - if ((sponsorDomainSeparator != domainSeparator).or(sponsorSignature.length != 0) || _hasNoActiveRegistration(sponsor, messageHash, typehash)) { - messageHash.signedBy(sponsor, sponsorSignature, sponsorDomainSeparator); - } - qualificationMessageHash.signedBy(allocator, allocatorSignature, domainSeparator); - - _emitClaim(sponsor, messageHash, allocator); - } - - function _processSplitClaimWithSponsorDomain( - bytes32 messageHash, - uint256 calldataPointer, - uint256 offsetToId, - bytes32 sponsorDomain, - bytes32 typehash, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - return _processSplitClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, sponsorDomain, typehash, operation); - } - - function _processBatchClaimWithSponsorDomain( - bytes32 messageHash, - uint256 calldataPointer, - uint256 offsetToId, - bytes32 sponsorDomain, - bytes32 typehash, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - return _processBatchClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, sponsorDomain, typehash, operation); - } - - function _processSplitBatchClaimWithSponsorDomain( - bytes32 messageHash, - uint256 calldataPointer, - uint256 offsetToId, - bytes32 sponsorDomain, - bytes32 typehash, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - return _processSplitBatchClaimWithQualificationAndSponsorDomain(messageHash, messageHash, calldataPointer, offsetToId, sponsorDomain, typehash, operation); - } - - function _processClaimWithQualificationAndSponsorDomain( - bytes32 messageHash, - bytes32 qualificationMessageHash, - uint256 calldataPointer, - uint256 offsetToId, - bytes32 sponsorDomainSeparator, - bytes32 typehash, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - uint256 id; - uint256 allocatedAmount; - address claimant; - uint256 amount; - - assembly ("memory-safe") { - let calldataPointerWithOffset := add(calldataPointer, offsetToId) - id := calldataload(calldataPointerWithOffset) - allocatedAmount := calldataload(add(calldataPointerWithOffset, 0x20)) - claimant := shr(96, shl(96, calldataload(add(calldataPointerWithOffset, 0x40)))) - amount := calldataload(add(calldataPointerWithOffset, 0x60)) - } - - _ensureValidScope(sponsorDomainSeparator, id); - - amount.withinAllocated(allocatedAmount); - - return operation(_validate(id.toAllocatorId(), messageHash, qualificationMessageHash, calldataPointer, sponsorDomainSeparator, typehash), claimant, id, amount); - } - - function _processSplitClaimWithQualificationAndSponsorDomain( - bytes32 messageHash, - bytes32 qualificationMessageHash, - uint256 calldataPointer, - uint256 offsetToId, - bytes32 sponsorDomainSeparator, - bytes32 typehash, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - uint256 id; - uint256 allocatedAmount; - SplitComponent[] calldata claimants; - - assembly ("memory-safe") { - let calldataPointerWithOffset := add(calldataPointer, offsetToId) - id := calldataload(calldataPointerWithOffset) - allocatedAmount := calldataload(add(calldataPointerWithOffset, 0x20)) - - let claimantsPtr := add(calldataPointer, calldataload(add(calldataPointerWithOffset, 0x40))) - claimants.offset := add(0x20, claimantsPtr) - claimants.length := calldataload(claimantsPtr) - } - - address sponsor = _validate(id.toAllocatorId(), messageHash, qualificationMessageHash, calldataPointer, sponsorDomainSeparator, typehash); - - _ensureValidScope(sponsorDomainSeparator, id); - - return _verifyAndProcessSplitComponents(sponsor, id, allocatedAmount, claimants, operation); - } - - function _processBatchClaimWithQualificationAndSponsorDomain( - bytes32 messageHash, - bytes32 qualificationMessageHash, - uint256 calldataPointer, - uint256 offsetToId, - bytes32 sponsorDomainSeparator, - bytes32 typehash, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - BatchClaimComponent[] calldata claims; - address claimant; - assembly ("memory-safe") { - let calldataPointerWithOffset := add(calldataPointer, offsetToId) - let claimsPtr := add(calldataPointer, calldataload(calldataPointerWithOffset)) - claims.offset := add(0x20, claimsPtr) - claims.length := calldataload(claimsPtr) - - claimant := calldataload(add(calldataPointerWithOffset, 0x20)) - } - - uint96 firstAllocatorId = claims[0].id.toAllocatorId(); - - address sponsor = _validate(firstAllocatorId, messageHash, qualificationMessageHash, calldataPointer, sponsorDomainSeparator, typehash); - - uint256 totalClaims = claims.length; - - assembly ("memory-safe") { - if iszero(totalClaims) { - // revert InvalidBatchAllocation() - mstore(0, 0x3a03d3bb) - revert(0x1c, 0x04) - } - } - - // TODO: many of the bounds checks on these array accesses can be skipped as an optimization - BatchClaimComponent calldata component = claims[0]; - uint256 id = component.id; - uint256 amount = component.amount; - uint256 errorBuffer = (component.allocatedAmount < amount).or((sponsorDomainSeparator != bytes32(0)).and(id.toScope() == Scope.ChainSpecific)).asUint256(); - - operation(sponsor, claimant, id, amount); - - unchecked { - for (uint256 i = 1; i < totalClaims; ++i) { - component = claims[i]; - id = component.id; - amount = component.amount; - errorBuffer |= - (id.toAllocatorId() != firstAllocatorId).or(component.allocatedAmount < amount).or((sponsorDomainSeparator != bytes32(0)).and(id.toScope() == Scope.ChainSpecific)).asUint256(); - - operation(sponsor, claimant, id, amount); - } - - if (errorBuffer.asBool()) { - for (uint256 i = 0; i < totalClaims; ++i) { - component = claims[i]; - component.amount.withinAllocated(component.allocatedAmount); - id = component.id; - _ensureValidScope(sponsorDomainSeparator, component.id); - } - - assembly ("memory-safe") { - // revert InvalidBatchAllocation() - mstore(0, 0x3a03d3bb) - revert(0x1c, 0x04) - } - } - } - - return true; - } - function _setReentrancyGuard() internal { _setTstorish(_REENTRANCY_GUARD_SLOT, 1); } @@ -675,412 +247,6 @@ contract InternalLogic is Tstorish { _clearTstorish(_REENTRANCY_GUARD_SLOT); } - function _processSplitBatchClaimWithQualificationAndSponsorDomain( - bytes32 messageHash, - bytes32 qualificationMessageHash, - uint256 calldataPointer, - uint256 offsetToId, - bytes32 sponsorDomainSeparator, - bytes32 typehash, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - SplitBatchClaimComponent[] calldata claims; - assembly ("memory-safe") { - let claimsPtr := add(calldataPointer, calldataload(add(calldataPointer, offsetToId))) - claims.offset := add(0x20, claimsPtr) - claims.length := calldataload(claimsPtr) - } - - uint96 firstAllocatorId = claims[0].id.toAllocatorId(); - - address sponsor = _validate(firstAllocatorId, messageHash, qualificationMessageHash, calldataPointer, sponsorDomainSeparator, typehash); - - uint256 totalClaims = claims.length; - uint256 errorBuffer = (totalClaims == 0).asUint256(); - uint256 id; - - unchecked { - for (uint256 i = 0; i < totalClaims; ++i) { - SplitBatchClaimComponent calldata claimComponent = claims[i]; - id = claimComponent.id; - errorBuffer |= (id.toAllocatorId() != firstAllocatorId).or((sponsorDomainSeparator != bytes32(0)).and(id.toScope() == Scope.ChainSpecific)).asUint256(); - - _verifyAndProcessSplitComponents(sponsor, id, claimComponent.allocatedAmount, claimComponent.portions, operation); - } - - if (errorBuffer.asBool()) { - for (uint256 i = 0; i < totalClaims; ++i) { - _ensureValidScope(sponsorDomainSeparator, claims[i].id); - } - - assembly ("memory-safe") { - // revert InvalidBatchAllocation() - mstore(0, 0x3a03d3bb) - revert(0x1c, 0x04) - } - } - } - - return true; - } - - function _processBasicClaim(BasicClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - return _processSimpleClaim.usingBasicClaim()(claimPayload.toMessageHash(), claimPayload, uint256(0xa0).asStubborn(), _typehashes(uint256(0).asStubborn()), operation); - } - - function _processQualifiedClaim(QualifiedClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); - return _processClaimWithQualification.usingQualifiedClaim()(messageHash, qualificationMessageHash, claimPayload, 0xe0, _typehashes(uint256(0).asStubborn()), operation); - } - - function _processClaimWithWitness(ClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSimpleClaim.usingClaimWithWitness()(messageHash, claimPayload, 0xe0, typehash, operation); - } - - function _processQualifiedClaimWithWitness(QualifiedClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processClaimWithQualification.usingQualifiedClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x120, typehash, operation); - } - - function _processMultichainClaim(MultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - return _processSimpleClaim.usingMultichainClaim()(claimPayload.toMessageHash(), claimPayload, 0xc0, _typehashes(uint256(2).asStubborn()), operation); - } - - function _processQualifiedMultichainClaim(QualifiedMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); - return _processClaimWithQualification.usingQualifiedMultichainClaim()(messageHash, qualificationMessageHash, claimPayload, 0x100, _typehashes(uint256(2).asStubborn()), operation); - } - - function _processMultichainClaimWithWitness(MultichainClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSimpleClaim.usingMultichainClaimWithWitness()(messageHash, claimPayload, 0x100, typehash, operation); - } - - function _processQualifiedMultichainClaimWithWitness(QualifiedMultichainClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processClaimWithQualification.usingQualifiedMultichainClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x140, typehash, operation); - } - - function _processQualifiedSplitBatchMultichainClaim(QualifiedSplitBatchMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); - return _processSplitBatchClaimWithQualification.usingQualifiedSplitBatchMultichainClaim()( - messageHash, qualificationMessageHash, claimPayload, 0x100, _typehashes(uint256(1).asStubborn()), operation - ); - } - - function _processSplitBatchMultichainClaimWithWitness(SplitBatchMultichainClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSimpleSplitBatchClaim.usingSplitBatchMultichainClaimWithWitness()(messageHash, claimPayload, 0x100, typehash, operation); - } - - function _processQualifiedSplitBatchMultichainClaimWithWitness( - QualifiedSplitBatchMultichainClaimWithWitness calldata claimPayload, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSplitBatchClaimWithQualification.usingQualifiedSplitBatchMultichainClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x140, typehash, operation); - } - - function _processSplitMultichainClaim(SplitMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - return _processSimpleSplitClaim.usingSplitMultichainClaim()(claimPayload.toMessageHash(), claimPayload, 0xc0, _typehashes(uint256(2).asStubborn()), operation); - } - - function _processQualifiedSplitMultichainClaim(QualifiedSplitMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); - return _processSplitClaimWithQualification.usingQualifiedSplitMultichainClaim()(messageHash, qualificationMessageHash, claimPayload, 0x100, _typehashes(uint256(2).asStubborn()), operation); - } - - function _processSplitMultichainClaimWithWitness(SplitMultichainClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSimpleSplitClaim.usingSplitMultichainClaimWithWitness()(messageHash, claimPayload, 0x100, typehash, operation); - } - - function _processQualifiedSplitMultichainClaimWithWitness( - QualifiedSplitMultichainClaimWithWitness calldata claimPayload, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSplitClaimWithQualification.usingQualifiedSplitMultichainClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x140, typehash, operation); - } - - function _processBatchMultichainClaim(BatchMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - return _processSimpleBatchClaim.usingBatchMultichainClaim()(claimPayload.toMessageHash(), claimPayload, 0xc0, _typehashes(uint256(2).asStubborn()), operation); - } - - function _processQualifiedBatchMultichainClaim(QualifiedBatchMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); - return _processBatchClaimWithQualification.usingQualifiedBatchMultichainClaim()(messageHash, qualificationMessageHash, claimPayload, 0x100, _typehashes(uint256(2).asStubborn()), operation); - } - - function _processBatchMultichainClaimWithWitness(BatchMultichainClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSimpleBatchClaim.usingBatchMultichainClaimWithWitness()(messageHash, claimPayload, 0x100, typehash, operation); - } - - function _processQualifiedBatchMultichainClaimWithWitness( - QualifiedBatchMultichainClaimWithWitness calldata claimPayload, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processBatchClaimWithQualification.usingQualifiedBatchMultichainClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x140, typehash, operation); - } - - function _processExogenousQualifiedBatchMultichainClaim( - ExogenousQualifiedBatchMultichainClaim calldata claimPayload, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); - return _processBatchClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedBatchMultichainClaim()( - messageHash, qualificationMessageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation - ); - } - - function _processExogenousBatchMultichainClaimWithWitness( - ExogenousBatchMultichainClaimWithWitness calldata claimPayload, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processBatchClaimWithSponsorDomain.usingExogenousBatchMultichainClaimWithWitness()( - messageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation - ); - } - - function _processExogenousQualifiedBatchMultichainClaimWithWitness( - ExogenousQualifiedBatchMultichainClaimWithWitness calldata claimPayload, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processBatchClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedBatchMultichainClaimWithWitness()( - messageHash, qualificationMessageHash, claimPayload, 0x180, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation - ); - } - - function _processExogenousMultichainClaim(ExogenousMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - return _processClaimWithSponsorDomain.usingExogenousMultichainClaim()( - claimPayload.toMessageHash(), claimPayload, 0x100, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation - ); - } - - function _processSplitBatchMultichainClaim(SplitBatchMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - return _processSimpleSplitBatchClaim.usingSplitBatchMultichainClaim()(claimPayload.toMessageHash(), claimPayload, 0xc0, _typehashes(uint256(2).asStubborn()), operation); - } - - function _processExogenousMultichainClaimWithWitness(ExogenousMultichainClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return - _processClaimWithSponsorDomain.usingExogenousMultichainClaimWithWitness()(messageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation); - } - - function _processExogenousQualifiedMultichainClaim(ExogenousQualifiedMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); - return _processClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedMultichainClaim()( - messageHash, qualificationMessageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation - ); - } - - function _processExogenousQualifiedSplitMultichainClaim( - ExogenousQualifiedSplitMultichainClaim calldata claimPayload, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); - return _processSplitClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedSplitMultichainClaim()( - messageHash, qualificationMessageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation - ); - } - - function _processExogenousQualifiedMultichainClaimWithWitness( - ExogenousQualifiedMultichainClaimWithWitness calldata claimPayload, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedMultichainClaimWithWitness()( - messageHash, qualificationMessageHash, claimPayload, 0x180, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation - ); - } - - function _processExogenousSplitMultichainClaim(ExogenousSplitMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - return _processSplitClaimWithSponsorDomain.usingExogenousSplitMultichainClaim()( - claimPayload.toMessageHash(), claimPayload, 0x100, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation - ); - } - - function _processExogenousSplitMultichainClaimWithWitness( - ExogenousSplitMultichainClaimWithWitness calldata claimPayload, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSplitClaimWithSponsorDomain.usingExogenousSplitMultichainClaimWithWitness()( - messageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation - ); - } - - function _processExogenousQualifiedSplitMultichainClaimWithWitness( - ExogenousQualifiedSplitMultichainClaimWithWitness calldata claimPayload, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSplitClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedSplitMultichainClaimWithWitness()( - messageHash, qualificationMessageHash, claimPayload, 0x180, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation - ); - } - - function _processExogenousBatchMultichainClaim(ExogenousBatchMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - return _processBatchClaimWithSponsorDomain.usingExogenousBatchMultichainClaim()( - claimPayload.toMessageHash(), claimPayload, 0x100, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation - ); - } - - function _processExogenousSplitBatchMultichainClaim(ExogenousSplitBatchMultichainClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - return _processSplitBatchClaimWithSponsorDomain.usingExogenousSplitBatchMultichainClaim()( - claimPayload.toMessageHash(), claimPayload, 0x100, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation - ); - } - - function _processExogenousQualifiedSplitBatchMultichainClaim( - ExogenousQualifiedSplitBatchMultichainClaim calldata claimPayload, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); - return _processSplitBatchClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedSplitBatchMultichainClaim()( - messageHash, qualificationMessageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), _typehashes(uint256(2).asStubborn()), operation - ); - } - - function _processExogenousSplitBatchMultichainClaimWithWitness( - ExogenousSplitBatchMultichainClaimWithWitness calldata claimPayload, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSplitBatchClaimWithSponsorDomain.usingExogenousSplitBatchMultichainClaimWithWitness()( - messageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation - ); - } - - function _processExogenousQualifiedSplitBatchMultichainClaimWithWitness( - ExogenousQualifiedSplitBatchMultichainClaimWithWitness calldata claimPayload, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSplitBatchClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedSplitBatchMultichainClaimWithWitness()( - messageHash, qualificationMessageHash, claimPayload, 0x180, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), typehash, operation - ); - } - - function _processSplitClaim(SplitClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - return _processSimpleSplitClaim.usingSplitClaim()(claimPayload.toMessageHash(), claimPayload, 0xa0, _typehashes(uint256(0).asStubborn()), operation); - } - - function _processQualifiedSplitClaim(QualifiedSplitClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); - return _processSplitClaimWithQualification.usingQualifiedSplitClaim()(messageHash, qualificationMessageHash, claimPayload, 0xe0, _typehashes(uint256(0).asStubborn()), operation); - } - - function _processSplitClaimWithWitness(SplitClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSimpleSplitClaim.usingSplitClaimWithWitness()(messageHash, claimPayload, 0xe0, typehash, operation); - } - - function _processQualifiedSplitClaimWithWitness(QualifiedSplitClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSplitClaimWithQualification.usingQualifiedSplitClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x120, typehash, operation); - } - - function _processBatchClaim(BatchClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - return _processSimpleBatchClaim.usingBatchClaim()(claimPayload.toMessageHash(), claimPayload, 0xa0, _typehashes(uint256(1).asStubborn()), operation); - } - - function _processSplitBatchClaim(SplitBatchClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - return _processSimpleSplitBatchClaim.usingSplitBatchClaim()(claimPayload.toMessageHash(), claimPayload, 0xa0, _typehashes(uint256(1).asStubborn()), operation); - } - - function _processQualifiedSplitBatchClaim(QualifiedSplitBatchClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); - return _processSplitBatchClaimWithQualification.usingQualifiedSplitBatchClaim()(messageHash, qualificationMessageHash, claimPayload, 0xe0, _typehashes(uint256(1).asStubborn()), operation); - } - - function _processQualifiedSplitBatchClaimWithWitness(QualifiedSplitBatchClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSplitBatchClaimWithQualification.usingQualifiedSplitBatchClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x120, typehash, operation); - } - - function _processSplitBatchClaimWithWitness(SplitBatchClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSimpleSplitBatchClaim.usingSplitBatchClaimWithWitness()(messageHash, claimPayload, 0xe0, typehash, operation); - } - - function _processQualifiedBatchClaim(QualifiedBatchClaim calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - (bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash(); - return _processBatchClaimWithQualification.usingQualifiedBatchClaim()(messageHash, qualificationMessageHash, claimPayload, 0xe0, _typehashes(uint256(1).asStubborn()), operation); - } - - function _processBatchClaimWithWitness(BatchClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) internal returns (bool) { - (bytes32 messageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processSimpleBatchClaim.usingBatchClaimWithWitness()(messageHash, claimPayload, 0xe0, typehash, operation); - } - - function _processQualifiedBatchClaimWithWitness(QualifiedBatchClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation) - internal - returns (bool) - { - (bytes32 messageHash, bytes32 qualificationMessageHash, bytes32 typehash) = claimPayload.toMessageHash(); - return _processBatchClaimWithQualification.usingQualifiedBatchClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x120, typehash, operation); - } - function _verifyBalancesAndPerformDeposits( uint256[] memory ids, ISignatureTransfer.TokenPermissions[] calldata permittedTokens, @@ -1114,44 +280,6 @@ contract InternalLogic is Tstorish { _clearTstorish(_REENTRANCY_GUARD_SLOT); } - function _verifyAndProcessSplitComponents( - address sponsor, - uint256 id, - uint256 allocatedAmount, - SplitComponent[] calldata claimants, - function(address, address, uint256, uint256) internal returns (bool) operation - ) internal returns (bool) { - uint256 totalClaims = claimants.length; - uint256 spentAmount = 0; - uint256 errorBuffer = (totalClaims == 0).asUint256(); - - unchecked { - for (uint256 i = 0; i < totalClaims; ++i) { - SplitComponent calldata component = claimants[i]; - uint256 amount = component.amount; - - uint256 updatedSpentAmount = amount + spentAmount; - errorBuffer |= (updatedSpentAmount < spentAmount).asUint256(); - spentAmount = updatedSpentAmount; - - operation(sponsor, component.claimant, id, amount); - } - } - - errorBuffer |= (allocatedAmount < spentAmount).asUint256(); - assembly ("memory-safe") { - if errorBuffer { - // revert AllocatedAmountExceeded(allocatedAmount, amount); - mstore(0, 0x3078b2f6) - mstore(0x20, allocatedAmount) - mstore(0x40, spentAmount) - revert(0x1c, 0x44) - } - } - - return true; - } - function _performBasicERC20Deposit(address token, address allocator, uint256 amount) internal returns (uint256 id) { _setTstorish(_REENTRANCY_GUARD_SLOT, 1); id = token.excludingNative().toIdIfRegistered(Scope.Multichain, ResetPeriod.TenMinutes, allocator); @@ -1636,10 +764,6 @@ contract InternalLogic is Tstorish { } } - function _hasNoActiveRegistration(address sponsor, bytes32 claimHash, bytes32 typehash) internal view returns (bool) { - return _getRegistrationStatus(sponsor, claimHash, typehash) <= block.timestamp; - } - function _isPermit2Deployed() internal view returns (bool) { if (_PERMIT2_INITIALLY_DEPLOYED) { return true; @@ -1813,33 +937,11 @@ contract InternalLogic is Tstorish { function _insertCompactDepositTypestringAt(uint256 memoryLocation) internal pure { assembly ("memory-safe") { mstore(memoryLocation, 0x96) - mstore(add(memoryLocation, 0x20), 0x436f6d706163744465706f736974207769746e65737329436f6d706163744465) - mstore(add(memoryLocation, 0x40), 0x706f736974286164647265737320616c6c6f6361746f722c75696e7438207265) - mstore(add(memoryLocation, 0x60), 0x736574506572696f642c75696e74382073636f70652c61646472657373207265) - mstore(add(memoryLocation, 0x96), 0x20746f6b656e2c75696e7432353620616d6f756e7429) - mstore(add(memoryLocation, 0x80), 0x63697069656e7429546f6b656e5065726d697373696f6e732861646472657373) - } - } - - function _ensureValidScope(bytes32 sponsorDomainSeparator, uint256 id) internal pure { - assembly ("memory-safe") { - if iszero(or(iszero(sponsorDomainSeparator), iszero(shr(255, id)))) { - // revert InvalidScope(id) - mstore(0, 0xa06356f5) - mstore(0x20, id) - revert(0x1c, 0x24) - } - } - } - - function _typehashes(uint256 i) internal pure returns (bytes32 typehash) { - assembly ("memory-safe") { - let m := mload(0x40) - mstore(0, COMPACT_TYPEHASH) - mstore(0x20, BATCH_COMPACT_TYPEHASH) - mstore(0x40, MULTICHAIN_COMPACT_TYPEHASH) - typehash := mload(shl(5, i)) - mstore(0x40, m) + mstore(add(memoryLocation, 0x20), COMPACT_DEPOSIT_TYPESTRING_FRAGMENT_ONE) + mstore(add(memoryLocation, 0x40), COMPACT_DEPOSIT_TYPESTRING_FRAGMENT_TWO) + mstore(add(memoryLocation, 0x60), COMPACT_DEPOSIT_TYPESTRING_FRAGMENT_THREE) + mstore(add(memoryLocation, 0x96), COMPACT_DEPOSIT_TYPESTRING_FRAGMENT_FIVE) + mstore(add(memoryLocation, 0x80), COMPACT_DEPOSIT_TYPESTRING_FRAGMENT_FOUR) } } diff --git a/src/types/EIP712Types.sol b/src/types/EIP712Types.sol index 2285534..18b4b8d 100644 --- a/src/types/EIP712Types.sol +++ b/src/types/EIP712Types.sol @@ -198,3 +198,9 @@ bytes32 constant TOKEN_PERMISSIONS_TYPESTRING_FRAGMENT_ONE = 0x29546f6b656e50657 // uint120(abi.decode(bytes("uint256 amount)"), (bytes15))) uint120 constant TOKEN_PERMISSIONS_TYPESTRING_FRAGMENT_TWO = 0x75696e7432353620616d6f756e7429; + +uint256 constant COMPACT_DEPOSIT_TYPESTRING_FRAGMENT_ONE = 0x436f6d706163744465706f736974207769746e65737329436f6d706163744465; +uint256 constant COMPACT_DEPOSIT_TYPESTRING_FRAGMENT_TWO = 0x706f736974286164647265737320616c6c6f6361746f722c75696e7438207265; +uint256 constant COMPACT_DEPOSIT_TYPESTRING_FRAGMENT_THREE = 0x736574506572696f642c75696e74382073636f70652c61646472657373207265; +uint256 constant COMPACT_DEPOSIT_TYPESTRING_FRAGMENT_FOUR = 0x63697069656e7429546f6b656e5065726d697373696f6e732861646472657373; +uint176 constant COMPACT_DEPOSIT_TYPESTRING_FRAGMENT_FIVE = 0x20746f6b656e2c75696e7432353620616d6f756e7429;