github.com/dim4egster/coreth@v0.10.2/plugin/evm/formatting.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package evm 5 6 import ( 7 "fmt" 8 9 "github.com/dim4egster/qmallgo/ids" 10 "github.com/dim4egster/qmallgo/utils/constants" 11 "github.com/dim4egster/qmallgo/utils/crypto" 12 "github.com/dim4egster/qmallgo/utils/formatting/address" 13 "github.com/ethereum/go-ethereum/common" 14 ethcrypto "github.com/ethereum/go-ethereum/crypto" 15 ) 16 17 // ParseLocalAddress takes in an address for this chain and produces the ID 18 func (vm *VM) ParseLocalAddress(addrStr string) (ids.ShortID, error) { 19 chainID, addr, err := vm.ParseAddress(addrStr) 20 if err != nil { 21 return ids.ShortID{}, err 22 } 23 if chainID != vm.ctx.ChainID { 24 return ids.ShortID{}, fmt.Errorf("expected chainID to be %q but was %q", 25 vm.ctx.ChainID, chainID) 26 } 27 return addr, nil 28 } 29 30 // FormatLocalAddress takes in a raw address and produces the formatted address 31 func (vm *VM) FormatLocalAddress(addr ids.ShortID) (string, error) { 32 return vm.FormatAddress(vm.ctx.ChainID, addr) 33 } 34 35 // FormatAddress takes in a chainID and a raw address and produces the formatted 36 // address 37 func (vm *VM) FormatAddress(chainID ids.ID, addr ids.ShortID) (string, error) { 38 chainIDAlias, err := vm.ctx.BCLookup.PrimaryAlias(chainID) 39 if err != nil { 40 return "", err 41 } 42 hrp := constants.GetHRP(vm.ctx.NetworkID) 43 return address.Format(chainIDAlias, hrp, addr.Bytes()) 44 } 45 46 // ParseEthAddress parses [addrStr] and returns an Ethereum address 47 func ParseEthAddress(addrStr string) (common.Address, error) { 48 if !common.IsHexAddress(addrStr) { 49 return common.Address{}, errInvalidAddr 50 } 51 return common.HexToAddress(addrStr), nil 52 } 53 54 // GetEthAddress returns the ethereum address derived from [privKey] 55 func GetEthAddress(privKey *crypto.PrivateKeySECP256K1R) common.Address { 56 return PublicKeyToEthAddress(privKey.PublicKey().(*crypto.PublicKeySECP256K1R)) 57 } 58 59 // PublicKeyToEthAddress returns the ethereum address derived from [pubKey] 60 func PublicKeyToEthAddress(pubKey *crypto.PublicKeySECP256K1R) common.Address { 61 return ethcrypto.PubkeyToAddress(*(pubKey.ToECDSA())) 62 }