2022-12-16 09:48:38 +00:00
|
|
|
// Copyright 2021 Evmos Foundation
|
|
|
|
// This file is part of Evmos' Ethermint library.
|
|
|
|
//
|
|
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
2018-08-24 18:56:43 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2022-01-03 16:18:13 +00:00
|
|
|
"bytes"
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
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
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-09-03 18:06:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2018-08-24 18:56:43 +00:00
|
|
|
)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
var (
|
2022-10-17 11:44:21 +00:00
|
|
|
_ authtypes.AccountI = (*EthAccount)(nil) //nolint: all
|
|
|
|
_ EthAccountI = (*EthAccount)(nil) //nolint: all
|
|
|
|
_ authtypes.GenesisAccount = (*EthAccount)(nil) //nolint: all
|
|
|
|
_ codectypes.UnpackInterfacesMessage = (*EthAccount)(nil) //nolint: all
|
2021-04-17 10:00:07 +00:00
|
|
|
)
|
2020-08-23 21:41:54 +00:00
|
|
|
|
2022-01-03 16:18:13 +00:00
|
|
|
var emptyCodeHash = crypto.Keccak256(nil)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// AccountTypeEOA defines the type for externally owned accounts (EOAs)
|
|
|
|
AccountTypeEOA = int8(iota + 1)
|
|
|
|
// AccountTypeContract defines the type for contract accounts
|
|
|
|
AccountTypeContract
|
|
|
|
)
|
|
|
|
|
2022-01-05 18:18:02 +00:00
|
|
|
// EthAccountI represents the interface of an EVM compatible account
|
|
|
|
type EthAccountI interface {
|
|
|
|
authtypes.AccountI
|
|
|
|
// EthAddress returns the ethereum Address representation of the AccAddress
|
|
|
|
EthAddress() common.Address
|
|
|
|
// CodeHash is the keccak256 hash of the contract code (if any)
|
|
|
|
GetCodeHash() common.Hash
|
|
|
|
// SetCodeHash sets the code hash to the account fields
|
|
|
|
SetCodeHash(code common.Hash) error
|
|
|
|
// Type returns the type of Ethereum Account (EOA or Contract)
|
|
|
|
Type() int8
|
|
|
|
}
|
|
|
|
|
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{},
|
2022-01-03 16:18:13 +00:00
|
|
|
CodeHash: common.BytesToHash(emptyCodeHash).String(),
|
2019-11-12 21:07:34 +00:00
|
|
|
}
|
2018-08-24 18:56:43 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
// GetBaseAccount returns base account.
|
|
|
|
func (acc EthAccount) GetBaseAccount() *authtypes.BaseAccount {
|
|
|
|
return acc.BaseAccount
|
|
|
|
}
|
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
// EthAddress returns the account address ethereum format.
|
2021-06-29 17:02:21 +00:00
|
|
|
func (acc EthAccount) EthAddress() common.Address {
|
|
|
|
return common.BytesToAddress(acc.GetAddress().Bytes())
|
2019-12-13 19:50:19 +00:00
|
|
|
}
|
2020-09-07 13:04:50 +00:00
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
// GetCodeHash returns the account code hash in byte format
|
|
|
|
func (acc EthAccount) GetCodeHash() common.Hash {
|
|
|
|
return common.HexToHash(acc.CodeHash)
|
2020-09-07 13:04:50 +00:00
|
|
|
}
|
2022-01-03 16:18:13 +00:00
|
|
|
|
2022-01-05 18:18:02 +00:00
|
|
|
// SetCodeHash sets the account code hash to the EthAccount fields
|
|
|
|
func (acc *EthAccount) SetCodeHash(codeHash common.Hash) error {
|
|
|
|
acc.CodeHash = codeHash.Hex()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-03 16:18:13 +00:00
|
|
|
// Type returns the type of Ethereum Account (EOA or Contract)
|
|
|
|
func (acc EthAccount) Type() int8 {
|
2022-10-10 10:38:33 +00:00
|
|
|
if bytes.Equal(emptyCodeHash, common.HexToHash(acc.CodeHash).Bytes()) {
|
2022-01-03 16:18:13 +00:00
|
|
|
return AccountTypeEOA
|
|
|
|
}
|
|
|
|
return AccountTypeContract
|
|
|
|
}
|