-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils.go
69 lines (58 loc) · 1.57 KB
/
utils.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
60
61
62
63
64
65
66
67
68
69
package main
import (
"strings"
peer "github.com/libp2p/go-libp2p-peer"
"golang.org/x/crypto/sha3"
pbmsg "github.com/ethresearch/sharding-p2p-poc/pb/message"
"github.com/golang/protobuf/proto"
)
func keccak(msg proto.Message) ([]byte, error) {
dataInBytes, err := proto.Marshal(msg)
if err != nil {
logger.Errorf("Failed to encode protobuf message: %v, err: %v", msg, err)
return nil, err
}
h := sha3.NewLegacyKeccak256()
if _, err := h.Write(dataInBytes); err != nil {
return nil, err
}
return h.Sum(nil), nil
}
func peerIDToString(peerID peer.ID) string {
return peerID.Pretty()
}
func stringToPeerID(peerIDStr string) (peer.ID, error) {
peerID, err := peer.IDB58Decode(peerIDStr)
if err != nil {
return "", err
}
return peerID, nil
}
func peersStringToPeerIDs(peersStr []string) ([]peer.ID, error) {
peerIDs := []peer.ID{}
for _, peerStr := range peersStr {
peerID, err := stringToPeerID(peerStr)
if err != nil {
return nil, err
}
peerIDs = append(peerIDs, peerID)
}
return peerIDs, nil
}
func peerIDsToPeersString(peerIDs []peer.ID) []string {
peerStrs := make([]string, 0)
for _, peerID := range peerIDs {
peerStrs = append(peerStrs, peerIDToString(peerID))
}
return peerStrs
}
func pbPeersToPeerIDs(msg *pbmsg.Peers) ([]peer.ID, error) {
return peersStringToPeerIDs(msg.Peers)
}
func peerIDsToPBPeers(peerIDs []peer.ID) *pbmsg.Peers {
return &pbmsg.Peers{Peers: peerIDsToPeersString(peerIDs)}
}
func shardTopicToShardID(topic string) string {
end := len(collationTopicFmt) - 2
return strings.Replace(topic, collationTopicFmt[0:end], "", -1)
}