-
Notifications
You must be signed in to change notification settings - Fork 4
/
signer.go
40 lines (37 loc) · 1009 Bytes
/
signer.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 go_cowswap
import (
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"log"
"math/big"
)
// NewSigner - Creates a transaction signer with private key
func NewSigner(privateKey string, chainId *big.Int) (*TransactionSigner, error) {
pk, err := crypto.HexToECDSA(privateKey)
if err != nil {
return nil, err
}
publicKey := pk.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}
signerPubKey := crypto.PubkeyToAddress(*publicKeyECDSA)
auth, err := bind.NewKeyedTransactorWithChainID(pk, chainId)
if err != nil {
return nil, err
}
signer := &TransactionSigner{
PrivateKey: pk,
SignerPubKey: signerPubKey,
Auth: auth,
}
return signer, nil
}
type TransactionSigner struct {
PrivateKey *ecdsa.PrivateKey
SignerPubKey common.Address
Auth *bind.TransactOpts
}