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

73 lines
1.8 KiB
Go
Raw Normal View History

package types
import (
2018-01-20 11:19:44 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
2018-03-02 01:24:07 -08:00
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
)
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
2018-04-07 00:08:53 -07:00
// auth.AccountStore 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 }
2018-03-20 18:22:15 -07:00
// Get the AccountDecoder function for the custom AppAccount
func GetAccountDecoder(cdc *wire.Codec) sdk.AccountDecoder {
2018-02-28 17:57:38 -08:00
return func(accBytes []byte) (res sdk.Account, err error) {
if len(accBytes) == 0 {
return nil, sdk.ErrTxDecode("accBytes are empty")
}
2018-02-28 17:57:38 -08:00
acct := new(AppAccount)
2018-04-09 11:05:03 -07:00
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
}
}
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-03-01 23:49:07 -08:00
Name string `json:"name"`
Address sdk.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,
2018-04-05 04:55:10 -07:00
Coins: aa.Coins.Sort(),
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,
2018-04-05 04:55:10 -07:00
Coins: ga.Coins.Sort(),
2018-02-13 05:36:08 -08:00
}
return &AppAccount{
BaseAccount: baseAcc,
Name: ga.Name,
}, nil
}