Skip to content

Commit

Permalink
qualified multichain with witness
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Oct 17, 2024
1 parent 70f5efd commit 65e7217
Show file tree
Hide file tree
Showing 5 changed files with 455 additions and 3 deletions.
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ solc = '0.8.28'
evm_version='cancun'
via_ir = true
# optimizer_runs = 4_294_967_295
optimizer_runs = 80_000
optimizer_runs = 25_000
bytecode_hash = 'none'
src = "src"
out = "out"
Expand Down
101 changes: 101 additions & 0 deletions src/TheCompact.sol
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,34 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
return _processExogenousMultichainClaimWithWitness(claimPayload, _release);
}

function claim(QualifiedMultichainClaimWithWitness calldata claimPayload)
external
returns (bool)
{
return _processQualifiedMultichainClaimWithWitness(claimPayload, _release);
}

function claimAndWithdraw(QualifiedMultichainClaimWithWitness calldata claimPayload)
external
returns (bool)
{
return _processQualifiedMultichainClaimWithWitness(claimPayload, _release);
}

function claim(ExogenousQualifiedMultichainClaimWithWitness calldata claimPayload)
external
returns (bool)
{
return _processExogenousQualifiedMultichainClaimWithWitness(claimPayload, _release);
}

function claimAndWithdraw(ExogenousQualifiedMultichainClaimWithWitness calldata claimPayload)
external
returns (bool)
{
return _processExogenousQualifiedMultichainClaimWithWitness(claimPayload, _release);
}

function enableForcedWithdrawal(uint256 id) external returns (uint256 withdrawableAt) {
// overflow check not necessary as reset period is capped
unchecked {
Expand Down Expand Up @@ -908,6 +936,24 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
);
}

function _notExpiredAndWithValidSignaturesQualifiedExogenousWithWitness(
bytes32 messageHash,
bytes32 qualificationMessageHash,
ExogenousQualifiedMultichainClaimWithWitness calldata claimPayload,
address allocator
) internal view {
_notExpiredAndSignedByBoth(
claimPayload.expires,
claimPayload.notarizedChainId.toNotarizedDomainSeparator(),
messageHash,
claimPayload.sponsor,
claimPayload.sponsorSignature,
qualificationMessageHash,
allocator,
claimPayload.allocatorSignature
);
}

// NOTE: this function expects that there's at least one array element
function _notExpiredAndWithValidSignaturesBatch(BatchClaim calldata claimPayload)
internal
Expand Down Expand Up @@ -1160,6 +1206,30 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
);
}

function _processQualifiedMultichainClaimWithWitness(
QualifiedMultichainClaimWithWitness calldata claimPayload,
function(address, address, uint256, uint256) internal returns (bool) operation
) internal returns (bool) {
(bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash();
address allocator = claimPayload.id.toRegisteredAllocatorWithConsumed(claimPayload.nonce);

_notExpiredAndWithValidQualifiedSignatures.usingQualifiedMultichainClaimWithWitness()(
messageHash, qualificationMessageHash, claimPayload, allocator
);

claimPayload.amount.withinAllocated(claimPayload.allocatedAmount);

return _emitAndOperate(
claimPayload.sponsor,
claimPayload.claimant,
claimPayload.id,
messageHash,
claimPayload.amount,
allocator,
operation
);
}

function _processExogenousMultichainClaim(
ExogenousMultichainClaim calldata claimPayload,
function(address, address, uint256, uint256) internal returns (bool) operation
Expand Down Expand Up @@ -1253,6 +1323,37 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
);
}

function _processExogenousQualifiedMultichainClaimWithWitness(
ExogenousQualifiedMultichainClaimWithWitness calldata claimPayload,
function(address, address, uint256, uint256) internal returns (bool) operation
) internal returns (bool) {
(bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash();

uint256 id = claimPayload.id;
uint256 amount = claimPayload.amount;
address allocator = id.toRegisteredAllocatorWithConsumed(claimPayload.nonce);

_notExpiredAndWithValidSignaturesQualifiedExogenousWithWitness(
messageHash, qualificationMessageHash, claimPayload, allocator
);

if (id.toScope() != Scope.Multichain) {
revert InvalidScope(id);
}

amount.withinAllocated(claimPayload.allocatedAmount);

return _emitAndOperate(
claimPayload.sponsor,
claimPayload.claimant,
id,
messageHash,
amount,
allocator,
operation
);
}

function _verifyAndProcessSplitComponents(
address sponsor,
bytes32 messageHash,
Expand Down
18 changes: 17 additions & 1 deletion src/lib/FunctionCastLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
QualifiedMultichainClaim,
ExogenousMultichainClaim,
ExogenousQualifiedMultichainClaim,
MultichainClaimWithWitness
MultichainClaimWithWitness,
QualifiedMultichainClaimWithWitness
} from "../types/MultichainClaims.sol";

import {
Expand Down Expand Up @@ -541,4 +542,19 @@ library FunctionCastLib {
fnOut := fnIn
}
}

function usingQualifiedMultichainClaimWithWitness(
function(bytes32, bytes32, QualifiedClaim calldata, address) internal view fnIn
)
internal
pure
returns (
function(bytes32, bytes32, QualifiedMultichainClaimWithWitness calldata, address) internal view
fnOut
)
{
assembly ("memory-safe") {
fnOut := fnIn
}
}
}
152 changes: 152 additions & 0 deletions src/lib/HashLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,48 @@ library HashLib {
}
}

function usingQualifiedMultichainClaimWithWitness(
function (MultichainClaim calldata, uint256, bytes32, bytes32)
internal
view
returns (bytes32) fnIn
)
internal
pure
returns (
function (QualifiedMultichainClaimWithWitness calldata, uint256, bytes32, bytes32)
internal
view
returns (bytes32) fnOut
)
{
assembly ("memory-safe") {
fnOut := fnIn
}
}

function usingQualifiedMultichainClaimWithWitness(
function (
QualifiedClaim calldata,
bytes32,
uint256
) internal pure returns (bytes32) fnIn
)
internal
pure
returns (
function (
QualifiedMultichainClaimWithWitness calldata,
bytes32,
uint256
) internal pure returns (bytes32) fnOut
)
{
assembly ("memory-safe") {
fnOut := fnIn
}
}

function usingQualifiedMultichainClaim(
function (
QualifiedClaim calldata,
Expand Down Expand Up @@ -677,6 +719,26 @@ library HashLib {
}
}

function usingExogenousQualifiedMultichainClaimWithWitness(
function (ExogenousMultichainClaim calldata, uint256, bytes32, bytes32)
internal
view
returns (bytes32) fnIn
)
internal
pure
returns (
function (ExogenousQualifiedMultichainClaimWithWitness calldata, uint256, bytes32, bytes32)
internal
view
returns (bytes32) fnOut
)
{
assembly ("memory-safe") {
fnOut := fnIn
}
}

function usingExogenousMultichainClaimWithWitness(
function (
QualifiedClaim calldata,
Expand Down Expand Up @@ -747,6 +809,22 @@ library HashLib {
)(claim, 0x40, allocationTypehash, multichainCompactTypehash);
}

function toMessageHash(QualifiedMultichainClaimWithWitness calldata claim)
internal
view
returns (bytes32 messageHash, bytes32 qualificationMessageHash)
{
(bytes32 allocationTypehash, bytes32 multichainCompactTypehash) =
usingQualifiedMultichainClaimWithWitness(getMultichainTypehashes)(claim);

messageHash = usingQualifiedMultichainClaimWithWitness(toMultichainClaimMessageHash)(
claim, 0x80, allocationTypehash, multichainCompactTypehash
);
qualificationMessageHash = usingQualifiedMultichainClaimWithWitness(
toQualificationMessageHash
)(claim, messageHash, 0x40);
}

function toMessageHash(QualifiedMultichainClaim calldata claim)
internal
view
Expand Down Expand Up @@ -805,6 +883,28 @@ library HashLib {
}
}

function usingExogenousQualifiedMultichainClaimWithWitness(
function(
QualifiedClaim calldata,
bytes32,
uint256
) internal pure returns (bytes32) fnIn
)
internal
pure
returns (
function(
ExogenousQualifiedMultichainClaimWithWitness calldata,
bytes32,
uint256
) internal pure returns (bytes32) fnOut
)
{
assembly ("memory-safe") {
fnOut := fnIn
}
}

function usingExogenousMultichainClaimWithWitness(
function (
MultichainClaimWithWitness calldata
Expand All @@ -823,6 +923,42 @@ library HashLib {
}
}

function usingExogenousQualifiedMultichainClaimWithWitness(
function (
MultichainClaimWithWitness calldata
) internal pure returns (bytes32, bytes32) fnIn
)
internal
pure
returns (
function (
ExogenousQualifiedMultichainClaimWithWitness calldata
) internal pure returns (bytes32, bytes32) fnOut
)
{
assembly ("memory-safe") {
fnOut := fnIn
}
}

function usingQualifiedMultichainClaimWithWitness(
function (
MultichainClaimWithWitness calldata
) internal pure returns (bytes32, bytes32) fnIn
)
internal
pure
returns (
function (
QualifiedMultichainClaimWithWitness calldata
) internal pure returns (bytes32, bytes32) fnOut
)
{
assembly ("memory-safe") {
fnOut := fnIn
}
}

function getMultichainTypehashes(MultichainClaimWithWitness calldata claim)
internal
pure
Expand Down Expand Up @@ -930,6 +1066,22 @@ library HashLib {
)(claim, messageHash, 0);
}

function toMessageHash(ExogenousQualifiedMultichainClaimWithWitness calldata claim)
internal
view
returns (bytes32 messageHash, bytes32 qualificationMessageHash)
{
(bytes32 allocationTypehash, bytes32 multichainCompactTypehash) =
usingExogenousQualifiedMultichainClaimWithWitness(getMultichainTypehashes)(claim);

messageHash = usingExogenousQualifiedMultichainClaimWithWitness(
toExogenousMultichainClaimMessageHash
)(claim, 0x80, allocationTypehash, multichainCompactTypehash);
qualificationMessageHash = usingExogenousQualifiedMultichainClaimWithWitness(
toQualificationMessageHash
)(claim, messageHash, 0x40);
}

function toPermit2WitnessHash(
address allocator,
address depositor,
Expand Down
Loading

0 comments on commit 65e7217

Please sign in to comment.