2021-04-21 20:09:37 -04:00
|
|
|
package protocol
|
2021-03-30 10:13:33 -04:00
|
|
|
|
2022-05-30 14:48:22 -04:00
|
|
|
import (
|
2023-10-30 16:25:21 -04:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/hash"
|
2023-02-06 18:16:20 -04: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 14:48:22 -04:00
|
|
|
)
|
2021-03-30 10:13:33 -04:00
|
|
|
|
2021-11-05 16:09:48 -04: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 10:13:33 -04:00
|
|
|
type Envelope struct {
|
2023-02-06 18:16:20 -04:00
|
|
|
msg *wpb.WakuMessage
|
2022-05-30 14:48:22 -04:00
|
|
|
hash []byte
|
|
|
|
index *pb.Index
|
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
|
2023-02-06 18:16:20 -04:00
|
|
|
func NewEnvelope(msg *wpb.WakuMessage, receiverTime int64, pubSubTopic string) *Envelope {
|
2023-03-04 17:51:51 -04:00
|
|
|
messageHash := msg.Hash(pubSubTopic)
|
2023-10-30 16:25:21 -04:00
|
|
|
digest := hash.SHA256([]byte(msg.ContentTopic), msg.Payload)
|
2021-03-30 10:13:33 -04:00
|
|
|
return &Envelope{
|
2022-05-30 14:48:22 -04:00
|
|
|
msg: msg,
|
2022-11-03 09:53:33 -04:00
|
|
|
hash: messageHash,
|
2022-05-30 14:48:22 -04:00
|
|
|
index: &pb.Index{
|
2023-10-30 16:25:21 -04:00
|
|
|
Digest: digest[:],
|
2022-05-30 14:48:22 -04:00
|
|
|
ReceiverTime: receiverTime,
|
2023-11-07 15:48:43 -04:00
|
|
|
SenderTime: msg.GetTimestamp(),
|
2022-05-30 14:48:22 -04:00
|
|
|
PubsubTopic: pubSubTopic,
|
|
|
|
},
|
2021-03-30 10:13:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 14:18:53 -04:00
|
|
|
// Message returns the WakuMessage associated to an Envelope
|
2023-02-06 18:16:20 -04:00
|
|
|
func (e *Envelope) Message() *wpb.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 {
|
2022-05-30 14:48:22 -04:00
|
|
|
return e.index.PubsubTopic
|
2021-04-28 11:11:32 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-11 10:24:05 -04:00
|
|
|
func (e *Envelope) Index() *pb.Index {
|
|
|
|
return e.index
|
2022-05-30 14:48:22 -04:00
|
|
|
}
|