2021-04-22 00:09:37 +00:00
|
|
|
package protocol
|
2021-03-30 14:13:33 +00:00
|
|
|
|
2021-04-22 00:09:37 +00:00
|
|
|
import "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 {
|
2021-04-28 15:11:32 +00:00
|
|
|
msg *pb.WakuMessage
|
|
|
|
pubsubTopic string
|
|
|
|
size int
|
|
|
|
hash []byte
|
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
|
2021-04-28 20:10:44 +00:00
|
|
|
func NewEnvelope(msg *pb.WakuMessage, pubSubTopic string) *Envelope {
|
|
|
|
data, _ := msg.Marshal()
|
2021-03-30 14:13:33 +00:00
|
|
|
return &Envelope{
|
2021-04-28 15:11:32 +00:00
|
|
|
msg: msg,
|
|
|
|
pubsubTopic: pubSubTopic,
|
2021-04-28 20:10:44 +00:00
|
|
|
size: len(data),
|
|
|
|
hash: pb.Hash(data),
|
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 {
|
|
|
|
return e.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 {
|
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
|
|
|
|
}
|