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.

47 lines
1.2 KiB

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