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