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

68 lines
1.7 KiB
Go
Raw Normal View History

package types
import (
2018-01-20 11:19:44 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
2018-02-13 05:36:08 -08:00
crypto "github.com/tendermint/go-crypto"
2018-02-28 17:57:38 -08:00
wire "github.com/tendermint/go-wire"
)
2018-01-20 11:19:44 -08:00
var _ sdk.Account = (*AppAccount)(nil)
2018-02-13 05:36:08 -08:00
// Custom extensions for this application. This is just an example of
// extending auth.BaseAccount with custom fields.
//
// This is compatible with the stock auth.AccountStore, since
// auth.AccountStore uses the flexible go-wire 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 }
2018-02-28 17:57:38 -08:00
// Get the ParseAccount function for the custom AppAccount
func GetParseAccount(cdc *wire.Codec) sdk.ParseAccount {
return func(accBytes []byte) (res sdk.Account, err error) {
acct := new(AppAccount)
err = cdc.UnmarshalBinary(accBytes, acct)
return acct, err
}
}
2018-02-13 05:36:08 -08:00
//___________________________________________________________________________________
2018-02-14 08:16:06 -08:00
// State to Unmarshal
type GenesisState struct {
Accounts []*GenesisAccount `json:"accounts"`
}
// GenesisAccount doesn't need pubkey or sequence
2018-02-13 05:36:08 -08:00
type GenesisAccount struct {
2018-02-14 08:16:06 -08:00
Name string `json:"name"`
Address crypto.Address `json:"address"`
Coins sdk.Coins `json:"coins"`
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,
Coins: aa.Coins,
2018-02-13 05:36:08 -08:00
}
}
2018-02-13 05:36:08 -08:00
// convert GenesisAccount to 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
baseAcc := auth.BaseAccount{
2018-02-14 08:16:06 -08:00
Address: ga.Address,
Coins: ga.Coins,
2018-02-13 05:36:08 -08:00
}
return &AppAccount{
BaseAccount: baseAcc,
Name: ga.Name,
}, nil
}