Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

types: update account pubkey JSON to string #494

Merged
merged 9 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### 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.

### Bug Fixes
Expand Down
1 change: 1 addition & 0 deletions app/ethermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func init() {
config := sdk.GetConfig()
ethermint.SetBech32Prefixes(config)
ethermint.SetBip44CoinType(config)
config.Seal()
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
}

const appName = "Ethermint"
Expand Down
31 changes: 15 additions & 16 deletions types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/exported"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

tmamino "github.com/tendermint/tendermint/crypto/encoding/amino"

ethcmn "github.com/ethereum/go-ethereum/common"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
)
Expand Down Expand Up @@ -79,7 +77,7 @@ func (acc *EthAccount) SetBalance(amt sdk.Int) {
type ethermintAccountPretty struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
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"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
CodeHash string `json:"code_hash" yaml:"code_hash"`
Expand All @@ -95,8 +93,10 @@ func (acc EthAccount) MarshalYAML() (interface{}, error) {
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
}

if acc.PubKey != nil {
alias.PubKey = acc.PubKey.Bytes()
var err error
alias.PubKey, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.PubKey)
if err != nil {
return nil, err
}

bz, err := yaml.Marshal(alias)
Expand All @@ -117,8 +117,10 @@ func (acc EthAccount) MarshalJSON() ([]byte, error) {
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
}

if acc.PubKey != nil {
alias.PubKey = acc.PubKey.Bytes()
var err error
alias.PubKey, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.PubKey)
if err != nil {
return nil, err
}

return json.Marshal(alias)
Expand All @@ -132,17 +134,14 @@ func (acc *EthAccount) UnmarshalJSON(bz []byte) error {
return err
}

if alias.PubKey != nil {
pubKey, err := tmamino.PubKeyFromBytes(alias.PubKey)
if err != nil {
return err
}

acc.BaseAccount.PubKey = pubKey
}

acc.BaseAccount.Coins = alias.Coins
acc.BaseAccount.Address = alias.Address

var err error
acc.BaseAccount.PubKey, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey)
if err != nil {
return err
}
acc.BaseAccount.AccountNumber = alias.AccountNumber
acc.BaseAccount.Sequence = alias.Sequence
acc.CodeHash = ethcmn.Hex2Bytes(alias.CodeHash)
Expand Down
2 changes: 2 additions & 0 deletions types/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ func TestSetBech32Prefixes(t *testing.T) {
func TestSetCoinType(t *testing.T) {
config := sdk.GetConfig()
require.Equal(t, sdk.CoinType, int(config.GetCoinType()))
require.Equal(t, sdk.FullFundraiserPath, config.GetFullFundraiserPath())

SetBip44CoinType(config)
require.Equal(t, Bip44CoinType, int(config.GetCoinType()))
require.Equal(t, sdk.GetConfig().GetCoinType(), config.GetCoinType())
require.Equal(t, sdk.GetConfig().GetFullFundraiserPath(), config.GetFullFundraiserPath())
}