You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.8 KiB

  1. package account
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "github.com/tendermint/tendermint/binary"
  7. "github.com/tendermint/tendermint/merkle"
  8. )
  9. // Signable is an interface for all signable things.
  10. // It typically removes signatures before serializing.
  11. type Signable interface {
  12. WriteSignBytes(w io.Writer, n *int64, err *error)
  13. }
  14. // SignBytes is a convenience method for getting the bytes to sign of a Signable.
  15. func SignBytes(o Signable) []byte {
  16. buf, n, err := new(bytes.Buffer), new(int64), new(error)
  17. o.WriteSignBytes(buf, n, err)
  18. if *err != nil {
  19. panic(err)
  20. }
  21. return buf.Bytes()
  22. }
  23. // HashSignBytes is a convenience method for getting the hash of the bytes of a signable
  24. func HashSignBytes(o Signable) []byte {
  25. return merkle.HashFromBinary(SignBytes(o))
  26. }
  27. //-----------------------------------------------------------------------------
  28. // Account resides in the application state, and is mutated by transactions
  29. // on the blockchain.
  30. // Serialized by binary.[read|write]Reflect
  31. type Account struct {
  32. Address []byte `json:"address"`
  33. PubKey PubKey `json:"pub_key"`
  34. Sequence uint `json:"sequence"`
  35. Balance uint64 `json:"balance"`
  36. Code []byte `json:"code"` // VM code
  37. StorageRoot []byte `json:"storage_root"` // VM storage merkle root.
  38. }
  39. func (acc *Account) Copy() *Account {
  40. accCopy := *acc
  41. return &accCopy
  42. }
  43. func (acc *Account) String() string {
  44. return fmt.Sprintf("Account{%X:%v C:%v S:%X}", acc.Address, acc.PubKey, len(acc.Code), acc.StorageRoot)
  45. }
  46. func AccountEncoder(o interface{}, w io.Writer, n *int64, err *error) {
  47. binary.WriteBinary(o.(*Account), w, n, err)
  48. }
  49. func AccountDecoder(r io.Reader, n *int64, err *error) interface{} {
  50. return binary.ReadBinary(&Account{}, r, n, err)
  51. }
  52. var AccountCodec = binary.Codec{
  53. Encode: AccountEncoder,
  54. Decode: AccountDecoder,
  55. }