2021-04-21 20:09:37 -04:00
|
|
|
package protocol
|
2021-03-30 10:13:33 -04:00
|
|
|
|
2021-04-21 20:09:37 -04:00
|
|
|
import "github.com/status-im/go-waku/waku/v2/protocol/pb"
|
2021-03-30 10:13:33 -04:00
|
|
|
|
|
|
|
type Envelope struct {
|
2021-04-28 11:11:32 -04:00
|
|
|
msg *pb.WakuMessage
|
|
|
|
pubsubTopic string
|
|
|
|
size int
|
|
|
|
hash []byte
|
2021-03-30 10:13:33 -04:00
|
|
|
}
|
|
|
|
|
2021-10-09 14:18:53 -04: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 16:10:44 -04:00
|
|
|
func NewEnvelope(msg *pb.WakuMessage, pubSubTopic string) *Envelope {
|
|
|
|
data, _ := msg.Marshal()
|
2021-03-30 10:13:33 -04:00
|
|
|
return &Envelope{
|
2021-04-28 11:11:32 -04:00
|
|
|
msg: msg,
|
|
|
|
pubsubTopic: pubSubTopic,
|
2021-04-28 16:10:44 -04:00
|
|
|
size: len(data),
|
|
|
|
hash: pb.Hash(data),
|
2021-03-30 10:13:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 14:18:53 -04:00
|
|
|
// Message returns the WakuMessage associated to an Envelope
|
2021-04-21 20:09:37 -04:00
|
|
|
func (e *Envelope) Message() *pb.WakuMessage {
|
2021-03-30 10:13:33 -04:00
|
|
|
return e.msg
|
|
|
|
}
|
|
|
|
|
2021-10-09 14:18:53 -04:00
|
|
|
// PubsubTopic returns the topic on which a WakuMessage was received
|
2021-04-28 11:11:32 -04:00
|
|
|
func (e *Envelope) PubsubTopic() string {
|
|
|
|
return e.pubsubTopic
|
|
|
|
}
|
|
|
|
|
2021-10-09 14:18:53 -04:00
|
|
|
// Hash returns a 32 byte hash calculated from the WakuMessage bytes
|
2021-04-11 19:43:59 -04:00
|
|
|
func (e *Envelope) Hash() []byte {
|
2021-03-30 10:13:33 -04:00
|
|
|
return e.hash
|
|
|
|
}
|
|
|
|
|
2021-10-09 14:18:53 -04:00
|
|
|
// Size returns the byte size of the WakuMessage
|
2021-03-30 10:13:33 -04:00
|
|
|
func (e *Envelope) Size() int {
|
|
|
|
return e.size
|
|
|
|
}
|