2018-01-12 11:49:53 -08:00
|
|
|
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"
|
2018-01-12 11:49:53 -08:00
|
|
|
"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
|
|
|
|
// auth.AccountStore uses the flexible go-wire library.
|
2018-01-12 11:49:53 -08:00
|
|
|
type AppAccount struct {
|
|
|
|
auth.BaseAccount
|
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-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) {
|
2018-03-20 18:40:58 -07:00
|
|
|
if len(accBytes) == 0 {
|
|
|
|
return nil, sdk.ErrTxDecode("accBytes are empty")
|
|
|
|
}
|
2018-02-28 17:57:38 -08:00
|
|
|
acct := new(AppAccount)
|
2018-03-03 15:24:24 -08:00
|
|
|
err = cdc.UnmarshalBinary(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,
|
|
|
|
Coins: aa.Coins,
|
2018-02-13 05:36:08 -08:00
|
|
|
}
|
2018-01-12 11:49:53 -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
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|