Skip to content

Commit

Permalink
reuse validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Oct 18, 2024
1 parent 59f4791 commit cd42898
Showing 1 changed file with 30 additions and 51 deletions.
81 changes: 30 additions & 51 deletions src/TheCompact.sol
Original file line number Diff line number Diff line change
Expand Up @@ -766,24 +766,14 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
return _processSplitClaimWithQualificationAndSponsorDomain(messageHash, qualificationMessageHash, calldataPointer, offsetToId, bytes32(0), operation);
}

function _processClaimWithQualificationAndSponsorDomain(
bytes32 messageHash,
bytes32 qualificationMessageHash,
uint256 calldataPointer,
uint256 offsetToId,
bytes32 sponsorDomainSeparator,
function(address, address, uint256, uint256) internal returns (bool) operation
) internal returns (bool) {
function _validate(bytes32 messageHash, bytes32 qualificationMessageHash, uint256 calldataPointer, uint256 offsetToId, bytes32 sponsorDomainSeparator) internal returns (address sponsor) {
bytes calldata allocatorSignature;
bytes calldata sponsorSignature;
address sponsor;
uint256 nonce;
uint256 expires;

uint256 id;
uint256 allocatedAmount;
address claimant;
uint256 amount;

assembly {
let allocatorSignaturePtr := add(calldataPointer, calldataload(calldataPointer))
Expand All @@ -801,8 +791,6 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
let calldataPointerWithOffset := add(calldataPointer, offsetToId)
id := calldataload(calldataPointerWithOffset)
allocatedAmount := calldataload(add(calldataPointerWithOffset, 0x20))
claimant := calldataload(add(calldataPointerWithOffset, 0x40)) // TODO: sanitize
amount := calldataload(add(calldataPointerWithOffset, 0x60))
}

expires.later();
Expand All @@ -822,44 +810,54 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
messageHash.signedBy(sponsor, sponsorSignature, sponsorDomainSeparator);
qualificationMessageHash.signedBy(allocator, allocatorSignature, domainSeparator);

amount.withinAllocated(allocatedAmount);

_emitClaim(sponsor, messageHash, allocator);

return operation(sponsor, claimant, id, amount);
return sponsor;
}

function _processSplitClaimWithQualificationAndSponsorDomain(
function _processClaimWithQualificationAndSponsorDomain(
bytes32 messageHash,
bytes32 qualificationMessageHash,
uint256 calldataPointer,
uint256 offsetToId,
bytes32 sponsorDomainSeparator,
function(address, address, uint256, uint256) internal returns (bool) operation
) internal returns (bool) {
bytes calldata allocatorSignature;
bytes calldata sponsorSignature;
address sponsor;
uint256 nonce;
uint256 expires;
address sponsor = _validate(messageHash, qualificationMessageHash, calldataPointer, offsetToId, sponsorDomainSeparator);

uint256 id;
uint256 allocatedAmount;
SplitComponent[] calldata claimants;
address claimant;
uint256 amount;

assembly {
let allocatorSignaturePtr := add(calldataPointer, calldataload(calldataPointer))
allocatorSignature.offset := add(0x20, allocatorSignaturePtr)
allocatorSignature.length := calldataload(allocatorSignaturePtr)
let calldataPointerWithOffset := add(calldataPointer, offsetToId)
id := calldataload(calldataPointerWithOffset)
allocatedAmount := calldataload(add(calldataPointerWithOffset, 0x20))
claimant := calldataload(add(calldataPointerWithOffset, 0x40)) // TODO: sanitize
amount := calldataload(add(calldataPointerWithOffset, 0x60))
}

let sponsorSignaturePtr := add(calldataPointer, calldataload(add(calldataPointer, 0x20)))
sponsorSignature.offset := add(0x20, sponsorSignaturePtr)
sponsorSignature.length := calldataload(sponsorSignaturePtr)
amount.withinAllocated(allocatedAmount);

sponsor := calldataload(add(calldataPointer, 0x40)) // TODO: sanitize
nonce := calldataload(add(calldataPointer, 0x60))
expires := calldataload(add(calldataPointer, 0x80))
return operation(sponsor, claimant, id, amount);
}

function _processSplitClaimWithQualificationAndSponsorDomain(
bytes32 messageHash,
bytes32 qualificationMessageHash,
uint256 calldataPointer,
uint256 offsetToId,
bytes32 sponsorDomainSeparator,
function(address, address, uint256, uint256) internal returns (bool) operation
) internal returns (bool) {
address sponsor = _validate(messageHash, qualificationMessageHash, calldataPointer, offsetToId, sponsorDomainSeparator);

uint256 id;
uint256 allocatedAmount;
SplitComponent[] calldata claimants;

assembly {
let calldataPointerWithOffset := add(calldataPointer, offsetToId)
id := calldataload(calldataPointerWithOffset)
allocatedAmount := calldataload(add(calldataPointerWithOffset, 0x20))
Expand All @@ -869,25 +867,6 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
claimants.length := calldataload(claimantsPtr)
}

expires.later();

uint96 allocatorId = id.toAllocatorId();
address allocator = allocatorId.fromRegisteredAllocatorIdWithConsumed(nonce);

if ((sponsorDomainSeparator != bytes32(0)).and(id.toScope() != Scope.Multichain)) {
revert InvalidScope(id);
}

bytes32 domainSeparator = _INITIAL_DOMAIN_SEPARATOR.toLatest(_INITIAL_CHAIN_ID);
assembly {
sponsorDomainSeparator := add(sponsorDomainSeparator, mul(iszero(sponsorDomainSeparator), domainSeparator))
}

messageHash.signedBy(sponsor, sponsorSignature, sponsorDomainSeparator);
qualificationMessageHash.signedBy(allocator, allocatorSignature, domainSeparator);

_emitClaim(sponsor, messageHash, allocator);

return _verifyAndProcessSplitComponents(sponsor, id, allocatedAmount, claimants, operation);
}

Expand Down

0 comments on commit cd42898

Please sign in to comment.