2021-04-22 00:09:37 +00:00
|
|
|
package protocol
|
2021-03-30 14:13:33 +00:00
|
|
|
|
2022-05-30 18:48:22 +00:00
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
|
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/pb"
|
|
|
|
)
|
2021-03-30 14:13:33 +00:00
|
|
|
|
2021-11-05 20:09:48 +00:00
|
|
|
// 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
|
2021-03-30 14:13:33 +00:00
|
|
|
type Envelope struct {
|
2022-05-30 18:48:22 +00:00
|
|
|
msg *pb.WakuMessage
|
|
|
|
size int
|
|
|
|
hash []byte
|
|
|
|
index *pb.Index
|
2021-03-30 14:13:33 +00:00
|
|
|
}
|
|
|
|
|
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
|
2022-05-30 18:48:22 +00:00
|
|
|
func NewEnvelope(msg *pb.WakuMessage, receiverTime int64, pubSubTopic string) *Envelope {
|
2021-04-28 20:10:44 +00:00
|
|
|
data, _ := msg.Marshal()
|
2022-05-30 18:48:22 +00:00
|
|
|
hash := sha256.Sum256(append([]byte(msg.ContentTopic), msg.Payload...))
|
|
|
|
|
2021-03-30 14:13:33 +00:00
|
|
|
return &Envelope{
|
2022-05-30 18:48:22 +00:00
|
|
|
msg: msg,
|
|
|
|
size: len(data),
|
|
|
|
hash: pb.Hash(data),
|
|
|
|
index: &pb.Index{
|
|
|
|
Digest: hash[:],
|
|
|
|
ReceiverTime: receiverTime,
|
|
|
|
SenderTime: msg.Timestamp,
|
|
|
|
PubsubTopic: pubSubTopic,
|
|
|
|
},
|
2021-03-30 14:13:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:18:53 +00:00
|
|
|
// Message returns the WakuMessage associated to an Envelope
|
2021-04-22 00:09:37 +00:00
|
|
|
func (e *Envelope) Message() *pb.WakuMessage {
|
2021-03-30 14:13:33 +00:00
|
|
|
return e.msg
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:18:53 +00:00
|
|
|
// PubsubTopic returns the topic on which a WakuMessage was received
|
2021-04-28 15:11:32 +00:00
|
|
|
func (e *Envelope) PubsubTopic() string {
|
2022-05-30 18:48:22 +00:00
|
|
|
return e.index.PubsubTopic
|
2021-04-28 15:11:32 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-03-30 14:13:33 +00:00
|
|
|
return e.hash
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:18:53 +00:00
|
|
|
// Size returns the byte size of the WakuMessage
|
2021-03-30 14:13:33 +00:00
|
|
|
func (e *Envelope) Size() int {
|
|
|
|
return e.size
|
|
|
|
}
|
2022-05-30 18:48:22 +00:00
|
|
|
|
|
|
|
func (env *Envelope) Index() *pb.Index {
|
|
|
|
return env.index
|
|
|
|
}
|