tendermint/blocks/account.go

54 lines
1.3 KiB
Go
Raw Normal View History

2014-06-05 02:34:45 -07:00
package blocks
import (
. "github.com/tendermint/tendermint/binary"
"io"
)
2014-06-05 11:04:56 -07:00
type AccountId struct {
Type Byte
Number UInt64
PubKey ByteSlice
2014-06-05 02:34:45 -07:00
}
const (
2014-06-05 11:04:56 -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-06-05 11:04:56 -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:04:56 -07:00
func (self *AccountId) WriteTo(w io.Writer) (n int64, err error) {
2014-06-05 02:34:45 -07:00
var n_ int64
2014-06-05 11:04:56 -07:00
n_, err = self.Type.WriteTo(w)
2014-06-05 02:34:45 -07:00
n += n_; if err != nil { return n, err }
2014-06-05 11:04:56 -07:00
if self.Type == ACCOUNT_TYPE_NUMBER ||
self.Type == ACCOUNT_TYPE_BOTH {
n_, err = self.Number.WriteTo(w)
n += n_; if err != nil { return n, err }
}
if self.Type == ACCOUNT_TYPE_PUBKEY ||
self.Type == ACCOUNT_TYPE_BOTH {
n_, err = self.PubKey.WriteTo(w)
n += n_; if err != nil { return n, err }
}
return
2014-06-05 02:34:45 -07:00
}
2014-06-05 11:04:56 -07:00
func AccountNumber(n UInt64) AccountId {
return AccountId{ACCOUNT_TYPE_NUMBER, n, nil}
2014-06-05 02:34:45 -07:00
}