2018-03-30 11:13:22 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/wire"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2018-04-05 06:16:54 -07:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/examples/democoin/x/cool"
|
|
|
|
"github.com/cosmos/cosmos-sdk/examples/democoin/x/pow"
|
2018-03-30 11:13:22 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ sdk.Account = (*AppAccount)(nil)
|
|
|
|
|
|
|
|
// 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-06 17:25:08 -07:00
|
|
|
// auth.AccountStore uses the flexible go-amino library.
|
2018-03-30 11:13:22 -07:00
|
|
|
type AppAccount struct {
|
|
|
|
auth.BaseAccount
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint
|
|
|
|
func (acc AppAccount) GetName() string { return acc.Name }
|
|
|
|
func (acc *AppAccount) SetName(name string) { acc.Name = name }
|
|
|
|
|
|
|
|
// Get the AccountDecoder function for the custom AppAccount
|
|
|
|
func GetAccountDecoder(cdc *wire.Codec) sdk.AccountDecoder {
|
|
|
|
return func(accBytes []byte) (res sdk.Account, err error) {
|
|
|
|
if len(accBytes) == 0 {
|
|
|
|
return nil, sdk.ErrTxDecode("accBytes are empty")
|
|
|
|
}
|
|
|
|
acct := new(AppAccount)
|
|
|
|
err = cdc.UnmarshalBinary(accBytes, &acct)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return acct, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//___________________________________________________________________________________
|
|
|
|
|
|
|
|
// State to Unmarshal
|
|
|
|
type GenesisState struct {
|
2018-04-05 06:16:54 -07:00
|
|
|
Accounts []*GenesisAccount `json:"accounts"`
|
2018-04-18 21:49:24 -07:00
|
|
|
POWGenesis pow.Genesis `json:"pow"`
|
|
|
|
CoolGenesis cool.Genesis `json:"cool"`
|
2018-03-30 11:13:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenesisAccount doesn't need pubkey or sequence
|
|
|
|
type GenesisAccount struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Address sdk.Address `json:"address"`
|
|
|
|
Coins sdk.Coins `json:"coins"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGenesisAccount(aa *AppAccount) *GenesisAccount {
|
|
|
|
return &GenesisAccount{
|
|
|
|
Name: aa.Name,
|
|
|
|
Address: aa.Address,
|
2018-04-05 04:55:10 -07:00
|
|
|
Coins: aa.Coins.Sort(),
|
2018-03-30 11:13:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert GenesisAccount to AppAccount
|
|
|
|
func (ga *GenesisAccount) ToAppAccount() (acc *AppAccount, err error) {
|
|
|
|
baseAcc := auth.BaseAccount{
|
|
|
|
Address: ga.Address,
|
2018-04-05 04:55:10 -07:00
|
|
|
Coins: ga.Coins.Sort(),
|
2018-03-30 11:13:22 -07:00
|
|
|
}
|
|
|
|
return &AppAccount{
|
|
|
|
BaseAccount: baseAcc,
|
|
|
|
Name: ga.Name,
|
|
|
|
}, nil
|
|
|
|
}
|