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

44 lines
1.1 KiB
Go
Raw Normal View History

2021-04-22 00:09:37 +00:00
package protocol
2021-04-22 00:09:37 +00:00
import "github.com/status-im/go-waku/waku/v2/protocol/pb"
type Envelope struct {
msg *pb.WakuMessage
pubsubTopic string
size int
hash []byte
}
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 *pb.WakuMessage, pubSubTopic string) *Envelope {
data, _ := msg.Marshal()
return &Envelope{
msg: msg,
pubsubTopic: pubSubTopic,
size: len(data),
hash: pb.Hash(data),
}
}
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 {
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.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
}
2021-10-09 18:18:53 +00:00
// Size returns the byte size of the WakuMessage
func (e *Envelope) Size() int {
return e.size
}