/
keystore.go
249 lines (213 loc) · 5.92 KB
/
keystore.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package commands
import (
"fmt"
"io"
"text/tabwriter"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
cmds "gx/ipfs/QmQkW9fnCsg9SLHdViiAh6qfBppodsPZVpU92dZLqYtEfs/go-ipfs-cmds"
options "gx/ipfs/QmXLwxifxwfc2bAwq6rdjbYqAsGzWsDE9RM5TWMGtykyj6/interface-go-ipfs-core/options"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)
var KeyCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Create and list IPNS name keypairs",
ShortDescription: `
'ipfs key gen' generates a new keypair for usage with IPNS and 'ipfs name
publish'.
> ipfs key gen --type=rsa --size=2048 mykey
> ipfs name publish --key=mykey QmSomeHash
'ipfs key list' lists the available keys.
> ipfs key list
self
mykey
`,
},
Subcommands: map[string]*cmds.Command{
"gen": keyGenCmd,
"list": keyListCmd,
"rename": keyRenameCmd,
"rm": keyRmCmd,
},
}
type KeyOutput struct {
Name string
Id string
}
type KeyOutputList struct {
Keys []KeyOutput
}
// KeyRenameOutput define the output type of keyRenameCmd
type KeyRenameOutput struct {
Was string
Now string
Id string
Overwrite bool
}
const (
keyStoreTypeOptionName = "type"
keyStoreSizeOptionName = "size"
)
var keyGenCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Create a new keypair",
},
Options: []cmdkit.Option{
cmdkit.StringOption(keyStoreTypeOptionName, "t", "type of the key to create [rsa, ed25519]"),
cmdkit.IntOption(keyStoreSizeOptionName, "s", "size of the key to generate"),
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("name", true, false, "name of key to create"),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
typ, f := req.Options[keyStoreTypeOptionName].(string)
if !f {
return fmt.Errorf("please specify a key type with --type")
}
name := req.Arguments[0]
if name == "self" {
return fmt.Errorf("cannot create key with name 'self'")
}
opts := []options.KeyGenerateOption{options.Key.Type(typ)}
size, sizefound := req.Options[keyStoreSizeOptionName].(int)
if sizefound {
opts = append(opts, options.Key.Size(size))
}
key, err := api.Key().Generate(req.Context, name, opts...)
if err != nil {
return err
}
return cmds.EmitOnce(res, &KeyOutput{
Name: name,
Id: key.ID().Pretty(),
})
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, ko *KeyOutput) error {
_, err := w.Write([]byte(ko.Id + "\n"))
return err
}),
},
Type: KeyOutput{},
}
var keyListCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "List all local keypairs",
},
Options: []cmdkit.Option{
cmdkit.BoolOption("l", "Show extra information about keys."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
keys, err := api.Key().List(req.Context)
if err != nil {
return err
}
list := make([]KeyOutput, 0, len(keys))
for _, key := range keys {
list = append(list, KeyOutput{Name: key.Name(), Id: key.ID().Pretty()})
}
return cmds.EmitOnce(res, &KeyOutputList{list})
},
Encoders: cmds.EncoderMap{
cmds.Text: keyOutputListEncoders(),
},
Type: KeyOutputList{},
}
const (
keyStoreForceOptionName = "force"
)
var keyRenameCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Rename a keypair",
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("name", true, false, "name of key to rename"),
cmdkit.StringArg("newName", true, false, "new name of the key"),
},
Options: []cmdkit.Option{
cmdkit.BoolOption(keyStoreForceOptionName, "f", "Allow to overwrite an existing key."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
name := req.Arguments[0]
newName := req.Arguments[1]
force, _ := req.Options[keyStoreForceOptionName].(bool)
key, overwritten, err := api.Key().Rename(req.Context, name, newName, options.Key.Force(force))
if err != nil {
return err
}
return cmds.EmitOnce(res, &KeyRenameOutput{
Was: name,
Now: newName,
Id: key.ID().Pretty(),
Overwrite: overwritten,
})
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, kro *KeyRenameOutput) error {
if kro.Overwrite {
fmt.Fprintf(w, "Key %s renamed to %s with overwriting\n", kro.Id, kro.Now)
} else {
fmt.Fprintf(w, "Key %s renamed to %s\n", kro.Id, kro.Now)
}
return nil
}),
},
Type: KeyRenameOutput{},
}
var keyRmCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Remove a keypair",
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("name", true, true, "names of keys to remove").EnableStdin(),
},
Options: []cmdkit.Option{
cmdkit.BoolOption("l", "Show extra information about keys."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
names := req.Arguments
list := make([]KeyOutput, 0, len(names))
for _, name := range names {
key, err := api.Key().Remove(req.Context, name)
if err != nil {
return err
}
list = append(list, KeyOutput{Name: name, Id: key.ID().Pretty()})
}
return cmds.EmitOnce(res, &KeyOutputList{list})
},
Encoders: cmds.EncoderMap{
cmds.Text: keyOutputListEncoders(),
},
Type: KeyOutputList{},
}
func keyOutputListEncoders() cmds.EncoderFunc {
return cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, list *KeyOutputList) error {
withID, _ := req.Options["l"].(bool)
tw := tabwriter.NewWriter(w, 1, 2, 1, ' ', 0)
for _, s := range list.Keys {
if withID {
fmt.Fprintf(tw, "%s\t%s\t\n", s.Id, s.Name)
} else {
fmt.Fprintf(tw, "%s\n", s.Name)
}
}
tw.Flush()
return nil
})
}