2019-06-05 16:26:17 -07:00
|
|
|
package types
|
2018-05-23 19:26:54 -07:00
|
|
|
|
|
|
|
import (
|
2019-09-12 12:22:25 -07:00
|
|
|
"bytes"
|
2020-02-25 12:22:28 -08:00
|
|
|
"encoding/json"
|
2018-05-23 22:09:01 -07:00
|
|
|
"errors"
|
2020-04-20 12:32:10 -07:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2018-05-23 22:09:01 -07:00
|
|
|
|
2020-08-17 05:47:31 -07:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-12-10 06:27:25 -08:00
|
|
|
"github.com/tendermint/tendermint/crypto"
|
2020-09-25 03:25:37 -07:00
|
|
|
"gopkg.in/yaml.v2"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
2020-09-25 01:41:16 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2018-05-23 19:26:54 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-09-25 01:41:16 -07:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2018-05-23 19:26:54 -07:00
|
|
|
)
|
|
|
|
|
2020-04-20 12:32:10 -07:00
|
|
|
var (
|
2020-05-20 12:21:00 -07:00
|
|
|
_ AccountI = (*BaseAccount)(nil)
|
|
|
|
_ GenesisAccount = (*BaseAccount)(nil)
|
|
|
|
_ GenesisAccount = (*ModuleAccount)(nil)
|
|
|
|
_ ModuleAccountI = (*ModuleAccount)(nil)
|
2020-04-20 12:32:10 -07:00
|
|
|
)
|
2018-05-23 22:09:01 -07:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
// NewBaseAccount creates a new BaseAccount object
|
2020-09-25 03:25:37 -07:00
|
|
|
//nolint:interfacer
|
2020-01-30 13:31:16 -08:00
|
|
|
func NewBaseAccount(address sdk.AccAddress, pubKey crypto.PubKey, accountNumber, sequence uint64) *BaseAccount {
|
2020-02-20 08:15:22 -08:00
|
|
|
acc := &BaseAccount{
|
2020-09-25 03:25:37 -07:00
|
|
|
Address: address.String(),
|
2019-05-16 08:25:32 -07:00
|
|
|
AccountNumber: accountNumber,
|
|
|
|
Sequence: sequence,
|
|
|
|
}
|
2020-02-20 08:15:22 -08:00
|
|
|
|
2020-09-25 01:41:16 -07:00
|
|
|
err := acc.SetPubKey(pubKey)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-02-20 08:15:22 -08:00
|
|
|
return acc
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// ProtoBaseAccount - a prototype function for BaseAccount
|
2020-05-20 12:21:00 -07:00
|
|
|
func ProtoBaseAccount() AccountI {
|
2018-06-25 19:05:47 -07:00
|
|
|
return &BaseAccount{}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// NewBaseAccountWithAddress - returns a new base account with a given address
|
2020-02-18 04:50:13 -08:00
|
|
|
func NewBaseAccountWithAddress(addr sdk.AccAddress) *BaseAccount {
|
|
|
|
return &BaseAccount{
|
2020-09-25 03:25:37 -07:00
|
|
|
Address: addr.String(),
|
2018-05-23 22:09:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:21:00 -07:00
|
|
|
// GetAddress - Implements sdk.AccountI.
|
2018-07-06 00:06:53 -07:00
|
|
|
func (acc BaseAccount) GetAddress() sdk.AccAddress {
|
2020-09-25 03:25:37 -07:00
|
|
|
addr, _ := sdk.AccAddressFromBech32(acc.Address)
|
|
|
|
return addr
|
2018-05-23 22:09:01 -07:00
|
|
|
}
|
|
|
|
|
2020-05-20 12:21:00 -07:00
|
|
|
// SetAddress - Implements sdk.AccountI.
|
2018-07-06 00:06:53 -07:00
|
|
|
func (acc *BaseAccount) SetAddress(addr sdk.AccAddress) error {
|
2018-05-23 22:09:01 -07:00
|
|
|
if len(acc.Address) != 0 {
|
|
|
|
return errors.New("cannot override BaseAccount address")
|
|
|
|
}
|
2020-02-18 04:50:13 -08:00
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
acc.Address = addr.String()
|
2018-05-23 22:09:01 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:21:00 -07:00
|
|
|
// GetPubKey - Implements sdk.AccountI.
|
2020-02-20 08:15:22 -08:00
|
|
|
func (acc BaseAccount) GetPubKey() (pk crypto.PubKey) {
|
2020-09-25 01:41:16 -07:00
|
|
|
if acc.PubKey == nil {
|
2020-02-18 04:50:13 -08:00
|
|
|
return nil
|
|
|
|
}
|
2020-09-25 01:41:16 -07:00
|
|
|
content, ok := acc.PubKey.GetCachedValue().(crypto.PubKey)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return content
|
2018-05-23 22:09:01 -07:00
|
|
|
}
|
|
|
|
|
2020-05-20 12:21:00 -07:00
|
|
|
// SetPubKey - Implements sdk.AccountI.
|
2018-05-23 22:09:01 -07:00
|
|
|
func (acc *BaseAccount) SetPubKey(pubKey crypto.PubKey) error {
|
2020-02-20 08:15:22 -08:00
|
|
|
if pubKey == nil {
|
|
|
|
acc.PubKey = nil
|
|
|
|
} else {
|
2020-09-25 01:41:16 -07:00
|
|
|
protoMsg, ok := pubKey.(proto.Message)
|
|
|
|
if !ok {
|
|
|
|
return sdkerrors.ErrInvalidPubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
any, err := codectypes.NewAnyWithValue(protoMsg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.PubKey = any
|
2020-02-18 04:50:13 -08:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:09:01 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:21:00 -07:00
|
|
|
// GetAccountNumber - Implements AccountI
|
2020-02-18 04:50:13 -08:00
|
|
|
func (acc BaseAccount) GetAccountNumber() uint64 {
|
2018-06-11 19:30:54 -07:00
|
|
|
return acc.AccountNumber
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:21:00 -07:00
|
|
|
// SetAccountNumber - Implements AccountI
|
2018-11-26 03:29:21 -08:00
|
|
|
func (acc *BaseAccount) SetAccountNumber(accNumber uint64) error {
|
2018-06-11 19:30:54 -07:00
|
|
|
acc.AccountNumber = accNumber
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:21:00 -07:00
|
|
|
// GetSequence - Implements sdk.AccountI.
|
2020-02-18 04:50:13 -08:00
|
|
|
func (acc BaseAccount) GetSequence() uint64 {
|
2018-05-23 22:09:01 -07:00
|
|
|
return acc.Sequence
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:21:00 -07:00
|
|
|
// SetSequence - Implements sdk.AccountI.
|
2018-11-26 03:29:21 -08:00
|
|
|
func (acc *BaseAccount) SetSequence(seq uint64) error {
|
2018-05-23 22:09:01 -07:00
|
|
|
acc.Sequence = seq
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-06 12:06:26 -08:00
|
|
|
// Validate checks for errors on the account fields
|
|
|
|
func (acc BaseAccount) Validate() error {
|
2020-09-25 03:25:37 -07:00
|
|
|
if acc.Address == "" || acc.PubKey == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
accAddr, err := sdk.AccAddressFromBech32(acc.Address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(acc.GetPubKey().Address().Bytes(), accAddr.Bytes()) {
|
2020-02-20 08:15:22 -08:00
|
|
|
return errors.New("account address and pubkey address do not match")
|
2019-11-06 12:06:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (acc BaseAccount) String() string {
|
2020-02-20 08:15:22 -08:00
|
|
|
out, _ := acc.MarshalYAML()
|
|
|
|
return out.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML returns the YAML representation of an account.
|
|
|
|
func (acc BaseAccount) MarshalYAML() (interface{}, error) {
|
2020-09-25 01:41:16 -07:00
|
|
|
bz, err := codec.MarshalYAML(codec.NewProtoCodec(codectypes.NewInterfaceRegistry()), &acc)
|
2020-02-20 08:15:22 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return string(bz), err
|
2019-09-12 12:22:25 -07:00
|
|
|
}
|
2020-02-25 12:22:28 -08:00
|
|
|
|
2020-09-25 01:41:16 -07:00
|
|
|
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
|
|
|
|
func (acc BaseAccount) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
|
|
|
|
if acc.PubKey == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var pubKey crypto.PubKey
|
|
|
|
return unpacker.UnpackAny(acc.PubKey, &pubKey)
|
|
|
|
}
|
|
|
|
|
2020-04-20 12:32:10 -07:00
|
|
|
// NewModuleAddress creates an AccAddress from the hash of the module's name
|
|
|
|
func NewModuleAddress(name string) sdk.AccAddress {
|
|
|
|
return sdk.AccAddress(crypto.AddressHash([]byte(name)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEmptyModuleAccount creates a empty ModuleAccount from a string
|
|
|
|
func NewEmptyModuleAccount(name string, permissions ...string) *ModuleAccount {
|
|
|
|
moduleAddress := NewModuleAddress(name)
|
|
|
|
baseAcc := NewBaseAccountWithAddress(moduleAddress)
|
|
|
|
|
|
|
|
if err := validatePermissions(permissions...); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ModuleAccount{
|
|
|
|
BaseAccount: baseAcc,
|
|
|
|
Name: name,
|
|
|
|
Permissions: permissions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewModuleAccount creates a new ModuleAccount instance
|
|
|
|
func NewModuleAccount(ba *BaseAccount, name string, permissions ...string) *ModuleAccount {
|
|
|
|
if err := validatePermissions(permissions...); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ModuleAccount{
|
|
|
|
BaseAccount: ba,
|
|
|
|
Name: name,
|
|
|
|
Permissions: permissions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasPermission returns whether or not the module account has permission.
|
|
|
|
func (ma ModuleAccount) HasPermission(permission string) bool {
|
|
|
|
for _, perm := range ma.Permissions {
|
|
|
|
if perm == permission {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetName returns the the name of the holder's module
|
|
|
|
func (ma ModuleAccount) GetName() string {
|
|
|
|
return ma.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPermissions returns permissions granted to the module account
|
|
|
|
func (ma ModuleAccount) GetPermissions() []string {
|
|
|
|
return ma.Permissions
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:21:00 -07:00
|
|
|
// SetPubKey - Implements AccountI
|
2020-04-20 12:32:10 -07:00
|
|
|
func (ma ModuleAccount) SetPubKey(pubKey crypto.PubKey) error {
|
|
|
|
return fmt.Errorf("not supported for module accounts")
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:21:00 -07:00
|
|
|
// SetSequence - Implements AccountI
|
2020-04-20 12:32:10 -07:00
|
|
|
func (ma ModuleAccount) SetSequence(seq uint64) error {
|
|
|
|
return fmt.Errorf("not supported for module accounts")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate checks for errors on the account fields
|
|
|
|
func (ma ModuleAccount) Validate() error {
|
|
|
|
if strings.TrimSpace(ma.Name) == "" {
|
|
|
|
return errors.New("module account name cannot be blank")
|
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
if ma.Address != sdk.AccAddress(crypto.AddressHash([]byte(ma.Name))).String() {
|
2020-04-20 12:32:10 -07:00
|
|
|
return fmt.Errorf("address %s cannot be derived from the module name '%s'", ma.Address, ma.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ma.BaseAccount.Validate()
|
|
|
|
}
|
|
|
|
|
|
|
|
type moduleAccountPretty struct {
|
|
|
|
Address sdk.AccAddress `json:"address" yaml:"address"`
|
|
|
|
PubKey string `json:"public_key" yaml:"public_key"`
|
|
|
|
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
|
|
|
|
Sequence uint64 `json:"sequence" yaml:"sequence"`
|
|
|
|
Name string `json:"name" yaml:"name"`
|
|
|
|
Permissions []string `json:"permissions" yaml:"permissions"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ma ModuleAccount) String() string {
|
|
|
|
out, _ := ma.MarshalYAML()
|
|
|
|
return out.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML returns the YAML representation of a ModuleAccount.
|
|
|
|
func (ma ModuleAccount) MarshalYAML() (interface{}, error) {
|
2020-09-25 03:25:37 -07:00
|
|
|
accAddr, err := sdk.AccAddressFromBech32(ma.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-20 12:32:10 -07:00
|
|
|
bs, err := yaml.Marshal(moduleAccountPretty{
|
2020-09-25 03:25:37 -07:00
|
|
|
Address: accAddr,
|
2020-04-20 12:32:10 -07:00
|
|
|
PubKey: "",
|
|
|
|
AccountNumber: ma.AccountNumber,
|
|
|
|
Sequence: ma.Sequence,
|
|
|
|
Name: ma.Name,
|
|
|
|
Permissions: ma.Permissions,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(bs), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON representation of a ModuleAccount.
|
|
|
|
func (ma ModuleAccount) MarshalJSON() ([]byte, error) {
|
2020-09-25 03:25:37 -07:00
|
|
|
accAddr, err := sdk.AccAddressFromBech32(ma.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-20 12:32:10 -07:00
|
|
|
return json.Marshal(moduleAccountPretty{
|
2020-09-25 03:25:37 -07:00
|
|
|
Address: accAddr,
|
2020-04-20 12:32:10 -07:00
|
|
|
PubKey: "",
|
|
|
|
AccountNumber: ma.AccountNumber,
|
|
|
|
Sequence: ma.Sequence,
|
|
|
|
Name: ma.Name,
|
|
|
|
Permissions: ma.Permissions,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals raw JSON bytes into a ModuleAccount.
|
|
|
|
func (ma *ModuleAccount) UnmarshalJSON(bz []byte) error {
|
|
|
|
var alias moduleAccountPretty
|
|
|
|
if err := json.Unmarshal(bz, &alias); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ma.BaseAccount = NewBaseAccount(alias.Address, nil, alias.AccountNumber, alias.Sequence)
|
|
|
|
ma.Name = alias.Name
|
|
|
|
ma.Permissions = alias.Permissions
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-20 12:21:00 -07:00
|
|
|
|
|
|
|
// AccountI is an interface used to store coins at a given address within state.
|
|
|
|
// It presumes a notion of sequence numbers for replay protection,
|
|
|
|
// a notion of account numbers for replay protection for previously pruned accounts,
|
|
|
|
// and a pubkey for authentication purposes.
|
|
|
|
//
|
|
|
|
// Many complex conditions can be used in the concrete struct which implements AccountI.
|
|
|
|
type AccountI interface {
|
2020-08-17 05:47:31 -07:00
|
|
|
proto.Message
|
|
|
|
|
2020-05-20 12:21:00 -07:00
|
|
|
GetAddress() sdk.AccAddress
|
|
|
|
SetAddress(sdk.AccAddress) error // errors if already set.
|
|
|
|
|
|
|
|
GetPubKey() crypto.PubKey // can return nil.
|
|
|
|
SetPubKey(crypto.PubKey) error
|
|
|
|
|
|
|
|
GetAccountNumber() uint64
|
|
|
|
SetAccountNumber(uint64) error
|
|
|
|
|
|
|
|
GetSequence() uint64
|
|
|
|
SetSequence(uint64) error
|
|
|
|
|
|
|
|
// Ensure that account implements stringer
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModuleAccountI defines an account interface for modules that hold tokens in
|
|
|
|
// an escrow.
|
|
|
|
type ModuleAccountI interface {
|
|
|
|
AccountI
|
|
|
|
|
|
|
|
GetName() string
|
|
|
|
GetPermissions() []string
|
|
|
|
HasPermission(string) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenesisAccounts defines a slice of GenesisAccount objects
|
|
|
|
type GenesisAccounts []GenesisAccount
|
|
|
|
|
|
|
|
// Contains returns true if the given address exists in a slice of GenesisAccount
|
|
|
|
// objects.
|
|
|
|
func (ga GenesisAccounts) Contains(addr sdk.Address) bool {
|
|
|
|
for _, acc := range ga {
|
|
|
|
if acc.GetAddress().Equals(addr) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenesisAccount defines a genesis account that embeds an AccountI with validation capabilities.
|
|
|
|
type GenesisAccount interface {
|
|
|
|
AccountI
|
|
|
|
|
|
|
|
Validate() error
|
|
|
|
}
|