Skip to content

Commit

Permalink
Add test for contract specific signature empty
Browse files Browse the repository at this point in the history
  • Loading branch information
cristovaoth committed Oct 27, 2023
1 parent d398b4d commit 6af30a6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
10 changes: 10 additions & 0 deletions contracts/test/TestSignature.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ contract ContractSignerMaybe is IERC1271 {
}
}

contract ContractSignerOnlyEmpty is IERC1271 {
function isValidSignature(
bytes32,
bytes memory contractSpecificSignature
) external pure override returns (bytes4) {
bool isValid = contractSpecificSignature.length == 0;
return isValid ? bytes4(0x1626ba7e) : bytes4(0x33333333);
}
}

contract ContractSignerFaulty {}

contract ContractSignerReturnSize {
Expand Down
51 changes: 50 additions & 1 deletion test/06_SignatureChecker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,55 @@ describe("SignatureChecker", async () => {
.to.emit(testSignature, "Hello")
.withArgs(AddressZero);
});

it("signer returns isValid for empty specific signature only", async () => {
const { testSignature, relayer } = await loadFixture(setup);

const ContractSigner = await hre.ethers.getContractFactory(
"ContractSignerOnlyEmpty"
);
const contractSigner = await ContractSigner.deploy();

const transaction = await testSignature.populateTransaction.goodbye(
0,
"0xbadfed"
);

const signatureGood = makeContractSignature(
transaction,
"0x",
keccak256(toUtf8Bytes("some irrelevant salt")),
contractSigner.address
);

const signatureBad = makeContractSignature(
transaction,
"0xffff",
keccak256(toUtf8Bytes("some irrelevant salt")),
contractSigner.address
);

const transactionWithGoodSig = {
...transaction,
data: `${transaction.data}${signatureGood.slice(2)}`,
};
const transactionWithBadSig = {
...transaction,
data: `${transaction.data}${signatureBad.slice(2)}`,
};

await expect(await relayer.sendTransaction(transaction))
.to.emit(testSignature, "Goodbye")
.withArgs(AddressZero);

await expect(await relayer.sendTransaction(transactionWithGoodSig))
.to.emit(testSignature, "Goodbye")
.withArgs(contractSigner.address);

await expect(await relayer.sendTransaction(transactionWithBadSig))
.to.emit(testSignature, "Goodbye")
.withArgs(AddressZero);
});
it("signer bad return size", async () => {
const { testSignature, relayer } = await loadFixture(setup);

Expand Down Expand Up @@ -427,7 +476,7 @@ function makeContractSignature(
r: string,
s?: string
) {
const dataBytesLength = (transaction.data?.length as number) / 2 - 1;
const dataBytesLength = ((transaction.data?.length as number) - 2) / 2;

r = defaultAbiCoder.encode(["address"], [r]);
s = s || defaultAbiCoder.encode(["uint256"], [dataBytesLength]);
Expand Down

0 comments on commit 6af30a6

Please sign in to comment.