Replace error strings with custom errors (#4261)

This commit is contained in:
Francisco Giordano
2023-06-06 01:08:31 -03:00
parent 253bfa68c2
commit 99a4cfca17
133 changed files with 3157 additions and 1204 deletions

View File

@ -1,4 +1,5 @@
const { expectRevert } = require('@openzeppelin/test-helpers');
require('@openzeppelin/test-helpers');
const { expectRevertCustomError } = require('../../helpers/customError');
const { toEthSignedMessageHash, toDataWithIntendedValidatorHash } = require('../../helpers/sign');
const { expect } = require('chai');
@ -51,17 +52,18 @@ contract('ECDSA', function (accounts) {
context('recover with invalid signature', function () {
it('with short signature', async function () {
await expectRevert(this.ecdsa.$recover(TEST_MESSAGE, '0x1234'), 'ECDSA: invalid signature length');
await expectRevertCustomError(this.ecdsa.$recover(TEST_MESSAGE, '0x1234'), 'ECDSAInvalidSignatureLength', [2]);
});
it('with long signature', async function () {
await expectRevert(
await expectRevertCustomError(
// eslint-disable-next-line max-len
this.ecdsa.$recover(
TEST_MESSAGE,
'0x01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789',
),
'ECDSA: invalid signature length',
'ECDSAInvalidSignatureLength',
[85],
);
});
});
@ -93,7 +95,7 @@ contract('ECDSA', function (accounts) {
// eslint-disable-next-line max-len
const signature =
'0x332ce75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e01c';
await expectRevert(this.ecdsa.$recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature');
await expectRevertCustomError(this.ecdsa.$recover(TEST_MESSAGE, signature), 'ECDSAInvalidSignature', []);
});
});
@ -141,11 +143,12 @@ contract('ECDSA', function (accounts) {
it('reverts wrong v values', async function () {
for (const v of ['00', '01']) {
const signature = signatureWithoutV + v;
await expectRevert(this.ecdsa.$recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature');
await expectRevertCustomError(this.ecdsa.$recover(TEST_MESSAGE, signature), 'ECDSAInvalidSignature', []);
await expectRevert(
await expectRevertCustomError(
this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
'ECDSA: invalid signature',
'ECDSAInvalidSignature',
[],
);
}
});
@ -153,9 +156,10 @@ contract('ECDSA', function (accounts) {
it('rejects short EIP2098 format', async function () {
const v = '1b'; // 27 = 1b.
const signature = signatureWithoutV + v;
await expectRevert(
await expectRevertCustomError(
this.ecdsa.$recover(TEST_MESSAGE, to2098Format(signature)),
'ECDSA: invalid signature length',
'ECDSAInvalidSignatureLength',
[64],
);
});
});
@ -203,11 +207,12 @@ contract('ECDSA', function (accounts) {
it('reverts invalid v values', async function () {
for (const v of ['00', '01']) {
const signature = signatureWithoutV + v;
await expectRevert(this.ecdsa.$recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature');
await expectRevertCustomError(this.ecdsa.$recover(TEST_MESSAGE, signature), 'ECDSAInvalidSignature', []);
await expectRevert(
await expectRevertCustomError(
this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
'ECDSA: invalid signature',
'ECDSAInvalidSignature',
[],
);
}
});
@ -215,9 +220,10 @@ contract('ECDSA', function (accounts) {
it('rejects short EIP2098 format', async function () {
const v = '1c'; // 27 = 1b.
const signature = signatureWithoutV + v;
await expectRevert(
await expectRevertCustomError(
this.ecdsa.$recover(TEST_MESSAGE, to2098Format(signature)),
'ECDSA: invalid signature length',
'ECDSAInvalidSignatureLength',
[64],
);
});
});
@ -227,10 +233,12 @@ contract('ECDSA', function (accounts) {
// eslint-disable-next-line max-len
const highSSignature =
'0xe742ff452d41413616a5bf43fe15dd88294e983d3d36206c2712f39083d638bde0a0fc89be718fbc1033e1d30d78be1c68081562ed2e97af876f286f3453231d1b';
await expectRevert(this.ecdsa.$recover(message, highSSignature), "ECDSA: invalid signature 's' value");
await expectRevert(
this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(highSSignature)),
"ECDSA: invalid signature 's' value",
const [r, v, s] = split(highSSignature);
await expectRevertCustomError(this.ecdsa.$recover(message, highSSignature), 'ECDSAInvalidSignatureS', [s]);
await expectRevertCustomError(
this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, r, v, s),
'ECDSAInvalidSignatureS',
[s],
);
expect(() => to2098Format(highSSignature)).to.throw("invalid signature 's' value");
});

View File

@ -1,10 +1,10 @@
require('@openzeppelin/test-helpers');
const { expectRevert } = require('@openzeppelin/test-helpers');
const { MerkleTree } = require('merkletreejs');
const keccak256 = require('keccak256');
const { expect } = require('chai');
const { expectRevertCustomError } = require('../../helpers/customError');
const MerkleProof = artifacts.require('$MerkleProof');
@ -106,23 +106,25 @@ contract('MerkleProof', function () {
const root = merkleTree.getRoot();
await expectRevert(
await expectRevertCustomError(
this.merkleProof.$multiProofVerify(
[leaves[1], fill, merkleTree.layers[1][1]],
[false, false, false],
root,
[leaves[0], badLeaf], // A, E
),
'MerkleProof: invalid multiproof',
'MerkleProofInvalidMultiproof',
[],
);
await expectRevert(
await expectRevertCustomError(
this.merkleProof.$multiProofVerifyCalldata(
[leaves[1], fill, merkleTree.layers[1][1]],
[false, false, false],
root,
[leaves[0], badLeaf], // A, E
),
'MerkleProof: invalid multiproof',
'MerkleProofInvalidMultiproof',
[],
);
});