-
Notifications
You must be signed in to change notification settings - Fork 913
/
p2p.go
77 lines (64 loc) · 1.46 KB
/
p2p.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
70
71
72
73
74
75
76
77
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/spf13/cobra"
)
func init() {
p2pCmd.AddCommand(p2pNewKeyCmd, p2pPeerIDCmd)
}
var p2pCmd = &cobra.Command{
Use: "p2p [subcommand]",
Short: "Collection of p2p related utilities",
}
var p2pNewKeyCmd = &cobra.Command{
Use: "new-key",
Short: "Generate and print new Ed25519 private key for p2p networking",
RunE: func(_ *cobra.Command, _ []string) error {
privkey, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
return err
}
raw, err := privkey.Raw()
if err != nil {
return err
}
fmt.Println(hex.EncodeToString(raw))
return nil
},
Args: cobra.NoArgs,
}
var p2pPeerIDCmd = &cobra.Command{
Use: "peer-id",
Short: "Get peer-id out of public or private Ed25519 key",
RunE: func(_ *cobra.Command, args []string) error {
decKey, err := hex.DecodeString(args[0])
if err != nil {
return err
}
privKey, err := crypto.UnmarshalEd25519PrivateKey(decKey)
if err != nil {
// try pubkey then
pubKey, err := crypto.UnmarshalEd25519PublicKey(decKey)
if err != nil {
return err
}
id, err := peer.IDFromPublicKey(pubKey)
if err != nil {
return err
}
fmt.Println(id.String())
return nil
}
id, err := peer.IDFromPrivateKey(privKey)
if err != nil {
return err
}
fmt.Println(id.String())
return nil
},
Args: cobra.ExactArgs(1),
}