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
|
|
|
|
2018-12-10 06:27:25 -08:00
|
|
|
"github.com/tendermint/tendermint/crypto"
|
2019-06-18 09:03:16 -07:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
2018-05-23 19:26:54 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-06-24 13:45:09 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/exported"
|
2018-05-23 19:26:54 -07:00
|
|
|
)
|
|
|
|
|
2020-04-20 12:32:10 -07:00
|
|
|
var (
|
|
|
|
_ exported.Account = (*BaseAccount)(nil)
|
|
|
|
_ exported.GenesisAccount = (*BaseAccount)(nil)
|
|
|
|
_ exported.GenesisAccount = (*ModuleAccount)(nil)
|
|
|
|
_ exported.ModuleAccountI = (*ModuleAccount)(nil)
|
|
|
|
)
|
2018-05-23 22:09:01 -07:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
// NewBaseAccount creates a new BaseAccount object
|
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{
|
2019-05-16 08:25:32 -07:00
|
|
|
Address: address,
|
|
|
|
AccountNumber: accountNumber,
|
|
|
|
Sequence: sequence,
|
|
|
|
}
|
2020-02-20 08:15:22 -08:00
|
|
|
|
|
|
|
acc.SetPubKey(pubKey)
|
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
|
2019-06-24 13:45:09 -07:00
|
|
|
func ProtoBaseAccount() exported.Account {
|
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{
|
2018-05-23 22:09:01 -07:00
|
|
|
Address: addr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// GetAddress - Implements sdk.Account.
|
2018-07-06 00:06:53 -07:00
|
|
|
func (acc BaseAccount) GetAddress() sdk.AccAddress {
|
2018-05-23 22:09:01 -07:00
|
|
|
return acc.Address
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// SetAddress - Implements sdk.Account.
|
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
|
|
|
|
2018-05-23 22:09:01 -07:00
|
|
|
acc.Address = addr
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// GetPubKey - Implements sdk.Account.
|
2020-02-20 08:15:22 -08:00
|
|
|
func (acc BaseAccount) GetPubKey() (pk crypto.PubKey) {
|
|
|
|
if len(acc.PubKey) == 0 {
|
2020-02-18 04:50:13 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-30 15:09:28 -07:00
|
|
|
amino.MustUnmarshalBinaryBare(acc.PubKey, &pk)
|
2020-02-20 08:15:22 -08:00
|
|
|
return pk
|
2018-05-23 22:09:01 -07:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// SetPubKey - Implements sdk.Account.
|
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 {
|
|
|
|
acc.PubKey = pubKey.Bytes()
|
2020-02-18 04:50:13 -08:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:09:01 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// GetAccountNumber - Implements Account
|
2020-02-18 04:50:13 -08:00
|
|
|
func (acc BaseAccount) GetAccountNumber() uint64 {
|
2018-06-11 19:30:54 -07:00
|
|
|
return acc.AccountNumber
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// SetAccountNumber - Implements Account
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// GetSequence - Implements sdk.Account.
|
2020-02-18 04:50:13 -08:00
|
|
|
func (acc BaseAccount) GetSequence() uint64 {
|
2018-05-23 22:09:01 -07:00
|
|
|
return acc.Sequence
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// SetSequence - Implements sdk.Account.
|
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-02-20 08:15:22 -08:00
|
|
|
if len(acc.PubKey) != 0 && acc.Address != nil &&
|
2020-02-18 04:50:13 -08:00
|
|
|
!bytes.Equal(acc.GetPubKey().Address().Bytes(), acc.Address.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
type baseAccountPretty 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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML returns the YAML representation of an account.
|
|
|
|
func (acc BaseAccount) MarshalYAML() (interface{}, error) {
|
|
|
|
alias := baseAccountPretty{
|
|
|
|
Address: acc.Address,
|
|
|
|
AccountNumber: acc.AccountNumber,
|
|
|
|
Sequence: acc.Sequence,
|
|
|
|
}
|
|
|
|
|
|
|
|
if acc.PubKey != nil {
|
|
|
|
pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.GetPubKey())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
alias.PubKey = pks
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := yaml.Marshal(alias)
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPubKey - Implements Account
|
|
|
|
func (ma ModuleAccount) SetPubKey(pubKey crypto.PubKey) error {
|
|
|
|
return fmt.Errorf("not supported for module accounts")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSequence - Implements Account
|
|
|
|
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-04-20 12:32:10 -07:00
|
|
|
if !ma.Address.Equals(sdk.AccAddress(crypto.AddressHash([]byte(ma.Name)))) {
|
|
|
|
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) {
|
|
|
|
bs, err := yaml.Marshal(moduleAccountPretty{
|
|
|
|
Address: ma.Address,
|
|
|
|
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) {
|
|
|
|
return json.Marshal(moduleAccountPretty{
|
|
|
|
Address: ma.Address,
|
|
|
|
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
|
|
|
|
}
|