2018-05-23 19:26:54 -07:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2018-05-23 22:09:01 -07:00
|
|
|
"errors"
|
|
|
|
|
2018-05-23 19:26:54 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-05-23 22:09:01 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/wire"
|
2018-05-23 19:26:54 -07:00
|
|
|
crypto "github.com/tendermint/go-crypto"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Account is a standard account using a sequence number for replay protection
|
|
|
|
// and a pubkey for authentication.
|
|
|
|
type Account interface {
|
|
|
|
GetAddress() sdk.Address
|
|
|
|
SetAddress(sdk.Address) error // errors if already set.
|
|
|
|
|
|
|
|
GetPubKey() crypto.PubKey // can return nil.
|
|
|
|
SetPubKey(crypto.PubKey) error
|
|
|
|
|
|
|
|
GetSequence() int64
|
|
|
|
SetSequence(int64) error
|
|
|
|
|
|
|
|
GetCoins() sdk.Coins
|
|
|
|
SetCoins(sdk.Coins) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountDecoder unmarshals account bytes
|
|
|
|
type AccountDecoder func(accountBytes []byte) (Account, error)
|
2018-05-23 22:09:01 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
// BaseAccount
|
|
|
|
|
|
|
|
var _ Account = (*BaseAccount)(nil)
|
|
|
|
|
|
|
|
// BaseAccount - base account structure.
|
|
|
|
// Extend this by embedding this in your AppAccount.
|
|
|
|
// See the examples/basecoin/types/account.go for an example.
|
|
|
|
type BaseAccount struct {
|
|
|
|
Address sdk.Address `json:"address"`
|
|
|
|
Coins sdk.Coins `json:"coins"`
|
|
|
|
PubKey crypto.PubKey `json:"public_key"`
|
|
|
|
Sequence int64 `json:"sequence"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBaseAccountWithAddress(addr sdk.Address) BaseAccount {
|
|
|
|
return BaseAccount{
|
|
|
|
Address: addr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements sdk.Account.
|
|
|
|
func (acc BaseAccount) GetAddress() sdk.Address {
|
|
|
|
return acc.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements sdk.Account.
|
|
|
|
func (acc *BaseAccount) SetAddress(addr sdk.Address) error {
|
|
|
|
if len(acc.Address) != 0 {
|
|
|
|
return errors.New("cannot override BaseAccount address")
|
|
|
|
}
|
|
|
|
acc.Address = addr
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements sdk.Account.
|
|
|
|
func (acc BaseAccount) GetPubKey() crypto.PubKey {
|
|
|
|
return acc.PubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements sdk.Account.
|
|
|
|
func (acc *BaseAccount) SetPubKey(pubKey crypto.PubKey) error {
|
|
|
|
acc.PubKey = pubKey
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements sdk.Account.
|
|
|
|
func (acc *BaseAccount) GetCoins() sdk.Coins {
|
|
|
|
return acc.Coins
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements sdk.Account.
|
|
|
|
func (acc *BaseAccount) SetCoins(coins sdk.Coins) error {
|
|
|
|
acc.Coins = coins
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements sdk.Account.
|
|
|
|
func (acc *BaseAccount) GetSequence() int64 {
|
|
|
|
return acc.Sequence
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements sdk.Account.
|
|
|
|
func (acc *BaseAccount) SetSequence(seq int64) error {
|
|
|
|
acc.Sequence = seq
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
// Wire
|
|
|
|
|
|
|
|
// Most users shouldn't use this, but this comes handy for tests.
|
|
|
|
func RegisterBaseAccount(cdc *wire.Codec) {
|
|
|
|
cdc.RegisterInterface((*Account)(nil), nil)
|
|
|
|
cdc.RegisterConcrete(&BaseAccount{}, "cosmos-sdk/BaseAccount", nil)
|
|
|
|
wire.RegisterCrypto(cdc)
|
|
|
|
}
|