func TestBinaryDecode(t *testing.T) {

	privAccount := GenPrivAccount()
	pubKey := privAccount.PubKey
	privKey := privAccount.PrivKey

	msg := CRandBytes(128)
	sig := privKey.Sign(msg)
	t.Logf("msg: %X, sig: %X", msg, sig)

	buf, n, err := new(bytes.Buffer), new(int64), new(error)
	binary.WriteBinary(sig, buf, n, err)
	if *err != nil {
		t.Fatalf("Failed to write Signature: %v", err)
	}

	if len(buf.Bytes()) != ed25519.SignatureSize+3 {
		// 1 byte TypeByte, 2 bytes length, 64 bytes signature bytes
		t.Fatalf("Unexpected signature write size: %v", len(buf.Bytes()))
	}
	if buf.Bytes()[0] != SignatureTypeEd25519 {
		t.Fatalf("Unexpected signature type byte")
	}

	sig2, ok := binary.ReadBinary(SignatureEd25519{}, buf, n, err).(SignatureEd25519)
	if !ok || *err != nil {
		t.Fatalf("Failed to read Signature: %v", err)
	}

	// Test the signature
	if !pubKey.VerifyBytes(msg, sig2) {
		t.Errorf("Account message signature verification failed")
	}
}
Beispiel #2
0
// General Convenience
func SimpleHashFromBinary(item interface{}) []byte {
	hasher, n, err := ripemd160.New(), new(int64), new(error)
	binary.WriteBinary(item, hasher, n, err)
	if *err != nil {
		panic(err)
	}
	return hasher.Sum(nil)
}
Beispiel #3
0
// Writes next msgPacket to w.
// Not goroutine-safe
func (ch *Channel) writeMsgPacketTo(w io.Writer) (n int64, err error) {
	packet := ch.nextMsgPacket()
	log.Debug("Write Msg Packet", "conn", ch.conn, "packet", packet)
	binary.WriteByte(packetTypeMsg, w, &n, &err)
	binary.WriteBinary(packet, w, &n, &err)
	if err != nil {
		ch.recentlySent += n
	}
	return
}
Beispiel #4
0
// NOTE: blocking
// Before creating a peer with newPeer(), perform a handshake on connection.
func peerHandshake(conn net.Conn, ourNodeInfo *types.NodeInfo) (*types.NodeInfo, error) {
	var peerNodeInfo = new(types.NodeInfo)
	var err1 error
	var err2 error
	Parallel(
		func() {
			var n int64
			binary.WriteBinary(ourNodeInfo, conn, &n, &err1)
		},
		func() {
			var n int64
			binary.ReadBinary(peerNodeInfo, conn, &n, &err2)
			log.Info("Peer handshake", "peerNodeInfo", peerNodeInfo)
		})
	if err1 != nil {
		return nil, err1
	}
	if err2 != nil {
		return nil, err2
	}
	return peerNodeInfo, nil
}
Beispiel #5
0
func ValidatorInfoEncoder(o interface{}, w io.Writer, n *int64, err *error) {
	binary.WriteBinary(o.(*ValidatorInfo), w, n, err)
}
Beispiel #6
0
func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int64, err *error) {
	binary.WriteBinary(o.(*Validator), w, n, err)
}
Beispiel #7
0
func AccountEncoder(o interface{}, w io.Writer, n *int64, err *error) {
	binary.WriteBinary(o.(*Account), w, n, err)
}