-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathaccount.spec.ts
More file actions
1536 lines (1261 loc) · 54.7 KB
/
account.spec.ts
File metadata and controls
1536 lines (1261 loc) · 54.7 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { walletContracts } from '@0xsequence/abi'
import { commons, v1, v2 } from '@0xsequence/core'
import { migrator } from '@0xsequence/migration'
import { ChainId, NetworkConfig } from '@0xsequence/network'
import { LocalRelayer, Relayer } from '@0xsequence/relayer'
import { tracker, trackers } from '@0xsequence/sessions'
import { Orchestrator } from '@0xsequence/signhub'
import * as utils from '@0xsequence/tests'
import { Wallet } from '@0xsequence/wallet'
import * as chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import { ethers } from 'ethers'
import hardhat from 'hardhat'
import { Account } from '../src/account'
import { AccountOrchestratorWrapper } from '../src/orchestrator/wrapper'
const { expect } = chai.use(chaiAsPromised)
const deterministic = false
describe('Account', () => {
let provider1: ethers.providers.JsonRpcProvider
let provider2: ethers.providers.JsonRpcProvider
let signer1: ethers.Signer
let signer2: ethers.Signer
let contexts: commons.context.VersionedContext
let networks: NetworkConfig[]
let tracker: tracker.ConfigTracker & migrator.PresignedMigrationTracker
let defaultArgs: {
contexts: commons.context.VersionedContext
networks: NetworkConfig[]
tracker: tracker.ConfigTracker & migrator.PresignedMigrationTracker
}
let defaultTx: commons.transaction.Transaction
const createNestedAccount = async (entropy: string, bootstrapInner = true, bootstrapOuter = true) => {
const signer = randomWallet(entropy)
const configInner = {
threshold: 1,
checkpoint: Math.floor(now() / 1000),
signers: [{ address: signer.address, weight: 1 }]
}
const accountInner = await Account.new({
...defaultArgs,
config: configInner,
orchestrator: new Orchestrator([signer])
})
if (bootstrapInner) {
await accountInner.doBootstrap(networks[0].chainId)
}
const configOuter = {
threshold: 1,
checkpoint: Math.floor(now() / 1000),
signers: [{ address: accountInner.address, weight: 1 }]
}
const accountOuter = await Account.new({
...defaultArgs,
config: configOuter,
orchestrator: new Orchestrator([new AccountOrchestratorWrapper(accountInner)])
})
if (bootstrapOuter) {
await accountOuter.doBootstrap(networks[0].chainId)
}
return { signer, accountInner, accountOuter }
}
const getEth = async (address: string, signer?: ethers.Signer) => {
if (signer === undefined) {
// Do both networks
await getEth(address, signer1)
await getEth(address, signer2)
return
}
// Signer sends the address some ETH for defaultTx use
const tx = await signer.sendTransaction({
to: address,
value: 10 // Should be plenty
})
await tx.wait()
}
before(async () => {
provider1 = new ethers.providers.Web3Provider(hardhat.network.provider as any)
provider2 = new ethers.providers.JsonRpcProvider('http://127.0.0.1:7048')
// TODO: Implement migrations on local config tracker
tracker = new trackers.local.LocalConfigTracker(provider1)
signer1 = provider1.getSigner()
signer2 = provider2.getSigner()
networks = [
{
chainId: 31337 as ChainId,
name: 'hardhat',
provider: provider1,
rpcUrl: '',
relayer: new LocalRelayer(signer1),
nativeToken: {
symbol: 'ETH',
name: 'Ether',
decimals: 18
}
},
{
chainId: 31338 as ChainId,
name: 'hardhat2',
provider: provider2,
rpcUrl: 'http://127.0.0.1:7048',
relayer: new LocalRelayer(signer2),
nativeToken: {
symbol: 'ETH',
name: 'Ether',
decimals: 18
}
}
]
const context1 = utils.context.deploySequenceContexts(signer1)
const context2 = utils.context.deploySequenceContexts(signer2)
expect(await context1).to.deep.equal(await context2)
contexts = await context1
defaultArgs = {
contexts,
networks,
tracker
}
defaultTx = {
to: await signer1.getAddress(),
value: 1
}
})
describe('New account', () => {
it('Should create a new account', async () => {
const signer = randomWallet('Should create a new account')
const config = {
threshold: 1,
checkpoint: Math.floor(now() / 1000),
signers: [{ address: signer.address, weight: 1 }]
}
const account = await Account.new({
...defaultArgs,
config,
orchestrator: new Orchestrator([signer])
})
expect(account).to.be.instanceOf(Account)
expect(account.address).to.not.be.undefined
await getEth(account.address)
const tx = await account.sendTransaction([defaultTx], networks[0].chainId)
expect(tx).to.not.be.undefined
const status = await account.status(networks[0].chainId)
expect(status.fullyMigrated).to.be.true
expect(status.onChain.deployed).to.be.true
expect(status.onChain.version).to.equal(2)
})
it('Should create new nested accounts', async () => {
const { accountInner, accountOuter } = await createNestedAccount('create new nested accounts', false, false)
await getEth(accountOuter.address)
await accountOuter.sendTransaction([defaultTx], networks[0].chainId)
const statusOuter = await accountOuter.status(networks[0].chainId)
expect(statusOuter.fullyMigrated).to.be.true
expect(statusOuter.onChain.deployed).to.be.true
expect(statusOuter.onChain.version).to.equal(2)
const statusInner = await accountInner.status(networks[0].chainId)
expect(statusInner.fullyMigrated).to.be.true
expect(statusInner.onChain.deployed).to.be.true
expect(statusInner.onChain.version).to.equal(2)
})
it('Should send tx on nested accounts', async () => {
const { accountInner, accountOuter } = await createNestedAccount('sent tx on nested accounts', true, true)
await getEth(accountOuter.address)
await accountOuter.sendTransaction([defaultTx], networks[0].chainId)
const statusOuter = await accountOuter.status(networks[0].chainId)
expect(statusOuter.fullyMigrated).to.be.true
expect(statusOuter.onChain.deployed).to.be.true
expect(statusOuter.onChain.version).to.equal(2)
const statusInner = await accountInner.status(networks[0].chainId)
expect(statusInner.fullyMigrated).to.be.true
expect(statusInner.onChain.deployed).to.be.true
expect(statusInner.onChain.version).to.equal(2)
})
it('Should send transactions on multiple networks', async () => {
const signer = randomWallet('Should send transactions on multiple networks')
const config = {
threshold: 1,
checkpoint: Math.floor(now() / 1000),
signers: [{ address: signer.address, weight: 1 }]
}
const account = await Account.new({
...defaultArgs,
config,
orchestrator: new Orchestrator([signer])
})
await getEth(account.address)
await account.sendTransaction([defaultTx], networks[0].chainId)
await account.sendTransaction([defaultTx], networks[1].chainId)
const status1 = await account.status(networks[0].chainId)
const status2 = await account.status(networks[1].chainId)
expect(status1.fullyMigrated).to.be.true
expect(status1.onChain.deployed).to.be.true
expect(status1.onChain.version).to.equal(2)
expect(status2.fullyMigrated).to.be.true
expect(status2.onChain.deployed).to.be.true
expect(status2.onChain.version).to.equal(2)
})
it('Should create a new account with many signers', async () => {
const signers = new Array(24).fill(0).map(() => randomWallet('Should create a new account with many signers'))
const config = {
threshold: 3,
checkpoint: Math.floor(now() / 1000),
signers: signers.map(signer => ({
address: signer.address,
weight: 1
}))
}
const rsigners = signers.sort(() => randomFraction('Should create a new account with many signers 2') - 0.5)
const account = await Account.new({
...defaultArgs,
config,
orchestrator: new Orchestrator(rsigners.slice(0, 4))
})
await getEth(account.address)
await account.sendTransaction([defaultTx], networks[0].chainId)
const status = await account.status(networks[0].chainId)
expect(status.fullyMigrated).to.be.true
expect(status.onChain.deployed).to.be.true
expect(status.onChain.version).to.equal(2)
})
it('Should sign and validate a message', async () => {
const signer = randomWallet('Should sign and validate a message')
const config = {
threshold: 1,
checkpoint: Math.floor(now() / 1000),
signers: [{ address: signer.address, weight: 1 }]
}
const account = await Account.new({
...defaultArgs,
config,
orchestrator: new Orchestrator([signer])
})
await account.doBootstrap(networks[0].chainId)
const msg = ethers.utils.toUtf8Bytes('Hello World')
const sig = await account.signMessage(msg, networks[0].chainId)
const valid = await commons.EIP1271.isValidEIP1271Signature(
account.address,
ethers.utils.keccak256(msg),
sig,
networks[0].provider!
)
expect(valid).to.be.true
})
it('Should sign and validate a message with nested account', async () => {
const { accountOuter } = await createNestedAccount('sign and validate nested')
const msg = ethers.utils.toUtf8Bytes('Hello World')
const sig = await accountOuter.signMessage(msg, networks[0].chainId)
const valid = await commons.EIP1271.isValidEIP1271Signature(
accountOuter.address,
ethers.utils.keccak256(msg),
sig,
networks[0].provider!
)
expect(valid).to.be.true
})
it('Should update account to new configuration', async () => {
const signer = randomWallet('Should update account to new configuration')
const simpleConfig1 = {
threshold: 1,
checkpoint: Math.floor(now() / 1000),
signers: [{ address: signer.address, weight: 1 }]
}
const config1 = v2.config.ConfigCoder.fromSimple(simpleConfig1)
const account = await Account.new({
...defaultArgs,
config: simpleConfig1,
orchestrator: new Orchestrator([signer])
})
const signer2a = randomWallet('Should update account to new configuration 2')
const signer2b = randomWallet('Should update account to new configuration 3')
const simpleConfig2 = {
threshold: 4,
checkpoint: Math.floor(now() / 1000) + 1,
signers: [
{
address: signer2a.address,
weight: 2
},
{
address: signer2b.address,
weight: 2
}
]
}
const config2 = v2.config.ConfigCoder.fromSimple(simpleConfig2)
await account.updateConfig(config2)
const status2 = await account.status(networks[0].chainId)
expect(status2.fullyMigrated).to.be.true
expect(status2.onChain.deployed).to.be.false
expect(status2.onChain.version).to.equal(2)
expect(status2.onChain.imageHash).to.deep.equal(v2.config.ConfigCoder.imageHashOf(config1))
expect(status2.imageHash).to.deep.equal(v2.config.ConfigCoder.imageHashOf(config2))
})
it('Should sign and validate a message without being deployed', async () => {
const signer = randomWallet('Should sign and validate a message without being deployed')
const config = {
threshold: 1,
checkpoint: Math.floor(now() / 1000),
signers: [{ address: signer.address, weight: 1 }]
}
const account = await Account.new({
...defaultArgs,
config,
orchestrator: new Orchestrator([signer])
})
const msg = ethers.utils.toUtf8Bytes('Hello World')
const sig = await account.signMessage(msg, networks[0].chainId, 'eip6492')
const valid = await account.reader(networks[0].chainId).isValidSignature(account.address, ethers.utils.keccak256(msg), sig)
expect(valid).to.be.true
})
it('Should sign and validate a message without being deployed with nested account', async () => {
const { accountOuter } = await createNestedAccount('sign and validate nested undeployed', true, false)
const msg = ethers.utils.toUtf8Bytes('Hello World')
const sig = await accountOuter.signMessage(msg, networks[0].chainId, 'eip6492')
const valid = await accountOuter
.reader(networks[0].chainId)
.isValidSignature(accountOuter.address, ethers.utils.keccak256(msg), sig)
expect(valid).to.be.true
})
it('Should sign and validate a message with undeployed nested account and signer', async () => {
// Testing that an undeployed account doesn't error as other signer can satisfy threshold
const signerA = randomWallet('Nested account signer A')
const signerB = randomWallet('Nested account signer B')
const configInner = {
threshold: 1,
checkpoint: Math.floor(now() / 1000),
signers: [{ address: signerA.address, weight: 1 }]
}
const accountInner = await Account.new({
...defaultArgs,
config: configInner,
orchestrator: new Orchestrator([signerA])
}) // Undeployed
const configOuter = {
threshold: 1,
checkpoint: Math.floor(now() / 1000),
signers: [
{ address: accountInner.address, weight: 1 },
{ address: signerB.address, weight: 1 }
]
}
const accountOuter = await Account.new({
...defaultArgs,
config: configOuter,
orchestrator: new Orchestrator([new AccountOrchestratorWrapper(accountInner), signerB])
})
await accountOuter.doBootstrap(networks[0].chainId)
const msg = ethers.utils.toUtf8Bytes('Hello World')
const sig = await accountOuter.signMessage(msg, networks[0].chainId)
const valid = await accountOuter
.reader(networks[0].chainId)
.isValidSignature(accountOuter.address, ethers.utils.keccak256(msg), sig)
expect(valid).to.be.true
})
it('Should refuse to sign when not deployed', async () => {
const signer = randomWallet('Should refuse to sign when not deployed')
const config = {
threshold: 1,
checkpoint: Math.floor(now() / 1000),
signers: [{ address: signer.address, weight: 1 }]
}
const account = await Account.new({
...defaultArgs,
config,
orchestrator: new Orchestrator([signer])
})
const msg = ethers.utils.toUtf8Bytes('Hello World')
const sig = account.signMessage(msg, networks[0].chainId, 'throw')
expect(sig).to.be.rejected
})
it('Should refuse to sign when not deployed (nested)', async () => {
const { accountOuter } = await createNestedAccount('refuse to sign undeployed', false, false)
const msg = ethers.utils.toUtf8Bytes('Hello World')
const sig = accountOuter.signMessage(msg, networks[0].chainId, 'eip6492') // Note EIP-6492 throws when nested not deployed
expect(sig).to.be.rejected
})
describe('After upgrading', () => {
let account: Account
let signer1: ethers.Wallet
let signer2a: ethers.Wallet
let signer2b: ethers.Wallet
let signerIndex = 1
beforeEach(async () => {
signer1 = randomWallet(`After upgrading ${signerIndex++}`)
const simpleConfig1 = {
threshold: 1,
checkpoint: Math.floor(now() / 1000) + 1,
signers: [{ address: signer1.address, weight: 1 }]
}
account = await Account.new({
...defaultArgs,
config: simpleConfig1,
orchestrator: new Orchestrator([signer1])
})
await getEth(account.address)
signer2a = randomWallet(`After upgrading ${signerIndex++}`)
signer2b = randomWallet(`After upgrading ${signerIndex++}`)
const simpleConfig2 = {
threshold: 4,
checkpoint: await account.status(0).then(s => ethers.BigNumber.from(s.checkpoint).add(1)),
signers: [
{
address: signer2a.address,
weight: 2
},
{
address: signer2b.address,
weight: 2
}
]
}
const config2 = v2.config.ConfigCoder.fromSimple(simpleConfig2)
await account.updateConfig(config2)
account.setOrchestrator(new Orchestrator([signer2a, signer2b]))
})
it('Should send a transaction', async () => {
const tx = await account.sendTransaction([defaultTx], networks[0].chainId)
expect(tx).to.not.be.undefined
const status = await account.status(networks[0].chainId)
expect(status.fullyMigrated).to.be.true
expect(status.onChain.deployed).to.be.true
expect(status.onChain.imageHash).to.equal(status.imageHash)
})
it('Should send a transaction on nested account', async () => {
const configOuter = {
threshold: 1,
checkpoint: Math.floor(now() / 1000),
signers: [{ address: account.address, weight: 1 }]
}
const accountOuter = await Account.new({
...defaultArgs,
config: configOuter,
orchestrator: new Orchestrator([new AccountOrchestratorWrapper(account)])
})
await accountOuter.doBootstrap(networks[0].chainId)
const tx = await accountOuter.sendTransaction([], networks[0].chainId)
expect(tx).to.not.be.undefined
const statusOuter = await accountOuter.status(networks[0].chainId)
expect(statusOuter.fullyMigrated).to.be.true
expect(statusOuter.onChain.deployed).to.be.true
expect(statusOuter.onChain.imageHash).to.equal(statusOuter.imageHash)
const status = await account.status(networks[0].chainId)
expect(status.fullyMigrated).to.be.true
expect(status.onChain.deployed).to.be.true
expect(status.onChain.imageHash).to.equal(status.imageHash)
})
it('Should send a transaction on undeployed nested account', async () => {
const configOuter = {
threshold: 1,
checkpoint: Math.floor(now() / 1000),
signers: [{ address: account.address, weight: 1 }]
}
const accountOuter = await Account.new({
...defaultArgs,
config: configOuter,
orchestrator: new Orchestrator([new AccountOrchestratorWrapper(account)])
})
await getEth(accountOuter.address)
const tx = await accountOuter.sendTransaction([defaultTx], networks[0].chainId)
expect(tx).to.not.be.undefined
const status = await account.status(networks[0].chainId)
expect(status.fullyMigrated).to.be.true
expect(status.onChain.deployed).to.be.true
expect(status.onChain.imageHash).to.equal(status.imageHash)
})
it('Should sign a message', async () => {
const msg = ethers.utils.toUtf8Bytes('Hello World')
const sig = await account.signMessage(msg, networks[0].chainId)
const canOnchainValidate = await account.status(networks[0].chainId).then(s => s.canOnchainValidate)
expect(canOnchainValidate).to.be.false
await account.doBootstrap(networks[0].chainId)
const valid = await commons.EIP1271.isValidEIP1271Signature(
account.address,
ethers.utils.keccak256(msg),
sig,
networks[0].provider!
)
expect(valid).to.be.true
})
it('Should fail to use old signer', async () => {
account.setOrchestrator(new Orchestrator([signer1]))
const tx = account.sendTransaction([defaultTx], networks[0].chainId)
await expect(tx).to.be.rejected
})
it('Should send a transaction on a different network', async () => {
const tx = await account.sendTransaction([defaultTx], networks[1].chainId)
expect(tx).to.not.be.undefined
const status = await account.status(networks[1].chainId)
expect(status.fullyMigrated).to.be.true
expect(status.onChain.deployed).to.be.true
expect(status.onChain.imageHash).to.equal(status.imageHash)
})
describe('After reloading the account', () => {
beforeEach(async () => {
account = new Account({
...defaultArgs,
address: account.address,
orchestrator: new Orchestrator([signer2a, signer2b])
})
await getEth(account.address)
})
it('Should send a transaction', async () => {
const tx = await account.sendTransaction([defaultTx], networks[0].chainId)
expect(tx).to.not.be.undefined
const status = await account.status(networks[0].chainId)
expect(status.fullyMigrated).to.be.true
expect(status.onChain.deployed).to.be.true
expect(status.onChain.imageHash).to.equal(status.imageHash)
})
it('Should sign a message', async () => {
const msg = ethers.utils.toUtf8Bytes('Hello World')
const sig = await account.signMessage(msg, networks[0].chainId)
const canOnchainValidate = await account.status(networks[0].chainId).then(s => s.canOnchainValidate)
expect(canOnchainValidate).to.be.false
await account.doBootstrap(networks[0].chainId)
const valid = await commons.EIP1271.isValidEIP1271Signature(
account.address,
ethers.utils.keccak256(msg),
sig,
networks[0].provider!
)
expect(valid).to.be.true
})
})
describe('After updating the config again', () => {
let signer3a: ethers.Wallet
let signer3b: ethers.Wallet
let signer3c: ethers.Wallet
let signerIndex = 1
let config3: v2.config.WalletConfig
beforeEach(async () => {
signer3a = randomWallet(`After updating the config again ${signerIndex++}`)
signer3b = randomWallet(`After updating the config again ${signerIndex++}`)
signer3c = randomWallet(`After updating the config again ${signerIndex++}`)
const simpleConfig3 = {
threshold: 5,
checkpoint: await account.status(0).then(s => ethers.BigNumber.from(s.checkpoint).add(1)),
signers: [
{
address: signer3a.address,
weight: 2
},
{
address: signer3b.address,
weight: 2
},
{
address: signer3c.address,
weight: 1
}
]
}
config3 = v2.config.ConfigCoder.fromSimple(simpleConfig3)
await account.updateConfig(config3)
account.setOrchestrator(new Orchestrator([signer3a, signer3b, signer3c]))
})
it('Should update account status', async () => {
const status = await account.status(networks[0].chainId)
expect(status.fullyMigrated).to.be.true
expect(status.onChain.deployed).to.be.false
expect(status.imageHash).to.equal(v2.config.ConfigCoder.imageHashOf(config3))
expect(status.presignedConfigurations.length).to.equal(2)
})
it('Should send a transaction', async () => {
const tx = await account.sendTransaction([defaultTx], networks[0].chainId)
expect(tx).to.not.be.undefined
const status = await account.status(networks[0].chainId)
expect(status.fullyMigrated).to.be.true
expect(status.onChain.deployed).to.be.true
expect(status.onChain.imageHash).to.equal(status.imageHash)
})
it('Should sign a message', async () => {
const msg = ethers.utils.toUtf8Bytes('Hello World')
const sig = await account.signMessage(msg, networks[0].chainId)
const canOnchainValidate = await account.status(networks[0].chainId).then(s => s.canOnchainValidate)
expect(canOnchainValidate).to.be.false
await account.doBootstrap(networks[0].chainId)
const status = await account.status(networks[0].chainId)
expect(status.onChain.imageHash).to.not.equal(status.imageHash)
const valid = await commons.EIP1271.isValidEIP1271Signature(
account.address,
ethers.utils.keccak256(msg),
sig,
networks[0].provider!
)
expect(valid).to.be.true
})
})
describe('After sending a transaction', () => {
beforeEach(async () => {
await account.sendTransaction([defaultTx], networks[0].chainId)
})
it('Should send a transaction in a different network', async () => {
const tx = await account.sendTransaction([defaultTx], networks[1].chainId)
expect(tx).to.not.be.undefined
const status = await account.status(networks[1].chainId)
expect(status.fullyMigrated).to.be.true
expect(status.onChain.deployed).to.be.true
expect(status.onChain.imageHash).to.equal(status.imageHash)
})
it('Should send a second transaction', async () => {
const tx = await account.sendTransaction([defaultTx], networks[0].chainId)
expect(tx).to.not.be.undefined
})
let signerIndex = 1
it('Should update the configuration again', async () => {
const signer2a = randomWallet(`Should update the configuration again ${signerIndex++}`)
const signer2b = randomWallet(`Should update the configuration again ${signerIndex++}`)
const signer2c = randomWallet(`Should update the configuration again ${signerIndex++}`)
const simpleConfig2 = {
threshold: 6,
checkpoint: await account.status(0).then(s => ethers.BigNumber.from(s.checkpoint).add(1)),
signers: [
{
address: signer2a.address,
weight: 3
},
{
address: signer2b.address,
weight: 3
},
{
address: signer2c.address,
weight: 3
}
]
}
const ogOnchainImageHash = await account.status(0).then(s => s.onChain.imageHash)
const imageHash1 = await account.status(0).then(s => s.imageHash)
const config2 = v2.config.ConfigCoder.fromSimple(simpleConfig2)
await account.updateConfig(config2)
const status1 = await account.status(networks[0].chainId)
const status2 = await account.status(networks[1].chainId)
expect(status1.fullyMigrated).to.be.true
expect(status1.onChain.deployed).to.be.true
expect(status1.onChain.imageHash).to.equal(imageHash1)
expect(status1.imageHash).to.equal(v2.config.ConfigCoder.imageHashOf(config2))
expect(status1.presignedConfigurations.length).to.equal(1)
expect(status2.fullyMigrated).to.be.true
expect(status2.onChain.deployed).to.be.false
expect(status2.onChain.imageHash).to.equal(ogOnchainImageHash)
expect(status2.imageHash).to.equal(v2.config.ConfigCoder.imageHashOf(config2))
expect(status2.presignedConfigurations.length).to.equal(2)
})
})
})
})
describe('Migrated wallet', () => {
it('Should migrate undeployed account', async () => {
// Old account may be an address that's not even deployed
const signer1 = randomWallet('Should migrate undeployed account')
const simpleConfig = {
threshold: 1,
checkpoint: 0,
signers: [
{
address: signer1.address,
weight: 1
}
]
}
const config = v1.config.ConfigCoder.fromSimple(simpleConfig)
const configv2 = v2.config.ConfigCoder.fromSimple(simpleConfig)
const imageHash = v1.config.ConfigCoder.imageHashOf(config)
const address = commons.context.addressOf(contexts[1], imageHash)
// Sessions server MUST have information about the old wallet
// in production this is retrieved from SequenceUtils contract
await tracker.saveCounterfactualWallet({ config, context: [contexts[1]] })
// Importing the account should work!
const account = new Account({ ...defaultArgs, address, orchestrator: new Orchestrator([signer1]) })
const status = await account.status(0)
expect(status.fullyMigrated).to.be.false
expect(status.onChain.deployed).to.be.false
expect(status.onChain.imageHash).to.equal(imageHash)
expect(status.imageHash).to.equal(imageHash)
expect(status.version).to.equal(1)
// Sending a transaction should fail (not fully migrated)
await getEth(account.address)
await expect(account.sendTransaction([defaultTx], networks[0].chainId)).to.be.rejected
// Should sign migration using the account
await account.signAllMigrations(c => c)
const status2 = await account.status(networks[0].chainId)
expect(status2.fullyMigrated).to.be.true
expect(status2.onChain.deployed).to.be.false
expect(status2.onChain.imageHash).to.equal(imageHash)
expect(status2.onChain.version).to.equal(1)
expect(status2.imageHash).to.equal(v2.config.ConfigCoder.imageHashOf(configv2))
expect(status2.version).to.equal(2)
// Send a transaction
const tx = await account.sendTransaction([defaultTx], networks[0].chainId)
expect(tx).to.not.be.undefined
const status3 = await account.status(networks[0].chainId)
expect(status3.fullyMigrated).to.be.true
expect(status3.onChain.deployed).to.be.true
expect(status3.onChain.imageHash).to.equal(v2.config.ConfigCoder.imageHashOf(configv2))
expect(status3.onChain.version).to.equal(2)
expect(status3.imageHash).to.equal(v2.config.ConfigCoder.imageHashOf(configv2))
expect(status3.version).to.equal(2)
// Send another transaction on another chain
const tx2 = await account.sendTransaction([defaultTx], networks[1].chainId)
expect(tx2).to.not.be.undefined
const status4 = await account.status(networks[1].chainId)
expect(status4.fullyMigrated).to.be.true
expect(status4.onChain.deployed).to.be.true
expect(status4.onChain.imageHash).to.equal(v2.config.ConfigCoder.imageHashOf(configv2))
expect(status4.onChain.version).to.equal(2)
expect(status4.imageHash).to.equal(v2.config.ConfigCoder.imageHashOf(configv2))
expect(status4.version).to.equal(2)
})
it('Should migrate a half-deployed account', async () => {
// Old account created with 3 signers, and already deployed
// in one of the chains
const signer1 = randomWallet('Should migrate a half-deployed account')
const signer2 = randomWallet('Should migrate a half-deployed account 2')
const signer3 = randomWallet('Should migrate a half-deployed account 3')
const simpleConfig = {
threshold: 2,
checkpoint: 0,
signers: [
{
address: signer1.address,
weight: 1
},
{
address: signer2.address,
weight: 1
},
{
address: signer3.address,
weight: 1
}
]
}
const config = v1.config.ConfigCoder.fromSimple(simpleConfig)
const imageHash = v1.config.ConfigCoder.imageHashOf(config)
const address = commons.context.addressOf(contexts[1], imageHash)
// Deploy the wallet on network 0
const deployTx = Wallet.buildDeployTransaction(contexts[1], imageHash)
await (networks[0].relayer! as Relayer).relay({
...deployTx,
chainId: networks[0].chainId,
intent: {
id: '0x00',
wallet: address
}
})
// Feed all information to sequence-sessions
// (on prod this would be imported from SequenceUtils)
await tracker.saveCounterfactualWallet({ config, context: Object.values(contexts) })
// Importing the account should work!
const account = new Account({
...defaultArgs,
address,
orchestrator: new Orchestrator([signer1, signer3])
})
// Status on network 0 should be deployed, network 1 not
// both should not be migrated, and use the original imageHash
const status1 = await account.status(networks[0].chainId)
expect(status1.fullyMigrated).to.be.false
expect(status1.onChain.deployed).to.be.true
expect(status1.onChain.imageHash).to.equal(imageHash)
expect(status1.onChain.version).to.equal(1)
expect(status1.imageHash).to.equal(imageHash)
expect(status1.version).to.equal(1)
const status2 = await account.status(networks[1].chainId)
expect(status2.fullyMigrated).to.be.false
expect(status2.onChain.deployed).to.be.false
expect(status2.onChain.imageHash).to.equal(imageHash)
expect(status2.onChain.version).to.equal(1)
expect(status2.imageHash).to.equal(imageHash)
expect(status2.version).to.equal(1)
// Signing transactions (on both networks) and signing messages should fail
await getEth(account.address)
await expect(account.sendTransaction([defaultTx], networks[0].chainId)).to.be.rejected
await expect(account.sendTransaction([defaultTx], networks[1].chainId)).to.be.rejected
await expect(account.signMessage('0x00', networks[0].chainId)).to.be.rejected
await expect(account.signMessage('0x00', networks[1].chainId)).to.be.rejected
await account.signAllMigrations(c => c)
// Sign a transaction on network 0 and network 1, both should work
// and should take the wallet on-chain up to speed
const configv2 = v2.config.ConfigCoder.fromSimple(simpleConfig)
const tx1 = await account.sendTransaction([defaultTx], networks[0].chainId)
expect(tx1).to.not.be.undefined
const status1b = await account.status(networks[0].chainId)
expect(status1b.fullyMigrated).to.be.true
expect(status1b.onChain.deployed).to.be.true
expect(status1b.onChain.imageHash).to.equal(v2.config.ConfigCoder.imageHashOf(configv2))
expect(status1b.onChain.version).to.equal(2)
expect(status1b.imageHash).to.equal(v2.config.ConfigCoder.imageHashOf(configv2))
expect(status1b.version).to.equal(2)
const tx2 = await account.sendTransaction([defaultTx], networks[1].chainId)
expect(tx2).to.not.be.undefined
const status2b = await account.status(networks[1].chainId)
expect(status2b).to.be.deep.equal(status1b)
})
it('Should migrate an upgraded wallet', async () => {
const signer1 = randomWallet('Should migrate an upgraded wallet')
const signer2 = randomWallet('Should migrate an upgraded wallet 2')
const signer3 = randomWallet('Should migrate an upgraded wallet 3')
const signer4 = randomWallet('Should migrate an upgraded wallet 4')
const simpleConfig1a = {
threshold: 3,
checkpoint: 0,
signers: [
{
address: signer1.address,
weight: 2
},
{
address: signer2.address,
weight: 2
},
{
address: signer3.address,
weight: 2
}
]
}
const config1a = v1.config.ConfigCoder.fromSimple(simpleConfig1a)
const imageHash1a = v1.config.ConfigCoder.imageHashOf(config1a)
const address = commons.context.addressOf(contexts[1], imageHash1a)
const simpleConfig1b = {
threshold: 3,
checkpoint: 0,
signers: [
{
address: signer1.address,
weight: 2
},