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{ wakuMsg := &pb.WakuMessage{
Payload: payload, Payload: payload,
Version: version, Version: version,
ContentTopic: req.Topic.String(), ContentTopic: req.Topic.ContentTopic(),
Timestamp: utils.GetUnixEpoch(), Timestamp: utils.GetUnixEpoch(),
} }

View File

@ -165,7 +165,13 @@ func (fs *Filters) NotifyWatchers(recvMessage *ReceivedMessage) {
fs.mutex.RLock() fs.mutex.RLock()
defer fs.mutex.RUnlock() 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 { for _, watcher := range candidates {
match := true match := true
if decodedMsg == nil { if decodedMsg == nil {

View File

@ -19,6 +19,9 @@
package common package common
import ( import (
"errors"
"strings"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
) )
@ -43,7 +46,6 @@ func BytesToTopic(b []byte) (t TopicType) {
func StringToTopic(s string) (t TopicType) { func StringToTopic(s string) (t TopicType) {
str, _ := hexutil.Decode(s) str, _ := hexutil.Decode(s)
return BytesToTopic(str) return BytesToTopic(str)
} }
// String converts a topic byte array to a string representation. // 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 { func (t *TopicType) UnmarshalText(input []byte) error {
return hexutil.UnmarshalFixedText("Topic", input, t[:]) 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
}