Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ipfs key {rm, rename} #3892

Merged
merged 10 commits into from
May 18, 2017
56 changes: 56 additions & 0 deletions core/commands/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var KeyCmd = &cmds.Command{
Subcommands: map[string]*cmds.Command{
"gen": KeyGenCmd,
"list": KeyListCmd,
"rm": KeyRmCmd,
},
}

Expand Down Expand Up @@ -202,6 +203,61 @@ var KeyListCmd = &cmds.Command{
Type: KeyOutputList{},
}

var KeyRmCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Remove a keypair",
},
Arguments: []cmds.Argument{
cmds.StringArg("name", true, false, "name of key to remove"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This command should allow for StdIn handling and multiple argument handling. Then it would be possible to remove multiple keys at the same time easily.

},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

name := req.Arguments()[0]
if name == "self" {
res.SetError(fmt.Errorf("cannot remove key with name 'self'"), cmds.ErrNormal)
return
}

removed, err := n.Repo.Keystore().Get(name)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

err = n.Repo.Keystore().Delete(name)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

pubKey := removed.GetPublic()

pid, err := peer.IDFromPublicKey(pubKey)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

res.SetOutput(&KeyOutput{
Name: name,
Id: pid.Pretty(),
})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v := res.Output().(*KeyOutput)
s := fmt.Sprintf("Removed key %s with Id: %s\n", v.Name, v.Id)
return strings.NewReader(s), nil
},
},
Type: KeyOutput{},
}

func keyOutputListMarshaler(res cmds.Response) (io.Reader, error) {
withId, _, _ := res.Request().Option("l").Bool()

Expand Down
8 changes: 8 additions & 0 deletions test/sharness/t0165-keystore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ test_key_cmd() {
PeerID="$(ipfs config Identity.PeerID)"
ipfs key list -l | grep "$PeerID self"
'

test_expect_success "key rm remove a key" '
ipfs key rm foobarsa
echo bazed > list_exp &&
echo self >> list_exp
ipfs key list | sort > list_out &&
test_cmp list_exp list_out
'
}

test_key_cmd
Expand Down