-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
40 lines (36 loc) · 1012 Bytes
/
auth.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
package shell
import (
"context"
"crypto/ecdsa"
"log"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)
func GetAuth(ctx context.Context, client *ethclient.Client, pvk string, limit uint64) (auth *bind.TransactOpts, err error) {
privateKey, err := crypto.HexToECDSA(pvk)
if err != nil {
return nil, err
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatalln("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(ctx, fromAddress)
if err != nil {
return nil, err
}
gasPrice, err := client.SuggestGasPrice(ctx)
if err != nil {
return nil, err
}
auth = bind.NewKeyedTransactor(privateKey)
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0)
auth.GasPrice = gasPrice
auth.GasLimit = limit
return auth, nil
}