2018-01-12 11:49:53 -08:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-01-20 11:19:44 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-01-12 11:49:53 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
)
|
|
|
|
|
2018-05-23 22:09:01 -07:00
|
|
|
var _ auth.Account = (*AppAccount)(nil)
|
2018-01-20 11:19:44 -08:00
|
|
|
|
2018-07-02 14:29:47 -07:00
|
|
|
// AppAccount is a custom extension for this application. It is an example of
|
|
|
|
// extending auth.BaseAccount with custom fields. It is compatible with the
|
2018-10-20 23:22:48 -07:00
|
|
|
// stock auth.AccountKeeper, since auth.AccountKeeper uses the flexible go-amino
|
2018-07-02 14:29:47 -07:00
|
|
|
// library.
|
2018-01-12 11:49:53 -08:00
|
|
|
type AppAccount struct {
|
|
|
|
auth.BaseAccount
|
2018-07-02 14:29:47 -07:00
|
|
|
|
2018-02-23 01:40:10 -08:00
|
|
|
Name string `json:"name"`
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
|
|
|
|
2018-02-13 05:36:08 -08:00
|
|
|
// nolint
|
|
|
|
func (acc AppAccount) GetName() string { return acc.Name }
|
|
|
|
func (acc *AppAccount) SetName(name string) { acc.Name = name }
|
|
|
|
|
2018-07-02 14:29:47 -07:00
|
|
|
// NewAppAccount returns a reference to a new AppAccount given a name and an
|
|
|
|
// auth.BaseAccount.
|
|
|
|
func NewAppAccount(name string, baseAcct auth.BaseAccount) *AppAccount {
|
|
|
|
return &AppAccount{BaseAccount: baseAcct, Name: name}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccountDecoder returns the AccountDecoder function for the custom
|
|
|
|
// AppAccount.
|
2018-09-13 11:17:32 -07:00
|
|
|
func GetAccountDecoder(cdc *codec.Codec) auth.AccountDecoder {
|
2018-07-02 14:29:47 -07:00
|
|
|
return func(accBytes []byte) (auth.Account, error) {
|
2018-03-20 18:40:58 -07:00
|
|
|
if len(accBytes) == 0 {
|
|
|
|
return nil, sdk.ErrTxDecode("accBytes are empty")
|
|
|
|
}
|
2018-07-02 14:29:47 -07:00
|
|
|
|
2018-02-28 17:57:38 -08:00
|
|
|
acct := new(AppAccount)
|
2018-07-02 14:29:47 -07:00
|
|
|
err := cdc.UnmarshalBinaryBare(accBytes, &acct)
|
2018-03-03 13:03:34 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-07-02 14:29:47 -07:00
|
|
|
|
2018-02-28 17:57:38 -08:00
|
|
|
return acct, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 14:29:47 -07:00
|
|
|
// GenesisState reflects the genesis state of the application.
|
2018-02-14 08:16:06 -08:00
|
|
|
type GenesisState struct {
|
2018-07-02 14:29:47 -07:00
|
|
|
Accounts []*GenesisAccount `json:"accounts"`
|
2018-02-14 08:16:06 -08:00
|
|
|
}
|
|
|
|
|
2018-07-02 14:29:47 -07:00
|
|
|
// GenesisAccount reflects a genesis account the application expects in it's
|
|
|
|
// genesis state.
|
2018-02-13 05:36:08 -08:00
|
|
|
type GenesisAccount struct {
|
2018-07-06 00:06:53 -07:00
|
|
|
Name string `json:"name"`
|
|
|
|
Address sdk.AccAddress `json:"address"`
|
|
|
|
Coins sdk.Coins `json:"coins"`
|
2018-02-13 05:36:08 -08:00
|
|
|
}
|
|
|
|
|
2018-07-02 14:29:47 -07:00
|
|
|
// NewGenesisAccount returns a reference to a new GenesisAccount given an
|
|
|
|
// AppAccount.
|
2018-02-13 05:36:08 -08:00
|
|
|
func NewGenesisAccount(aa *AppAccount) *GenesisAccount {
|
|
|
|
return &GenesisAccount{
|
2018-02-14 08:16:06 -08:00
|
|
|
Name: aa.Name,
|
|
|
|
Address: aa.Address,
|
2018-04-05 04:55:10 -07:00
|
|
|
Coins: aa.Coins.Sort(),
|
2018-02-13 05:36:08 -08:00
|
|
|
}
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
|
|
|
|
2018-07-02 14:29:47 -07:00
|
|
|
// ToAppAccount converts a GenesisAccount to an AppAccount.
|
2018-02-14 08:16:06 -08:00
|
|
|
func (ga *GenesisAccount) ToAppAccount() (acc *AppAccount, err error) {
|
2018-02-13 05:36:08 -08:00
|
|
|
return &AppAccount{
|
2018-07-02 14:29:47 -07:00
|
|
|
Name: ga.Name,
|
|
|
|
BaseAccount: auth.BaseAccount{
|
|
|
|
Address: ga.Address,
|
|
|
|
Coins: ga.Coins.Sort(),
|
|
|
|
},
|
2018-02-13 05:36:08 -08:00
|
|
|
}, nil
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|