use 23/WAKU2-TOPICS format for content topics (#2281)

This commit is contained in:
RichΛrd 2021-08-17 11:27:28 -04:00 committed by GitHub
parent fc16588cf1
commit f6dc6f752a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 3 deletions

View File

@ -247,7 +247,7 @@ func (api *PublicWakuAPI) Post(ctx context.Context, req NewMessage) (hexutil.Byt
wakuMsg := &pb.WakuMessage{
Payload: payload,
Version: version,
ContentTopic: req.Topic.String(),
ContentTopic: req.Topic.ContentTopic(),
Timestamp: utils.GetUnixEpoch(),
}

View File

@ -165,7 +165,13 @@ func (fs *Filters) NotifyWatchers(recvMessage *ReceivedMessage) {
fs.mutex.RLock()
defer fs.mutex.RUnlock()
candidates := fs.GetWatchersByTopic(StringToTopic(recvMessage.Envelope.Message().ContentTopic))
topic, err := ExtractTopicFromContentTopic(recvMessage.Envelope.Message().ContentTopic)
if err != nil {
log.Trace(err.Error(), "topic", recvMessage.Envelope.Message().ContentTopic)
return
}
candidates := fs.GetWatchersByTopic(*topic)
for _, watcher := range candidates {
match := true
if decodedMsg == nil {

View File

@ -19,6 +19,9 @@
package common
import (
"errors"
"strings"
"github.com/ethereum/go-ethereum/common/hexutil"
)
@ -43,7 +46,6 @@ func BytesToTopic(b []byte) (t TopicType) {
func StringToTopic(s string) (t TopicType) {
str, _ := hexutil.Decode(s)
return BytesToTopic(str)
}
// String converts a topic byte array to a string representation.
@ -60,3 +62,25 @@ func (t TopicType) MarshalText() ([]byte, error) {
func (t *TopicType) UnmarshalText(input []byte) error {
return hexutil.UnmarshalFixedText("Topic", input, t[:])
}
// Converts a topic to its 23/WAKU2-TOPICS representation
func (t TopicType) ContentTopic() string {
enc := hexutil.Encode(t[:])
return "/waku/1/" + enc + "/rfc26"
}
func ExtractTopicFromContentTopic(s string) (*TopicType, error) {
p := strings.Split(s, "/")
if len(p) != 5 || p[1] != "waku" || p[2] != "1" || p[4] != "rfc26" {
return nil, errors.New("invalid content topic format")
}
str, err := hexutil.Decode(p[3])
if err != nil {
return nil, err
}
result := BytesToTopic(str)
return &result, nil
}