Skip to content

Commit 75c0a59

Browse files
Leo Ariasnventuro
authored andcommitted
Add missing tests to ECSDA (#1248)
* fix: refactor sign.js and related tests * fix: remove unused dep * fix: update package.json correctly * Add missing tests to ECRecovery * fix lint * Reorganize the tests * Reuse signature * fix static errors * Apply suggestions by @frangion and @nventuro * Remove only * More suggestions * Remove unnecessary max-len * remove only
1 parent 947de54 commit 75c0a59

File tree

1 file changed

+98
-37
lines changed

1 file changed

+98
-37
lines changed

test/library/ECDSA.test.js

Lines changed: 98 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,117 @@ const WRONG_MESSAGE = web3.sha3('Nope');
1111

1212
contract('ECDSA', function ([_, anyone]) {
1313
beforeEach(async function () {
14-
this.mock = await ECDSAMock.new();
14+
this.ecdsa = await ECDSAMock.new();
1515
});
1616

17-
it('recover v0', async function () {
18-
// Signature generated outside ganache with method web3.eth.sign(signer, message)
19-
const signer = '0x2cc1166f6212628a0deef2b33befb2187d35b86c';
20-
// eslint-disable-next-line max-len
21-
const signature = '0x5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be89200';
22-
(await this.mock.recover(TEST_MESSAGE, signature)).should.equal(signer);
23-
});
17+
context('recover with valid signature', function () {
18+
context('with v0 signature', function () {
19+
// Signature generated outside ganache with method web3.eth.sign(signer, message)
20+
const signer = '0x2cc1166f6212628a0deef2b33befb2187d35b86c';
21+
// eslint-disable-next-line max-len
22+
const signatureWithoutVersion = '0x5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be892';
2423

25-
it('recover v1', async function () {
26-
// Signature generated outside ganache with method web3.eth.sign(signer, message)
27-
const signer = '0x1e318623ab09fe6de3c9b8672098464aeda9100e';
28-
// eslint-disable-next-line max-len
29-
const signature = '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e001';
30-
(await this.mock.recover(TEST_MESSAGE, signature)).should.equal(signer);
31-
});
24+
context('with 00 as version value', function () {
25+
it('works', async function () {
26+
const version = '00';
27+
const signature = signatureWithoutVersion + version;
28+
(await this.ecdsa.recover(TEST_MESSAGE, signature)).should.equal(signer);
29+
});
30+
});
3231

33-
it('recover using web3.eth.sign()', async function () {
34-
// Create the signature
35-
const signature = signMessage(anyone, TEST_MESSAGE);
32+
context('with 27 as version value', function () {
33+
it('works', async function () {
34+
const version = '1b'; // 27 = 1b.
35+
const signature = signatureWithoutVersion + version;
36+
(await this.ecdsa.recover(TEST_MESSAGE, signature)).should.equal(signer);
37+
});
38+
});
3639

37-
// Recover the signer address from the generated message and signature.
38-
(await this.mock.recover(
39-
toEthSignedMessageHash(TEST_MESSAGE),
40-
signature
41-
)).should.equal(anyone);
42-
});
40+
context('with wrong version', function () {
41+
it('returns 0', async function () {
42+
// The last two hex digits are the signature version.
43+
// The only valid values are 0, 1, 27 and 28.
44+
const version = '02';
45+
const signature = signatureWithoutVersion + version;
46+
(await this.ecdsa.recover(TEST_MESSAGE, signature)).should.equal(
47+
'0x0000000000000000000000000000000000000000');
48+
});
49+
});
50+
});
4351

44-
it('recover using web3.eth.sign() should return wrong signer', async function () {
45-
// Create the signature
46-
const signature = signMessage(anyone, TEST_MESSAGE);
52+
context('with v1 signature', function () {
53+
const signer = '0x1e318623ab09fe6de3c9b8672098464aeda9100e';
54+
// eslint-disable-next-line max-len
55+
const signatureWithoutVersion = '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e0';
4756

48-
// Recover the signer address from the generated message and wrong signature.
49-
(await this.mock.recover(WRONG_MESSAGE, signature)).should.not.equal(anyone);
57+
context('with 01 as version value', function () {
58+
it('works', async function () {
59+
const version = '01';
60+
const signature = signatureWithoutVersion + version;
61+
(await this.ecdsa.recover(TEST_MESSAGE, signature)).should.equal(signer);
62+
});
63+
});
64+
65+
context('with 28 signature', function () {
66+
it('works', async function () {
67+
const version = '1c'; // 28 = 1c.
68+
const signature = signatureWithoutVersion + version;
69+
(await this.ecdsa.recover(TEST_MESSAGE, signature)).should.equal(signer);
70+
});
71+
});
72+
73+
context('with wrong version', function () {
74+
it('returns 0', async function () {
75+
// The last two hex digits are the signature version.
76+
// The only valid values are 0, 1, 27 and 28.
77+
const version = '02';
78+
const signature = signatureWithoutVersion + version;
79+
(await this.ecdsa.recover(TEST_MESSAGE, signature)).should.equal(
80+
'0x0000000000000000000000000000000000000000');
81+
});
82+
});
83+
});
84+
85+
context('using web3.eth.sign', function () {
86+
context('with correct signature', function () {
87+
it('returns signer address', async function () {
88+
// Create the signature
89+
const signature = signMessage(anyone, TEST_MESSAGE);
90+
91+
// Recover the signer address from the generated message and signature.
92+
(await this.ecdsa.recover(
93+
toEthSignedMessageHash(TEST_MESSAGE),
94+
signature
95+
)).should.equal(anyone);
96+
});
97+
});
98+
99+
context('with wrong signature', function () {
100+
it('does not return signer address', async function () {
101+
// Create the signature
102+
const signature = signMessage(anyone, TEST_MESSAGE);
103+
104+
// Recover the signer address from the generated message and wrong signature.
105+
(await this.ecdsa.recover(WRONG_MESSAGE, signature)).should.not.equal(anyone);
106+
});
107+
});
108+
});
50109
});
51110

52-
// @TODO - remove `skip` once we upgrade to solc^0.5
53-
it.skip('recover should revert when a small hash is sent', async function () {
54-
// Create the signature
55-
const signature = signMessage(anyone, TEST_MESSAGE);
56-
await expectThrow(
57-
this.mock.recover(TEST_MESSAGE.substring(2), signature)
58-
);
111+
context('with small hash', function () {
112+
// @TODO - remove `skip` once we upgrade to solc^0.5
113+
it.skip('reverts', async function () {
114+
// Create the signature
115+
const signature = signMessage(anyone, TEST_MESSAGE);
116+
await expectThrow(
117+
this.ecdsa.recover(TEST_MESSAGE.substring(2), signature)
118+
);
119+
});
59120
});
60121

61122
context('toEthSignedMessage', function () {
62123
it('should prefix hashes correctly', async function () {
63-
(await this.mock.toEthSignedMessageHash(TEST_MESSAGE)).should.equal(toEthSignedMessageHash(TEST_MESSAGE));
124+
(await this.ecdsa.toEthSignedMessageHash(TEST_MESSAGE)).should.equal(toEthSignedMessageHash(TEST_MESSAGE));
64125
});
65126
});
66127
});

0 commit comments

Comments
 (0)