1
1
import chai from 'chai' ;
2
- import type { Contract } from 'ethers' ;
3
- import { BigNumber as BN } from 'ethers' ;
4
- import { SigningKey } from 'ethers/lib/utils' ;
2
+ import { SigningKey , randomBytes , hexlify , ZeroHash , toBeHex } from 'ethers' ;
5
3
import { ethers } from 'hardhat' ;
6
4
7
5
import { componentSign } from '../protocol/validators' ;
6
+ import type { MockCryptography } from '../typechain' ;
8
7
9
8
const { expect } = chai ;
10
9
11
- const SECP256K1N = BN . from (
10
+ const SECP256K1N = BigInt (
12
11
'0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141'
13
12
) ;
14
13
15
14
describe ( 'ECDSA' , async ( ) => {
16
- let mockCrypto : Contract ;
15
+ let mockCrypto : MockCryptography ;
17
16
let signer : SigningKey ;
18
17
before ( async ( ) => {
19
- const mockCryptoFactory = await ethers . getContractFactory (
20
- 'MockCryptography'
21
- ) ;
22
- mockCrypto = await mockCryptoFactory . deploy ( ) ;
23
- await mockCrypto . deployed ( ) ;
18
+ mockCrypto = await ethers . deployContract ( 'MockCryptography' ) ;
24
19
25
20
signer = new SigningKey (
26
21
'0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d'
27
22
) ;
28
23
} ) ;
29
24
30
25
it ( 'rejects component signatures with high s-value' , async ( ) => {
31
- const msg = ethers . utils . hexlify ( ethers . utils . randomBytes ( 32 ) ) ;
26
+ const msg = hexlify ( randomBytes ( 32 ) ) ;
32
27
const sig = await componentSign ( signer , msg ) ;
33
- const vOrig = BN . from ( sig . v ) . sub ( 27 ) ; // take v as 0 or 1
28
+ const vOrig = BigInt ( sig . v ) - 27n ; // take v as 0 or 1
34
29
35
30
// flip v and ensure it is 27 or 28
36
- const vFlipped = vOrig . xor ( 1 ) . add ( 27 ) ;
31
+ const vFlipped = ( vOrig ^ 1n ) + 27n ;
37
32
// flip s to secp256k1n - original s. This defines a unique
38
33
// signature over the same data, which we want to reject.
39
- const sFlipped = SECP256K1N . sub ( sig . s ) ;
40
- const badSig = { v : vFlipped , r : sig . r , s : sFlipped } ;
34
+ const sFlipped = SECP256K1N - BigInt ( sig . s ) ;
35
+ const badSig = { v : vFlipped , r : sig . r , s : toBeHex ( sFlipped ) } ;
41
36
42
37
await expect (
43
38
mockCrypto . addressFromSignatureComponents (
@@ -50,10 +45,10 @@ describe('ECDSA', async () => {
50
45
} ) ;
51
46
52
47
it ( 'rejects component signatures from the zero address' , async ( ) => {
53
- const msg = ethers . utils . hexlify ( ethers . utils . randomBytes ( 32 ) ) ;
54
- const sig = await componentSign ( signer , msg ) ;
48
+ const msg = hexlify ( randomBytes ( 32 ) ) ;
49
+ const sig = componentSign ( signer , msg ) ;
55
50
// an r value < 1 makes the signature invalid. ecrecover will return 0x0
56
- const badSig = { v : sig . v , r : ethers . constants . HashZero , s : sig . s } ;
51
+ const badSig = { v : sig . v , r : ZeroHash , s : sig . s } ;
57
52
58
53
await expect (
59
54
mockCrypto . addressFromSignatureComponents (
@@ -66,27 +61,26 @@ describe('ECDSA', async () => {
66
61
} ) ;
67
62
68
63
it ( 'rejects invalid compact signatures' , async ( ) => {
69
- const msg = ethers . utils . hexlify ( ethers . utils . randomBytes ( 32 ) ) ;
70
- const sig = await componentSign ( signer , msg ) ;
64
+ const msg = hexlify ( randomBytes ( 32 ) ) ;
65
+ const sig = componentSign ( signer , msg ) ;
71
66
72
67
// an r value < 1 makes the signature invalid. ecrecover will return 0x0
73
- const badRValue = ethers . constants . HashZero ;
68
+ const badRValue = ZeroHash ;
74
69
75
- const badSigCompact = badRValue . concat ( sig . _vs . slice ( 2 ) ) ;
70
+ const badSigCompact = badRValue . concat ( sig . yParityAndS . slice ( 2 ) ) ;
76
71
await expect (
77
72
mockCrypto . addressFromSignature ( badSigCompact , msg )
78
73
) . to . be . revertedWith ( 'signature-invalid' ) ;
74
+ // // signature too short
79
75
80
- // signature too short
81
-
82
- const shortSig = sig . r . concat ( sig . _vs . slice ( 4 ) ) ;
76
+ const shortSig = sig . r . concat ( sig . yParityAndS . slice ( 4 ) ) ;
83
77
await expect (
84
78
mockCrypto . addressFromSignature ( shortSig , msg )
85
79
) . to . be . revertedWith ( 'signature-invalid-length' ) ;
86
80
87
81
// signature too long
88
82
89
- const longSig = sig . r . concat ( sig . _vs . slice ( 2 ) ) . concat ( 'aa' ) ;
83
+ const longSig = sig . r . concat ( sig . yParityAndS . slice ( 2 ) ) . concat ( 'aa' ) ;
90
84
await expect (
91
85
mockCrypto . addressFromSignature ( longSig , msg )
92
86
) . to . be . revertedWith ( 'signature-invalid-length' ) ;
0 commit comments