-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathwallet.go
More file actions
1117 lines (952 loc) · 32.4 KB
/
wallet.go
File metadata and controls
1117 lines (952 loc) · 32.4 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
package sequence
import (
"context"
"fmt"
"math/big"
"github.com/0xsequence/ethkit/ethcoder"
"github.com/0xsequence/ethkit/ethrpc"
"github.com/0xsequence/ethkit/ethtxn"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/ethkit/go-ethereum/core/types"
"github.com/0xsequence/go-sequence/core"
v1 "github.com/0xsequence/go-sequence/core/v1"
v2 "github.com/0xsequence/go-sequence/core/v2"
v3 "github.com/0xsequence/go-sequence/core/v3"
"github.com/0xsequence/go-sequence/services/keymachine"
)
type WalletOptions[C core.WalletConfig] struct {
// Config is the wallet multi-sig configuration. Note: the first config of any wallet
// before it is deployed is used to derive it's the account address of the wallet.
Config C
// Context is the WalletContext of deployed wallet-contract modules for the Smart Wallet.
// NOTE: if a WalletContext is not provided, then `V1SequenceContext()` value is used.
Context *WalletContext
// Skips config sorting and keeps signers order as-is
SkipSortSigners bool
// Address used for the wallet
// if this value is defined, the address derived from the sequence config is ignored
Address common.Address
}
func GenericNewWallet[C core.WalletConfig](walletOptions WalletOptions[C], signers ...Signer) (*Wallet[C], error) {
seqContext := SequenceContextForWalletConfig(walletOptions.Config)
if walletOptions.Context != nil {
seqContext = *walletOptions.Context
}
// Check if wallet config is usable
if err := walletOptions.Config.IsUsable(); err != nil {
return nil, fmt.Errorf("sequence.GenericNewWallet: %w", err)
}
// Generate address
address := walletOptions.Address
if address == (common.Address{}) {
var err error
address, err = AddressFromWalletConfig(walletOptions.Config, seqContext)
if err != nil {
return nil, fmt.Errorf("sequence.GenericNewWallet: %w", err)
}
}
// Check signers
for _, signer := range signers {
_, canSignMessage := signer.(MessageSigner)
_, canSignDigest := signer.(DigestSigner)
if !canSignMessage && !canSignDigest {
return nil, fmt.Errorf("sequence.Wallet#UseSigners: signer is not a valid signer")
}
}
w := &Wallet[C]{
config: walletOptions.Config,
context: seqContext,
address: address,
estimator: NewEstimator(),
skipSortSigners: walletOptions.SkipSortSigners,
}
w.signers = signers
return w, nil
}
func V1NewWallet(walletOptions WalletOptions[*v1.WalletConfig], signers ...Signer) (*Wallet[*v1.WalletConfig], error) {
return GenericNewWallet(walletOptions, signers...)
}
func V2NewWallet(walletOptions WalletOptions[*v2.WalletConfig], signers ...Signer) (*Wallet[*v2.WalletConfig], error) {
return GenericNewWallet(walletOptions, signers...)
}
func V3NewWallet(walletOptions WalletOptions[*v3.WalletConfig], signers ...Signer) (*Wallet[*v3.WalletConfig], error) {
return GenericNewWallet(walletOptions, signers...)
}
func NewWallet(walletOptions WalletOptions[*v2.WalletConfig], signers ...Signer) (*Wallet[*v2.WalletConfig], error) {
return V2NewWallet(walletOptions, signers...)
}
func GenericNewWalletSingleOwner[C core.WalletConfig](owner Signer, optContext ...WalletContext) (*Wallet[C], error) {
var typeOfWallet C
seqContext := SequenceContextForWalletConfig(typeOfWallet)
if len(optContext) > 0 {
seqContext = optContext[0]
}
_, canSignMessage := owner.(MessageSigner)
_, canSignDigest := owner.(DigestSigner)
if !canSignMessage && !canSignDigest {
return nil, fmt.Errorf("sequence.Wallet#UseSigners: %s is not a valid signer, canSignMessage: %v, canSignDigest: %v", owner.Address(), canSignMessage, canSignDigest)
}
if _, ok := core.WalletConfig(typeOfWallet).(*v1.WalletConfig); ok {
// new wallet config v1
var config core.WalletConfig = &v1.WalletConfig{
Threshold_: 1, // big.NewInt(1),
Signers_: v1.WalletConfigSigners{
{Weight: 1, Address: owner.Address()},
},
}
// new sequence v1 wallet
return GenericNewWallet(WalletOptions[C]{
Config: config.(C),
Context: &seqContext,
}, owner)
} else if _, ok := core.WalletConfig(typeOfWallet).(*v2.WalletConfig); ok {
// new wallet config v2
var config core.WalletConfig = &v2.WalletConfig{
Threshold_: 1, // big.NewInt(1),
Tree: &v2.WalletConfigTreeAddressLeaf{
Weight: 1, Address: owner.Address(),
},
}
// new sequence v2 wallet
return GenericNewWallet(WalletOptions[C]{
Config: config.(C),
Context: &seqContext,
}, owner)
} else if _, ok := core.WalletConfig(typeOfWallet).(*v3.WalletConfig); ok {
// new wallet config v3
var config core.WalletConfig = &v3.WalletConfig{
Threshold_: 1, // big.NewInt(1),
Tree: &v3.WalletConfigTreeAddressLeaf{
Weight: 1, Address: owner.Address(),
},
}
// new sequence v3 wallet
return GenericNewWallet(WalletOptions[C]{
Config: config.(C),
Context: &seqContext,
}, owner)
} else {
return nil, fmt.Errorf("sequence.GenericNewWalletSingleOwner: unsupported wallet config type")
}
}
// V1NewWalletSingleOwner creates a new Sequence v1 wallet with a single owner.
//
// Deprecated: use NewWalletSingleOwner instead. V1NewWalletSingleOwner is kept for historical
// compatibility and should not be used.
func V1NewWalletSingleOwner(owner Signer, optContext ...WalletContext) (*Wallet[*v1.WalletConfig], error) {
return GenericNewWalletSingleOwner[*v1.WalletConfig](owner, optContext...)
}
// V2NewWalletSingleOwner creates a new Sequence v2 wallet with a single owner.
func V2NewWalletSingleOwner(owner Signer, optContext ...WalletContext) (*Wallet[*v2.WalletConfig], error) {
return GenericNewWalletSingleOwner[*v2.WalletConfig](owner, optContext...)
}
// V3NewWalletSingleOwner creates a new Sequence v3 wallet with a single owner.
func V3NewWalletSingleOwner(owner Signer, optContext ...WalletContext) (*Wallet[*v3.WalletConfig], error) {
return GenericNewWalletSingleOwner[*v3.WalletConfig](owner, optContext...)
}
// NewWalletSingleOwner creates a new Sequence v2 wallet with a single owner.
func NewWalletSingleOwner(owner Signer, optContext ...WalletContext) (*Wallet[*v2.WalletConfig], error) {
return V2NewWalletSingleOwner(owner, optContext...)
}
func GenericNewWalletWithCoreWalletConfig[C core.WalletConfig](wallet *Wallet[C]) *Wallet[core.WalletConfig] {
return &Wallet[core.WalletConfig]{
context: wallet.context,
config: wallet.config,
signers: wallet.signers,
provider: wallet.provider,
relayer: wallet.relayer,
address: wallet.address,
skipSortSigners: wallet.skipSortSigners,
chainID: wallet.chainID,
}
}
func V1NewWalletWithCoreWalletConfig(wallet *Wallet[*v1.WalletConfig]) *Wallet[core.WalletConfig] {
return &Wallet[core.WalletConfig]{
context: wallet.context,
config: wallet.config,
signers: wallet.signers,
provider: wallet.provider,
relayer: wallet.relayer,
address: wallet.address,
skipSortSigners: wallet.skipSortSigners,
chainID: wallet.chainID,
}
}
func V2NewWalletWithCoreWalletConfig(wallet *Wallet[*v2.WalletConfig]) *Wallet[core.WalletConfig] {
return &Wallet[core.WalletConfig]{
context: wallet.context,
config: wallet.config,
signers: wallet.signers,
provider: wallet.provider,
relayer: wallet.relayer,
address: wallet.address,
skipSortSigners: wallet.skipSortSigners,
chainID: wallet.chainID,
}
}
func V3NewWalletWithCoreWalletConfig(wallet *Wallet[*v3.WalletConfig]) *Wallet[core.WalletConfig] {
return &Wallet[core.WalletConfig]{
context: wallet.context,
config: wallet.config,
signers: wallet.signers,
provider: wallet.provider,
relayer: wallet.relayer,
address: wallet.address,
skipSortSigners: wallet.skipSortSigners,
chainID: wallet.chainID,
}
}
func NewWalletWithCoreWalletConfig(wallet *Wallet[*v2.WalletConfig]) *Wallet[core.WalletConfig] {
return V2NewWalletWithCoreWalletConfig(wallet)
}
type Wallet[C core.WalletConfig] struct {
context WalletContext
config C
signers []Signer
provider *ethrpc.Provider
estimator *Estimator
relayer Relayer
sessions keymachine.Sessions
address common.Address
skipSortSigners bool
chainID *big.Int
}
var (
ErrUnknownChainID = fmt.Errorf("chainID is unknown")
ErrProviderNotSet = fmt.Errorf("provider is not set")
ErrRelayerNotSet = fmt.Errorf("relayer is not set")
)
func (w *Wallet[C]) UseConfig(config C) (*Wallet[C], error) {
ww, err := GenericNewWallet(WalletOptions[C]{
Config: config,
Context: &w.context,
SkipSortSigners: w.skipSortSigners,
Address: w.address,
})
if err != nil {
return nil, fmt.Errorf("sequence.Wallet#UseConfig: %w", err)
}
if w.provider != nil {
err = ww.SetProvider(w.provider)
if err != nil {
return nil, fmt.Errorf("sequence.Wallet#UseConfig setProvider: %w", err)
}
}
if w.relayer != nil {
err = ww.SetRelayer(w.relayer)
if err != nil {
return nil, fmt.Errorf("sequence.Wallet#UseConfig setRelayer: %w", err)
}
}
return ww, nil
}
func (w *Wallet[C]) UseSigners(signers ...Signer) (*Wallet[C], error) {
ww, err := GenericNewWallet(WalletOptions[C]{
Config: w.config,
Context: &w.context,
SkipSortSigners: w.skipSortSigners,
Address: w.address,
})
if err != nil {
return nil, fmt.Errorf("sequence.Wallet#UseSigners: %w", err)
}
if w.provider != nil {
err = ww.SetProvider(w.provider)
if err != nil {
return nil, fmt.Errorf("sequence.Wallet#UseSigners connect: %w", err)
}
}
if w.relayer != nil {
err = ww.SetRelayer(w.relayer)
if err != nil {
return nil, fmt.Errorf("sequence.Wallet#UseSigners connect: %w", err)
}
}
for _, signer := range signers {
_, canSignMessage := signer.(MessageSigner)
_, canSignDigest := signer.(DigestSigner)
if !canSignMessage && !canSignDigest {
return nil, fmt.Errorf("sequence.Wallet#UseSigners: signer is not a valid signer")
}
}
ww.signers = signers
return ww, nil
}
func (w *Wallet[C]) Connect(provider *ethrpc.Provider, relayer Relayer) error {
err := w.SetProvider(provider)
if err != nil {
return err
}
err = w.SetRelayer(relayer)
if err != nil {
return err
}
return nil
}
func (w *Wallet[C]) SetProvider(provider *ethrpc.Provider) error {
chainID, err := provider.ChainID(context.Background())
if err != nil {
return fmt.Errorf("sequence.Wallet#SetProvider: %w", err)
}
w.chainID = chainID
w.provider = provider
return nil
}
func (w *Wallet[C]) SetRelayer(relayer Relayer) error {
w.relayer = relayer
return nil
}
func (w *Wallet[C]) SetSessions(sessions keymachine.Sessions) error {
w.sessions = sessions
return nil
}
func (w *Wallet[C]) UpdateSessionsWallet(ctx context.Context) error {
if w.sessions == nil {
return fmt.Errorf("sequence.Wallet#UpdateSessions: sessions are not set")
}
var version int
if _, ok := core.WalletConfig(w.config).(*v1.WalletConfig); ok {
version = 1
} else if _, ok := core.WalletConfig(w.config).(*v2.WalletConfig); ok {
version = 2
} else if _, ok := core.WalletConfig(w.config).(*v3.WalletConfig); ok {
version = 3
} else {
return fmt.Errorf("sequence.Wallet#UpdateSessions: unknown wallet config version")
}
err := w.sessions.SaveWallet(ctx, version, w.config)
if err != nil {
return fmt.Errorf("sequence.Wallet#UpdateSessions: %w", err)
}
return nil
}
func (w *Wallet[C]) UpdateSessionsConfig(ctx context.Context) error {
if w.sessions == nil {
return fmt.Errorf("sequence.Wallet#UpdateSessions: sessions are not set")
}
var version int
if _, ok := core.WalletConfig(w.config).(*v1.WalletConfig); ok {
version = 1
} else if _, ok := core.WalletConfig(w.config).(*v2.WalletConfig); ok {
version = 2
} else if _, ok := core.WalletConfig(w.config).(*v3.WalletConfig); ok {
version = 3
} else {
return fmt.Errorf("sequence.Wallet#UpdateSessions: unknown wallet config version")
}
err := w.sessions.SaveConfig(ctx, version, w.config)
if err != nil {
return fmt.Errorf("sequence.Wallet#UpdateSessions: %w", err)
}
return nil
}
// SetChainID will set the wallet's associated chainID. However, for most part, this will automatically
// be set by the provider rpc.
func (w *Wallet[C]) SetChainID(chainID *big.Int) {
w.chainID = chainID
}
func (w *Wallet[C]) GetProvider() *ethrpc.Provider {
return w.provider
}
func (w *Wallet[C]) GetRelayer() Relayer {
return w.relayer
}
func (w *Wallet[C]) GetChainID() *big.Int {
return w.chainID
}
func (w *Wallet[C]) GetWalletContext() WalletContext {
return w.context
}
func (w *Wallet[C]) GetWalletConfig() C {
return w.config
}
func (w *Wallet[C]) Address() common.Address {
return w.address
}
func (w *Wallet[C]) ImageHash() (common.Hash, error) {
return w.config.ImageHash().Hash, nil
}
func (w *Wallet[C]) GetSignerAddresses() []common.Address {
as := []common.Address{}
for _, s := range w.signers {
as = append(as, s.Address())
}
return as
}
func (w *Wallet[C]) GetSigner(signer core.Signer) Signer {
for _, signer_ := range w.signers {
if signer_.Address() == signer.Address {
return signer_
}
}
return nil
}
func (w *Wallet[C]) GetSignerWeight() *big.Int {
signers := make(map[core.Signer]uint16, len(w.signers))
for _, signer := range w.signers {
signers[core.Signer{Address: signer.Address()}] = 0
}
return big.NewInt(0).SetUint64(uint64(w.config.SignersWeight(signers)))
}
func (w *Wallet[C]) GetNonce(optBlockNum ...*big.Int) (*big.Int, error) {
if w.relayer == nil {
return nil, ErrRelayerNotSet
}
var blockNum *big.Int
if len(optBlockNum) > 0 {
blockNum = optBlockNum[0]
}
return w.relayer.GetNonce(context.Background(), w.config, w.context, nil, blockNum)
}
func (w *Wallet[C]) GetTransactionCount(optBlockNum ...*big.Int) (*big.Int, error) {
return w.GetNonce(optBlockNum...)
}
func (w *Wallet[C]) SignMessage(msg []byte) ([]byte, error) {
return w.SignDigest(context.Background(), MessageDigest(msg))
}
func (w *Wallet[C]) SignTypedData(typedData *ethcoder.TypedData) ([]byte, []byte, error) {
digest, encodedTypedData, err := typedData.Encode()
if err != nil {
return nil, nil, err
}
signature, err := w.SignDigest(context.Background(), common.Hash(digest))
if err != nil {
return nil, nil, err
}
return signature, encodedTypedData, nil
}
var (
_ MessageSigner = (*Wallet[*v1.WalletConfig])(nil)
_ MessageSigner = (*Wallet[*v2.WalletConfig])(nil)
_ MessageSigner = (*Wallet[*v3.WalletConfig])(nil)
)
func (w *Wallet[C]) SignDigest(ctx context.Context, digest common.Hash, optChainID ...*big.Int) ([]byte, error) {
if w.sessions != nil {
err := w.UpdateSessionsConfig(ctx)
if err != nil {
return nil, fmt.Errorf("sequence.Wallet#SignDigest: %w", err)
}
}
if (optChainID == nil && len(optChainID) == 0) && w.chainID == nil {
return nil, fmt.Errorf("sequence.Wallet#SignDigest: %w", ErrUnknownChainID)
}
var chainID *big.Int
if len(optChainID) > 0 {
chainID = optChainID[0]
} else {
chainID = w.chainID
}
subDigest, err := SubDigest(chainID, w.Address(), digest)
if err != nil {
return nil, fmt.Errorf("SignDigest, subDigestOf: %w", err)
}
sign := func(ctx context.Context, signer core.Signer, signatures []core.SignerSignature) (core.SignerSignatureType, []byte, error) {
signer_ := w.GetSigner(signer)
if signer_ == nil {
// signer isn't available, just include the config value of address
// without it's signature
return 0, nil, core.ErrSigningNoSigner
}
switch signer__ := signer_.(type) {
// sequence.Wallet / Signing Service / Guard
case DigestSigner:
sigValue, err := signer__.SignDigest(ctx, common.BytesToHash(subDigest), chainID)
if err != nil {
return 0, nil, fmt.Errorf("signer.SignDigest subDigest: %w", err)
}
// Sequence Wallet SignDigest returns a signature without a type
_, pc1 := signer_.(*Wallet[*v1.WalletConfig])
_, pc2 := signer_.(*Wallet[*v2.WalletConfig])
if pc1 || pc2 {
return core.SignerSignatureTypeEIP1271, sigValue, nil
}
return core.SignerSignatureType(sigValue[len(sigValue)-1]), sigValue[:len(sigValue)-1], nil
// Ethereum Wallet Signer
case MessageSigner:
sigValue, err := signer__.SignMessage(subDigest)
if err != nil {
return 0, nil, fmt.Errorf("signer.SignMessage subDigest: %w", err)
}
return core.SignerSignatureTypeEthSign, sigValue, nil
default:
return 0, nil, fmt.Errorf("signer %T is not supported", signer_)
}
}
res, _, err := w.buildSignature(ctx, sign, chainID)
return res, err
}
func (w *Wallet[C]) SignV3Payload(ctx context.Context, payload core.Payload, optChainID ...*big.Int) ([]byte, core.Signature[C], error) {
if (optChainID == nil && len(optChainID) == 0) && w.chainID == nil {
return nil, nil, fmt.Errorf("sequence.Wallet#SignDigest: %w", ErrUnknownChainID)
}
var chainID *big.Int
if len(optChainID) > 0 {
chainID = optChainID[0]
} else {
chainID = w.chainID
}
opHash := payload.Digest()
sign := func(ctx context.Context, signer core.Signer, signatures []core.SignerSignature) (core.SignerSignatureType, []byte, error) {
signer_ := w.GetSigner(signer)
if signer_ == nil {
// signer isn't available, just include the config value of address
// without it's signature
return 0, nil, core.ErrSigningNoSigner
}
switch signer__ := signer_.(type) {
// sequence.Wallet / Signing Service / Guard
case DigestSigner:
sigValue, err := signer__.SignDigest(ctx, opHash.Hash, chainID)
if err != nil {
return 0, nil, fmt.Errorf("signer.SignDigest subDigest: %w", err)
}
// Sequence Wallet SignDigest returns a signature without a type
_, pc3 := signer_.(*Wallet[*v3.WalletConfig])
if pc3 {
return core.SignerSignatureTypeEIP1271, sigValue, nil
}
return core.SignerSignatureType(sigValue[len(sigValue)-1]), sigValue[:len(sigValue)-1], nil
// Ethereum Wallet Signer
case MessageSigner:
sigValue, err := signer__.SignMessage(opHash.Bytes())
if err != nil {
return 0, nil, fmt.Errorf("signer.SignMessage subDigest: %w", err)
}
return core.SignerSignatureTypeEthSign, sigValue, nil
default:
return 0, nil, fmt.Errorf("signer %T is not supported", signer_)
}
}
res, sig, err := w.buildSignature(ctx, sign, chainID)
return res, sig, err
}
var (
_ DigestSigner = (*Wallet[*v1.WalletConfig])(nil)
_ DigestSigner = (*Wallet[*v2.WalletConfig])(nil)
)
func (w *Wallet[C]) SignTransaction(ctx context.Context, txn *Transaction) (*SignedTransactions, error) {
return w.SignTransactions(ctx, Transactions{txn})
}
func (w *Wallet[C]) SignTransactions(ctx context.Context, txns Transactions) (*SignedTransactions, error) {
if len(txns) == 0 {
return nil, fmt.Errorf("cannot sign an empty set of transactions")
}
var err error
// If a transaction has 0 gasLimit and not revertOnError
// compute all new gas limits
estimateGas := false
for _, txn := range txns {
if !txn.RevertOnError && (txn.GasLimit == nil || txn.GasLimit.Cmp(big.NewInt(0)) == 0) {
estimateGas = true
break
}
}
if estimateGas {
results, err := w.relayer.Simulate(ctx, w.address, txns)
if err != nil {
return nil, fmt.Errorf("unable to simulate for gas limits: %w", err)
}
if len(results) != len(txns) {
return nil, fmt.Errorf("simulate returned %d results for %d transactions", len(results), len(txns))
}
for i, transaction := range txns {
if transaction.GasLimit == nil {
transaction.GasLimit = new(big.Int).SetUint64(results[i].GasLimit)
}
}
}
// load nonce from transactions
nonce, err := txns.Nonce()
if err != nil {
return nil, fmt.Errorf("cannot load nonce from transactions: %w", err)
}
// if nonce is undefined
// load latest nonce from wallet
if nonce == nil {
nonce, err = w.GetNonce()
if err != nil {
return nil, err
}
}
switch core.WalletConfig(w.config).(type) {
case *v1.WalletConfig, *v2.WalletConfig:
bundle := Transaction{
Transactions: txns,
Nonce: nonce,
}
// Get transactions digest
digest, err := bundle.Digest()
if err != nil {
return nil, err
}
// Sign the transactions
sig, err := w.SignDigest(ctx, digest)
if err != nil {
return nil, err
}
return &SignedTransactions{
ChainID: w.chainID,
WalletAddress: w.address,
WalletConfig: w.config,
WalletContext: w.context,
Transactions: txns,
Nonce: nonce,
Digest: digest,
Signature: sig,
}, nil
case *v3.WalletConfig:
space, nonce := DecodeNonce(nonce)
payload, err := txns.Payload(w.address, w.chainID, space, nonce)
if err != nil {
return nil, err
}
digest := payload.Digest()
// Sign the transactions
sig, _, err := w.SignV3Payload(ctx, payload)
if err != nil {
return nil, err
}
return &SignedTransactions{
ChainID: w.chainID,
WalletAddress: w.address,
WalletConfig: w.config,
WalletContext: w.context,
Transactions: txns,
Space: space,
Nonce: nonce,
Digest: digest.Hash,
Signature: sig,
}, nil
}
return nil, fmt.Errorf("unknown wallet config type")
}
// GetSignedIntentTransactionWithIntentOperation creates an intent signature for a v3.CallsPayload.
func (w *Wallet[C]) GetSignedIntentTransactionWithIntentOperation(ctx context.Context, call *v3.CallsPayload) (*SignedTransactions, error) {
return w.GetSignedIntentPayload(ctx, call)
}
// GetSignedIntentTransactionsWithIntentOperations creates an intent signature for multiple v3.CallsPayload objects.
func (w *Wallet[C]) GetSignedIntentTransactionsWithIntentOperations(ctx context.Context, calls []*v3.CallsPayload) (*SignedTransactions, error) {
if len(calls) == 0 {
return nil, fmt.Errorf("cannot sign an empty set of payloads")
}
// For now, we only support a single payload in the batch
if len(calls) > 1 {
return nil, fmt.Errorf("multiple payloads not supported yet, please use CreateIntentDigestTree for multiple payloads")
}
return w.GetSignedIntentPayload(ctx, calls[0])
}
// GetSignedIntentPayload is the core implementation for creating intent signatures.
func (w *Wallet[C]) GetSignedIntentPayload(ctx context.Context, payload *v3.CallsPayload) (*SignedTransactions, error) {
// Sign the payload
sig, _, err := w.SignV3Payload(ctx, payload)
if err != nil {
return nil, err
}
// Convert payload back to Transactions for compatibility with SignedTransactions
txns := make(Transactions, len(payload.Calls))
for i, call := range payload.Calls {
txns[i] = &Transaction{
To: call.To,
Value: call.Value,
Data: call.Data,
GasLimit: call.GasLimit,
DelegateCall: call.DelegateCall,
RevertOnError: call.BehaviorOnError == v3.BehaviorOnErrorRevert,
Nonce: payload.Nonce,
}
}
// Return the signed transactions
return &SignedTransactions{
ChainID: w.chainID,
WalletAddress: w.address,
WalletConfig: w.config,
WalletContext: w.context,
Transactions: txns,
Space: payload.Space,
Nonce: payload.Nonce,
Digest: payload.Digest().Hash,
Signature: sig,
}, nil
}
func (w *Wallet[C]) SendTransaction(ctx context.Context, signedTxns *SignedTransactions, feeQuote ...*RelayerFeeQuote) (MetaTxnID, *types.Transaction, ethtxn.WaitReceipt, error) {
return w.SendTransactions(ctx, signedTxns, feeQuote...)
}
func (w *Wallet[C]) SendTransactions(ctx context.Context, signedTxns *SignedTransactions, feeQuote ...*RelayerFeeQuote) (MetaTxnID, *types.Transaction, ethtxn.WaitReceipt, error) {
if w.relayer == nil {
return "", nil, nil, ErrRelayerNotSet
}
return w.relayer.Relay(ctx, signedTxns, feeQuote...)
}
func (w *Wallet[C]) FeeOptions(ctx context.Context, txs Transactions) ([]*RelayerFeeOption, *RelayerFeeQuote, error) {
if w.relayer == nil {
return []*RelayerFeeOption{}, nil, ErrRelayerNotSet
}
// prepare for signed txs
nonce, err := txs.Nonce()
if err != nil {
return nil, nil, fmt.Errorf("cannot load nonce from transactions: %w", err)
}
if nonce == nil {
nonce, err = w.GetNonce()
if err != nil {
return nil, nil, err
}
}
bundle := Transaction{
Transactions: txs,
Nonce: nonce,
}
// get transactions digest
digest, err := bundle.Digest()
if err != nil {
return nil, nil, fmt.Errorf("cannot get digest from transactions: %w", err)
}
// prepare for fee estimation
areEOAs, err := w.estimator.AreEOAs(ctx, w.provider, w.config)
if err != nil {
return nil, nil, fmt.Errorf("estimator areEOAs error: %w", err)
}
willSign, err := w.estimator.PickSigners(ctx, w.config, areEOAs)
if err != nil {
return nil, nil, fmt.Errorf("estimator pickSigners error: %w", err)
}
sig := w.estimator.BuildStubSignature(w.config, willSign, areEOAs)
// signed txs
signedTxs := &SignedTransactions{
ChainID: w.chainID,
WalletAddress: w.address,
WalletConfig: w.config,
WalletContext: w.context,
Transactions: txs,
Nonce: nonce,
Digest: digest,
Signature: sig,
}
// get fee options
return w.relayer.FeeOptions(ctx, signedTxs)
}
func (w *Wallet[C]) IsDeployed() (bool, error) {
if w.provider == nil {
return false, ErrProviderNotSet
}
return IsWalletDeployed(w.provider, w.Address())
}
func (w *Wallet[C]) Deploy(ctx context.Context, transactions ...*Transaction) (MetaTxnID, *types.Transaction, ethtxn.WaitReceipt, error) {
return w.DeployWithImageHash(ctx, w.config.ImageHash(), transactions...)
}
func (w *Wallet[C]) DeployWithImageHash(ctx context.Context, imageHash core.ImageHash, transactions ...*Transaction) (MetaTxnID, *types.Transaction, ethtxn.WaitReceipt, error) {
if w.relayer == nil {
return "", nil, nil, ErrRelayerNotSet
}
isDeployed, err := w.IsDeployed()
if err == nil && isDeployed {
return "", nil, nil, fmt.Errorf("already deployed")
}
walletAddress, walletFactoryAddress, deploymentData, err := EncodeWalletDeploymentWithImageHash(w.config, w.context, imageHash)
if err != nil {
return "", nil, nil, err
}
if w.address != (common.Address{}) && w.address != walletAddress {
return "", nil, nil, fmt.Errorf("derived address %v does not match wallet address %v", walletAddress, w.address)
}
inner := Transactions{{To: walletFactoryAddress, Data: deploymentData, RevertOnError: true}}
if len(transactions) != 0 {
signed, err := w.SignTransactions(ctx, transactions)
if err != nil {
return "", nil, nil, fmt.Errorf("unable to sign post-deployment transactions: %w", err)
}
space := signed.Space
if space == nil {
space = new(big.Int)
}
nonce, err := EncodeNonce(space, signed.Nonce)
if err != nil {
return "", nil, nil, fmt.Errorf("unable to encode nonce for post-deployment transactions: %w", err)
}
inner = append(inner, &Transaction{
To: walletAddress,
Value: common.Big0,
GasLimit: common.Big0,
Transactions: signed.Transactions,
Nonce: nonce,
Signature: signed.Signature,
})
}
var digest common.Hash
switch core.WalletConfig(w.config).(type) {
case *v1.WalletConfig, *v2.WalletConfig:
digest, err = ComputeGuestExecDigest(inner)
case *v3.WalletConfig:
var payload v3.CallsPayload
payload, err = inner.Payload(w.context.GuestModuleAddress, w.chainID, nil, nil)
if err == nil {
digest = payload.Digest().Hash
}
}
if err != nil {
return "", nil, nil, fmt.Errorf("unable to compute guest subdigest: %w", err)
}
outer := SignedTransactions{
ChainID: w.chainID,
WalletAddress: w.context.GuestModuleAddress,
WalletConfig: w.config,
WalletContext: w.context,
Transactions: inner,
Space: new(big.Int),
Nonce: new(big.Int),
Digest: digest,
}
return w.relayer.Relay(ctx, &outer)
}
// func (w *Wallet) UpdateConfig() // TODO in future
// func (w *Wallet) PublishConfig() // TODO in future
func (w *Wallet[C]) IsValidSignature(digest common.Hash, signature []byte) (bool, error) {
if w.provider == nil {
return false, ErrProviderNotSet
}
// todo: this is a hack to get around the fact that the signature verification is not available in WalletConfig
var generalWalletConfig core.WalletConfig = w.config
if _, ok := generalWalletConfig.(*v3.WalletConfig); ok {
sig, err := v3.Core.DecodeSignature(signature)
if err != nil {
return false, err
}
config, weight, err := sig.Recover(context.Background(), v3.NewDigestPayload(w.address, w.chainID, digest), w.provider)
if err != nil {
return false, err
} else {
return weight.Cmp(new(big.Int).SetUint64(uint64(config.Threshold()))) >= 0, nil
}
} else if _, ok := generalWalletConfig.(*v2.WalletConfig); ok {
sig, err := v2.Core.DecodeSignature(signature)
if err != nil {
return false, err
}
config, weight, err := sig.Recover(context.Background(), v2.Digest(digest, w.address, w.chainID), w.provider)
if err != nil {
return false, err
} else {
return weight.Cmp(new(big.Int).SetUint64(uint64(config.Threshold()))) >= 0, nil
}
} else if _, ok := generalWalletConfig.(*v1.WalletConfig); ok {
sig, err := v1.Core.DecodeSignature(signature)
if err != nil {
return false, err
}
config, weight, err := sig.Recover(context.Background(), v1.Digest(digest, w.address, w.chainID), w.provider)
if err != nil {
return false, err
} else {
return weight.Cmp(new(big.Int).SetUint64(uint64(config.Threshold()))) >= 0, nil
}
} else {
return false, fmt.Errorf("unknown wallet config type")
}
}
func (w *Wallet[C]) buildSignature(ctx context.Context, sign core.SigningFunction, chainID *big.Int) ([]byte, core.Signature[C], error) {
var coreWalletConfig core.WalletConfig = w.config
if config, ok := coreWalletConfig.(*v1.WalletConfig); ok {
sig, err := config.BuildSignature(ctx, sign, false)