/
wallet.go
41 lines (34 loc) · 1.05 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
package client
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
// EthereumWallet is the implementation to allow testing with ETH based wallets
type EthereumWallet struct {
privateKey string
address common.Address
}
// NewEthereumWallet returns the instantiated ETH wallet based on a given private key
func NewEthereumWallet(pk string) (*EthereumWallet, error) {
privateKey, err := crypto.HexToECDSA(pk)
if err != nil {
return nil, fmt.Errorf("invalid private key: %v", err)
}
return &EthereumWallet{
privateKey: pk,
address: crypto.PubkeyToAddress(privateKey.PublicKey),
}, nil
}
// RawPrivateKey returns raw private key if it has some encoding or in bytes
func (e *EthereumWallet) RawPrivateKey() interface{} {
return e.privateKey
}
// PrivateKey returns the private key for a given Ethereum wallet
func (e *EthereumWallet) PrivateKey() string {
return e.privateKey
}
// Address returns the ETH address for a given wallet
func (e *EthereumWallet) Address() string {
return e.address.String()
}