-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwallet.go
52 lines (46 loc) · 1.1 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
package evmc
import (
"crypto/ecdsa"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
type Wallet struct {
pk *ecdsa.PrivateKey
address string
}
func NewWallet(privateKey string) (*Wallet, error) {
if privateKey[:2] == "0x" {
privateKey = privateKey[2:]
}
pk, err := crypto.HexToECDSA(privateKey)
if err != nil {
return nil, err
}
return &Wallet{
pk: pk,
address: crypto.PubkeyToAddress(pk.PublicKey).Hex(),
}, nil
}
func (w *Wallet) SignTx(sendingTx *SendingTx, chainID uint64) (hash, rawTx string, err error) {
if chainID == 0 {
return "", "", errors.New("chainID is zero")
}
gethTx := types.NewTx(sendingTx.txData)
signedTx, err := types.SignTx(gethTx, types.LatestSignerForChainID(new(big.Int).SetUint64(chainID)), w.pk)
if err != nil {
return "", "", err
}
raw, err := signedTx.MarshalBinary()
if err != nil {
return "", "", err
}
hash = signedTx.Hash().Hex()
rawTx = hexutil.Encode(raw)
return
}
func (w *Wallet) Address() string {
return w.address
}