@@ -19,18 +19,26 @@ describe('ECDSA', function () {
19
19
20
20
describe ( 'recover with invalid signature' , function ( ) {
21
21
it ( 'with short signature' , async function ( ) {
22
- await expect ( this . mock . $recover ( TEST_MESSAGE , '0x1234' ) )
22
+ const signature = '0x1234' ;
23
+
24
+ await expect ( this . mock . $recover ( TEST_MESSAGE , signature ) )
25
+ . to . be . revertedWithCustomError ( this . mock , 'ECDSAInvalidSignatureLength' )
26
+ . withArgs ( 2 ) ;
27
+
28
+ await expect ( this . mock . $recoverCalldata ( TEST_MESSAGE , signature ) )
23
29
. to . be . revertedWithCustomError ( this . mock , 'ECDSAInvalidSignatureLength' )
24
30
. withArgs ( 2 ) ;
25
31
} ) ;
26
32
27
33
it ( 'with long signature' , async function ( ) {
28
- await expect (
29
- this . mock . $recover (
30
- TEST_MESSAGE ,
31
- '0x01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789' ,
32
- ) ,
33
- )
34
+ const signature =
35
+ '0x01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789' ;
36
+
37
+ await expect ( this . mock . $recover ( TEST_MESSAGE , signature ) )
38
+ . to . be . revertedWithCustomError ( this . mock , 'ECDSAInvalidSignatureLength' )
39
+ . withArgs ( 85 ) ;
40
+
41
+ await expect ( this . mock . $recoverCalldata ( TEST_MESSAGE , signature ) )
34
42
. to . be . revertedWithCustomError ( this . mock , 'ECDSAInvalidSignatureLength' )
35
43
. withArgs ( 85 ) ;
36
44
} ) ;
@@ -43,23 +51,29 @@ describe('ECDSA', function () {
43
51
const signature = await this . signer . signMessage ( TEST_MESSAGE ) ;
44
52
45
53
// Recover the signer address from the generated message and signature.
46
- expect ( await this . mock . $recover ( ethers . hashMessage ( TEST_MESSAGE ) , signature ) ) . to . equal ( this . signer ) ;
47
- expect ( await this . mock . $recoverCalldata ( ethers . hashMessage ( TEST_MESSAGE ) , signature ) ) . to . equal ( this . signer ) ;
54
+ await expect ( this . mock . $recover ( ethers . hashMessage ( TEST_MESSAGE ) , signature ) ) . to . eventually . equal ( this . signer ) ;
55
+ await expect ( this . mock . $recoverCalldata ( ethers . hashMessage ( TEST_MESSAGE ) , signature ) ) . to . eventually . equal (
56
+ this . signer ,
57
+ ) ;
48
58
} ) ;
49
59
50
60
it ( 'returns signer address with correct signature for arbitrary length message' , async function ( ) {
51
61
// Create the signature
52
62
const signature = await this . signer . signMessage ( NON_HASH_MESSAGE ) ;
53
63
54
64
// Recover the signer address from the generated message and signature.
55
- expect ( await this . mock . $recover ( ethers . hashMessage ( NON_HASH_MESSAGE ) , signature ) ) . to . equal ( this . signer ) ;
56
- expect ( await this . mock . $recoverCalldata ( ethers . hashMessage ( NON_HASH_MESSAGE ) , signature ) ) . to . equal ( this . signer ) ;
65
+ await expect ( this . mock . $recover ( ethers . hashMessage ( NON_HASH_MESSAGE ) , signature ) ) . to . eventually . equal (
66
+ this . signer ,
67
+ ) ;
68
+ await expect ( this . mock . $recoverCalldata ( ethers . hashMessage ( NON_HASH_MESSAGE ) , signature ) ) . to . eventually . equal (
69
+ this . signer ,
70
+ ) ;
57
71
} ) ;
58
72
59
73
it ( 'returns a different address' , async function ( ) {
60
74
const signature = await this . signer . signMessage ( TEST_MESSAGE ) ;
61
- expect ( await this . mock . $recover ( WRONG_MESSAGE , signature ) ) . to . not . be . equal ( this . signer ) ;
62
- expect ( await this . mock . $recoverCalldata ( WRONG_MESSAGE , signature ) ) . to . not . be . equal ( this . signer ) ;
75
+ await expect ( this . mock . $recover ( WRONG_MESSAGE , signature ) ) . to . eventually . not . equal ( this . signer ) ;
76
+ await expect ( this . mock . $recoverCalldata ( WRONG_MESSAGE , signature ) ) . to . eventually . not . equal ( this . signer ) ;
63
77
} ) ;
64
78
65
79
it ( 'reverts with invalid signature' , async function ( ) {
@@ -85,22 +99,24 @@ describe('ECDSA', function () {
85
99
it ( 'works with correct v value' , async function ( ) {
86
100
const v = '0x1b' ; // 27 = 1b.
87
101
const signature = ethers . concat ( [ signatureWithoutV , v ] ) ;
88
- expect ( await this . mock . $recover ( TEST_MESSAGE , signature ) ) . to . equal ( signer ) ;
89
- expect ( await this . mock . $recoverCalldata ( TEST_MESSAGE , signature ) ) . to . equal ( signer ) ;
102
+ await expect ( this . mock . $recover ( TEST_MESSAGE , signature ) ) . to . eventually . equal ( signer ) ;
103
+ await expect ( this . mock . $recoverCalldata ( TEST_MESSAGE , signature ) ) . to . eventually . equal ( signer ) ;
90
104
91
105
const { r, s, yParityAndS : vs } = ethers . Signature . from ( signature ) ;
92
- expect ( await this . mock . getFunction ( '$recover(bytes32,uint8,bytes32,bytes32)' ) ( TEST_MESSAGE , v , r , s ) ) . to . equal (
93
- signer ,
94
- ) ;
106
+ await expect (
107
+ this . mock . getFunction ( '$recover(bytes32,uint8,bytes32,bytes32)' ) ( TEST_MESSAGE , v , r , s ) ,
108
+ ) . to . eventually . equal ( signer ) ;
95
109
96
- expect ( await this . mock . getFunction ( '$recover(bytes32,bytes32,bytes32)' ) ( TEST_MESSAGE , r , vs ) ) . to . equal ( signer ) ;
110
+ await expect (
111
+ this . mock . getFunction ( '$recover(bytes32,bytes32,bytes32)' ) ( TEST_MESSAGE , r , vs ) ,
112
+ ) . to . eventually . equal ( signer ) ;
97
113
} ) ;
98
114
99
115
it ( 'rejects incorrect v value' , async function ( ) {
100
116
const v = '0x1c' ; // 28 = 1c.
101
117
const signature = ethers . concat ( [ signatureWithoutV , v ] ) ;
102
- expect ( await this . mock . $recover ( TEST_MESSAGE , signature ) ) . to . not . equal ( signer ) ;
103
- expect ( await this . mock . $recoverCalldata ( TEST_MESSAGE , signature ) ) . to . not . equal ( signer ) ;
118
+ await expect ( this . mock . $recover ( TEST_MESSAGE , signature ) ) . to . eventually . not . equal ( signer ) ;
119
+ await expect ( this . mock . $recoverCalldata ( TEST_MESSAGE , signature ) ) . to . eventually . not . equal ( signer ) ;
104
120
105
121
const { r, s, yParityAndS : vs } = ethers . Signature . from ( signature ) ;
106
122
expect (
@@ -154,29 +170,31 @@ describe('ECDSA', function () {
154
170
it ( 'works with correct v value' , async function ( ) {
155
171
const v = '0x1c' ; // 28 = 1c.
156
172
const signature = ethers . concat ( [ signatureWithoutV , v ] ) ;
157
- expect ( await this . mock . $recover ( TEST_MESSAGE , signature ) ) . to . equal ( signer ) ;
158
- expect ( await this . mock . $recoverCalldata ( TEST_MESSAGE , signature ) ) . to . equal ( signer ) ;
173
+ await expect ( this . mock . $recover ( TEST_MESSAGE , signature ) ) . to . eventually . equal ( signer ) ;
174
+ await expect ( this . mock . $recoverCalldata ( TEST_MESSAGE , signature ) ) . to . eventually . equal ( signer ) ;
159
175
160
176
const { r, s, yParityAndS : vs } = ethers . Signature . from ( signature ) ;
161
- expect ( await this . mock . getFunction ( '$recover(bytes32,uint8,bytes32,bytes32)' ) ( TEST_MESSAGE , v , r , s ) ) . to . equal (
162
- signer ,
163
- ) ;
177
+ await expect (
178
+ this . mock . getFunction ( '$recover(bytes32,uint8,bytes32,bytes32)' ) ( TEST_MESSAGE , v , r , s ) ,
179
+ ) . to . eventually . equal ( signer ) ;
164
180
165
- expect ( await this . mock . getFunction ( '$recover(bytes32,bytes32,bytes32)' ) ( TEST_MESSAGE , r , vs ) ) . to . equal ( signer ) ;
181
+ await expect (
182
+ this . mock . getFunction ( '$recover(bytes32,bytes32,bytes32)' ) ( TEST_MESSAGE , r , vs ) ,
183
+ ) . to . eventually . equal ( signer ) ;
166
184
} ) ;
167
185
168
186
it ( 'rejects incorrect v value' , async function ( ) {
169
187
const v = '0x1b' ; // 27 = 1b.
170
188
const signature = ethers . concat ( [ signatureWithoutV , v ] ) ;
171
- expect ( await this . mock . $recover ( TEST_MESSAGE , signature ) ) . to . not . equal ( signer ) ;
172
- expect ( await this . mock . $recoverCalldata ( TEST_MESSAGE , signature ) ) . to . not . equal ( signer ) ;
189
+ await expect ( this . mock . $recover ( TEST_MESSAGE , signature ) ) . to . not . equal ( signer ) ;
190
+ await expect ( this . mock . $recoverCalldata ( TEST_MESSAGE , signature ) ) . to . not . equal ( signer ) ;
173
191
174
192
const { r, s, yParityAndS : vs } = ethers . Signature . from ( signature ) ;
175
193
expect (
176
194
await this . mock . getFunction ( '$recover(bytes32,uint8,bytes32,bytes32)' ) ( TEST_MESSAGE , v , r , s ) ,
177
195
) . to . not . equal ( signer ) ;
178
196
179
- expect ( await this . mock . getFunction ( '$recover(bytes32,bytes32,bytes32)' ) ( TEST_MESSAGE , r , vs ) ) . to . not . equal (
197
+ await expect ( this . mock . getFunction ( '$recover(bytes32,bytes32,bytes32)' ) ( TEST_MESSAGE , r , vs ) ) . to . not . equal (
180
198
signer ,
181
199
) ;
182
200
} ) ;
@@ -236,4 +254,65 @@ describe('ECDSA', function () {
236
254
expect ( ( ) => ethers . Signature . from ( highSSignature ) ) . to . throw ( 'non-canonical s' ) ;
237
255
} ) ;
238
256
} ) ;
257
+
258
+ describe ( 'parse signature' , function ( ) {
259
+ it ( '65 and 64 bytes signatures' , async function ( ) {
260
+ // Create the signature
261
+ const signature = await this . signer . signMessage ( TEST_MESSAGE ) . then ( ethers . Signature . from ) ;
262
+
263
+ await expect ( this . mock . $parse ( signature . serialized ) ) . to . eventually . deep . equal ( [
264
+ signature . v ,
265
+ signature . r ,
266
+ signature . s ,
267
+ ] ) ;
268
+ await expect ( this . mock . $parse ( signature . compactSerialized ) ) . to . eventually . deep . equal ( [
269
+ signature . v ,
270
+ signature . r ,
271
+ signature . s ,
272
+ ] ) ;
273
+ await expect ( this . mock . $parseCalldata ( signature . serialized ) ) . to . eventually . deep . equal ( [
274
+ signature . v ,
275
+ signature . r ,
276
+ signature . s ,
277
+ ] ) ;
278
+ await expect ( this . mock . $parseCalldata ( signature . compactSerialized ) ) . to . eventually . deep . equal ( [
279
+ signature . v ,
280
+ signature . r ,
281
+ signature . s ,
282
+ ] ) ;
283
+ } ) ;
284
+
285
+ it ( 'with short signature' , async function ( ) {
286
+ const signature = '0x1234' ;
287
+
288
+ await expect ( this . mock . $parse ( signature ) ) . to . eventually . deep . equal ( [ 0n , ethers . ZeroHash , ethers . ZeroHash ] ) ;
289
+
290
+ await expect ( this . mock . $parseCalldata ( signature ) ) . to . eventually . deep . equal ( [
291
+ 0n ,
292
+ ethers . ZeroHash ,
293
+ ethers . ZeroHash ,
294
+ ] ) ;
295
+ } ) ;
296
+
297
+ it ( 'with long signature' , async function ( ) {
298
+ const signature =
299
+ '0x01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789' ;
300
+
301
+ await expect ( this . mock . $recover ( TEST_MESSAGE , signature ) )
302
+ . to . be . revertedWithCustomError ( this . mock , 'ECDSAInvalidSignatureLength' )
303
+ . withArgs ( 85 ) ;
304
+
305
+ await expect ( this . mock . $recoverCalldata ( TEST_MESSAGE , signature ) )
306
+ . to . be . revertedWithCustomError ( this . mock , 'ECDSAInvalidSignatureLength' )
307
+ . withArgs ( 85 ) ;
308
+
309
+ await expect ( this . mock . $parse ( signature ) ) . to . eventually . deep . equal ( [ 0n , ethers . ZeroHash , ethers . ZeroHash ] ) ;
310
+
311
+ await expect ( this . mock . $parseCalldata ( signature ) ) . to . eventually . deep . equal ( [
312
+ 0n ,
313
+ ethers . ZeroHash ,
314
+ ethers . ZeroHash ,
315
+ ] ) ;
316
+ } ) ;
317
+ } ) ;
239
318
} ) ;
0 commit comments