@cryptotaxi247 / kubo / commits / 3aa6d78ee

Implement ipfs key rename

License: MIT Signed-off-by: Michael Muré <batolettre@gmail.com>

Michael Muré committed May 4, 2017 at 17:30 UTC 3aa6d78ee1e6a3ab56595517434f55d31f5ca913
1 file changed +108 -3
core/commands/keystore.go
+108 -3
@@ -33,9 +33,10 @@ var KeyCmd = &cmds.Command{
33 `,
34 },
35 Subcommands: map[string]*cmds.Command{
36 - "gen": keyGenCmd,
37 - "list": keyListCmd,
38 - "rm": keyRmCmd,
36 + "gen": keyGenCmd,
37 + "list": keyListCmd,
38 + "rename": keyRenameCmd,
39 + "rm": keyRmCmd,
40 },
41 }
42
@@ -48,6 +49,13 @@ type KeyOutputList struct {
49 Keys []KeyOutput
50 }
51
52 +type KeyRenameOutput struct {
53 + Was string
54 + Now string
55 + Id string
56 + Overwrite bool
57 +}
58 +
59 var keyGenCmd = &cmds.Command{
60 Helptext: cmds.HelpText{
61 Tagline: "Create a new keypair",
@@ -203,6 +211,103 @@ var keyListCmd = &cmds.Command{
211 Type: KeyOutputList{},
212 }
213
214 +var keyRenameCmd = &cmds.Command{
215 + Helptext: cmds.HelpText{
216 + Tagline: "Rename a keypair",
217 + },
218 + Arguments: []cmds.Argument{
219 + cmds.StringArg("name", true, false, "name of key to rename"),
220 + cmds.StringArg("newName", true, false, "new name of the key"),
221 + },
222 + Options: []cmds.Option{
223 + cmds.BoolOption("force", "f", "Allow to overwrite an existing key."),
224 + },
225 + Run: func(req cmds.Request, res cmds.Response) {
226 + n, err := req.InvocContext().GetNode()
227 + if err != nil {
228 + res.SetError(err, cmds.ErrNormal)
229 + return
230 + }
231 +
232 + ks := n.Repo.Keystore()
233 +
234 + name := req.Arguments()[0]
235 + newName := req.Arguments()[1]
236 +
237 + if name == "self" {
238 + res.SetError(fmt.Errorf("cannot rename key with name 'self'"), cmds.ErrNormal)
239 + return
240 + }
241 +
242 + if newName == "self" {
243 + res.SetError(fmt.Errorf("cannot overwrite key with name 'self'"), cmds.ErrNormal)
244 + return
245 + }
246 +
247 + oldKey, err := ks.Get(name)
248 + if err != nil {
249 + res.SetError(fmt.Errorf("no key named %s was found", name), cmds.ErrNormal)
250 + return
251 + }
252 +
253 + pubKey := oldKey.GetPublic()
254 +
255 + pid, err := peer.IDFromPublicKey(pubKey)
256 + if err != nil {
257 + res.SetError(err, cmds.ErrNormal)
258 + return
259 + }
260 +
261 + overwrite := false
262 + force, _, _ := res.Request().Option("f").Bool()
263 + if force && ks.Has(newName) {
264 + overwrite = true
265 + err := ks.Delete(newName)
266 + if err != nil {
267 + res.SetError(err, cmds.ErrNormal)
268 + return
269 + }
270 + }
271 +
272 + err = ks.Put(newName, oldKey)
273 + if err != nil {
274 + res.SetError(err, cmds.ErrNormal)
275 + return
276 + }
277 +
278 + err = ks.Delete(name)
279 + if err != nil {
280 + res.SetError(err, cmds.ErrNormal)
281 + return
282 + }
283 +
284 + res.SetOutput(&KeyRenameOutput{
285 + Was: name,
286 + Now: newName,
287 + Id: pid.Pretty(),
288 + Overwrite: overwrite,
289 + })
290 + },
291 + Marshalers: cmds.MarshalerMap{
292 + cmds.Text: func(res cmds.Response) (io.Reader, error) {
293 + k, ok := res.Output().(*KeyRenameOutput)
294 + if !ok {
295 + return nil, fmt.Errorf("expected a KeyRenameOutput as command result")
296 + }
297 +
298 + buf := new(bytes.Buffer)
299 +
300 + if k.Overwrite {
301 + fmt.Fprintf(buf, "Key %s renamed to %s with overwriting\n", k.Id, k.Now)
302 + } else {
303 + fmt.Fprintf(buf, "Key %s renamed to %s\n", k.Id, k.Now)
304 + }
305 + return buf, nil
306 + },
307 + },
308 + Type: KeyRenameOutput{},
309 +}
310 +
311 var keyRmCmd = &cmds.Command{
312 Helptext: cmds.HelpText{
313 Tagline: "Remove a keypair",