-
Notifications
You must be signed in to change notification settings - Fork 8
/
id.go
45 lines (37 loc) · 1.04 KB
/
id.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
package peer
import (
ic "github.com/libp2p/go-libp2p/core/crypto"
ip "github.com/libp2p/go-libp2p/core/peer"
b58 "github.com/mr-tron/base58/base58"
)
// ID is a peer identifier.
type ID = ip.ID
// IDFromBytes cast a string to ID type, and validate
// the id to make sure it is a multihash.
func IDFromBytes(b []byte) (ID, error) {
return ip.IDFromBytes(b)
}
// IDB58Decode returns a b58-decoded Peer ID.
func IDB58Decode(s string) (ID, error) {
return ip.Decode(s)
}
// IDB58Encode returns b58-encoded string
func IDB58Encode(id ID) string {
return b58.Encode([]byte(id))
}
// IDFromPublicKey returns the Peer ID corresponding to pk
func IDFromPublicKey(pk ic.PubKey) (ID, error) {
return ip.IDFromPublicKey(pk)
}
// IDFromPrivateKey returns the Peer ID corresponding to sk
func IDFromPrivateKey(sk ic.PrivKey) (ID, error) {
return ip.IDFromPrivateKey(sk)
}
// IDsToString converts a slice of IDs to strings.
func IDsToString(ids []ID) []string {
out := make([]string, len(ids))
for i := range ids {
out[i] = ids[i].String()
}
return out
}