55// @ts -nocheck
66const assert = require ( 'assert' ) ;
77import { ethers , network } from 'hardhat' ;
8- import BN from 'bn.js' ;
98import { toFixedHex , toHex } from '@webb-tools/utils' ;
10- import EC from 'elliptic' ;
11- const ec = new EC . ec ( 'secp256k1' ) ;
129const TruffleAssert = require ( 'truffle-assertions' ) ;
1310
1411import { Governable , Governable__factory } from '@webb-tools/contracts' ;
15- import { ethers } from 'ethers' ;
12+ import type { Signer } from 'ethers' ;
1613import { ZERO_BYTES32 } from '@webb-tools/utils' ;
1714import { keccak256 , recoverAddress , solidityPack } from 'ethers/lib/utils' ;
1815
1916describe ( 'Governable Contract' , ( ) => {
2017 let governableInstance : Governable ;
21- let sender : ethers . Signer ;
22- let nextGovernor : ethers . Signer ;
23- let arbSigner : ethers . Signer ;
18+ let sender : Signer ;
19+ let nextGovernor : Signer ;
20+ let arbSigner : Signer ;
2421
2522 beforeEach ( async ( ) => {
2623 const signers = await ethers . getSigners ( ) ;
@@ -60,8 +57,7 @@ describe('Governable Contract', () => {
6057
6158 it ( 'should check ownership is transferred to new governor via signed public key' , async ( ) => {
6259 const wallet = ethers . Wallet . createRandom ( ) ;
63- const key = ec . keyFromPrivate ( wallet . privateKey . slice ( 2 ) , 'hex' ) ;
64- const pubkey = key . getPublic ( ) . encode ( 'hex' ) . slice ( 2 ) ;
60+ const pubkey = ethers . utils . computePublicKey ( wallet . privateKey , false ) . slice ( 4 ) ;
6561
6662 const publicKey = '0x' + pubkey ;
6763 let nextGovernorAddress = ethers . utils . getAddress (
@@ -71,11 +67,7 @@ describe('Governable Contract', () => {
7167 await governableInstance . transferOwnership ( nextGovernorAddress , 1 ) ;
7268
7369 const dummy = ethers . Wallet . createRandom ( ) ;
74- const dummyPubkey = ec
75- . keyFromPrivate ( dummy . privateKey , 'hex' )
76- . getPublic ( )
77- . encode ( 'hex' )
78- . slice ( 2 ) ;
70+ const dummyPubkey = ethers . utils . computePublicKey ( dummy . privateKey , false ) . slice ( 4 ) ;
7971
8072 nextGovernorAddress = ethers . utils . getAddress (
8173 '0x' + ethers . utils . keccak256 ( '0x' + dummyPubkey ) . slice ( - 40 )
@@ -100,24 +92,8 @@ describe('Governable Contract', () => {
10092 ]
10193 ) ;
10294
103- let msg = ethers . utils . arrayify ( ethers . utils . keccak256 ( prehashed ) ) ;
104- let signature = key . sign ( msg ) ;
105- let expandedSig = {
106- r : '0x' + signature . r . toString ( 'hex' ) ,
107- s : '0x' + signature . s . toString ( 'hex' ) ,
108- v : signature . recoveryParam + 27 ,
109- } ;
110-
111- // Transaction malleability fix if s is too large (Bitcoin allows it, Ethereum rejects it)
112- // https://ethereum.stackexchange.com/questions/55245/why-is-s-in-transaction-signature-limited-to-n-21
113- let sig ;
114- try {
115- sig = ethers . utils . joinSignature ( expandedSig ) ;
116- } catch ( e ) {
117- expandedSig . s = '0x' + new BN ( ec . curve . n ) . sub ( signature . s ) . toString ( 'hex' ) ;
118- expandedSig . v = expandedSig . v === 27 ? 28 : 27 ;
119- sig = ethers . utils . joinSignature ( expandedSig ) ;
120- }
95+ const digest = ethers . utils . keccak256 ( prehashed ) ;
96+ const sig = ethers . utils . joinSignature ( wallet . _signingKey ( ) . signDigest ( digest ) ) ;
12197
12298 const tx = await governableInstance . transferOwnershipWithSignature (
12399 refreshProposal . voterMerkleRoot ,
@@ -158,8 +134,7 @@ describe('Governable Contract', () => {
158134
159135 assert . strictEqual ( ( await governableInstance . refreshNonce ( ) ) . toString ( ) , '0' ) ;
160136 const wallet = ethers . Wallet . createRandom ( ) ;
161- const key = ec . keyFromPrivate ( wallet . privateKey . slice ( 2 ) , 'hex' ) ;
162- const pubkey = key . getPublic ( ) . encode ( 'hex' ) . slice ( 2 ) ;
137+ const pubkey = ethers . utils . computePublicKey ( wallet . privateKey , false ) . slice ( 4 ) ;
163138 const publicKey = '0x' + pubkey ;
164139 let nextGovernorAddress = ethers . utils . getAddress (
165140 '0x' + ethers . utils . keccak256 ( publicKey ) . slice ( - 40 )
@@ -168,11 +143,7 @@ describe('Governable Contract', () => {
168143 await governableInstance . transferOwnership ( nextGovernorAddress , 1 ) ;
169144
170145 const dummy = ethers . Wallet . createRandom ( ) ;
171- const dummyPubkey = ec
172- . keyFromPrivate ( dummy . privateKey , 'hex' )
173- . getPublic ( )
174- . encode ( 'hex' )
175- . slice ( 2 ) ;
146+ const dummyPubkey = ethers . utils . computePublicKey ( dummy . privateKey , false ) . slice ( 4 ) ;
176147
177148 const nonce = 2 ;
178149
@@ -222,24 +193,8 @@ describe('Governable Contract', () => {
222193 ]
223194 ) ;
224195
225- let msg = ethers . utils . arrayify ( ethers . utils . keccak256 ( prehashed ) ) ;
226- let signature = key . sign ( msg ) ;
227- let expandedSig = {
228- r : '0x' + signature . r . toString ( 'hex' ) ,
229- s : '0x' + signature . s . toString ( 'hex' ) ,
230- v : signature . recoveryParam + 27 ,
231- } ;
232-
233- // Transaction malleability fix if s is too large (Bitcoin allows it, Ethereum rejects it)
234- // https://ethereum.stackexchange.com/questions/55245/why-is-s-in-transaction-signature-limited-to-n-21
235- let sig : any ;
236- try {
237- sig = ethers . utils . joinSignature ( expandedSig ) ;
238- } catch ( e ) {
239- expandedSig . s = '0x' + new BN ( ec . curve . n ) . sub ( signature . s ) . toString ( 'hex' ) ;
240- expandedSig . v = expandedSig . v === 27 ? 28 : 27 ;
241- sig = ethers . utils . joinSignature ( expandedSig ) ;
242- }
196+ const digest = ethers . utils . keccak256 ( prehashed ) ;
197+ const sig = ethers . utils . joinSignature ( wallet . _signingKey ( ) . signDigest ( digest ) ) ;
243198
244199 const tx = await governableInstance . transferOwnershipWithSignature (
245200 refreshProposal . voterMerkleRoot ,
0 commit comments