Skip to content

Commit

Permalink
signature verification tests for reference and optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
emo-eth committed Jun 12, 2022
1 parent 261441a commit 81f0cac
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 28 deletions.
2 changes: 1 addition & 1 deletion reference/lib/ReferenceGettersAndDerivers.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

import { ConsiderationItem, OfferItem, OrderParameters } from "contracts/lib/ConsiderationStructs.sol";
import { ConsiderationItem, OfferItem, OrderParameters } from "../../contracts/lib/ConsiderationStructs.sol";

import { ReferenceConsiderationBase } from "./ReferenceConsiderationBase.sol";

Expand Down
5 changes: 1 addition & 4 deletions reference/lib/ReferenceSignatureVerification.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ contract ReferenceSignatureVerification is SignatureVerificationErrors {
address recoveredSigner = ecrecover(digest, v, r, s);

// Disallow invalid signers.
if (recoveredSigner == address(0)) {
if (recoveredSigner == address(0) || recoveredSigner != signer) {
revert InvalidSigner();
// Should a signer be recovered, but it doesn't match the signer...
} else if (recoveredSigner != signer) {
// Attempt EIP-1271 static call to signer in case it's a contract.
_assertValidEIP1271Signature(signer, digest, signature);
}
}

Expand Down
133 changes: 110 additions & 23 deletions test/foundry/SignatureVerification.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@ pragma solidity >=0.8.13;
import { SignatureVerification } from "../../contracts/lib/SignatureVerification.sol";
import { ReferenceSignatureVerification } from "../../reference/lib/ReferenceSignatureVerification.sol";
import { GettersAndDerivers } from "../../contracts/lib/GettersAndDerivers.sol";
import { ReferenceGettersAndDerivers } from "../../reference/lib/ReferenceGettersAndDerivers.sol";
import { BaseOrderTest } from "./utils/BaseOrderTest.sol";
import { OrderParameters } from "../../contracts/lib/ConsiderationStructs.sol";
import { ConsiderationInterface } from "../../contracts/interfaces/ConsiderationInterface.sol";

contract GettersAndDeriversImpl is GettersAndDerivers {
interface GetterAndDeriver {
function deriveOrderHash(
OrderParameters memory orderParameters,
uint256 counter
) external returns (bytes32 orderHash);

function domainSeparator() external returns (bytes32);

function deriveEIP712Digest(bytes32 _domainSeparator_, bytes32 orderHash)
external
returns (bytes32 value);
}

contract GettersAndDeriversImpl is GetterAndDeriver, GettersAndDerivers {
constructor(address conduitController)
GettersAndDerivers(conduitController)
{}
Expand All @@ -32,17 +47,50 @@ contract GettersAndDeriversImpl is GettersAndDerivers {
}
}

contract SignatureVerificationTest is BaseOrderTest, SignatureVerification {
GettersAndDeriversImpl gettersAndDeriversImpl;
contract ReferenceGettersAndDeriversImpl is
GetterAndDeriver,
ReferenceGettersAndDerivers
{
constructor(address conduitController)
ReferenceGettersAndDerivers(conduitController)
{}

function deriveOrderHash(
OrderParameters memory orderParameters,
uint256 counter
) public view returns (bytes32 orderHash) {
return _deriveOrderHash(orderParameters, counter);
}

function domainSeparator() public view returns (bytes32) {
return _domainSeparator();
}

function deriveEIP712Digest(bytes32 _domainSeparator_, bytes32 orderHash)
public
pure
returns (bytes32 value)
{
return _deriveEIP712Digest(_domainSeparator_, orderHash);
}
}

contract SignatureVerifierLogic is BaseOrderTest, SignatureVerification {
GetterAndDeriver getterAndDeriver;

function setUp() public override {
super.setUp();
gettersAndDeriversImpl = new GettersAndDeriversImpl(
address(conduitController)
constructor(
address _conduitController,
ConsiderationInterface _consideration
) {
getterAndDeriver = GetterAndDeriver(
new GettersAndDeriversImpl(address(_conduitController))
);

vm.label(address(getterAndDeriver), "getterAndDeriver");
consideration = _consideration;
}

function testSignatureVerificationDirtyScratchSpace() public {
function signatureVerificationDirtyScratchSpace() external {
addErc721OfferItem(1);
addEthConsiderationItem(alice, 1);

Expand All @@ -52,43 +100,82 @@ contract SignatureVerificationTest is BaseOrderTest, SignatureVerification {
bytes32 orderHash = consideration.getOrderHash(baseOrderComponents);
bytes memory signature = signOrder(consideration, bobPk, orderHash);

bytes32 domainSeparator = getterAndDeriver.domainSeparator();
bytes32 digest = getterAndDeriver.deriveEIP712Digest(
domainSeparator,
orderHash
);

// store bob's address in scratch space
assembly {
mstore(0x0, sload(bob.slot))
}

bytes32 domainSeparator = gettersAndDeriversImpl.domainSeparator();
bytes32 digest = gettersAndDeriversImpl.deriveEIP712Digest(
domainSeparator,
orderHash
);
// figure out digest and pass in here
// might revert with diff error code?
vm.expectRevert(abi.encodeWithSignature("InvalidSigner()"));
_assertValidSignature(alice, digest, signature);
}
}

contract ReferenceSignatureVerificationTest is
contract ReferenceSignatureVerifierLogic is
BaseOrderTest,
ReferenceSignatureVerification
{
function testSignatureVerificationDirtyScratchSpace() public {
GetterAndDeriver getterAndDeriver;

constructor(
address _conduitController,
ConsiderationInterface _consideration
) {
getterAndDeriver = GetterAndDeriver(
new ReferenceGettersAndDeriversImpl(address(_conduitController))
);
vm.label(address(getterAndDeriver), "referenceGetterAndDeriver");
consideration = _consideration;
}

function referenceSignatureVerificationDirtyScratchSpace() external {
addErc721OfferItem(1);
addEthConsiderationItem(alice, 1);

// create order where alice is offerer, but signer is *BOB*
bytes memory signature;
configureOrderParameters(alice);
_configureOrderComponents(consideration.getCounter(alice));
bytes32 orderHash = consideration.getOrderHash(baseOrderComponents);
bytes memory signature = signOrder(consideration, bobPk, orderHash);

bytes32 domainSeparator = getterAndDeriver.domainSeparator();
bytes32 digest = getterAndDeriver.deriveEIP712Digest(
domainSeparator,
orderHash
);

// store bob's address in scratch space
assembly {
mstore(0x0, sload(bob.slot))
}

bytes32 digest;
// figure out digest and pass in here
// might revert with diff error code?
vm.expectRevert(abi.encodeWithSignature("InvalidSigner()"));
_assertValidSignature(alice, digest, signature);
}
}

contract SignatureVerificationTest is BaseOrderTest {
function test(function() external fn) internal {
try fn() {} catch (bytes memory reason) {
assertPass(reason);
}
}

function testSignatureVerification() public {
SignatureVerifierLogic logic = new SignatureVerifierLogic(
address(conduitController),
consideration
);
vm.expectRevert(abi.encodeWithSignature("InvalidSigner()"));
logic.signatureVerificationDirtyScratchSpace();
ReferenceSignatureVerifierLogic referenceLogic = new ReferenceSignatureVerifierLogic(
address(conduitController),
consideration
);
vm.expectRevert(abi.encodeWithSignature("InvalidSigner()"));
referenceLogic.referenceSignatureVerificationDirtyScratchSpace();
}
}

0 comments on commit 81f0cac

Please sign in to comment.