types: update account pubkey JSON to string (#494)

* types: update account pubkey JSON to string

* changelog

* Update app/ethermint.go

* tests

* update

* fix secp256k1 public key formatting (#501)

* use Compress and Decompress pubkey for secp256k1 keys

* cleanup

* update estimate gas test

* comments

Co-authored-by: noot <36753753+noot@users.noreply.github.com>
This commit is contained in:
Federico Kunze 2020-09-07 15:04:50 +02:00 committed by GitHub
parent 820bf0f1aa
commit d3529dd959
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 110 additions and 26 deletions

View File

@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements ### Improvements
* (types) [\#494](https://github.com/ChainSafe/ethermint/pull/494) Update `EthAccount` public key JSON type to `string`.
* (app) [\#471](https://github.com/ChainSafe/ethermint/pull/471) Add `x/upgrade` module for managing software updates. * (app) [\#471](https://github.com/ChainSafe/ethermint/pull/471) Add `x/upgrade` module for managing software updates.
* (`x/evm`) [\#458](https://github.com/ChainSafe/ethermint/pull/458) Define parameter for token denomination used for the EVM module. * (`x/evm`) [\#458](https://github.com/ChainSafe/ethermint/pull/458) Define parameter for token denomination used for the EVM module.
* (`x/evm`) [\#443](https://github.com/ChainSafe/ethermint/issues/443) Support custom Ethereum `ChainConfig` params. * (`x/evm`) [\#443](https://github.com/ChainSafe/ethermint/issues/443) Support custom Ethereum `ChainConfig` params.

View File

@ -33,7 +33,7 @@ func GenerateKey() (PrivKeySecp256k1, error) {
// PubKey returns the ECDSA private key's public key. // PubKey returns the ECDSA private key's public key.
func (privkey PrivKeySecp256k1) PubKey() tmcrypto.PubKey { func (privkey PrivKeySecp256k1) PubKey() tmcrypto.PubKey {
ecdsaPKey := privkey.ToECDSA() ecdsaPKey := privkey.ToECDSA()
return PubKeySecp256k1(ethcrypto.FromECDSAPub(&ecdsaPKey.PublicKey)) return PubKeySecp256k1(ethcrypto.CompressPubkey(&ecdsaPKey.PublicKey))
} }
// Bytes returns the raw ECDSA private key bytes. // Bytes returns the raw ECDSA private key bytes.
@ -58,8 +58,12 @@ func (privkey PrivKeySecp256k1) Equals(other tmcrypto.PrivKey) bool {
} }
// ToECDSA returns the ECDSA private key as a reference to ecdsa.PrivateKey type. // ToECDSA returns the ECDSA private key as a reference to ecdsa.PrivateKey type.
// The function will panic if the private key is invalid.
func (privkey PrivKeySecp256k1) ToECDSA() *ecdsa.PrivateKey { func (privkey PrivKeySecp256k1) ToECDSA() *ecdsa.PrivateKey {
key, _ := ethcrypto.ToECDSA(privkey) key, err := ethcrypto.ToECDSA(privkey)
if err != nil {
panic(err)
}
return key return key
} }
@ -68,17 +72,23 @@ func (privkey PrivKeySecp256k1) ToECDSA() *ecdsa.PrivateKey {
var _ tmcrypto.PubKey = (*PubKeySecp256k1)(nil) var _ tmcrypto.PubKey = (*PubKeySecp256k1)(nil)
// PubKeySecp256k1 defines a type alias for an ecdsa.PublicKey that implements // PubKeySecp256k1 defines a type alias for an ecdsa.PublicKey that implements Tendermint's PubKey
// Tendermint's PubKey interface. // interface. It represents the 33-byte compressed public key format.
type PubKeySecp256k1 []byte type PubKeySecp256k1 []byte
// Address returns the address of the ECDSA public key. // Address returns the address of the ECDSA public key.
// The function will panic if the public key is invalid.
func (key PubKeySecp256k1) Address() tmcrypto.Address { func (key PubKeySecp256k1) Address() tmcrypto.Address {
pubk, _ := ethcrypto.UnmarshalPubkey(key) pubk, err := ethcrypto.DecompressPubkey(key)
if err != nil {
panic(err)
}
return tmcrypto.Address(ethcrypto.PubkeyToAddress(*pubk).Bytes()) return tmcrypto.Address(ethcrypto.PubkeyToAddress(*pubk).Bytes())
} }
// Bytes returns the raw bytes of the ECDSA public key. // Bytes returns the raw bytes of the ECDSA public key.
// The function panics if the key cannot be marshaled to bytes.
func (key PubKeySecp256k1) Bytes() []byte { func (key PubKeySecp256k1) Bytes() []byte {
bz, err := CryptoCodec.MarshalBinaryBare(key) bz, err := CryptoCodec.MarshalBinaryBare(key)
if err != nil { if err != nil {

View File

@ -43,7 +43,7 @@ Cosmos `sdk.AccAddress`.
- Address (Bech32): `eth1crwhac03z2pcgu88jfnqnwu66xlthlz2rhljah` - Address (Bech32): `eth1crwhac03z2pcgu88jfnqnwu66xlthlz2rhljah`
- Address (Hex): `0xc0dd7ee1f112838470e7926609bb9ad1bebbfc4a` - Address (Hex): `0xc0dd7ee1f112838470e7926609bb9ad1bebbfc4a`
- Public Key (Bech32): `ethpub1pfqnmk6pqnwwuw0h9hj58t2hyzwvqc3truhhp5tl5hfucezcfy2rs8470nkyzju2vmk645fzmw2wveaqcqek767kwa0es9rmxe9nmmjq84cpny3fvj6tpg` - Compressed Public Key (Bech32): `ethpub1pfqnmk6pqnwwuw0h9hj58t2hyzwvqc3truhhp5tl5hfucezcfy2rs8470nkyzju2vmk645fzmw2wveaqcqek767kwa0es9rmxe9nmmjq84cpny3fvj6tpg`
## Next {hide} ## Next {hide}

View File

@ -758,7 +758,7 @@ func TestEth_EstimateGas(t *testing.T) {
err := json.Unmarshal(rpcRes.Result, &gas) err := json.Unmarshal(rpcRes.Result, &gas)
require.NoError(t, err, string(rpcRes.Result)) require.NoError(t, err, string(rpcRes.Result))
require.Equal(t, "0x1051d", gas) require.Equal(t, "0xfd40", gas)
} }
func TestEth_EstimateGas_ContractDeployment(t *testing.T) { func TestEth_EstimateGas_ContractDeployment(t *testing.T) {

View File

@ -11,8 +11,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/auth/exported"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
tmamino "github.com/tendermint/tendermint/crypto/encoding/amino"
ethcmn "github.com/ethereum/go-ethereum/common" ethcmn "github.com/ethereum/go-ethereum/common"
ethcrypto "github.com/ethereum/go-ethereum/crypto" ethcrypto "github.com/ethereum/go-ethereum/crypto"
) )
@ -79,7 +77,7 @@ func (acc *EthAccount) SetBalance(amt sdk.Int) {
type ethermintAccountPretty struct { type ethermintAccountPretty struct {
Address sdk.AccAddress `json:"address" yaml:"address"` Address sdk.AccAddress `json:"address" yaml:"address"`
Coins sdk.Coins `json:"coins" yaml:"coins"` Coins sdk.Coins `json:"coins" yaml:"coins"`
PubKey []byte `json:"public_key" yaml:"public_key"` PubKey string `json:"public_key" yaml:"public_key"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"` AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Sequence uint64 `json:"sequence" yaml:"sequence"` Sequence uint64 `json:"sequence" yaml:"sequence"`
CodeHash string `json:"code_hash" yaml:"code_hash"` CodeHash string `json:"code_hash" yaml:"code_hash"`
@ -95,8 +93,13 @@ func (acc EthAccount) MarshalYAML() (interface{}, error) {
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash), CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
} }
var err error
if acc.PubKey != nil { if acc.PubKey != nil {
alias.PubKey = acc.PubKey.Bytes() alias.PubKey, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.PubKey)
if err != nil {
return nil, err
}
} }
bz, err := yaml.Marshal(alias) bz, err := yaml.Marshal(alias)
@ -117,8 +120,13 @@ func (acc EthAccount) MarshalJSON() ([]byte, error) {
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash), CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
} }
var err error
if acc.PubKey != nil { if acc.PubKey != nil {
alias.PubKey = acc.PubKey.Bytes() alias.PubKey, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.PubKey)
if err != nil {
return nil, err
}
} }
return json.Marshal(alias) return json.Marshal(alias)
@ -126,26 +134,34 @@ func (acc EthAccount) MarshalJSON() ([]byte, error) {
// UnmarshalJSON unmarshals raw JSON bytes into an EthAccount. // UnmarshalJSON unmarshals raw JSON bytes into an EthAccount.
func (acc *EthAccount) UnmarshalJSON(bz []byte) error { func (acc *EthAccount) UnmarshalJSON(bz []byte) error {
acc.BaseAccount = &authtypes.BaseAccount{} var (
var alias ethermintAccountPretty alias ethermintAccountPretty
err error
)
if err := json.Unmarshal(bz, &alias); err != nil { if err := json.Unmarshal(bz, &alias); err != nil {
return err return err
} }
if alias.PubKey != nil { acc.BaseAccount = &authtypes.BaseAccount{
pubKey, err := tmamino.PubKeyFromBytes(alias.PubKey) Coins: alias.Coins,
Address: alias.Address,
AccountNumber: alias.AccountNumber,
Sequence: alias.Sequence,
}
acc.CodeHash = ethcmn.Hex2Bytes(alias.CodeHash)
if alias.PubKey != "" {
acc.BaseAccount.PubKey, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey)
if err != nil { if err != nil {
return err return err
} }
acc.BaseAccount.PubKey = pubKey
} }
acc.BaseAccount.Coins = alias.Coins
acc.BaseAccount.Address = alias.Address
acc.BaseAccount.AccountNumber = alias.AccountNumber
acc.BaseAccount.Sequence = alias.Sequence
acc.CodeHash = ethcmn.Hex2Bytes(alias.CodeHash)
return nil return nil
} }
// String implements the fmt.Stringer interface
func (acc EthAccount) String() string {
out, _ := yaml.Marshal(acc)
return string(out)
}

View File

@ -2,6 +2,7 @@ package types
import ( import (
"encoding/json" "encoding/json"
"fmt"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -35,7 +36,7 @@ func TestEthermintAccountJSON(t *testing.T) {
require.Equal(t, string(bz1), string(bz)) require.Equal(t, string(bz1), string(bz))
var a EthAccount var a EthAccount
require.NoError(t, json.Unmarshal(bz, &a)) require.NoError(t, a.UnmarshalJSON(bz))
require.Equal(t, ethAcc.String(), a.String()) require.Equal(t, ethAcc.String(), a.String())
require.Equal(t, ethAcc.PubKey, a.PubKey) require.Equal(t, ethAcc.PubKey, a.PubKey)
} }
@ -58,3 +59,55 @@ func TestSecpPubKeyJSON(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, pubk, pubkey) require.Equal(t, pubk, pubkey)
} }
func TestEthermintAccount_String(t *testing.T) {
pubkey := secp256k1.GenPrivKey().PubKey()
addr := sdk.AccAddress(pubkey.Address())
balance := sdk.NewCoins(sdk.NewCoin(DenomDefault, sdk.OneInt()))
baseAcc := auth.NewBaseAccount(addr, balance, pubkey, 10, 50)
ethAcc := EthAccount{BaseAccount: baseAcc, CodeHash: []byte{1, 2}}
config := sdk.GetConfig()
SetBech32Prefixes(config)
bech32pubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pubkey)
require.NoError(t, err)
accountStr := fmt.Sprintf(`|
address: %s
coins:
- denom: aphoton
amount: "1"
public_key: %s
account_number: 10
sequence: 50
code_hash: "0102"
`, addr, bech32pubkey)
require.Equal(t, accountStr, ethAcc.String())
i, err := ethAcc.MarshalYAML()
require.NoError(t, err)
var ok bool
accountStr, ok = i.(string)
require.True(t, ok)
require.Contains(t, accountStr, addr.String())
require.Contains(t, accountStr, bech32pubkey)
}
func TestEthermintAccount_MarshalJSON(t *testing.T) {
pubkey := secp256k1.GenPrivKey().PubKey()
addr := sdk.AccAddress(pubkey.Address())
balance := sdk.NewCoins(sdk.NewCoin(DenomDefault, sdk.OneInt()))
baseAcc := auth.NewBaseAccount(addr, balance, pubkey, 10, 50)
ethAcc := &EthAccount{BaseAccount: baseAcc, CodeHash: []byte{1, 2}}
bz, err := ethAcc.MarshalJSON()
require.NoError(t, err)
res := new(EthAccount)
err = res.UnmarshalJSON(bz)
require.NoError(t, err)
require.Equal(t, ethAcc, res)
}

View File

@ -10,6 +10,8 @@ import (
func TestSetBech32Prefixes(t *testing.T) { func TestSetBech32Prefixes(t *testing.T) {
config := sdk.GetConfig() config := sdk.GetConfig()
config = sdk.NewConfig() // reset config values
require.Equal(t, sdk.Bech32PrefixAccAddr, config.GetBech32AccountAddrPrefix()) require.Equal(t, sdk.Bech32PrefixAccAddr, config.GetBech32AccountAddrPrefix())
require.Equal(t, sdk.Bech32PrefixAccPub, config.GetBech32AccountPubPrefix()) require.Equal(t, sdk.Bech32PrefixAccPub, config.GetBech32AccountPubPrefix())
require.Equal(t, sdk.Bech32PrefixValAddr, config.GetBech32ValidatorAddrPrefix()) require.Equal(t, sdk.Bech32PrefixValAddr, config.GetBech32ValidatorAddrPrefix())
@ -36,8 +38,10 @@ func TestSetBech32Prefixes(t *testing.T) {
func TestSetCoinType(t *testing.T) { func TestSetCoinType(t *testing.T) {
config := sdk.GetConfig() config := sdk.GetConfig()
require.Equal(t, sdk.CoinType, int(config.GetCoinType())) require.Equal(t, sdk.CoinType, int(config.GetCoinType()))
require.Equal(t, sdk.FullFundraiserPath, config.GetFullFundraiserPath())
SetBip44CoinType(config) SetBip44CoinType(config)
require.Equal(t, Bip44CoinType, int(config.GetCoinType())) require.Equal(t, Bip44CoinType, int(config.GetCoinType()))
require.Equal(t, sdk.GetConfig().GetCoinType(), config.GetCoinType()) require.Equal(t, sdk.GetConfig().GetCoinType(), config.GetCoinType())
require.Equal(t, sdk.GetConfig().GetFullFundraiserPath(), config.GetFullFundraiserPath())
} }