Replace revert strings with custom errors (#4261)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: Francisco <fg@frang.io>
This commit is contained in:
Ernesto García
2023-06-12 17:41:52 -06:00
committed by GitHub
parent 08fd777f6d
commit b425a72240
138 changed files with 3220 additions and 1287 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");
});