-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwallet.go
840 lines (713 loc) · 23.9 KB
/
wallet.go
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
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"
"github.com/0xsequence/go-sequence/sessions/proto"
)
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 = AddressFromImageHash(walletOptions.Config.ImageHash().Hex(), 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[*v1.WalletConfig](walletOptions, signers...)
}
func V2NewWallet(walletOptions WalletOptions[*v2.WalletConfig], signers ...Signer) (*Wallet[*v2.WalletConfig], error) {
return GenericNewWallet[*v2.WalletConfig](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[C](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[C](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...)
}
// 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 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 proto.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 proto.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 {
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 {
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]) IsSignerAvailable(address common.Address) bool {
_, ok := w.GetSigner(address)
return ok
}
func (w *Wallet[C]) GetSigner(address common.Address) (Signer, bool) {
for _, s := range w.signers {
if s.Address() == address {
return s, true
}
}
return nil, false
}
func (w *Wallet[C]) GetSignerWeight() *big.Int {
var signers = make([]common.Address, 0, len(w.signers))
for _, s := range w.signers {
signers = append(signers, s.Address())
}
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)
var _ MessageSigner = (*Wallet[*v2.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 optChainID != nil && 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, signerAddress common.Address, signatures []core.SignerSignature) (core.SignerSignatureType, []byte, error) {
signer, _ := w.GetSigner(signerAddress)
if signer == nil {
// signer isn't available, just include the config value of address
// without it's signature
return 0, nil, core.ErrSigningNoSigner
}
switch signerTyped := signer.(type) {
// sequence.Wallet / Signing Service / Guard
case DigestSigner:
sigValue, err := signerTyped.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 := signerTyped.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
}
var _ DigestSigner = (*Wallet[*v1.WalletConfig])(nil)
var _ 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 {
txns, err = w.relayer.EstimateGasLimits(ctx, w.config, w.context, txns)
if err != nil {
return nil, fmt.Errorf("estimateGas failed for sequence transactions: %w", err)
}
}
// 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
}
}
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
}
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) (MetaTxnID, *types.Transaction, ethtxn.WaitReceipt, error) {
if w.relayer == nil {
return "", nil, nil, ErrRelayerNotSet
}
walletAddress, walletFactoryAddress, deploymentData, err := EncodeWalletDeployment(w.config, w.context)
if err != nil {
return "", nil, nil, err
}
if w.address != (common.Address{}) && w.address != walletAddress {
return "", nil, nil, fmt.Errorf("wallet address %s does not match the address derived from the config %s", w.address, walletAddress)
}
var txn = &Transaction{
RevertOnError: true,
To: walletFactoryAddress,
Data: deploymentData,
}
// for v1 sequence wallet default does not work
if _, ok := core.WalletConfig(w.config).(*v1.WalletConfig); ok {
// TODO: Move this hardcoded gas limit to a configuration
// or fix it with a contract patch
txn.GasLimit = big.NewInt(131072)
}
signerTxn, err := w.SignTransaction(ctx, txn)
if err != nil {
return "", nil, nil, err
}
return w.relayer.Relay(ctx, signerTxn)
}
// 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.(*v2.WalletConfig); ok {
sig, err := v2.Core.DecodeSignature(signature)
if err != nil {
return false, err
}
_, _, err = sig.Recover(context.Background(), core.Digest{Hash: digest}, w.address, w.chainID, w.provider)
if err != nil {
return false, err
} else {
return true, nil
}
} else if _, ok := generalWalletConfig.(*v1.WalletConfig); ok {
sig, err := v1.Core.DecodeSignature(signature)
if err != nil {
return false, err
}
_, _, err = sig.Recover(context.Background(), core.Digest{Hash: digest}, w.address, w.chainID, w.provider)
if err != nil {
return false, err
} else {
return true, 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.(*v2.WalletConfig); ok {
var (
sig core.Signature[*v2.WalletConfig]
err error
)
if chainID.Sign() == 0 {
sig, err = config.BuildNoChainIDSignature(ctx, sign, false)
if err != nil {
return nil, nil, fmt.Errorf("SignDigest, BuildNoChainIDSignature: %w", err)
}
} else {
sig, err = config.BuildRegularSignature(ctx, sign, false)
if err != nil {
return nil, nil, fmt.Errorf("SignDigest, BuildRegularSignature: %w", err)
}
}
sigEnc, err := sig.Data()
if err != nil {
return nil, nil, fmt.Errorf("SignDigest, sig.Data: %w", err)
}
sigTyped, _ := sig.(core.Signature[C])
// todo: implement core.Signature[core.WalletConfig] wrapper
return sigEnc, sigTyped, nil
} else if config, ok := coreWalletConfig.(*v1.WalletConfig); ok {
sig, err := config.BuildSignature(ctx, sign, false)
if err != nil {
return nil, nil, fmt.Errorf("SignDigest, BuildSignature: %w", err)
}
sigEnc, err := sig.Data()
if err != nil {
return nil, nil, fmt.Errorf("SignDigest, sig.Data: %w", err)
}
sigTyped, _ := sig.(core.Signature[C])
// todo: implement core.Signature[core.WalletConfig] wrapper
return sigEnc, sigTyped, nil
} else {
return nil, nil, fmt.Errorf("SignDigest, unknown config type")
}
}
func IsWalletDeployed(provider *ethrpc.Provider, walletAddress common.Address) (bool, error) {
code, err := provider.CodeAt(context.Background(), walletAddress, nil)
if err != nil {
return false, err
}
if len(code) > 2 {
return true, nil
}
return false, nil
}
// AuxData is the data that is signed by the wallet
type AuxData struct {
Msg []byte
Sig []byte
ChainID *big.Int
Address *common.Address
}
func (a *AuxData) Pack() ([]byte, error) {
return ethcoder.AbiCoder(
[]string{"address", "uint256", "bytes", "bytes"},
[]interface{}{a.Address, a.ChainID, &a.Msg, &a.Sig},
)
}
func ContextWithAuxData(ctx context.Context, auxData *AuxData) context.Context {
return context.WithValue(ctx, "auxData", auxData)
}
func AuxDataFromContext(ctx context.Context) (*AuxData, error) {
auxData, ok := ctx.Value("auxData").(*AuxData)
if !ok {
return nil, fmt.Errorf("auxData not found in context")
}
return auxData, nil
}
var (
// ImageHashUpdatedEventSig is emitted anytime wallet config is updated.
ImageHashUpdatedEventSig = MustEncodeSig("ImageHashUpdated(bytes32)")
// ImplementationUpdatedEventSig is emitted anytime a wallet's mainModule is changed,
// this is a rare occurence.
ImplementationUpdatedEventSig = MustEncodeSig("ImplementationUpdated(address)")
)
// https://github.com/0xsequence/wallet-contracts/blob/master/src/contracts/Wallet.sol#L57-L59
const WalletContractBytecode = "0x603a600e3d39601a805130553df3363d3d373d3d3d363d30545af43d82803e903d91601857fd5bf3"
var walletContractBytecode = common.FromHex(WalletContractBytecode)