github.com/status-im/status-go@v1.1.0/wakuv2/common/topic.go (about)

     1  // Copyright 2019 The Waku Library Authors.
     2  //
     3  // The Waku library is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Lesser General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // (at your option) any later version.
     7  //
     8  // The Waku library is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty off
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    11  // GNU Lesser General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Lesser General Public License
    14  // along with the Waku library. If not, see <http://www.gnu.org/licenses/>.
    15  //
    16  // This software uses the go-ethereum library, which is licensed
    17  // under the GNU Lesser General Public Library, version 3 or any later.
    18  
    19  package common
    20  
    21  import (
    22  	"errors"
    23  	"strings"
    24  
    25  	"golang.org/x/exp/maps"
    26  
    27  	"github.com/ethereum/go-ethereum/common/hexutil"
    28  )
    29  
    30  // TopicType represents a cryptographically secure, probabilistic partial
    31  // classifications of a message, determined as the first (leftmost) 4 bytes of the
    32  // SHA3 hash of some arbitrary data given by the original author of the message.
    33  type TopicType [TopicLength]byte
    34  
    35  type TopicSet map[TopicType]struct{}
    36  
    37  func NewTopicSet(topics []TopicType) TopicSet {
    38  	s := make(TopicSet, len(topics))
    39  	for _, t := range topics {
    40  		s[t] = struct{}{}
    41  	}
    42  	return s
    43  }
    44  
    45  func NewTopicSetFromBytes(byteArrays [][]byte) TopicSet {
    46  	topics := make([]TopicType, len(byteArrays))
    47  	for i, byteArr := range byteArrays {
    48  		topics[i] = BytesToTopic(byteArr)
    49  	}
    50  	return NewTopicSet(topics)
    51  }
    52  
    53  // BytesToTopic converts from the byte array representation of a topic
    54  // into the TopicType type.
    55  func BytesToTopic(b []byte) (t TopicType) {
    56  	sz := TopicLength
    57  	if x := len(b); x < TopicLength {
    58  		sz = x
    59  	}
    60  	for i := 0; i < sz; i++ {
    61  		t[i] = b[i]
    62  	}
    63  	return t
    64  }
    65  
    66  func StringToTopic(s string) (t TopicType) {
    67  	str, _ := hexutil.Decode(s)
    68  	return BytesToTopic(str)
    69  }
    70  
    71  // String converts a topic byte array to a string representation.
    72  func (t *TopicType) String() string {
    73  	return hexutil.Encode(t[:])
    74  }
    75  
    76  // MarshalText returns the hex representation of t.
    77  func (t TopicType) MarshalText() ([]byte, error) {
    78  	return hexutil.Bytes(t[:]).MarshalText()
    79  }
    80  
    81  // UnmarshalText parses a hex representation to a topic.
    82  func (t *TopicType) UnmarshalText(input []byte) error {
    83  	return hexutil.UnmarshalFixedText("Topic", input, t[:])
    84  }
    85  
    86  // Converts a topic to its 23/WAKU2-TOPICS representation
    87  func (t TopicType) ContentTopic() string {
    88  	enc := hexutil.Encode(t[:])
    89  	return "/waku/1/" + enc + "/rfc26"
    90  }
    91  
    92  func ExtractTopicFromContentTopic(s string) (TopicType, error) {
    93  	p := strings.Split(s, "/")
    94  
    95  	if len(p) != 5 || p[1] != "waku" || p[2] != "1" || p[4] != "rfc26" {
    96  		return TopicType{}, errors.New("invalid content topic format")
    97  	}
    98  
    99  	str, err := hexutil.Decode(p[3])
   100  	if err != nil {
   101  		return TopicType{}, err
   102  	}
   103  
   104  	result := BytesToTopic(str)
   105  	return result, nil
   106  }
   107  
   108  func (t TopicSet) ContentTopics() []string {
   109  	contentTopics := make([]string, len(t))
   110  	for i, ct := range maps.Keys(t) {
   111  		contentTopics[i] = ct.ContentTopic()
   112  	}
   113  	return contentTopics
   114  }