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 (
|
2023-03-04 21:51:51 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/hash"
|
2023-02-06 22:16:20 +00:00
|
|
|
wpb "github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/store/pb"
|
2022-05-30 18:48:22 +00:00
|
|
|
)
|
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 {
|
2023-02-06 22:16:20 +00:00
|
|
|
msg *wpb.WakuMessage
|
2022-05-30 18:48:22 +00:00
|
|
|
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
|
2023-02-06 22:16:20 +00:00
|
|
|
func NewEnvelope(msg *wpb.WakuMessage, receiverTime int64, pubSubTopic string) *Envelope {
|
2023-03-04 21:51:51 +00:00
|
|
|
messageHash := msg.Hash(pubSubTopic)
|
2023-09-07 21:39:10 +00:00
|
|
|
digest := hash.SHA256([]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,
|
2022-11-03 13:53:33 +00:00
|
|
|
hash: messageHash,
|
2022-05-30 18:48:22 +00:00
|
|
|
index: &pb.Index{
|
2023-09-07 21:39:10 +00:00
|
|
|
Digest: digest[:],
|
2022-05-30 18:48:22 +00:00
|
|
|
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
|
2023-02-06 22:16:20 +00:00
|
|
|
func (e *Envelope) Message() *wpb.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
|
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
func (e *Envelope) Index() *pb.Index {
|
|
|
|
return e.index
|
2022-05-30 18:48:22 +00:00
|
|
|
}
|