2018-08-24 18:56:43 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2020-09-08 15:58:19 +00:00
|
|
|
"bytes"
|
2019-12-13 19:50:19 +00:00
|
|
|
"encoding/json"
|
2018-10-03 00:22:15 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2020-07-02 15:19:48 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2018-08-24 18:56:43 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-09-08 15:58:19 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2019-11-13 17:00:21 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2020-07-02 15:19:48 +00:00
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
var (
|
|
|
|
_ authtypes.AccountI = (*EthAccount)(nil)
|
|
|
|
_ authtypes.GenesisAccount = (*EthAccount)(nil)
|
|
|
|
_ codectypes.UnpackInterfacesMessage = (*EthAccount)(nil)
|
|
|
|
)
|
2020-08-23 21:41:54 +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.
|
2021-04-17 10:00:07 +00:00
|
|
|
func ProtoAccount() authtypes.AccountI {
|
2020-04-22 19:26:01 +00:00
|
|
|
return &EthAccount{
|
2021-04-17 10:00:07 +00:00
|
|
|
BaseAccount: &authtypes.BaseAccount{},
|
2020-04-22 19:26:01 +00:00
|
|
|
CodeHash: ethcrypto.Keccak256(nil),
|
2019-11-12 21:07:34 +00:00
|
|
|
}
|
2018-08-24 18:56:43 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
// EthAddress returns the account address ethereum format.
|
|
|
|
func (acc EthAccount) EthAddress() ethcmn.Address {
|
2021-04-17 10:00:07 +00:00
|
|
|
return ethcmn.BytesToAddress(acc.GetAddress().Bytes())
|
2020-08-23 21:41:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 19:50:19 +00:00
|
|
|
type ethermintAccountPretty struct {
|
2021-04-17 10:00:07 +00:00
|
|
|
Address string `json:"address" yaml:"address"`
|
|
|
|
EthAddress string `json:"eth_address" yaml:"eth_address"`
|
|
|
|
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"`
|
2019-12-13 19:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-09-08 13:57:49 +00:00
|
|
|
EthAddress: acc.EthAddress().String(),
|
2019-12-13 19:50:19 +00:00
|
|
|
AccountNumber: acc.AccountNumber,
|
|
|
|
Sequence: acc.Sequence,
|
|
|
|
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
|
|
|
|
}
|
|
|
|
|
2020-09-07 13:04:50 +00:00
|
|
|
var err error
|
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
if acc.PubKey != nil {
|
2021-04-17 10:00:07 +00:00
|
|
|
alias.PubKey, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.GetPubKey())
|
2020-09-07 13:04:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-23 21:41:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 19:50:19 +00:00
|
|
|
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) {
|
2020-09-08 15:58:19 +00:00
|
|
|
var ethAddress = ""
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
if acc.BaseAccount != nil && acc.Address != "" {
|
2020-09-08 15:58:19 +00:00
|
|
|
ethAddress = acc.EthAddress().String()
|
|
|
|
}
|
|
|
|
|
2019-12-13 19:50:19 +00:00
|
|
|
alias := ethermintAccountPretty{
|
|
|
|
Address: acc.Address,
|
2020-09-08 15:58:19 +00:00
|
|
|
EthAddress: ethAddress,
|
2019-12-13 19:50:19 +00:00
|
|
|
AccountNumber: acc.AccountNumber,
|
|
|
|
Sequence: acc.Sequence,
|
|
|
|
CodeHash: ethcmn.Bytes2Hex(acc.CodeHash),
|
|
|
|
}
|
|
|
|
|
2020-09-07 13:04:50 +00:00
|
|
|
var err error
|
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
if acc.PubKey != nil {
|
2021-04-17 10:00:07 +00:00
|
|
|
alias.PubKey, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.GetPubKey())
|
2020-09-07 13:04:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-23 21:41:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 19:50:19 +00:00
|
|
|
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 {
|
2020-09-07 13:04:50 +00:00
|
|
|
var (
|
|
|
|
alias ethermintAccountPretty
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2019-12-13 19:50:19 +00:00
|
|
|
if err := json.Unmarshal(bz, &alias); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-08 15:58:19 +00:00
|
|
|
switch {
|
2021-04-17 10:00:07 +00:00
|
|
|
case alias.Address != "" && alias.EthAddress != "":
|
2020-09-08 15:58:19 +00:00
|
|
|
// Both addresses provided. Verify correctness
|
|
|
|
ethAddress := ethcmn.HexToAddress(alias.EthAddress)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
var address sdk.AccAddress
|
|
|
|
address, err = sdk.AccAddressFromBech32(alias.Address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ethAddressFromAccAddress := ethcmn.BytesToAddress(address.Bytes())
|
|
|
|
|
|
|
|
if !bytes.Equal(ethAddress.Bytes(), address.Bytes()) {
|
2020-09-08 15:58:19 +00:00
|
|
|
err = sdkerrors.Wrapf(
|
|
|
|
sdkerrors.ErrInvalidAddress,
|
|
|
|
"expected %s, got %s",
|
|
|
|
ethAddressFromAccAddress.String(), ethAddress.String(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
case alias.Address != "" && alias.EthAddress == "":
|
2020-09-08 15:58:19 +00:00
|
|
|
// unmarshal sdk.AccAddress only. Do nothing here
|
2021-04-17 10:00:07 +00:00
|
|
|
case alias.Address == "" && alias.EthAddress != "":
|
2020-09-08 15:58:19 +00:00
|
|
|
// retrieve sdk.AccAddress from ethereum address
|
|
|
|
ethAddress := ethcmn.HexToAddress(alias.EthAddress)
|
2021-04-17 10:00:07 +00:00
|
|
|
alias.Address = sdk.AccAddress(ethAddress.Bytes()).String()
|
|
|
|
case alias.Address == "" && alias.EthAddress == "":
|
2020-09-08 15:58:19 +00:00
|
|
|
err = sdkerrors.Wrapf(
|
|
|
|
sdkerrors.ErrInvalidAddress,
|
|
|
|
"account must contain address in Ethereum Hex or Cosmos Bech32 format",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-07 13:04:50 +00:00
|
|
|
acc.BaseAccount = &authtypes.BaseAccount{
|
|
|
|
Address: alias.Address,
|
|
|
|
AccountNumber: alias.AccountNumber,
|
|
|
|
Sequence: alias.Sequence,
|
|
|
|
}
|
|
|
|
|
|
|
|
if alias.PubKey != "" {
|
2021-04-17 10:00:07 +00:00
|
|
|
pubkey, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey)
|
2019-12-13 19:50:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
if err := acc.SetPubKey(pubkey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-13 19:50:19 +00:00
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
acc.CodeHash = ethcmn.HexToHash(alias.CodeHash).Bytes()
|
|
|
|
|
2019-12-13 19:50:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-09-07 13:04:50 +00:00
|
|
|
|
|
|
|
// String implements the fmt.Stringer interface
|
|
|
|
func (acc EthAccount) String() string {
|
|
|
|
out, _ := yaml.Marshal(acc)
|
|
|
|
return string(out)
|
|
|
|
}
|