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.

68 lines
1.9 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(chainID string, 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(chainID string, o Signable) []byte {
  16. buf, n, err := new(bytes.Buffer), new(int64), new(error)
  17. o.WriteSignBytes(chainID, buf, n, err)
  18. if *err != nil {
  19. // SOMETHING HAS GONE HORRIBLY WRONG
  20. panic(err)
  21. }
  22. return buf.Bytes()
  23. }
  24. // HashSignBytes is a convenience method for getting the hash of the bytes of a signable
  25. func HashSignBytes(chainID string, o Signable) []byte {
  26. return merkle.SimpleHashFromBinary(SignBytes(chainID, o))
  27. }
  28. //-----------------------------------------------------------------------------
  29. // Account resides in the application state, and is mutated by transactions
  30. // on the blockchain.
  31. // Serialized by binary.[read|write]Reflect
  32. type Account struct {
  33. Address []byte `json:"address"`
  34. PubKey PubKey `json:"pub_key"`
  35. Sequence int `json:"sequence"`
  36. Balance int64 `json:"balance"`
  37. Code []byte `json:"code"` // VM code
  38. StorageRoot []byte `json:"storage_root"` // VM storage merkle root.
  39. }
  40. func (acc *Account) Copy() *Account {
  41. accCopy := *acc
  42. return &accCopy
  43. }
  44. func (acc *Account) String() string {
  45. return fmt.Sprintf("Account{%X:%v C:%v S:%X}", acc.Address, acc.PubKey, len(acc.Code), acc.StorageRoot)
  46. }
  47. func AccountEncoder(o interface{}, w io.Writer, n *int64, err *error) {
  48. binary.WriteBinary(o.(*Account), w, n, err)
  49. }
  50. func AccountDecoder(r io.Reader, n *int64, err *error) interface{} {
  51. return binary.ReadBinary(&Account{}, r, n, err)
  52. }
  53. var AccountCodec = binary.Codec{
  54. Encode: AccountEncoder,
  55. Decode: AccountDecoder,
  56. }