From 8d558630caa1ddaaec5965323621de7ab7f2c714 Mon Sep 17 00:00:00 2001 From: Benjie Date: Wed, 26 Jul 2023 19:12:23 +0100 Subject: [PATCH] fix(p2p): handshake signature --- CHANGELOG.md | 2 ++ p2p/peer.go | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2cc0d15..189f942e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Fixed ABI encoder for `SymbolCode` type. +* Fixed P2P HandshakeMessage signature generation + #### Deprecated ### [**0.10.2**](https://github.com/eoscanada/eos-go/releases/tag/v0.10.2) (January 19th, 2022) diff --git a/p2p/peer.go b/p2p/peer.go index a69bc671..b658552f 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -4,6 +4,8 @@ import ( "bufio" "bytes" "crypto/rand" + "crypto/sha256" + "encoding/binary" "encoding/hex" "fmt" "io" @@ -264,18 +266,23 @@ func (p *Peer) SendTime() error { } func (p *Peer) SendHandshake(info *HandshakeInfo) error { - publicKey, err := ecc.NewPublicKey("PUB_K1_1111111111111111111111111111111114T1Anm") + privateKey, err := ecc.NewRandomPrivateKey() if err != nil { - return fmt.Errorf("sending handshake to %s: create public key: %w", p.Address, err) + return fmt.Errorf("sending handshake to %s: create private key: %w", p.Address, err) } + publicKey := privateKey.PublicKey() zlog.Debug("SendHandshake", zap.String("peer", p.Address), zap.Object("info", info)) tstamp := eos.Tstamp{Time: info.HeadBlockTime} - signature := ecc.Signature{ - Curve: ecc.CurveK1, - Content: make([]byte, 65, 65), + timestampBytes := make([]byte, 8) + binary.LittleEndian.PutUint64(timestampBytes, uint64(tstamp.Time.UnixNano())) + + token := sha256.Sum256(timestampBytes) + signature, err := privateKey.Sign(token[:]) + if err != nil { + return fmt.Errorf("sending handshake to %s: sign timestamp: %w", p.Address, err) } handshake := &eos.HandshakeMessage{ @@ -284,7 +291,7 @@ func (p *Peer) SendHandshake(info *HandshakeInfo) error { NodeID: p.NodeID, Key: publicKey, Time: tstamp, - Token: make([]byte, 32, 32), + Token: token[:], Signature: signature, P2PAddress: p.Name, LastIrreversibleBlockNum: info.LastIrreversibleBlockNum,