tendermint/account/account.go

75 lines
2.1 KiB
Go
Raw Normal View History

package account
import (
"bytes"
"fmt"
"io"
2015-07-25 15:45:45 -07:00
"github.com/tendermint/tendermint/wire"
2015-07-19 16:42:52 -07:00
. "github.com/tendermint/tendermint/common"
2015-04-15 19:14:08 -07:00
"github.com/tendermint/tendermint/merkle"
2015-05-15 21:48:04 -07:00
ptypes "github.com/tendermint/tendermint/permission/types"
)
// Signable is an interface for all signable things.
// It typically removes signatures before serializing.
type Signable interface {
2015-05-29 14:53:57 -07:00
WriteSignBytes(chainID string, w io.Writer, n *int64, err *error)
}
// SignBytes is a convenience method for getting the bytes to sign of a Signable.
2015-05-29 14:53:57 -07:00
func SignBytes(chainID string, o Signable) []byte {
buf, n, err := new(bytes.Buffer), new(int64), new(error)
2015-05-29 14:53:57 -07:00
o.WriteSignBytes(chainID, buf, n, err)
if *err != nil {
2015-07-19 16:42:52 -07:00
PanicCrisis(err)
}
return buf.Bytes()
}
2015-04-15 19:14:08 -07:00
// HashSignBytes is a convenience method for getting the hash of the bytes of a signable
2015-05-29 14:53:57 -07:00
func HashSignBytes(chainID string, o Signable) []byte {
2015-06-18 20:19:39 -07:00
return merkle.SimpleHashFromBinary(SignBytes(chainID, o))
2015-04-15 19:14:08 -07:00
}
//-----------------------------------------------------------------------------
// Account resides in the application state, and is mutated by transactions
// on the blockchain.
2015-07-25 15:45:45 -07:00
// Serialized by wire.[read|write]Reflect
type Account struct {
2015-05-01 17:26:49 -07:00
Address []byte `json:"address"`
PubKey PubKey `json:"pub_key"`
Sequence int `json:"sequence"`
Balance int64 `json:"balance"`
2015-05-01 17:26:49 -07:00
Code []byte `json:"code"` // VM code
StorageRoot []byte `json:"storage_root"` // VM storage merkle root.
2015-05-12 17:40:19 -07:00
Permissions ptypes.AccountPermissions `json:"permissions"`
}
func (acc *Account) Copy() *Account {
accCopy := *acc
return &accCopy
}
func (acc *Account) String() string {
2015-07-11 18:01:21 -07:00
if acc == nil {
return "nil-Account"
}
return fmt.Sprintf("Account{%X:%v B:%v C:%v S:%X P:%s}", acc.Address, acc.PubKey, acc.Balance, len(acc.Code), acc.StorageRoot, acc.Permissions)
}
func AccountEncoder(o interface{}, w io.Writer, n *int64, err *error) {
2015-07-25 15:45:45 -07:00
wire.WriteBinary(o.(*Account), w, n, err)
}
func AccountDecoder(r io.Reader, n *int64, err *error) interface{} {
2015-07-25 15:45:45 -07:00
return wire.ReadBinary(&Account{}, r, n, err)
}
2015-07-25 15:45:45 -07:00
var AccountCodec = wire.Codec{
Encode: AccountEncoder,
Decode: AccountDecoder,
}