2016-03-20 03:00:43 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/tendermint/go-crypto"
|
2017-05-21 12:36:46 -07:00
|
|
|
"github.com/tendermint/go-wire"
|
2016-03-20 03:00:43 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Account struct {
|
2017-03-21 13:52:42 -07:00
|
|
|
PubKey crypto.PubKey `json:"pub_key"` // May be nil, if not known.
|
|
|
|
Sequence int `json:"sequence"`
|
|
|
|
Balance Coins `json:"coins"`
|
2016-03-20 03:00:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (acc *Account) Copy() *Account {
|
2017-02-16 18:36:49 -08:00
|
|
|
if acc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-20 03:00:43 -07:00
|
|
|
accCopy := *acc
|
|
|
|
return &accCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (acc *Account) String() string {
|
|
|
|
if acc == nil {
|
|
|
|
return "nil-Account"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("Account{%v %v %v}",
|
|
|
|
acc.PubKey, acc.Sequence, acc.Balance)
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
type PrivAccount struct {
|
2017-03-21 13:52:42 -07:00
|
|
|
crypto.PrivKey
|
2016-03-20 03:00:43 -07:00
|
|
|
Account
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
type AccountGetter interface {
|
|
|
|
GetAccount(addr []byte) *Account
|
|
|
|
}
|
|
|
|
|
2016-03-29 14:25:17 -07:00
|
|
|
type AccountSetter interface {
|
2016-03-24 12:17:26 -07:00
|
|
|
SetAccount(addr []byte, acc *Account)
|
|
|
|
}
|
|
|
|
|
2016-03-29 14:25:17 -07:00
|
|
|
type AccountGetterSetter interface {
|
2016-03-24 12:17:26 -07:00
|
|
|
GetAccount(addr []byte) *Account
|
|
|
|
SetAccount(addr []byte, acc *Account)
|
2016-03-20 03:00:43 -07:00
|
|
|
}
|
2017-05-21 12:36:46 -07:00
|
|
|
|
|
|
|
func AccountKey(addr []byte) []byte {
|
|
|
|
return append([]byte("base/a/"), addr...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAccount(store KVStore, addr []byte) *Account {
|
|
|
|
data := store.Get(AccountKey(addr))
|
|
|
|
if len(data) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var acc *Account
|
|
|
|
err := wire.ReadBinaryBytes(data, &acc)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Error reading account %X error: %v",
|
|
|
|
data, err.Error()))
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetAccount(store KVStore, addr []byte, acc *Account) {
|
|
|
|
accBytes := wire.BinaryBytes(acc)
|
|
|
|
store.Set(AccountKey(addr), accBytes)
|
|
|
|
}
|