github.com/status-im/status-go@v1.1.0/protocol/transport/topic.go (about) 1 package transport 2 3 import ( 4 "crypto/ecdsa" 5 "encoding/hex" 6 "math/big" 7 "strconv" 8 9 "github.com/status-im/status-go/eth-node/crypto" 10 "github.com/status-im/status-go/eth-node/types" 11 ) 12 13 const discoveryTopic = "contact-discovery" 14 15 var ( 16 // The number of partitions. 17 nPartitions = big.NewInt(5000) 18 ) 19 20 // ToTopic converts a string to a whisper topic. 21 func ToTopic(s string) []byte { 22 return crypto.Keccak256([]byte(s))[:types.TopicLength] 23 } 24 25 func StrToPublicKey(str string) (*ecdsa.PublicKey, error) { 26 publicKeyBytes, err := hex.DecodeString(str) 27 if err != nil { 28 return nil, err 29 } 30 return crypto.UnmarshalPubkey(publicKeyBytes) 31 } 32 33 func PublicKeyToStr(publicKey *ecdsa.PublicKey) string { 34 return hex.EncodeToString(crypto.FromECDSAPub(publicKey)) 35 } 36 37 func PersonalDiscoveryTopic(publicKey *ecdsa.PublicKey) string { 38 return "contact-discovery-" + PublicKeyToStr(publicKey) 39 } 40 41 // PartitionedTopic returns the associated partitioned topic string 42 // with the given public key. 43 func PartitionedTopic(publicKey *ecdsa.PublicKey) string { 44 partition := big.NewInt(0) 45 partition.Mod(publicKey.X, nPartitions) 46 return "contact-discovery-" + strconv.FormatInt(partition.Int64(), 10) 47 } 48 49 func ContactCodeTopic(publicKey *ecdsa.PublicKey) string { 50 return "0x" + PublicKeyToStr(publicKey) + "-contact-code" 51 } 52 53 func NegotiatedTopic(publicKey *ecdsa.PublicKey) string { 54 return "0x" + PublicKeyToStr(publicKey) + "-negotiated" 55 } 56 57 func DiscoveryTopic() string { 58 return discoveryTopic 59 } 60 61 func CommunityShardInfoTopic(communityID string) string { 62 return communityID + CommunityShardInfoTopicPrefix() 63 } 64 65 func CommunityShardInfoTopicPrefix() string { 66 return "-shard-info" 67 }