From f9e7589ffdd9219e6d55f55cf6954da9fa42f0e8 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Sun, 11 Apr 2021 19:43:59 -0400 Subject: [PATCH] generate hashes of messages --- waku/common/envelope.go | 6 +++--- waku/v2/node/wakunode2.go | 20 +++++++++++--------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/waku/common/envelope.go b/waku/common/envelope.go index 6b623644..fbc50db6 100644 --- a/waku/common/envelope.go +++ b/waku/common/envelope.go @@ -5,10 +5,10 @@ import "github.com/status-im/go-waku/waku/v2/protocol" type Envelope struct { msg *protocol.WakuMessage size int - hash [32]byte + hash []byte } -func NewEnvelope(msg *protocol.WakuMessage, size int, hash [32]byte) *Envelope { +func NewEnvelope(msg *protocol.WakuMessage, size int, hash []byte) *Envelope { return &Envelope{ msg: msg, size: size, @@ -20,7 +20,7 @@ func (e *Envelope) Message() *protocol.WakuMessage { return e.msg } -func (e *Envelope) Hash() [32]byte { +func (e *Envelope) Hash() []byte { return e.hash } diff --git a/waku/v2/node/wakunode2.go b/waku/v2/node/wakunode2.go index 6b28b0f8..fecdb323 100644 --- a/waku/v2/node/wakunode2.go +++ b/waku/v2/node/wakunode2.go @@ -3,13 +3,13 @@ package node import ( "context" "crypto/ecdsa" - "crypto/sha256" "errors" "fmt" "net" "sync" "time" + gcrypto "github.com/ethereum/go-ethereum/crypto" proto "github.com/golang/protobuf/proto" logging "github.com/ipfs/go-log" "github.com/libp2p/go-libp2p" @@ -272,7 +272,7 @@ func (node *WakuNode) Subscribe(topic *Topic) (*Subscription, error) { return } - envelope := common.NewEnvelope(wakuMessage, len(msg.Data), sha256.Sum256(msg.Data)) + envelope := common.NewEnvelope(wakuMessage, len(msg.Data), gcrypto.Keccak256(msg.Data)) subscription.C <- envelope } } @@ -323,37 +323,39 @@ func (node *WakuNode) upsertTopic(topic *Topic) (*wakurelay.Topic, error) { return pubSubTopic, nil } -func (node *WakuNode) Publish(message *protocol.WakuMessage, topic *Topic) error { +func (node *WakuNode) Publish(message *protocol.WakuMessage, topic *Topic) ([]byte, error) { // Publish a `WakuMessage` to a PubSub topic. `WakuMessage` should contain a // `contentTopic` field for light node functionality. This field may be also // be omitted. if node.pubsub == nil { - return errors.New("PubSub hasn't been set. Execute mountWakuRelay() or setPubSub() first") + return nil, errors.New("PubSub hasn't been set. Execute mountWakuRelay() or setPubSub() first") } if message == nil { - return errors.New("message can't be null") + return nil, errors.New("message can't be null") } pubSubTopic, err := node.upsertTopic(topic) if err != nil { - return err + return nil, err } out, err := proto.Marshal(message) if err != nil { - return err + return nil, err } err = pubSubTopic.Publish(node.ctx, out) if err != nil { - return err + return nil, err } - return nil + hash := gcrypto.Keccak256(out) + + return hash, nil } func (w *WakuNode) DialPeer(address string) error {