2019-06-28 13:11:27 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2019-11-06 12:06:26 -08:00
|
|
|
"encoding/json"
|
2019-09-12 12:22:25 -07:00
|
|
|
"errors"
|
2019-06-28 13:11:27 -07:00
|
|
|
"fmt"
|
2019-09-12 12:22:25 -07:00
|
|
|
"strings"
|
2019-06-28 13:11:27 -07:00
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/crypto"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-09-12 12:22:25 -07:00
|
|
|
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
2019-06-28 13:11:27 -07:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/supply/exported"
|
|
|
|
)
|
|
|
|
|
2019-09-12 12:22:25 -07:00
|
|
|
var (
|
|
|
|
_ authexported.GenesisAccount = (*ModuleAccount)(nil)
|
|
|
|
_ exported.ModuleAccountI = (*ModuleAccount)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Register the ModuleAccount type as a GenesisAccount so that when no
|
|
|
|
// concrete GenesisAccount types exist and **default** genesis state is used,
|
|
|
|
// the genesis state will serialize correctly.
|
|
|
|
authtypes.RegisterAccountTypeCodec(&ModuleAccount{}, "cosmos-sdk/ModuleAccount")
|
|
|
|
}
|
2019-06-28 13:11:27 -07:00
|
|
|
|
|
|
|
// ModuleAccount defines an account for modules that holds coins on a pool
|
|
|
|
type ModuleAccount struct {
|
|
|
|
*authtypes.BaseAccount
|
2019-11-06 12:06:26 -08:00
|
|
|
|
2019-07-06 12:42:06 -07:00
|
|
|
Name string `json:"name" yaml:"name"` // name of the module
|
|
|
|
Permissions []string `json:"permissions" yaml:"permissions"` // permissions of module account
|
2019-06-28 13:11:27 -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)))
|
|
|
|
}
|
|
|
|
|
2019-09-12 12:22:25 -07:00
|
|
|
// NewEmptyModuleAccount creates a empty ModuleAccount from a string
|
2019-07-06 12:42:06 -07:00
|
|
|
func NewEmptyModuleAccount(name string, permissions ...string) *ModuleAccount {
|
2019-06-28 13:11:27 -07:00
|
|
|
moduleAddress := NewModuleAddress(name)
|
|
|
|
baseAcc := authtypes.NewBaseAccountWithAddress(moduleAddress)
|
|
|
|
|
2019-07-06 12:42:06 -07:00
|
|
|
if err := validatePermissions(permissions...); err != nil {
|
2019-06-28 13:11:27 -07:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ModuleAccount{
|
|
|
|
BaseAccount: &baseAcc,
|
|
|
|
Name: name,
|
2019-07-06 12:42:06 -07:00
|
|
|
Permissions: permissions,
|
2019-06-28 13:11:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewModuleAccount creates a new ModuleAccount instance
|
|
|
|
func NewModuleAccount(ba *authtypes.BaseAccount,
|
2019-07-06 12:42:06 -07:00
|
|
|
name string, permissions ...string) *ModuleAccount {
|
2019-06-28 13:11:27 -07:00
|
|
|
|
2019-07-06 12:42:06 -07:00
|
|
|
if err := validatePermissions(permissions...); err != nil {
|
2019-06-28 13:11:27 -07:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ModuleAccount{
|
|
|
|
BaseAccount: ba,
|
|
|
|
Name: name,
|
2019-07-06 12:42:06 -07:00
|
|
|
Permissions: permissions,
|
2019-06-28 13:11:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 12:42:06 -07:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
// GetName returns the the name of the holder's module
|
|
|
|
func (ma ModuleAccount) GetName() string {
|
|
|
|
return ma.Name
|
|
|
|
}
|
|
|
|
|
2019-07-06 12:42:06 -07:00
|
|
|
// GetPermissions returns permissions granted to the module account
|
|
|
|
func (ma ModuleAccount) GetPermissions() []string {
|
|
|
|
return ma.Permissions
|
2019-06-28 13:11:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2019-09-12 12:22:25 -07:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2019-11-06 12:06:26 -08:00
|
|
|
type moduleAccountPretty struct {
|
|
|
|
Address sdk.AccAddress `json:"address" yaml:"address"`
|
|
|
|
Coins sdk.Coins `json:"coins" yaml:"coins"`
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
// MarshalYAML returns the YAML representation of a ModuleAccount.
|
|
|
|
func (ma ModuleAccount) MarshalYAML() (interface{}, error) {
|
2019-11-06 12:06:26 -08:00
|
|
|
bs, err := yaml.Marshal(moduleAccountPretty{
|
2019-06-28 13:11:27 -07:00
|
|
|
Address: ma.Address,
|
|
|
|
Coins: ma.Coins,
|
|
|
|
PubKey: "",
|
|
|
|
AccountNumber: ma.AccountNumber,
|
|
|
|
Sequence: ma.Sequence,
|
|
|
|
Name: ma.Name,
|
2019-07-06 12:42:06 -07:00
|
|
|
Permissions: ma.Permissions,
|
2019-06-28 13:11:27 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(bs), nil
|
|
|
|
}
|
2019-11-06 12:06:26 -08:00
|
|
|
|
|
|
|
// MarshalJSON returns the JSON representation of a ModuleAccount.
|
|
|
|
func (ma ModuleAccount) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(moduleAccountPretty{
|
|
|
|
Address: ma.Address,
|
|
|
|
Coins: ma.Coins,
|
|
|
|
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 = authtypes.NewBaseAccount(alias.Address, alias.Coins, nil, alias.AccountNumber, alias.Sequence)
|
|
|
|
ma.Name = alias.Name
|
|
|
|
ma.Permissions = alias.Permissions
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|