forked from zksync-sdk/zksync2-go
/
util.go
60 lines (55 loc) · 1.49 KB
/
util.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
package accounts
import (
"context"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"math/big"
)
// ensureContext is a helper method to ensure a context is not nil, even if the
// user specified it as such.
func ensureContext(ctx context.Context) context.Context {
if ctx == nil {
return context.Background()
}
return ctx
}
// ensureCallOpts is a helper method to ensure a CallOpts is not nil, even if the
// user specified it as such.
func ensureCallOpts(opts *CallOpts) *CallOpts {
if opts == nil {
return &CallOpts{Context: context.Background()}
}
return opts
}
func ensureTransactOpts(auth *TransactOpts) *TransactOpts {
if auth == nil {
return &TransactOpts{
Context: context.Background(),
}
}
if auth.Context == nil {
auth.Context = context.Background()
}
return auth
}
func newTransactorWithSigner(signer *Signer, chainID *big.Int) (*bind.TransactOpts, error) {
if chainID == nil {
return nil, bind.ErrNoChainID
}
keyAddr := (*signer).Address()
latestSigner := types.LatestSignerForChainID(chainID)
return &bind.TransactOpts{
From: keyAddr,
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != keyAddr {
return nil, bind.ErrNotAuthorized
}
signature, err := (*signer).SignHash(latestSigner.Hash(tx).Bytes())
if err != nil {
return nil, err
}
return tx.WithSignature(latestSigner, signature)
},
}, nil
}