-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathImmutableDeployment.spec.ts
More file actions
399 lines (338 loc) · 13.2 KB
/
ImmutableDeployment.spec.ts
File metadata and controls
399 lines (338 loc) · 13.2 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import { ethers as hardhat } from 'hardhat'
import { ethers } from 'ethers'
import { getContractAddress } from '@ethersproject/address'
import {
Factory,
Factory__factory,
MainModule__factory,
ImmutableSigner,
ImmutableSigner__factory,
LatestWalletImplLocator__factory,
StartupWalletImpl__factory,
MainModuleDynamicAuth,
MainModuleDynamicAuth__factory,
} from '../src'
import {
encodeImageHash,
expect,
addressOf,
encodeMetaTransactionsData,
walletMultiSign,
ethSign,
} from './utils'
import { LatestWalletImplLocator, StartupWalletImpl } from 'src/gen/typechain'
describe('E2E Immutable Wallet Deployment', () => {
let contractDeployerEOA: ethers.Signer
let relayerEOA: ethers.Signer
let userEOA: ethers.Signer
let immutableEOA: ethers.Signer
let randomEOA: ethers.Signer
let adminEOA: ethers.Signer
let walletDeployerEOA: ethers.Signer
// All contracts involved in the wallet ecosystem
let factory: Factory
let mainModuleDynamicAuth: MainModuleDynamicAuth
let immutableSigner: ImmutableSigner
let moduleLocator: LatestWalletImplLocator
let startupWallet: StartupWalletImpl
const WALLET_FACTORY_NONCE = 1
const STARTUP_WALLET_NONCE = 2
const IMMUTABLE_SIGNER_NONCE = 3
const LOCATOR_NONCE = 4
const MAIN_MODULE_DYNAMIC_AUTH_NONCE = 5
beforeEach(async () => {
[
userEOA,
immutableEOA,
randomEOA,
adminEOA,
walletDeployerEOA,
relayerEOA,
contractDeployerEOA
] = await hardhat.getSigners()
await hardhat.provider.send("hardhat_reset", [])
// Matches the production environment where the first transaction (nonce 0)
// is used for testing.
contractDeployerEOA.sendTransaction({ to: ethers.constants.AddressZero, value: 0 })
// Nonce 1
factory = await new Factory__factory()
.connect(contractDeployerEOA)
.deploy(await adminEOA.getAddress(), await walletDeployerEOA.getAddress())
// Calculate the locator address ahead of time
const moduleLocatorAddress = getContractAddress({
from: await contractDeployerEOA.getAddress(),
nonce: LOCATOR_NONCE
})
// Nonce 2
startupWallet = await new StartupWalletImpl__factory()
.connect(contractDeployerEOA)
.deploy(moduleLocatorAddress)
// Nonce 3
immutableSigner = await new ImmutableSigner__factory()
.connect(contractDeployerEOA)
.deploy(await adminEOA.getAddress(), await adminEOA.getAddress(), await immutableEOA.getAddress())
// NOTE: Those could possibly be deployed by a separate account instead.
// Nonce 4
moduleLocator = await new LatestWalletImplLocator__factory()
.connect(contractDeployerEOA)
.deploy(await adminEOA.getAddress(), await walletDeployerEOA.getAddress())
// Nonce 5
mainModuleDynamicAuth = await new MainModuleDynamicAuth__factory()
.connect(contractDeployerEOA)
.deploy(factory.address, startupWallet.address, immutableSigner.address)
// Setup the latest implementation address
await moduleLocator
.connect(walletDeployerEOA)
.changeWalletImplementation(mainModuleDynamicAuth.address)
})
it('Should create deterministic contract addresses', async () => {
// Generate deployed contract addresses offchain from the deployer address
// and fixed nonces.
const factoryAddress = getContractAddress({
from: await contractDeployerEOA.getAddress(),
nonce: WALLET_FACTORY_NONCE
})
const startupWalletAddress = getContractAddress({
from: await contractDeployerEOA.getAddress(),
nonce: STARTUP_WALLET_NONCE
})
const immutableSignerAddress = getContractAddress({
from: await contractDeployerEOA.getAddress(),
nonce: IMMUTABLE_SIGNER_NONCE
})
const locatorAddress = getContractAddress({
from: await contractDeployerEOA.getAddress(),
nonce: LOCATOR_NONCE
})
const mainModuleAddress = getContractAddress({
from: await contractDeployerEOA.getAddress(),
nonce: MAIN_MODULE_DYNAMIC_AUTH_NONCE
})
// Check they match against the actual deployed addresses
expect(factory.address).to.equal(factoryAddress)
expect(mainModuleDynamicAuth.address).to.equal(mainModuleAddress)
expect(immutableSigner.address).to.equal(immutableSignerAddress)
expect(startupWallet.address).to.equal(startupWalletAddress)
expect(moduleLocator.address).to.equal(locatorAddress)
})
it('Should execute a transaction signed by the ImmutableSigner', async () => {
// Deploy wallet
const walletSalt = encodeImageHash(2, [
{ weight: 1, address: await userEOA.getAddress() },
{ weight: 1, address: immutableSigner.address }
])
const walletAddress = addressOf(factory.address, startupWallet.address, walletSalt)
const walletDeploymentTx = await factory.connect(walletDeployerEOA).deploy(startupWallet.address, walletSalt)
await walletDeploymentTx.wait()
// Connect to the generated user address
const wallet = MainModule__factory.connect(walletAddress, relayerEOA)
// Transfer funds to the SCW
const transferTx = await relayerEOA.sendTransaction({ to: walletAddress, value: 1 })
await transferTx.wait()
// Return funds
const transaction = {
delegateCall: false,
revertOnError: true,
gasLimit: 1000000,
target: await randomEOA.getAddress(),
value: 1,
data: []
}
// Build meta-transaction
const networkId = (await hardhat.provider.getNetwork()).chainId
const nonce = 0
const data = encodeMetaTransactionsData(wallet.address, [transaction], networkId, nonce)
const signature = await walletMultiSign(
[
{ weight: 1, owner: userEOA as ethers.Wallet },
// 03 -> Call the address' isValidSignature()
{ weight: 1, owner: immutableSigner.address, signature: (await ethSign(immutableEOA as ethers.Wallet, data)) + '03' }
],
2,
data,
false
)
const originalBalance = await randomEOA.getBalance();
const executionTx = await wallet.execute([transaction], nonce, signature)
await executionTx.wait()
expect(await randomEOA.getBalance()).to.equal(originalBalance.add(1))
})
it('Should execute multiple transactions signed by the ImmutableSigner', async () => {
// Deploy wallet
const walletSalt = encodeImageHash(2, [
{ weight: 1, address: await userEOA.getAddress() },
{ weight: 1, address: immutableSigner.address }
])
const walletAddress = addressOf(factory.address, startupWallet.address, walletSalt)
const walletDeploymentTx = await factory.connect(walletDeployerEOA).deploy(startupWallet.address, walletSalt)
await walletDeploymentTx.wait()
// Connect to the generated user address
const wallet = MainModule__factory.connect(walletAddress, relayerEOA)
// Transfer funds to the SCW
const transferTx = await relayerEOA.sendTransaction({ to: walletAddress, value: 5 })
await transferTx.wait()
for (const nonce of [0, 1, 2, 3, 4]) {
// Return funds
const transaction = {
delegateCall: false,
revertOnError: true,
gasLimit: 1000000,
target: await randomEOA.getAddress(),
value: 1,
data: []
}
// Build meta-transaction
const networkId = (await hardhat.provider.getNetwork()).chainId
const data = encodeMetaTransactionsData(wallet.address, [transaction], networkId, nonce)
const signature = await walletMultiSign(
[
{ weight: 1, owner: userEOA as ethers.Wallet },
// 03 -> Call the address' isValidSignature()
{ weight: 1, owner: immutableSigner.address, signature: (await ethSign(immutableEOA as ethers.Wallet, data)) + '03' }
],
2,
data,
false
)
const originalBalance = await randomEOA.getBalance();
const executionTx = await wallet.execute([transaction], nonce, signature)
await executionTx.wait()
expect(await randomEOA.getBalance()).to.equal(originalBalance.add(1))
}
})
it('Should not execute a transaction not signed by the ImmutableSigner', async () => {
// Deploy wallet
const walletSalt = encodeImageHash(2, [
{ weight: 1, address: await userEOA.getAddress() },
{ weight: 1, address: immutableSigner.address }
])
const walletAddress = addressOf(factory.address, startupWallet.address, walletSalt)
const walletDeploymentTx = await factory.connect(walletDeployerEOA).deploy(startupWallet.address, walletSalt)
await walletDeploymentTx.wait()
// Connect to the generated user address
const wallet = MainModule__factory.connect(walletAddress, relayerEOA)
// Transfer funds to the SCW
const transferTx = await relayerEOA.sendTransaction({ to: walletAddress, value: 1 })
await transferTx.wait()
// Return funds
const transaction = {
delegateCall: false,
revertOnError: true,
gasLimit: 1000000,
target: await randomEOA.getAddress(),
value: 1,
data: []
}
// Build meta-transaction
const networkId = (await hardhat.provider.getNetwork()).chainId
const nonce = 0
const data = encodeMetaTransactionsData(wallet.address, [transaction], networkId, nonce)
const signature = await walletMultiSign(
[
{ weight: 1, owner: userEOA as ethers.Wallet },
{ weight: 1, owner: immutableSigner.address, signature: (await ethSign(randomEOA as ethers.Wallet, data)) + '03' }
],
2,
data,
false
)
await expect(wallet.execute([transaction], nonce, signature)).to.be.revertedWith(
'ModuleAuthDynamic#_signatureValidation: INVALID_SIGNATURE'
)
})
it('Should not execute a transaction signed by the ImmutableSigner with the incorrect data', async () => {
// Deploy wallet
const walletSalt = encodeImageHash(2, [
{ weight: 1, address: await userEOA.getAddress() },
{ weight: 1, address: immutableSigner.address }
])
const walletAddress = addressOf(factory.address, startupWallet.address, walletSalt)
const walletDeploymentTx = await factory.connect(walletDeployerEOA).deploy(startupWallet.address, walletSalt)
await walletDeploymentTx.wait()
// Connect to the generated user address
const wallet = MainModule__factory.connect(walletAddress, relayerEOA)
// Transfer funds to the SCW
const transferTx = await relayerEOA.sendTransaction({ to: walletAddress, value: 1 })
await transferTx.wait()
// Return funds
const transaction = {
delegateCall: false,
revertOnError: true,
gasLimit: 1000000,
target: await relayerEOA.getAddress(),
value: 1,
data: []
}
// Build meta-transaction
const networkId = (await hardhat.provider.getNetwork()).chainId
const nonce = 0
const data = encodeMetaTransactionsData(wallet.address, [transaction], networkId, nonce)
const meaninglessDataDisturbance = '05'
const signature = await walletMultiSign(
[
{ weight: 1, owner: userEOA as ethers.Wallet },
// 03 -> Call the address' isValidSignature()
{
weight: 1,
owner: immutableSigner.address,
signature: (await ethSign(immutableEOA as ethers.Wallet, data + meaninglessDataDisturbance)) + '03'
}
],
2,
data,
false
)
await expect(wallet.execute([transaction], nonce, signature)).to.be.revertedWith(
'ModuleAuthDynamic#_signatureValidation: INVALID_SIGNATURE'
)
})
it('Should not execute a transaction signed by an outdated signer', async () => {
// Deploy wallet
const walletSalt = encodeImageHash(2, [
{ weight: 1, address: await userEOA.getAddress() },
{ weight: 1, address: immutableSigner.address }
])
const walletAddress = addressOf(factory.address, startupWallet.address, walletSalt)
const walletDeploymentTx = await factory.connect(walletDeployerEOA).deploy(startupWallet.address, walletSalt)
await walletDeploymentTx.wait()
// Connect to the generated user address
const wallet = MainModule__factory.connect(walletAddress, relayerEOA)
// Transfer funds to the SCW
const transferTx = await relayerEOA.sendTransaction({ to: walletAddress, value: 1 })
await transferTx.wait()
// Change the signer
expect(await immutableSigner.connect(adminEOA).updateSigner(await randomEOA.getAddress()))
.to.emit(immutableSigner, 'SignerUpdated')
.withArgs(await immutableEOA.getAddress(), await randomEOA.getAddress())
// Return funds
const transaction = {
delegateCall: false,
revertOnError: true,
gasLimit: 1000000,
target: await relayerEOA.getAddress(),
value: 1,
data: []
}
// Build meta-transaction
const networkId = (await hardhat.provider.getNetwork()).chainId
const nonce = 0
const data = encodeMetaTransactionsData(wallet.address, [transaction], networkId, nonce)
const signature = await walletMultiSign(
[
{ weight: 1, owner: userEOA as ethers.Wallet },
// 03 -> Call the address' isValidSignature()
{
weight: 1,
owner: immutableSigner.address,
signature: (await ethSign(immutableEOA as ethers.Wallet, data)) + '03'
}
],
2,
data,
false
)
await expect(wallet.execute([transaction], nonce, signature)).to.be.revertedWith(
'ModuleAuthDynamic#_signatureValidation: INVALID_SIGNATURE'
)
})
})