提交 3faf8974 作者: Jeromy Johnson 提交者: GitHub

Merge pull request #3581 from ipfs/kevina/keystore

"ipfs key list": include the hash of the key id in addition to the name
package commands package commands
import ( import (
"bytes"
"crypto/rand" "crypto/rand"
"errors"
"fmt" "fmt"
"io" "io"
"sort" "sort"
"strings" "strings"
"text/tabwriter"
cmds "github.com/ipfs/go-ipfs/commands" cmds "github.com/ipfs/go-ipfs/commands"
...@@ -28,6 +31,10 @@ type KeyOutput struct { ...@@ -28,6 +31,10 @@ type KeyOutput struct {
Id string Id string
} }
type KeyOutputList struct {
Keys []KeyOutput
}
var KeyGenCmd = &cmds.Command{ var KeyGenCmd = &cmds.Command{
Helptext: cmds.HelpText{ Helptext: cmds.HelpText{
Tagline: "Create a new keypair", Tagline: "Create a new keypair",
...@@ -125,7 +132,7 @@ var KeyGenCmd = &cmds.Command{ ...@@ -125,7 +132,7 @@ var KeyGenCmd = &cmds.Command{
return nil, fmt.Errorf("expected a KeyOutput as command result") return nil, fmt.Errorf("expected a KeyOutput as command result")
} }
return strings.NewReader(k.Id), nil return strings.NewReader(k.Id + "\n"), nil
}, },
}, },
Type: KeyOutput{}, Type: KeyOutput{},
...@@ -135,6 +142,9 @@ var KeyListCmd = &cmds.Command{ ...@@ -135,6 +142,9 @@ var KeyListCmd = &cmds.Command{
Helptext: cmds.HelpText{ Helptext: cmds.HelpText{
Tagline: "List all local keypairs", Tagline: "List all local keypairs",
}, },
Options: []cmds.Option{
cmds.BoolOption("l", "Show extra information about keys."),
},
Run: func(req cmds.Request, res cmds.Response) { Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode() n, err := req.InvocContext().GetNode()
if err != nil { if err != nil {
...@@ -149,10 +159,52 @@ var KeyListCmd = &cmds.Command{ ...@@ -149,10 +159,52 @@ var KeyListCmd = &cmds.Command{
} }
sort.Strings(keys) sort.Strings(keys)
res.SetOutput(&stringList{keys})
list := make([]KeyOutput, 0, len(keys))
for _, key := range keys {
privKey, err := n.Repo.Keystore().Get(key)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
pubKey := privKey.GetPublic()
pid, err := peer.IDFromPublicKey(pubKey)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
list = append(list, KeyOutput{Name: key, Id: pid.Pretty()})
}
res.SetOutput(&KeyOutputList{list})
}, },
Marshalers: cmds.MarshalerMap{ Marshalers: cmds.MarshalerMap{
cmds.Text: stringListMarshaler, cmds.Text: keyOutputListMarshaler,
}, },
Type: stringList{}, Type: KeyOutputList{},
}
func keyOutputListMarshaler(res cmds.Response) (io.Reader, error) {
withId, _, _ := res.Request().Option("l").Bool()
list, ok := res.Output().(*KeyOutputList)
if !ok {
return nil, errors.New("failed to cast []KeyOutput")
}
buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
for _, s := range list.Keys {
if withId {
fmt.Fprintf(w, "%s\t%s\t\n", s.Id, s.Name)
} else {
fmt.Fprintf(w, "%s\n", s.Name)
}
}
w.Flush()
return buf, nil
} }
#!/bin/sh
#
# Copyright (c) 2017 Jeromy Johnson
# MIT Licensed; see the LICENSE file in this repository.
#
test_description="Test keystore commands"
. lib/test-lib.sh
test_init_ipfs
test_key_cmd() {
test_expect_success "create a new rsa key" '
rsahash=$(ipfs key gen foobarsa --type=rsa --size=2048)
'
test_expect_success "create a new ed25519 key" '
edhash=$(ipfs key gen bazed --type=ed25519)
'
test_expect_success "both keys show up in list output" '
echo bazed > list_exp &&
echo foobarsa >> list_exp &&
ipfs key list | sort > list_out &&
test_cmp list_exp list_out
'
test_expect_success "key hashes show up in long list output" '
ipfs key list -l | grep $edhash > /dev/null &&
ipfs key list -l | grep $rsahash > /dev/null
'
}
test_key_cmd
test_done
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论