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.

48 lines
1.1 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package blocks
  2. import (
  3. . "github.com/tendermint/tendermint/binary"
  4. . "github.com/tendermint/tendermint/common"
  5. "io"
  6. )
  7. type AccountId struct {
  8. Type Byte
  9. Number UInt64
  10. PubKey ByteSlice
  11. }
  12. const (
  13. ACCOUNT_TYPE_NUMBER = Byte(0x01)
  14. ACCOUNT_TYPE_PUBKEY = Byte(0x02)
  15. ACCOUNT_TYPE_BOTH = Byte(0x03)
  16. )
  17. func ReadAccountId(r io.Reader) AccountId {
  18. switch t := ReadByte(r); t {
  19. case ACCOUNT_TYPE_NUMBER:
  20. return AccountId{t, ReadUInt64(r), nil}
  21. case ACCOUNT_TYPE_PUBKEY:
  22. return AccountId{t, 0, ReadByteSlice(r)}
  23. case ACCOUNT_TYPE_BOTH:
  24. return AccountId{t, ReadUInt64(r), ReadByteSlice(r)}
  25. default:
  26. Panicf("Unknown AccountId type %x", t)
  27. return AccountId{}
  28. }
  29. }
  30. func (self AccountId) WriteTo(w io.Writer) (n int64, err error) {
  31. n, err = WriteTo(self.Type, w, n, err)
  32. if self.Type == ACCOUNT_TYPE_NUMBER || self.Type == ACCOUNT_TYPE_BOTH {
  33. n, err = WriteTo(self.Number, w, n, err)
  34. }
  35. if self.Type == ACCOUNT_TYPE_PUBKEY || self.Type == ACCOUNT_TYPE_BOTH {
  36. n, err = WriteTo(self.PubKey, w, n, err)
  37. }
  38. return
  39. }
  40. func AccountNumber(n UInt64) AccountId {
  41. return AccountId{ACCOUNT_TYPE_NUMBER, n, nil}
  42. }