2020-09-09 13:53:14 +00:00
|
|
|
package types_test
|
2019-12-13 19:50:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-09-07 13:04:50 +00:00
|
|
|
"fmt"
|
2019-12-13 19:50:19 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2020-07-02 15:19:48 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2020-09-08 15:58:19 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2020-09-08 15:58:19 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
cryptocodec "github.com/cosmos/ethermint/crypto/codec"
|
2020-10-06 18:57:55 +00:00
|
|
|
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
|
2021-06-04 13:45:37 +00:00
|
|
|
ethermintcodec "github.com/cosmos/ethermint/encoding/codec"
|
2020-09-09 13:53:14 +00:00
|
|
|
"github.com/cosmos/ethermint/types"
|
2019-12-13 19:50:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-04-17 10:00:07 +00:00
|
|
|
amino := codec.NewLegacyAmino()
|
|
|
|
cryptocodec.RegisterCrypto(amino)
|
2019-12-13 19:50:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
type AccountTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
account *types.EthAccount
|
2021-04-17 10:00:07 +00:00
|
|
|
cdc codec.JSONMarshaler
|
2020-09-09 13:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AccountTestSuite) SetupTest() {
|
2021-04-17 10:00:07 +00:00
|
|
|
privKey, err := ethsecp256k1.GenerateKey()
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
pubKey := privKey.PubKey()
|
|
|
|
addr := sdk.AccAddress(pubKey.Address())
|
|
|
|
baseAcc := authtypes.NewBaseAccount(addr, pubKey, 10, 50)
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.account = &types.EthAccount{
|
|
|
|
BaseAccount: baseAcc,
|
2021-04-17 10:00:07 +00:00
|
|
|
CodeHash: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2},
|
2020-09-09 13:53:14 +00:00
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
interfaceRegistry := codectypes.NewInterfaceRegistry()
|
|
|
|
ethermintcodec.RegisterInterfaces(interfaceRegistry)
|
|
|
|
suite.cdc = codec.NewProtoCodec(interfaceRegistry)
|
2020-09-09 13:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccountTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(AccountTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AccountTestSuite) TestEthermintAccountJSON() {
|
|
|
|
bz, err := json.Marshal(suite.account)
|
|
|
|
suite.Require().NoError(err)
|
2019-12-13 19:50:19 +00:00
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
bz1, err := suite.account.MarshalJSON()
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(string(bz1), string(bz))
|
2019-12-13 19:50:19 +00:00
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
var a types.EthAccount
|
2021-04-17 10:00:07 +00:00
|
|
|
suite.Require().NoError(a.UnmarshalJSON(bz1))
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().Equal(suite.account.String(), a.String())
|
2021-04-17 10:00:07 +00:00
|
|
|
suite.Require().Equal(suite.account.GetPubKey(), a.GetPubKey())
|
2019-12-13 19:50:19 +00:00
|
|
|
}
|
2020-09-07 13:04:50 +00:00
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
func (suite *AccountTestSuite) TestEthermintAccount_String() {
|
2020-09-07 13:04:50 +00:00
|
|
|
config := sdk.GetConfig()
|
2020-09-09 13:53:14 +00:00
|
|
|
types.SetBech32Prefixes(config)
|
2020-09-07 13:04:50 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
bech32pubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, suite.account.GetPubKey())
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().NoError(err)
|
2020-09-07 13:04:50 +00:00
|
|
|
|
|
|
|
accountStr := fmt.Sprintf(`|
|
|
|
|
address: %s
|
2020-09-08 13:57:49 +00:00
|
|
|
eth_address: %s
|
2020-09-07 13:04:50 +00:00
|
|
|
public_key: %s
|
|
|
|
account_number: 10
|
|
|
|
sequence: 50
|
2021-04-17 10:00:07 +00:00
|
|
|
code_hash: "0000000000000000000000000000000000000000000000000000000000000102"
|
2020-09-09 13:53:14 +00:00
|
|
|
`, suite.account.Address, suite.account.EthAddress().String(), bech32pubkey)
|
2020-09-07 13:04:50 +00:00
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().Equal(accountStr, suite.account.String())
|
2020-09-07 13:04:50 +00:00
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
i, err := suite.account.MarshalYAML()
|
|
|
|
suite.Require().NoError(err)
|
2020-09-07 13:04:50 +00:00
|
|
|
|
|
|
|
var ok bool
|
|
|
|
accountStr, ok = i.(string)
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().True(ok)
|
2021-04-17 10:00:07 +00:00
|
|
|
suite.Require().Contains(accountStr, suite.account.Address)
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().Contains(accountStr, bech32pubkey)
|
2020-09-07 13:04:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
func (suite *AccountTestSuite) TestEthermintAccount_MarshalJSON() {
|
2021-04-17 10:00:07 +00:00
|
|
|
bz, err := json.Marshal(suite.account)
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().NoError(err)
|
2020-09-07 13:04:50 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
bz1, err := suite.account.MarshalJSON()
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().NoError(err)
|
2021-04-17 10:00:07 +00:00
|
|
|
suite.Require().Equal(string(bz1), string(bz))
|
|
|
|
|
|
|
|
var a *types.EthAccount
|
|
|
|
suite.Require().NoError(json.Unmarshal(bz, &a))
|
|
|
|
suite.Require().Equal(suite.account.String(), a.String())
|
2020-09-08 15:58:19 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
bech32pubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, suite.account.GetPubKey())
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().NoError(err)
|
2020-09-08 15:58:19 +00:00
|
|
|
|
|
|
|
// test that the sdk.AccAddress is populated from the hex address
|
|
|
|
jsonAcc := fmt.Sprintf(
|
2021-04-17 10:00:07 +00:00
|
|
|
`{"address":"","eth_address":"%s","public_key":"%s","account_number":10,"sequence":50,"code_hash":"0102"}`,
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.account.EthAddress().String(), bech32pubkey,
|
2020-09-08 15:58:19 +00:00
|
|
|
)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
res := new(types.EthAccount)
|
2020-09-08 15:58:19 +00:00
|
|
|
err = res.UnmarshalJSON([]byte(jsonAcc))
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().NoError(err)
|
2021-04-17 10:00:07 +00:00
|
|
|
suite.Require().Equal(suite.account.Address, res.Address)
|
2020-09-08 15:58:19 +00:00
|
|
|
|
|
|
|
jsonAcc = fmt.Sprintf(
|
2021-04-17 10:00:07 +00:00
|
|
|
`{"address":"","eth_address":"","public_key":"%s","account_number":10,"sequence":50,"code_hash":"0102"}`,
|
2020-09-08 15:58:19 +00:00
|
|
|
bech32pubkey,
|
|
|
|
)
|
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
res = new(types.EthAccount)
|
2020-09-08 15:58:19 +00:00
|
|
|
err = res.UnmarshalJSON([]byte(jsonAcc))
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().Error(err, "should fail if both address are empty")
|
2020-09-08 15:58:19 +00:00
|
|
|
|
|
|
|
// test that the sdk.AccAddress is populated from the hex address
|
|
|
|
jsonAcc = fmt.Sprintf(
|
2021-04-17 10:00:07 +00:00
|
|
|
`{"address": "%s","eth_address":"0x0000000000000000000000000000000000000000","public_key":"%s","account_number":10,"sequence":50,"code_hash":"0102"}`,
|
|
|
|
suite.account.Address, bech32pubkey,
|
2020-09-08 15:58:19 +00:00
|
|
|
)
|
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
res = new(types.EthAccount)
|
2020-09-08 15:58:19 +00:00
|
|
|
err = res.UnmarshalJSON([]byte(jsonAcc))
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().Error(err, "should fail if addresses mismatch")
|
2020-09-07 13:04:50 +00:00
|
|
|
}
|