2018-08-24 18:56:43 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2019-12-13 19:50:19 +00:00
|
|
|
"encoding/json"
|
2018-10-03 00:22:15 +00:00
|
|
|
|
2018-08-24 18:56:43 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2019-11-04 20:45:02 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/exported"
|
2019-11-13 17:00:21 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2019-12-13 19:50:19 +00:00
|
|
|
tmamino "github.com/tendermint/tendermint/crypto/encoding/amino"
|
|
|
|
"gopkg.in/yaml.v2"
|
2018-08-24 18:56:43 +00:00
|
|
|
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
2020-04-22 19:26:01 +00:00
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
2018-08-24 18:56:43 +00:00
|
|
|
)
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
var _ exported.Account = (*EthAccount)(nil)
|
|
|
|
var _ exported.GenesisAccount = (*EthAccount)(nil)
|
2018-10-24 13:46:26 +00:00
|
|
|
|
2018-10-03 14:57:02 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Main Ethermint account
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
// ProtoAccount defines the prototype function for BaseAccount used for an
|
|
|
|
// AccountKeeper.
|
|
|
|
func ProtoAccount() exported.Account {
|
|
|
|
return &EthAccount{
|
|
|
|
BaseAccount: &auth.BaseAccount{},
|
|
|
|
CodeHash: ethcrypto.Keccak256(nil),
|
2019-11-12 21:07:34 +00:00
|
|
|
}
|
2018-08-24 18:56:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 19:50:19 +00:00
|
|
|
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"`
|
|
|
|
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
|
|
|
|
Sequence uint64 `json:"sequence" yaml:"sequence"`
|
|
|
|
CodeHash string `json:"code_hash" yaml:"code_hash"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML returns the YAML representation of an account.
|
2020-04-22 19:26:01 +00:00
|
|
|
func (acc EthAccount) MarshalYAML() (interface{}, error) {
|
2019-12-13 19:50:19 +00:00
|
|
|
alias := ethermintAccountPretty{
|
|
|
|
Address: acc.Address,
|
2020-04-22 19:26:01 +00:00
|
|
|
PubKey: acc.PubKey,
|
2019-12-13 19:50:19 +00:00
|
|
|
AccountNumber: acc.AccountNumber,
|
|
|
|
Sequence: acc.Sequence,
|
|
|
|
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := yaml.Marshal(alias)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(bz), err
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
// MarshalJSON returns the JSON representation of an EthAccount.
|
|
|
|
func (acc EthAccount) MarshalJSON() ([]byte, error) {
|
2019-12-13 19:50:19 +00:00
|
|
|
alias := ethermintAccountPretty{
|
|
|
|
Address: acc.Address,
|
2020-04-22 19:26:01 +00:00
|
|
|
PubKey: acc.PubKey,
|
2019-12-13 19:50:19 +00:00
|
|
|
AccountNumber: acc.AccountNumber,
|
|
|
|
Sequence: acc.Sequence,
|
|
|
|
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(alias)
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
// UnmarshalJSON unmarshals raw JSON bytes into an EthAccount.
|
|
|
|
func (acc *EthAccount) UnmarshalJSON(bz []byte) error {
|
2019-12-13 19:50:19 +00:00
|
|
|
acc.BaseAccount = &authtypes.BaseAccount{}
|
|
|
|
var alias ethermintAccountPretty
|
|
|
|
if err := json.Unmarshal(bz, &alias); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if alias.PubKey != nil {
|
|
|
|
pubk, err := tmamino.PubKeyFromBytes(alias.PubKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
acc.BaseAccount.PubKey = pubk.Bytes()
|
2019-12-13 19:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
acc.BaseAccount.Address = alias.Address
|
|
|
|
acc.BaseAccount.AccountNumber = alias.AccountNumber
|
|
|
|
acc.BaseAccount.Sequence = alias.Sequence
|
|
|
|
acc.CodeHash = ethcmn.Hex2Bytes(alias.CodeHash)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|