tendermint/blocks/account.go

49 lines
1.1 KiB
Go
Raw Normal View History

2014-06-05 02:34:45 -07:00
package blocks
import (
2014-07-01 14:50:24 -07:00
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
"io"
2014-06-05 02:34:45 -07:00
)
2014-06-05 11:04:56 -07:00
type AccountId struct {
2014-07-01 14:50:24 -07:00
Type Byte
Number UInt64
PubKey ByteSlice
2014-06-05 02:34:45 -07:00
}
const (
2014-07-01 14:50:24 -07:00
ACCOUNT_TYPE_NUMBER = Byte(0x01)
ACCOUNT_TYPE_PUBKEY = Byte(0x02)
ACCOUNT_TYPE_BOTH = Byte(0x03)
2014-06-05 02:34:45 -07:00
)
func ReadAccountId(r io.Reader) AccountId {
2014-07-01 14:50:24 -07:00
switch t := ReadByte(r); t {
case ACCOUNT_TYPE_NUMBER:
return AccountId{t, ReadUInt64(r), nil}
case ACCOUNT_TYPE_PUBKEY:
return AccountId{t, 0, ReadByteSlice(r)}
case ACCOUNT_TYPE_BOTH:
return AccountId{t, ReadUInt64(r), ReadByteSlice(r)}
default:
Panicf("Unknown AccountId type %x", t)
return AccountId{}
}
2014-06-05 02:34:45 -07:00
}
2014-06-05 11:45:18 -07:00
func (self AccountId) WriteTo(w io.Writer) (n int64, err error) {
2014-07-01 14:50:24 -07:00
n, err = WriteOnto(self.Type, w, n, err)
if self.Type == ACCOUNT_TYPE_NUMBER || self.Type == ACCOUNT_TYPE_BOTH {
n, err = WriteOnto(self.Number, w, n, err)
}
if self.Type == ACCOUNT_TYPE_PUBKEY || self.Type == ACCOUNT_TYPE_BOTH {
n, err = WriteOnto(self.PubKey, w, n, err)
}
return
2014-06-05 02:34:45 -07:00
}
2014-06-05 11:04:56 -07:00
func AccountNumber(n UInt64) AccountId {
2014-07-01 14:50:24 -07:00
return AccountId{ACCOUNT_TYPE_NUMBER, n, nil}
2014-06-05 02:34:45 -07:00
}