2019-06-05 16:26:17 -07:00
|
|
|
package types
|
2018-05-23 19:26:54 -07:00
|
|
|
|
|
|
|
import (
|
2019-09-12 12:22:25 -07:00
|
|
|
"bytes"
|
2020-02-25 12:22:28 -08:00
|
|
|
"encoding/json"
|
2018-05-23 22:09:01 -07:00
|
|
|
"errors"
|
|
|
|
|
2018-12-10 06:27:25 -08:00
|
|
|
"github.com/tendermint/tendermint/crypto"
|
2019-06-18 09:03:16 -07:00
|
|
|
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"
|
2019-06-24 13:45:09 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/exported"
|
2018-05-23 19:26:54 -07:00
|
|
|
)
|
|
|
|
|
2019-06-24 13:45:09 -07:00
|
|
|
var _ exported.Account = (*BaseAccount)(nil)
|
2019-09-12 12:22:25 -07:00
|
|
|
var _ exported.GenesisAccount = (*BaseAccount)(nil)
|
2018-05-23 22:09:01 -07:00
|
|
|
|
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{
|
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
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// ProtoBaseAccount - a prototype function for BaseAccount
|
2019-06-24 13:45:09 -07:00
|
|
|
func ProtoBaseAccount() exported.Account {
|
2018-06-25 19:05:47 -07:00
|
|
|
return &BaseAccount{}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// NewBaseAccountWithAddress - returns a new base account with a given address
|
2020-02-18 04:50:13 -08:00
|
|
|
func NewBaseAccountWithAddress(addr sdk.AccAddress) *BaseAccount {
|
|
|
|
return &BaseAccount{
|
2018-05-23 22:09:01 -07:00
|
|
|
Address: addr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// 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")
|
|
|
|
}
|
2020-02-18 04:50:13 -08:00
|
|
|
|
2018-05-23 22:09:01 -07:00
|
|
|
acc.Address = addr
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// GetPubKey - Implements sdk.Account.
|
2020-02-20 08:15:22 -08:00
|
|
|
func (acc BaseAccount) GetPubKey() (pk crypto.PubKey) {
|
|
|
|
if len(acc.PubKey) == 0 {
|
2020-02-18 04:50:13 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08: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()
|
2020-02-18 04:50:13 -08:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:09:01 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// GetAccountNumber - Implements Account
|
2020-02-18 04:50:13 -08:00
|
|
|
func (acc BaseAccount) GetAccountNumber() uint64 {
|
2018-06-11 19:30:54 -07:00
|
|
|
return acc.AccountNumber
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// SetAccountNumber - Implements Account
|
2018-11-26 03:29:21 -08:00
|
|
|
func (acc *BaseAccount) SetAccountNumber(accNumber uint64) error {
|
2018-06-11 19:30:54 -07:00
|
|
|
acc.AccountNumber = accNumber
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// GetSequence - Implements sdk.Account.
|
2020-02-18 04:50:13 -08:00
|
|
|
func (acc BaseAccount) GetSequence() uint64 {
|
2018-05-23 22:09:01 -07:00
|
|
|
return acc.Sequence
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
// SetSequence - Implements sdk.Account.
|
2018-11-26 03:29:21 -08:00
|
|
|
func (acc *BaseAccount) SetSequence(seq uint64) error {
|
2018-05-23 22:09:01 -07:00
|
|
|
acc.Sequence = seq
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-06 12:06:26 -08:00
|
|
|
// 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 &&
|
2020-02-18 04:50:13 -08:00
|
|
|
!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")
|
2019-11-06 12:06:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-09-12 12:22:25 -07:00
|
|
|
}
|
2020-02-25 12:22:28 -08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|