コード例 #1
0
ファイル: msg.go プロジェクト: nictuku/bitz
// XXX return errors too
func readVarIntList(r io.Reader) []uint64 {
	length, _, err := encVarint.ReadVarInt(r)
	if err != nil {
		log.Println(err)
		return nil
	}
	x := make([]uint64, length)
	for i := range x {
		if x[i], _, err = encVarint.ReadVarInt(r); err != nil {
			log.Println(err)
			return nil
		}
	}
	return x
}
コード例 #2
0
ファイル: msg.go プロジェクト: nictuku/bitz
func parseMsg(r io.Reader) (m msg, err error) {
	m.PowNonce = readBytes8(r)
	// TODO:
	buf := new(bytes.Buffer)
	if _, err := io.Copy(buf, r); err != nil {
		return m, err
	}
	r = buf
	if err := checkProofOfWork(buf.Bytes(), m.PowNonce); err != nil {
		return m, err
	}
	// TODO: Soon moving to uint32 in the wire.
	m.Time = uint64(readUint32(r))
	m.StreamNumber, _, err = encVarint.ReadVarInt(r)
	if err != nil {
		return m, fmt.Errorf("parseMsg reading Stream Number: %v\n", err)
	}
	buf = new(bytes.Buffer)
	if n, err := io.Copy(buf, r); err != nil {
		return m, err
	} else if n == 0 {
		return m, fmt.Errorf("parseMsg Encrypted content empty")
	}
	m.Encrypted = buf.Bytes()
	return m, nil
}
コード例 #3
0
ファイル: msg.go プロジェクト: nictuku/bitz
func parseInv(r io.Reader) ([]inventoryVector, error) {
	count, _, err := encVarint.ReadVarInt(r)
	if err != nil {
		return nil, err
	}
	ivs := make([]inventoryVector, count)
	err = binary.Read(r, binary.BigEndian, ivs)
	return ivs, err
}
コード例 #4
0
ファイル: msg_test.go プロジェクト: nictuku/bitz
func TestVarIntDecode(t *testing.T) {
	// This was obtained by debugging a PyBitMessage message. Only the first 4
	// bytes are relevant. The problem I had was with endianess if the varint
	// decoding library (bitcoin and bitmessage use different endianess.)
	count, x, err := encVarint.ReadVarInt(bytes.NewBuffer([]byte("\xfd\x10y\xc2\xeb\xbf\x12\xe0k:")))
	if err != nil {
		t.Errorf("got err: %v", err.Error())
	}
	if count != 4217 {
		t.Errorf("wanted 4217, got %d (%d)", count, x)
	}
}
コード例 #5
0
ファイル: msg.go プロジェクト: nictuku/bitz
func readNetworkAddressList(r io.Reader) ([]extendedNetworkAddress, error) {
	length, _, err := encVarint.ReadVarInt(r)
	if err != nil {
		return nil, err
	}
	log.Println("entries", length)
	addrs := make([]extendedNetworkAddress, length)
	for i := range addrs {
		addr := extendedNetworkAddress{}
		if err := binary.Read(r, binary.BigEndian, &addr); err != nil {
			return nil, err
		}
		addrs[i] = addr
	}
	return addrs, nil
}
コード例 #6
0
ファイル: msg.go プロジェクト: nictuku/bitz
func parseBroadcast(r io.Reader) (b broadcast, err error) {
	b.PowNonce = readBytes8(r)
	buf := new(bytes.Buffer)
	if _, err := io.Copy(buf, r); err != nil {
		return b, err
	}
	r = buf
	if err := checkProofOfWork(buf.Bytes(), b.PowNonce); err != nil {
		return b, err
	}
	// TODO: Soon moving to uint32 in the wire.
	b.Time = uint64(readUint32(r))
	b.BroadcastVersion, _, err = encVarint.ReadVarInt(r)
	if err != nil {
		return b, fmt.Errorf("parseBroadcast reading broadcast version: %v\n", err)
	}
	if b.BroadcastVersion != 1 {
		return b, fmt.Errorf("I do not yet support Broadcasts of version %d", b.BroadcastVersion)
	}
	b.AddressVersion, _, err = encVarint.ReadVarInt(r)
	if err != nil {
		return b, fmt.Errorf("parseBroadcast reading address version: %v\n", err)
	}
	b.StreamNumber, _, err = encVarint.ReadVarInt(r)
	if err != nil {
		return b, fmt.Errorf("parseBroadcast reading Stream Number: %v\n", err)
	}
	b.Behavior = readUint32(r)
	if b.Behavior != 1 {
		log.Printf("warning: parseBroadcast unknown behavior mask: %x\n", b.Behavior)
	}
	if err = binary.Read(r, binary.BigEndian, &b.PublicSigningKey); err != nil {
		return b, fmt.Errorf("parseBroadcast PublicSigningKey err: %v\n", err)
	}
	if err = binary.Read(r, binary.BigEndian, &b.PublicEncryptionKey); err != nil {
		return b, fmt.Errorf("parseBroadcast PublicEncryptionKey err: %v\n", err)
	}
	if err = binary.Read(r, binary.BigEndian, &b.AddressHash); err != nil {
		return b, fmt.Errorf("parseBroadcast AddressHash err: %v\n", err)
	}
	// PyBitMessage just writes '\x02'.
	b.Encoding, _, err = encVarint.ReadVarInt(r)
	if err != nil {
		return b, fmt.Errorf("parseBroadcast reading encoding: %v\n", err)
	}
	if b.MessageLength, _, err = encVarint.ReadVarInt(r); err != nil {
		return b, fmt.Errorf("parseBroadcast reading message length: %v\n", err)
	}
	b.Message = make([]byte, b.MessageLength)
	if _, err = r.Read(b.Message); err != nil {
		return b, fmt.Errorf("parseBroadcast reading message: %v\n", err)
	}
	if b.SigLength, _, err = encVarint.ReadVarInt(r); err != nil {
		return b, fmt.Errorf("parseBroadcast reading siglength: %v\n", err)
	}
	b.Signature = make([]byte, b.SigLength)
	if _, err = r.Read(b.Message); err != nil {
		return b, fmt.Errorf("parseBroadcast reading signature: %v\n", err)
	}
	return b, nil
}