go-waku/waku/v2/protocol/envelope.go

54 lines
1.5 KiB
Go
Raw Normal View History

2021-04-22 00:09:37 +00:00
package protocol
import (
"github.com/waku-org/go-waku/waku/v2/hash"
wpb "github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/protocol/store/pb"
)
// Envelope contains information about the pubsub topic of a WakuMessage
// and a hash used to identify a message based on the bytes of a WakuMessage
// protobuffer
type Envelope struct {
msg *wpb.WakuMessage
hash []byte
index *pb.Index
}
2021-10-09 18:18:53 +00:00
// NewEnvelope creates a new Envelope that contains a WakuMessage
// It's used as a way to know to which Pubsub topic belongs a WakuMessage
// as well as generating a hash based on the bytes that compose the message
func NewEnvelope(msg *wpb.WakuMessage, receiverTime int64, pubSubTopic string) *Envelope {
messageHash := msg.Hash(pubSubTopic)
digest := hash.SHA256([]byte(msg.ContentTopic), msg.Payload)
return &Envelope{
msg: msg,
hash: messageHash,
index: &pb.Index{
Digest: digest[:],
ReceiverTime: receiverTime,
SenderTime: msg.GetTimestamp(),
PubsubTopic: pubSubTopic,
},
}
}
2021-10-09 18:18:53 +00:00
// Message returns the WakuMessage associated to an Envelope
func (e *Envelope) Message() *wpb.WakuMessage {
return e.msg
}
2021-10-09 18:18:53 +00:00
// PubsubTopic returns the topic on which a WakuMessage was received
func (e *Envelope) PubsubTopic() string {
return e.index.PubsubTopic
}
2021-10-09 18:18:53 +00:00
// Hash returns a 32 byte hash calculated from the WakuMessage bytes
2021-04-11 23:43:59 +00:00
func (e *Envelope) Hash() []byte {
return e.hash
}
func (e *Envelope) Index() *pb.Index {
return e.index
}