generate hashes of messages

This commit is contained in:
Richard Ramos 2021-04-11 19:43:59 -04:00
parent c7550f29ce
commit f9e7589ffd
No known key found for this signature in database
GPG Key ID: 80D4B01265FDFE8F
2 changed files with 14 additions and 12 deletions

View File

@ -5,10 +5,10 @@ import "github.com/status-im/go-waku/waku/v2/protocol"
type Envelope struct { type Envelope struct {
msg *protocol.WakuMessage msg *protocol.WakuMessage
size int 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{ return &Envelope{
msg: msg, msg: msg,
size: size, size: size,
@ -20,7 +20,7 @@ func (e *Envelope) Message() *protocol.WakuMessage {
return e.msg return e.msg
} }
func (e *Envelope) Hash() [32]byte { func (e *Envelope) Hash() []byte {
return e.hash return e.hash
} }

View File

@ -3,13 +3,13 @@ package node
import ( import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/sha256"
"errors" "errors"
"fmt" "fmt"
"net" "net"
"sync" "sync"
"time" "time"
gcrypto "github.com/ethereum/go-ethereum/crypto"
proto "github.com/golang/protobuf/proto" proto "github.com/golang/protobuf/proto"
logging "github.com/ipfs/go-log" logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p"
@ -272,7 +272,7 @@ func (node *WakuNode) Subscribe(topic *Topic) (*Subscription, error) {
return 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 subscription.C <- envelope
} }
} }
@ -323,37 +323,39 @@ func (node *WakuNode) upsertTopic(topic *Topic) (*wakurelay.Topic, error) {
return pubSubTopic, nil 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 // Publish a `WakuMessage` to a PubSub topic. `WakuMessage` should contain a
// `contentTopic` field for light node functionality. This field may be also // `contentTopic` field for light node functionality. This field may be also
// be omitted. // be omitted.
if node.pubsub == nil { 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 { 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) pubSubTopic, err := node.upsertTopic(topic)
if err != nil { if err != nil {
return err return nil, err
} }
out, err := proto.Marshal(message) out, err := proto.Marshal(message)
if err != nil { if err != nil {
return err return nil, err
} }
err = pubSubTopic.Publish(node.ctx, out) err = pubSubTopic.Publish(node.ctx, out)
if err != nil { if err != nil {
return err return nil, err
} }
return nil hash := gcrypto.Keccak256(out)
return hash, nil
} }
func (w *WakuNode) DialPeer(address string) error { func (w *WakuNode) DialPeer(address string) error {