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"
|
2020-08-23 21:41:54 +00:00
|
|
|
"fmt"
|
2018-10-03 00:22:15 +00:00
|
|
|
|
2020-07-02 15:19:48 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
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"
|
2018-08-24 18:56:43 +00:00
|
|
|
"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"
|
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
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
func init() {
|
|
|
|
authtypes.RegisterAccountTypeCodec(&EthAccount{}, EthAccountName)
|
|
|
|
}
|
|
|
|
|
2018-10-03 14:57:02 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Main Ethermint account
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
// EthAccount implements the auth.Account interface and embeds an
|
|
|
|
// auth.BaseAccount type. It is compatible with the auth.AccountKeeper.
|
|
|
|
type EthAccount struct {
|
|
|
|
*authtypes.BaseAccount `json:"base_account" yaml:"base_account"`
|
|
|
|
CodeHash []byte `json:"code_hash" yaml:"code_hash"`
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
// EthAddress returns the account address ethereum format.
|
|
|
|
func (acc EthAccount) EthAddress() ethcmn.Address {
|
|
|
|
return ethcmn.BytesToAddress(acc.Address.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove on SDK v0.40
|
|
|
|
|
|
|
|
// Balance returns the balance of an account.
|
2020-09-09 13:53:14 +00:00
|
|
|
func (acc EthAccount) Balance(denom string) sdk.Int {
|
|
|
|
return acc.GetCoins().AmountOf(denom)
|
2020-08-23 21:41:54 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
// SetBalance sets an account's balance of the given coin denomination.
|
|
|
|
//
|
|
|
|
// CONTRACT: assumes the denomination is valid.
|
|
|
|
func (acc *EthAccount) SetBalance(denom string, amt sdk.Int) {
|
2020-08-23 21:41:54 +00:00
|
|
|
coins := acc.GetCoins()
|
2020-09-09 13:53:14 +00:00
|
|
|
diff := amt.Sub(coins.AmountOf(denom))
|
2020-08-23 21:41:54 +00:00
|
|
|
switch {
|
|
|
|
case diff.IsPositive():
|
|
|
|
// Increase coins to amount
|
2020-09-09 13:53:14 +00:00
|
|
|
coins = coins.Add(sdk.NewCoin(denom, diff))
|
2020-08-23 21:41:54 +00:00
|
|
|
case diff.IsNegative():
|
|
|
|
// Decrease coins to amount
|
2020-09-09 13:53:14 +00:00
|
|
|
coins = coins.Sub(sdk.NewCoins(sdk.NewCoin(denom, diff.Neg())))
|
2020-08-23 21:41:54 +00:00
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := acc.SetCoins(coins); err != nil {
|
2020-09-09 13:53:14 +00:00
|
|
|
panic(fmt.Errorf("could not set %s coins for address %s: %w", denom, acc.EthAddress().String(), err))
|
2020-08-23 21:41:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 19:50:19 +00:00
|
|
|
type ethermintAccountPretty struct {
|
|
|
|
Address sdk.AccAddress `json:"address" yaml:"address"`
|
2020-09-08 13:57:49 +00:00
|
|
|
EthAddress string `json:"eth_address" yaml:"eth_address"`
|
2019-12-13 19:50:19 +00:00
|
|
|
Coins sdk.Coins `json:"coins" yaml:"coins"`
|
2020-09-07 13:04:50 +00:00
|
|
|
PubKey string `json:"public_key" yaml:"public_key"`
|
2019-12-13 19:50:19 +00:00
|
|
|
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-09-08 13:57:49 +00:00
|
|
|
EthAddress: acc.EthAddress().String(),
|
2020-08-23 21:41:54 +00:00
|
|
|
Coins: acc.Coins,
|
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 {
|
2020-09-07 13:04:50 +00:00
|
|
|
alias.PubKey, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.PubKey)
|
|
|
|
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 = ""
|
|
|
|
|
|
|
|
if acc.BaseAccount != nil && acc.Address != nil {
|
|
|
|
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,
|
2020-08-23 21:41:54 +00:00
|
|
|
Coins: acc.Coins,
|
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 {
|
2020-09-07 13:04:50 +00:00
|
|
|
alias.PubKey, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.PubKey)
|
|
|
|
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 {
|
|
|
|
case !alias.Address.Empty() && alias.EthAddress != "":
|
|
|
|
// Both addresses provided. Verify correctness
|
|
|
|
ethAddress := ethcmn.HexToAddress(alias.EthAddress)
|
|
|
|
ethAddressFromAccAddress := ethcmn.BytesToAddress(alias.Address.Bytes())
|
|
|
|
|
|
|
|
if !bytes.Equal(ethAddress.Bytes(), alias.Address.Bytes()) {
|
|
|
|
err = sdkerrors.Wrapf(
|
|
|
|
sdkerrors.ErrInvalidAddress,
|
|
|
|
"expected %s, got %s",
|
|
|
|
ethAddressFromAccAddress.String(), ethAddress.String(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
case !alias.Address.Empty() && alias.EthAddress == "":
|
|
|
|
// unmarshal sdk.AccAddress only. Do nothing here
|
|
|
|
case alias.Address.Empty() && alias.EthAddress != "":
|
|
|
|
// retrieve sdk.AccAddress from ethereum address
|
|
|
|
ethAddress := ethcmn.HexToAddress(alias.EthAddress)
|
|
|
|
alias.Address = sdk.AccAddress(ethAddress.Bytes())
|
|
|
|
case alias.Address.Empty() && alias.EthAddress == "":
|
|
|
|
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{
|
|
|
|
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)
|
2019-12-13 19:50:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|