laconicd/x/evm/types/utils.go
Austin Abell 69333ec1b3
circleCI config and linting fixes (#3)
* Fixed circleci config and fixed linting warnings

* Updated circleCI for go version 1.12 and split jobs for build/testing

* updated go version to 1.12.5 for circleCI

* Go mod tidy dependencies

* Updated linting tools and cleared up code lint smells

* Added workflow to run build and test jobs

* Moved linting command to build workflow

* Get dependencies before linting by default

* Added go module flag to linter and increased deadline to pull packages
2019-07-02 15:36:22 -04:00

49 lines
1.2 KiB
Go

package types
import (
"fmt"
"github.com/cosmos/ethermint/crypto"
ethcmn "github.com/ethereum/go-ethereum/common"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
ethsha "github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/rlp"
"github.com/pkg/errors"
)
// GenerateEthAddress generates an Ethereum address.
func GenerateEthAddress() ethcmn.Address {
priv, err := crypto.GenerateKey()
if err != nil {
panic(err)
}
return ethcrypto.PubkeyToAddress(priv.PublicKey)
}
// ValidateSigner attempts to validate a signer for a given slice of bytes over
// which a signature and signer is given. An error is returned if address
// derived from the signature and bytes signed does not match the given signer.
func ValidateSigner(signBytes, sig []byte, signer ethcmn.Address) error {
pk, err := ethcrypto.SigToPub(signBytes, sig)
if err != nil {
return errors.Wrap(err, "failed to derive public key from signature")
} else if ethcrypto.PubkeyToAddress(*pk) != signer {
return fmt.Errorf("invalid signature for signer: %s", signer)
}
return nil
}
func rlpHash(x interface{}) (hash ethcmn.Hash) {
hasher := ethsha.NewKeccak256()
// nolint:errcheck
rlp.Encode(hasher, x)
hasher.Sum(hash[:0])
return
}