2018-08-24 18:56:43 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
"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
|
|
|
|
2019-12-13 19:50:19 +00:00
|
|
|
tmamino "github.com/tendermint/tendermint/crypto/encoding/amino"
|
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.
|
|
|
|
func (acc EthAccount) Balance() sdk.Int {
|
|
|
|
return acc.GetCoins().AmountOf(DenomDefault)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBalance sets an account's balance of photons
|
|
|
|
func (acc *EthAccount) SetBalance(amt sdk.Int) {
|
|
|
|
coins := acc.GetCoins()
|
|
|
|
diff := amt.Sub(coins.AmountOf(DenomDefault))
|
|
|
|
switch {
|
|
|
|
case diff.IsPositive():
|
|
|
|
// Increase coins to amount
|
|
|
|
coins = coins.Add(sdk.NewCoin(DenomDefault, diff))
|
|
|
|
case diff.IsNegative():
|
|
|
|
// Decrease coins to amount
|
|
|
|
coins = coins.Sub(sdk.NewCoins(sdk.NewCoin(DenomDefault, diff.Neg())))
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := acc.SetCoins(coins); err != nil {
|
|
|
|
panic(fmt.Errorf("could not set coins for address %s: %w", acc.EthAddress().String(), err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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-08-23 21:41:54 +00:00
|
|
|
if acc.PubKey != nil {
|
|
|
|
alias.PubKey = acc.PubKey.Bytes()
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-12-13 19:50:19 +00:00
|
|
|
alias := ethermintAccountPretty{
|
|
|
|
Address: acc.Address,
|
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-08-23 21:41:54 +00:00
|
|
|
if acc.PubKey != nil {
|
|
|
|
alias.PubKey = acc.PubKey.Bytes()
|
|
|
|
}
|
|
|
|
|
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 {
|
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 {
|
2020-08-23 21:41:54 +00:00
|
|
|
pubKey, err := tmamino.PubKeyFromBytes(alias.PubKey)
|
2019-12-13 19:50:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
acc.BaseAccount.PubKey = pubKey
|
2019-12-13 19:50:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
acc.BaseAccount.Coins = alias.Coins
|
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
|
|
|
|
}
|