laconicd/x/evm/types/utils.go
Austin Abell cfac906f92
Ethermint key generation (#78)
* WIP setting up Ethereum key CLI commands

* Functional key gen and showing Ethereum address

* Cleaned up changes

* WIP setting up Ethereum key CLI commands

* Functional key gen and showing Ethereum address

* Cleaned up changes

* Changed address to cosmos specific address

* Remove default bech32 prefixes and add basic add command test

* Changed Private key type to slice of bytes for compatibility and storability

* switch back to using cosmos crypto Keybase interfaces

* Changed key output to ethereum addressing instead of bitcoin and key generation to allow seeding from mnemonic and bip39 password

* Updated show command and added test

* Remove prefix requirement for showing keys and added existing keys commands to CLI temporarily

* Removed unnecessary duplicate code

* Readd prefixes for accounts temporarily

* Fix linting issue

* Remove TODO for setting PK to specific length of bytes (all functions use slice)

* Cleaned up descriptions to remove multi-sigs
2019-08-11 10:42:46 -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"
"github.com/ethereum/go-ethereum/rlp"
"github.com/pkg/errors"
"golang.org/x/crypto/sha3"
)
// GenerateEthAddress generates an Ethereum address.
func GenerateEthAddress() ethcmn.Address {
priv, err := crypto.GenerateKey()
if err != nil {
panic(err)
}
return ethcrypto.PubkeyToAddress(priv.ToECDSA().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 := sha3.NewLegacyKeccak256()
// nolint:errcheck
rlp.Encode(hasher, x)
hasher.Sum(hash[:0])
return hash
}