2022-12-16 09:48:38 +00:00
|
|
|
// Copyright 2021 Evmos Foundation
|
|
|
|
// This file is part of Evmos' Ethermint library.
|
|
|
|
//
|
|
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
2021-05-12 13:08:31 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-06-02 08:06:12 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
|
2021-05-12 13:08:31 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/crypto/ethsecp256k1"
|
2021-05-12 13:08:31 +00:00
|
|
|
)
|
|
|
|
|
2021-06-02 08:06:12 +00:00
|
|
|
// NewAddrKey generates an Ethereum address and its corresponding private key.
|
|
|
|
func NewAddrKey() (common.Address, cryptotypes.PrivKey) {
|
|
|
|
privkey, _ := ethsecp256k1.GenerateKey()
|
2021-09-07 17:29:24 +00:00
|
|
|
key, err := privkey.ToECDSA()
|
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2021-06-02 08:06:12 +00:00
|
|
|
|
|
|
|
return addr, privkey
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateAddress generates an Ethereum address.
|
|
|
|
func GenerateAddress() common.Address {
|
|
|
|
addr, _ := NewAddrKey()
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2021-05-12 13:08:31 +00:00
|
|
|
var _ keyring.Signer = &Signer{}
|
|
|
|
|
|
|
|
// Signer defines a type that is used on testing for signing MsgEthereumTx
|
|
|
|
type Signer struct {
|
|
|
|
privKey cryptotypes.PrivKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSigner(sk cryptotypes.PrivKey) keyring.Signer {
|
|
|
|
return &Signer{
|
|
|
|
privKey: sk,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign signs the message using the underlying private key
|
|
|
|
func (s Signer) Sign(_ string, msg []byte) ([]byte, cryptotypes.PubKey, error) {
|
|
|
|
if s.privKey.Type() != ethsecp256k1.KeyType {
|
|
|
|
return nil, nil, fmt.Errorf(
|
|
|
|
"invalid private key type for signing ethereum tx; expected %s, got %s",
|
|
|
|
ethsecp256k1.KeyType,
|
|
|
|
s.privKey.Type(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := s.privKey.Sign(msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sig, s.privKey.PubKey(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignByAddress sign byte messages with a user key providing the address.
|
|
|
|
func (s Signer) SignByAddress(address sdk.Address, msg []byte) ([]byte, cryptotypes.PubKey, error) {
|
|
|
|
signer := sdk.AccAddress(s.privKey.PubKey().Address())
|
|
|
|
if !signer.Equals(address) {
|
|
|
|
return nil, nil, fmt.Errorf("address mismatch: signer %s ≠ given address %s", signer, address)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Sign("", msg)
|
|
|
|
}
|