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.

74 lines
2.1 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. ptypes "github.com/tendermint/tendermint/permission/types"
  9. )
  10. // Signable is an interface for all signable things.
  11. // It typically removes signatures before serializing.
  12. type Signable interface {
  13. WriteSignBytes(chainID string, w io.Writer, n *int64, err *error)
  14. }
  15. // SignBytes is a convenience method for getting the bytes to sign of a Signable.
  16. func SignBytes(chainID string, o Signable) []byte {
  17. buf, n, err := new(bytes.Buffer), new(int64), new(error)
  18. o.WriteSignBytes(chainID, buf, n, err)
  19. if *err != nil {
  20. // SOMETHING HAS GONE HORRIBLY WRONG
  21. panic(err)
  22. }
  23. return buf.Bytes()
  24. }
  25. // HashSignBytes is a convenience method for getting the hash of the bytes of a signable
  26. func HashSignBytes(chainID string, o Signable) []byte {
  27. return merkle.SimpleHashFromBinary(SignBytes(chainID, o))
  28. }
  29. //-----------------------------------------------------------------------------
  30. // Account resides in the application state, and is mutated by transactions
  31. // on the blockchain.
  32. // Serialized by binary.[read|write]Reflect
  33. type Account struct {
  34. Address []byte `json:"address"`
  35. PubKey PubKey `json:"pub_key"`
  36. Sequence int `json:"sequence"`
  37. Balance int64 `json:"balance"`
  38. Code []byte `json:"code"` // VM code
  39. StorageRoot []byte `json:"storage_root"` // VM storage merkle root.
  40. Permissions ptypes.AccountPermissions `json:"permissions"`
  41. }
  42. func (acc *Account) Copy() *Account {
  43. accCopy := *acc
  44. return &accCopy
  45. }
  46. func (acc *Account) String() string {
  47. if acc == nil {
  48. return "nil-Account"
  49. }
  50. 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)
  51. }
  52. func AccountEncoder(o interface{}, w io.Writer, n *int64, err *error) {
  53. binary.WriteBinary(o.(*Account), w, n, err)
  54. }
  55. func AccountDecoder(r io.Reader, n *int64, err *error) interface{} {
  56. return binary.ReadBinary(&Account{}, r, n, err)
  57. }
  58. var AccountCodec = binary.Codec{
  59. Encode: AccountEncoder,
  60. Decode: AccountDecoder,
  61. }