cosmos-sdk/x/auth/types/account.go

192 lines
4.4 KiB
Go
Raw Normal View History

package types
2018-05-23 19:26:54 -07:00
import (
"bytes"
"encoding/json"
2018-05-23 22:09:01 -07:00
"errors"
2018-12-10 06:27:25 -08:00
"github.com/tendermint/tendermint/crypto"
yaml "gopkg.in/yaml.v2"
2018-12-10 06:27:25 -08:00
2020-02-20 08:15:22 -08:00
"github.com/cosmos/cosmos-sdk/codec"
2018-05-23 19:26:54 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/exported"
2018-05-23 19:26:54 -07:00
)
var _ exported.Account = (*BaseAccount)(nil)
var _ exported.GenesisAccount = (*BaseAccount)(nil)
2018-05-23 22:09:01 -07:00
Merge PR #4159: Module/Genesis Generalization * first commit * gaia cleanup * ... * staking multihooks * missing module function return args * bank module name constant * working, module interface for x/ * got this thing compiling * make test compiles and passes * remove expanded simulation invariants * genesis issue * continued * continued * register crisis routes thought mm * begin blocker to mm * end blocker to mm * empty routes not initialized * move gaia initChainer sanity check to baseapp * remove codecs from module manager * reorging genesis stuff * module manager passed by reference/bugfixes from working last commit int int * move invariant checks from gaia to crisis * typo * basic refactors cmd/gaia/init * working * MultiStakingHooks from types to x/staking/types int * default module manager order of operations from input modules * working * typo * add AppModuleBasic * moduleBasicManager / non-test code compiles * working attempting to get tests passing * make test passes * sim random genesis fix * export bug * ... * genutil module * genutil working * refactored - happy with non-testing code in cmd/ * ... * lint fixes * comment improvement * cli test fix * compile housing * working through compile errors * working gettin' compilin' * non-test code compiles * move testnet to its own module * reworking tests int * bez staging PR 1 comments * concise module function-of names * moved all tests from genesis_test.go to other genutil tests * genaccounts package, add genutil and genaccounts to app.go * docs for genutil genaccounts * genaccounts iterate fn * non-test code with genaccounts/ now compiles * working test compiling * debugging tests * resolved all make test compile errors * test debuggin * resolved all unit tests, introduced param module * cli-test compile fixes * staking initialization bug * code comment improvements, changelog entries * BasicGaiaApp -> ModuleBasics * highlevel explanation in types/module.go * @alexanderbez comment revisions * @fedekunze PR comments * @alexanderbez PR comments (x2) * @cwgoes comments (minor updates) * @fedekunze suggestions * panic on init with multiple validator updates from different modules * initchain panic makes validate genesis fail int * AppModuleGenesis seperation int * test * remove init panic logic in validate genesis replaced with TODO * set maxprocs to match system's GOMAXPROCS * Update circleci * Cap maxprocs in CI to 4 * @alexanderbez recent comments addressed * less blocks in twouble sims int * runsim error output flag * -e on import_export as well * error out int * Try to fix failures * runsim
2019-05-16 08:25:32 -07:00
// NewBaseAccount creates a new BaseAccount object
2020-01-30 13:31:16 -08:00
func NewBaseAccount(address sdk.AccAddress, pubKey crypto.PubKey, accountNumber, sequence uint64) *BaseAccount {
2020-02-20 08:15:22 -08:00
acc := &BaseAccount{
Merge PR #4159: Module/Genesis Generalization * first commit * gaia cleanup * ... * staking multihooks * missing module function return args * bank module name constant * working, module interface for x/ * got this thing compiling * make test compiles and passes * remove expanded simulation invariants * genesis issue * continued * continued * register crisis routes thought mm * begin blocker to mm * end blocker to mm * empty routes not initialized * move gaia initChainer sanity check to baseapp * remove codecs from module manager * reorging genesis stuff * module manager passed by reference/bugfixes from working last commit int int * move invariant checks from gaia to crisis * typo * basic refactors cmd/gaia/init * working * MultiStakingHooks from types to x/staking/types int * default module manager order of operations from input modules * working * typo * add AppModuleBasic * moduleBasicManager / non-test code compiles * working attempting to get tests passing * make test passes * sim random genesis fix * export bug * ... * genutil module * genutil working * refactored - happy with non-testing code in cmd/ * ... * lint fixes * comment improvement * cli test fix * compile housing * working through compile errors * working gettin' compilin' * non-test code compiles * move testnet to its own module * reworking tests int * bez staging PR 1 comments * concise module function-of names * moved all tests from genesis_test.go to other genutil tests * genaccounts package, add genutil and genaccounts to app.go * docs for genutil genaccounts * genaccounts iterate fn * non-test code with genaccounts/ now compiles * working test compiling * debugging tests * resolved all make test compile errors * test debuggin * resolved all unit tests, introduced param module * cli-test compile fixes * staking initialization bug * code comment improvements, changelog entries * BasicGaiaApp -> ModuleBasics * highlevel explanation in types/module.go * @alexanderbez comment revisions * @fedekunze PR comments * @alexanderbez PR comments (x2) * @cwgoes comments (minor updates) * @fedekunze suggestions * panic on init with multiple validator updates from different modules * initchain panic makes validate genesis fail int * AppModuleGenesis seperation int * test * remove init panic logic in validate genesis replaced with TODO * set maxprocs to match system's GOMAXPROCS * Update circleci * Cap maxprocs in CI to 4 * @alexanderbez recent comments addressed * less blocks in twouble sims int * runsim error output flag * -e on import_export as well * error out int * Try to fix failures * runsim
2019-05-16 08:25:32 -07:00
Address: address,
AccountNumber: accountNumber,
Sequence: sequence,
}
2020-02-20 08:15:22 -08:00
acc.SetPubKey(pubKey)
return acc
Merge PR #4159: Module/Genesis Generalization * first commit * gaia cleanup * ... * staking multihooks * missing module function return args * bank module name constant * working, module interface for x/ * got this thing compiling * make test compiles and passes * remove expanded simulation invariants * genesis issue * continued * continued * register crisis routes thought mm * begin blocker to mm * end blocker to mm * empty routes not initialized * move gaia initChainer sanity check to baseapp * remove codecs from module manager * reorging genesis stuff * module manager passed by reference/bugfixes from working last commit int int * move invariant checks from gaia to crisis * typo * basic refactors cmd/gaia/init * working * MultiStakingHooks from types to x/staking/types int * default module manager order of operations from input modules * working * typo * add AppModuleBasic * moduleBasicManager / non-test code compiles * working attempting to get tests passing * make test passes * sim random genesis fix * export bug * ... * genutil module * genutil working * refactored - happy with non-testing code in cmd/ * ... * lint fixes * comment improvement * cli test fix * compile housing * working through compile errors * working gettin' compilin' * non-test code compiles * move testnet to its own module * reworking tests int * bez staging PR 1 comments * concise module function-of names * moved all tests from genesis_test.go to other genutil tests * genaccounts package, add genutil and genaccounts to app.go * docs for genutil genaccounts * genaccounts iterate fn * non-test code with genaccounts/ now compiles * working test compiling * debugging tests * resolved all make test compile errors * test debuggin * resolved all unit tests, introduced param module * cli-test compile fixes * staking initialization bug * code comment improvements, changelog entries * BasicGaiaApp -> ModuleBasics * highlevel explanation in types/module.go * @alexanderbez comment revisions * @fedekunze PR comments * @alexanderbez PR comments (x2) * @cwgoes comments (minor updates) * @fedekunze suggestions * panic on init with multiple validator updates from different modules * initchain panic makes validate genesis fail int * AppModuleGenesis seperation int * test * remove init panic logic in validate genesis replaced with TODO * set maxprocs to match system's GOMAXPROCS * Update circleci * Cap maxprocs in CI to 4 * @alexanderbez recent comments addressed * less blocks in twouble sims int * runsim error output flag * -e on import_export as well * error out int * Try to fix failures * runsim
2019-05-16 08:25:32 -07:00
}
// ProtoBaseAccount - a prototype function for BaseAccount
func ProtoBaseAccount() exported.Account {
return &BaseAccount{}
}
// NewBaseAccountWithAddress - returns a new base account with a given address
func NewBaseAccountWithAddress(addr sdk.AccAddress) *BaseAccount {
return &BaseAccount{
2018-05-23 22:09:01 -07:00
Address: addr,
}
}
// GetAddress - Implements sdk.Account.
2018-07-06 00:06:53 -07:00
func (acc BaseAccount) GetAddress() sdk.AccAddress {
2018-05-23 22:09:01 -07:00
return acc.Address
}
// SetAddress - Implements sdk.Account.
2018-07-06 00:06:53 -07:00
func (acc *BaseAccount) SetAddress(addr sdk.AccAddress) error {
2018-05-23 22:09:01 -07:00
if len(acc.Address) != 0 {
return errors.New("cannot override BaseAccount address")
}
2018-05-23 22:09:01 -07:00
acc.Address = addr
return nil
}
// GetPubKey - Implements sdk.Account.
2020-02-20 08:15:22 -08:00
func (acc BaseAccount) GetPubKey() (pk crypto.PubKey) {
if len(acc.PubKey) == 0 {
return nil
}
2020-02-20 08:15:22 -08:00
codec.Cdc.MustUnmarshalBinaryBare(acc.PubKey, &pk)
return pk
2018-05-23 22:09:01 -07:00
}
// SetPubKey - Implements sdk.Account.
2018-05-23 22:09:01 -07:00
func (acc *BaseAccount) SetPubKey(pubKey crypto.PubKey) error {
2020-02-20 08:15:22 -08:00
if pubKey == nil {
acc.PubKey = nil
} else {
acc.PubKey = pubKey.Bytes()
}
2018-05-23 22:09:01 -07:00
return nil
}
// GetAccountNumber - Implements Account
func (acc BaseAccount) GetAccountNumber() uint64 {
return acc.AccountNumber
}
// SetAccountNumber - Implements Account
func (acc *BaseAccount) SetAccountNumber(accNumber uint64) error {
acc.AccountNumber = accNumber
return nil
}
// GetSequence - Implements sdk.Account.
func (acc BaseAccount) GetSequence() uint64 {
2018-05-23 22:09:01 -07:00
return acc.Sequence
}
// SetSequence - Implements sdk.Account.
func (acc *BaseAccount) SetSequence(seq uint64) error {
2018-05-23 22:09:01 -07:00
acc.Sequence = seq
return nil
}
// Validate checks for errors on the account fields
func (acc BaseAccount) Validate() error {
2020-02-20 08:15:22 -08:00
if len(acc.PubKey) != 0 && acc.Address != nil &&
!bytes.Equal(acc.GetPubKey().Address().Bytes(), acc.Address.Bytes()) {
2020-02-20 08:15:22 -08:00
return errors.New("account address and pubkey address do not match")
}
return nil
}
func (acc BaseAccount) String() string {
2020-02-20 08:15:22 -08:00
out, _ := acc.MarshalYAML()
return out.(string)
}
type baseAccountPretty struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
PubKey string `json:"public_key" yaml:"public_key"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
}
// MarshalYAML returns the YAML representation of an account.
func (acc BaseAccount) MarshalYAML() (interface{}, error) {
alias := baseAccountPretty{
Address: acc.Address,
AccountNumber: acc.AccountNumber,
Sequence: acc.Sequence,
}
if acc.PubKey != nil {
pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.GetPubKey())
if err != nil {
return nil, err
}
alias.PubKey = pks
}
bz, err := yaml.Marshal(alias)
if err != nil {
return nil, err
}
return string(bz), err
}
// MarshalJSON returns the JSON representation of a BaseAccount.
func (acc BaseAccount) MarshalJSON() ([]byte, error) {
alias := baseAccountPretty{
Address: acc.Address,
AccountNumber: acc.AccountNumber,
Sequence: acc.Sequence,
}
if acc.PubKey != nil {
pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.GetPubKey())
if err != nil {
return nil, err
}
alias.PubKey = pks
}
return json.Marshal(alias)
}
// UnmarshalJSON unmarshals raw JSON bytes into a BaseAccount.
func (acc *BaseAccount) UnmarshalJSON(bz []byte) error {
var alias baseAccountPretty
if err := json.Unmarshal(bz, &alias); err != nil {
return err
}
// NOTE: This will not work for multisig-based accounts as their Bech32
// encoding is too long.
if alias.PubKey != "" {
pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey)
if err != nil {
return err
}
acc.PubKey = pk.Bytes()
}
acc.Address = alias.Address
acc.AccountNumber = alias.AccountNumber
acc.Sequence = alias.Sequence
return nil
}