-
Notifications
You must be signed in to change notification settings - Fork 376
/
p2p.go
201 lines (175 loc) · 4.94 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"bufio"
"context"
crand "crypto/rand"
"encoding/base64"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
libp2p_ci "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/peterbourgon/ff/v3/ffcli"
)
const (
DefaultP2PKeySize = 2048
DefaultP2PKeyType = "Ed25519"
)
func p2pIdentityGenerateCommand() *ffcli.Command {
var (
path string
noprint bool
kind string
size int
)
fsBuilder := func() (*flag.FlagSet, error) {
fs := flag.NewFlagSet("berty p2p generate-id [falgs] <path>", flag.ExitOnError)
fs.BoolVar(&noprint, "noprint", false, "do no print anything")
fs.StringVar(&kind, "type", DefaultP2PKeyType, "specify the type of key")
fs.IntVar(&size, "size", DefaultP2PKeySize, "for RSA key only, specfy the bit size of the key")
return fs, nil
}
return &ffcli.Command{
Name: "generate-id",
ShortUsage: "berty p2p generate-id [flags] [path]",
ShortHelp: "helper to generate a key identity for libp2p service",
FlagSetBuilder: fsBuilder,
Options: ffSubcommandOptions(),
UsageFunc: usageFunc,
Exec: func(ctx context.Context, args []string) error {
switch len(args) {
case 1:
path = args[0]
case 0:
default:
return flag.ErrHelp
}
var output io.Writer = os.Stdout
var stderr io.Writer = os.Stderr
if noprint {
output = ioutil.Discard
stderr = ioutil.Discard
} else {
o, _ := os.Stdout.Stat()
if (o.Mode() & os.ModeCharDevice) != os.ModeCharDevice {
// if redirect to a pipe discard stderr
stderr = ioutil.Discard
}
}
priv, err := generateNewKeyPair(kind, size, crand.Reader)
if err != nil {
return fmt.Errorf("unable to generate key pair: %w", err)
}
peer, err := peer.IDFromPrivateKey(priv)
if err != nil {
return fmt.Errorf("unable to get peer from private key: %w", err)
}
kbyte, err := libp2p_ci.MarshalPrivateKey(priv)
if err != nil {
return fmt.Errorf("unable to marshal private key: %w", err)
}
if path != "" {
if err := os.WriteFile(path, kbyte, 0o600); err != nil {
return fmt.Errorf("unable to write file `%s`: %w", path, err)
}
fmt.Fprintf(stderr, "generated %s key in: %s\n", priv.Type().String(), path)
fmt.Fprintf(stderr, "peer id: ")
fmt.Fprint(output, peer.String())
fmt.Fprintln(stderr)
} else {
encoded := base64.StdEncoding.EncodeToString(kbyte)
fmt.Fprintln(stderr, "generated base64 encoded key:")
fmt.Fprint(output, encoded)
fmt.Fprintln(stderr)
}
return nil
},
}
}
func p2pIdentityLoadCommand() *ffcli.Command {
var path string
fsBuilder := func() (*flag.FlagSet, error) {
fs := flag.NewFlagSet("berty p2p load-id [path]", flag.ExitOnError)
return fs, nil
}
return &ffcli.Command{
Name: "load-id",
ShortUsage: "berty p2p load-id [path]",
ShortHelp: "helper to display peerid or related information from libp2p key from path or stdin",
FlagSetBuilder: fsBuilder,
Options: ffSubcommandOptions(),
UsageFunc: usageFunc,
Exec: func(ctx context.Context, args []string) error {
switch len(args) {
case 1:
path = args[0]
case 0:
default:
return flag.ErrHelp
}
var output io.Writer = os.Stdout
var stderr io.Writer = os.Stderr
o, _ := os.Stdout.Stat()
if (o.Mode() & os.ModeCharDevice) != os.ModeCharDevice {
// if redirect to a pipe discard stderr
stderr = ioutil.Discard
}
// load raw key data
var data string
if path != "" {
file, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("unable to read file `%s`: %w", path, err)
}
data = string(file)
} else {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
data = scanner.Text()
}
// decode base64 data if needed
var kbyte []byte
if bdata, err := base64.StdEncoding.DecodeString(data); err == nil {
kbyte = bdata
} else {
kbyte = []byte(data)
}
priv, err := libp2p_ci.UnmarshalPrivateKey(kbyte)
if err != nil {
return fmt.Errorf("unable to unmarshal private key: %w", err)
}
peer, err := peer.IDFromPrivateKey(priv)
if err != nil {
return fmt.Errorf("unable to get peer from private key: %w", err)
}
fmt.Fprintf(stderr, "type: %s\n", priv.Type().String())
fmt.Fprintf(stderr, "peer id: ")
fmt.Fprint(output, peer.String())
fmt.Fprintln(stderr)
return nil
},
}
}
func p2pCommand() *ffcli.Command {
fsBuilder := func() (*flag.FlagSet, error) {
fs := flag.NewFlagSet("berty p2p [command]", flag.ExitOnError)
return fs, nil
}
return &ffcli.Command{
Name: "p2p",
ShortUsage: "berty p2p [command]",
ShortHelp: "helper around libp2p",
FlagSetBuilder: fsBuilder,
Options: ffSubcommandOptions(),
UsageFunc: usageFunc,
Exec: func(ctx context.Context, args []string) error {
return flag.ErrHelp
},
Subcommands: []*ffcli.Command{
p2pIdentityGenerateCommand(),
p2pIdentityLoadCommand(),
},
}
}