cosmos-sdk/examples/basecoin/types/account.go

82 lines
2.2 KiB
Go
Raw Normal View History

package types
import (
"github.com/cosmos/cosmos-sdk/codec"
2018-01-20 11:19:44 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
"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
// AppAccount is a custom extension for this application. It is an example of
// extending auth.BaseAccount with custom fields. It is compatible with the
// stock auth.AccountKeeper, since auth.AccountKeeper uses the flexible go-amino
// library.
type AppAccount struct {
auth.BaseAccount
2018-02-23 01:40:10 -08:00
Name string `json:"name"`
}
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 }
// 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.
func GetAccountDecoder(cdc *codec.Codec) auth.AccountDecoder {
return func(accBytes []byte) (auth.Account, error) {
if len(accBytes) == 0 {
return nil, sdk.ErrTxDecode("accBytes are empty")
}
2018-02-28 17:57:38 -08:00
acct := new(AppAccount)
err := cdc.UnmarshalBinaryBare(accBytes, &acct)
2018-03-03 13:03:34 -08:00
if err != nil {
panic(err)
}
2018-02-28 17:57:38 -08:00
return acct, err
}
}
// GenesisState reflects the genesis state of the application.
2018-02-14 08:16:06 -08:00
type GenesisState struct {
Accounts []*GenesisAccount `json:"accounts"`
2018-02-14 08:16:06 -08: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
}
// 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
}
}
// 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{
Name: ga.Name,
BaseAccount: auth.BaseAccount{
Address: ga.Address,
Coins: ga.Coins.Sort(),
},
2018-02-13 05:36:08 -08:00
}, nil
}