github.com/bcskill/bcschain/v3@v3.4.9-beta2/crypto/account.go (about) 1 package crypto 2 3 /* 4 Convenience object for dealing with keys/addresses 5 */ 6 7 import ( 8 "crypto/ecdsa" 9 "encoding/hex" 10 "strings" 11 ) 12 13 func CreateKey() (*Key, error) { 14 key, err := GenerateKey() 15 if err != nil { 16 return nil, err 17 } 18 return &Key{ 19 key: key, 20 }, nil 21 } 22 23 func ParsePrivateKeyHex(pkHex string) (*Key, error) { 24 fromPK := strings.TrimPrefix(pkHex, "0x") 25 key, err := HexToECDSA(fromPK) 26 if err != nil { 27 return nil, err 28 } 29 return &Key{ 30 key: key, 31 }, nil 32 } 33 34 type Key struct { 35 key *ecdsa.PrivateKey 36 } 37 38 func (a *Key) PrivateKey() *ecdsa.PrivateKey { 39 return a.key 40 } 41 42 func (a *Key) PublicKeyHex() string { 43 return PubkeyToAddress(a.key.PublicKey).Hex() 44 } 45 46 func (a *Key) PrivateKeyHex() string { 47 return "0x" + hex.EncodeToString(a.key.D.Bytes()) 48 }