Example #1
0
func (p *Peer) handleHello(m *Manager, hello *protocol.Hello) {
	cookie, err := p.getSessionCookie()
	if err != nil {
		glog.Errorf("%s:Bad cookie: %s", p.String(), err.Error())
		p.UpdateStatus(HelloFailed)
		return
	}
	pubKey, err := crypto.ParsePublicKeyFromHash(hello.GetNodePublic())
	if err != nil {
		glog.Errorf("Bad public key: %X", hello.GetNodePublic())
		p.UpdateStatus(HelloFailed)
		return
	}
	ok, err := crypto.Verify(pubKey.SerializeUncompressed(), hello.GetNodeProof(), cookie)
	if !ok {
		glog.Errorf("%s:Bad signature: %X public key: %X hash: %X", p.String(), hello.GetNodeProof(), hello.GetNodePublic(), cookie)
		p.UpdateStatus(HelloFailed)
		return
	}
	if err != nil {
		glog.Errorf("%s:Bad signature verification: %s", p.String(), err.Error())
		p.UpdateStatus(HelloFailed)
		return
	}
	proof, err := m.Key.Sign(cookie)
	if err != nil {
		glog.Errorf("%s:Bad signature creation: %X", p.String(), cookie)
		p.UpdateStatus(HelloFailed)
		return
	}
	if err := p.ProcessHello(hello); err != nil {
		glog.Errorf("%s:%s", p.String(), err.Error())
		return
	}
	port, _ := strconv.ParseUint(m.Port, 10, 32)
	p.Outgoing <- &protocol.TMHello{
		FullVersion:     proto.String(m.Name),
		ProtoVersion:    proto.Uint32(uint32(maxVersion)),
		ProtoVersionMin: proto.Uint32(uint32(minVersion)),
		NodePublic:      []byte(m.PublicKey.String()),
		NodeProof:       proof,
		Ipv4Port:        proto.Uint32(uint32(port)),
		NetTime:         proto.Uint64(uint64(data.Now().Uint32())),
		NodePrivate:     proto.Bool(true),
		TestNet:         proto.Bool(false),
	}
	if hello.ProofOfWork != nil {
		go p.handleProofOfWork(hello.ProofOfWork)
	}
}
Example #2
0
func (m *TMHello) Extend() (ExtendedMessage, error) {
	key, err := crypto.ParsePublicKeyFromHash(m.NodePublic)
	if err != nil {
		return nil, err
	}
	sig, err := crypto.ParseSignature(m.NodeProof)
	if err != nil {
		return nil, err
	}
	return &Hello{
		TMHello:    m,
		Version:    fmt.Sprintf("%d.%d", m.GetProtoVersion()>>16, m.GetProtoVersion()&0xFFFF),
		MinVersion: fmt.Sprintf("%d.%d", m.GetProtoVersionMin()>>16, m.GetProtoVersionMin()&0xFFFF),
		PublicKey:  key,
		Signature:  sig,
	}, nil
}