forked from cerc-io/laconicd-deprecated
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:
+34
-18
@@ -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"
|
||||
)
|
||||
@@ -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"`
|
||||
@@ -95,8 +93,13 @@ func (acc EthAccount) MarshalYAML() (interface{}, error) {
|
||||
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
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)
|
||||
@@ -117,8 +120,13 @@ func (acc EthAccount) MarshalJSON() ([]byte, error) {
|
||||
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
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)
|
||||
@@ -126,26 +134,34 @@ func (acc EthAccount) MarshalJSON() ([]byte, error) {
|
||||
|
||||
// UnmarshalJSON unmarshals raw JSON bytes into an EthAccount.
|
||||
func (acc *EthAccount) UnmarshalJSON(bz []byte) error {
|
||||
acc.BaseAccount = &authtypes.BaseAccount{}
|
||||
var alias ethermintAccountPretty
|
||||
var (
|
||||
alias ethermintAccountPretty
|
||||
err error
|
||||
)
|
||||
|
||||
if err := json.Unmarshal(bz, &alias); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if alias.PubKey != nil {
|
||||
pubKey, err := tmamino.PubKeyFromBytes(alias.PubKey)
|
||||
acc.BaseAccount = &authtypes.BaseAccount{
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
// String implements the fmt.Stringer interface
|
||||
func (acc EthAccount) String() string {
|
||||
out, _ := yaml.Marshal(acc)
|
||||
return string(out)
|
||||
}
|
||||
|
||||
+54
-1
@@ -2,6 +2,7 @@ package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -35,7 +36,7 @@ func TestEthermintAccountJSON(t *testing.T) {
|
||||
require.Equal(t, string(bz1), string(bz))
|
||||
|
||||
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.PubKey, a.PubKey)
|
||||
}
|
||||
@@ -58,3 +59,55 @@ func TestSecpPubKeyJSON(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
|
||||
func TestSetBech32Prefixes(t *testing.T) {
|
||||
config := sdk.GetConfig()
|
||||
config = sdk.NewConfig() // reset config values
|
||||
|
||||
require.Equal(t, sdk.Bech32PrefixAccAddr, config.GetBech32AccountAddrPrefix())
|
||||
require.Equal(t, sdk.Bech32PrefixAccPub, config.GetBech32AccountPubPrefix())
|
||||
require.Equal(t, sdk.Bech32PrefixValAddr, config.GetBech32ValidatorAddrPrefix())
|
||||
@@ -36,8 +38,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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user