Remove export option from generate
rendaw committed
Aug 1, 2020 at 10:45 UTC
356ca3aefad5c68b0c315e5f9a6af215661e51f8
4 files changed
+43
-137
core/commands/keystore.go
+21
-88
@@ -7,8 +7,6 @@ import (
7
8
cmds "github.com/ipfs/go-ipfs-cmds"
9
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
10
- "github.com/ipfs/go-ipfs/core/coreapi"
11
- repo "github.com/ipfs/go-ipfs/repo"
10
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
11
options "github.com/ipfs/interface-go-ipfs-core/options"
12
"github.com/libp2p/go-libp2p-core/crypto"
@@ -35,12 +33,12 @@ publish'.
33
`,
34
},
35
Subcommands: map[string]*cmds.Command{
38
- "gen": keyGenCmd,
39
- "export": keyExportCmd,
40
- "import": keyImportCmd,
41
- "list": keyListCmd,
42
- "rename": keyRenameCmd,
43
- "rm": keyRmCmd,
36
+ "gen": keyGenCmd,
37
+ "export": keyExportCmd,
38
+ "import": keyImportCmd,
39
+ "list": keyListCmd,
40
+ "rename": keyRenameCmd,
41
+ "rm": keyRmCmd,
42
},
43
}
44
@@ -49,12 +47,6 @@ type KeyOutput struct {
47
Id string
48
}
49
52
-type GenerateKeyOutput struct {
53
- Name string
54
- Id string
55
- Sk string
56
-}
57
-
50
type ExportKeyOutput struct {
51
Sk string
52
}
@@ -75,8 +67,6 @@ const (
67
keyStoreTypeOptionName = "type"
68
keyStoreSizeOptionName = "size"
69
keyFormatOptionName = "format"
78
- keyExportOptionName = "export"
79
- keyNoStoreOptionName = "no-store"
70
)
71
72
var keyGenCmd = &cmds.Command{
@@ -87,57 +77,24 @@ var keyGenCmd = &cmds.Command{
77
cmds.StringOption(keyStoreTypeOptionName, "t", "type of the key to create: rsa, ed25519").WithDefault("rsa"),
78
cmds.IntOption(keyStoreSizeOptionName, "s", "size of the key to generate"),
79
cmds.StringOption(keyFormatOptionName, "f", "output format: b58mh or b36cid").WithDefault("b58mh"),
90
- cmds.BoolOption(keyExportOptionName, "e", "return generated key for later re-import").WithDefault(false),
91
- cmds.BoolOption(keyNoStoreOptionName, "n", "don't add the key to the keychain").WithDefault(false),
80
},
81
Arguments: []cmds.Argument{
94
- cmds.StringArg("name", false, false, "name of key to create, required unless -n specified"),
82
+ cmds.StringArg("name", true, false, "name of key to create"),
83
},
84
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
85
+ api, err := cmdenv.GetApi(env, req)
86
+ if err != nil {
87
+ return err
88
+ }
89
+
90
typ, f := req.Options[keyStoreTypeOptionName].(string)
91
if !f {
92
return fmt.Errorf("please specify a key type with --type")
93
}
94
102
- store := !req.Options[keyNoStoreOptionName].(bool)
103
- export := req.Options[keyExportOptionName].(bool)
104
-
105
- var name string
106
- var r repo.Repo = nil
107
- defer func() {
108
- if r != nil {
109
- r.Close()
110
- }
111
- }()
112
- if store {
113
- if len(req.Arguments) == 0 {
114
- return fmt.Errorf("you must specify a key name")
115
- }
116
-
117
- name = req.Arguments[0]
118
-
119
- if name == "self" {
120
- return fmt.Errorf("cannot create key with name 'self'")
121
- }
122
-
123
- cfgRoot, err := cmdenv.GetConfigRoot(env)
124
- if err != nil {
125
- return err
126
- }
127
-
128
- r, err = fsrepo.Open(cfgRoot)
129
- if err != nil {
130
- return err
131
- }
132
-
133
- _, err = r.Keystore().Get(name)
134
- if err == nil {
135
- return fmt.Errorf("key with name '%s' already exists", name)
136
- }
137
- }
138
-
139
- if !store && !export {
140
- return fmt.Errorf("you must export key if not storing")
95
+ name := req.Arguments[0]
96
+ if name == "self" {
97
+ return fmt.Errorf("cannot create key with name 'self'")
98
}
99
100
opts := []options.KeyGenerateOption{options.Key.Type(typ)}
@@ -146,52 +103,28 @@ var keyGenCmd = &cmds.Command{
103
if sizefound {
104
opts = append(opts, options.Key.Size(size))
105
}
149
- if err := verifyFormatLabel(req.Options[keyFormatOptionName].(string)); err != nil {
150
- return err
151
- }
152
-
153
- sk, pk, err := coreapi.GenerateKey(opts...)
154
- if err != nil {
106
+ if err = verifyFormatLabel(req.Options[keyFormatOptionName].(string)); err != nil {
107
return err
108
}
109
158
- if store {
159
- err = r.Keystore().Put(name, sk)
160
- if err != nil {
161
- return err
162
- }
163
- }
110
+ key, err := api.Key().Generate(req.Context, name, opts...)
111
165
- pid, err := peer.IDFromPublicKey(pk)
112
if err != nil {
113
return err
114
}
115
170
- var encoded string
171
- if export {
172
- encoded, err = encodeSKForExport(sk)
173
- if err != nil {
174
- return err
175
- }
176
- }
177
-
178
- return cmds.EmitOnce(res, &GenerateKeyOutput{
116
+ return cmds.EmitOnce(res, &KeyOutput{
117
Name: name,
180
- Id: formatID(pid, req.Options[keyFormatOptionName].(string)),
181
- Sk: encoded,
118
+ Id: formatID(key.ID(), req.Options[keyFormatOptionName].(string)),
119
})
120
},
121
Encoders: cmds.EncoderMap{
185
- cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, ko *GenerateKeyOutput) error {
186
- if ko.Sk != "" {
187
- _, err := w.Write([]byte(ko.Sk + "\n"))
188
- return err
189
- }
122
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, ko *KeyOutput) error {
123
_, err := w.Write([]byte(ko.Id + "\n"))
124
return err
125
}),
126
},
194
- Type: GenerateKeyOutput{},
127
+ Type: KeyOutput{},
128
}
129
130
func verifyFormatLabel(formatLabel string) error {
core/coreapi/key.go
+16
-26
@@ -37,11 +37,21 @@ func (k *key) ID() peer.ID {
37
return k.peerID
38
}
39
40
-// GenerateKey generates a new keypair and returns it
41
-func GenerateKey(opts ...caopts.KeyGenerateOption) (crypto.PrivKey, crypto.PubKey, error) {
40
+// Generate generates new key, stores it in the keystore under the specified
41
+// name and returns a base58 encoded multihash of its public key.
42
+func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.KeyGenerateOption) (coreiface.Key, error) {
43
options, err := caopts.KeyGenerateOptions(opts...)
44
if err != nil {
44
- return nil, nil, err
45
+ return nil, err
46
+ }
47
+
48
+ if name == "self" {
49
+ return nil, fmt.Errorf("cannot create key with name 'self'")
50
+ }
51
+
52
+ _, err = api.repo.Keystore().Get(name)
53
+ if err == nil {
54
+ return nil, fmt.Errorf("key with name '%s' already exists", name)
55
}
56
57
var sk crypto.PrivKey
@@ -55,7 +65,7 @@ func GenerateKey(opts ...caopts.KeyGenerateOption) (crypto.PrivKey, crypto.PubKe
65
66
priv, pub, err := crypto.GenerateKeyPairWithReader(crypto.RSA, options.Size, rand.Reader)
67
if err != nil {
58
- return nil, nil, err
68
+ return nil, err
69
}
70
71
sk = priv
@@ -63,33 +73,13 @@ func GenerateKey(opts ...caopts.KeyGenerateOption) (crypto.PrivKey, crypto.PubKe
73
case "ed25519":
74
priv, pub, err := crypto.GenerateEd25519Key(rand.Reader)
75
if err != nil {
66
- return nil, nil, err
76
+ return nil, err
77
}
78
79
sk = priv
80
pk = pub
81
default:
72
- return nil, nil, fmt.Errorf("unrecognized key type: %s", options.Algorithm)
73
- }
74
-
75
- return sk, pk, nil
76
-}
77
-
78
-// Generate generates new key, stores it in the keystore under the specified
79
-// name and returns a base58 encoded multihash of its public key.
80
-func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.KeyGenerateOption) (coreiface.Key, error) {
81
- if name == "self" {
82
- return nil, fmt.Errorf("cannot create key with name 'self'")
83
- }
84
-
85
- _, err := api.repo.Keystore().Get(name)
86
- if err == nil {
87
- return nil, fmt.Errorf("key with name '%s' already exists", name)
88
- }
89
-
90
- sk, pk, err := GenerateKey(opts...)
91
- if err != nil {
92
- return nil, err
82
+ return nil, fmt.Errorf("unrecognized key type: %s", options.Algorithm)
83
}
84
85
err = api.repo.Keystore().Put(name, sk)
test/sharness/lib/test-lib.sh
+2
-2
@@ -493,10 +493,10 @@ test_check_rsa2048_sk() {
493
494
test_check_ed25519_sk() {
495
sklen=$(echo "$1" | tr -dC "[:alnum:]" | wc -c | tr -d " ") &&
496
- if (($sklen < 1600)); then
496
+ test "$sklen" = "93" || {
497
echo "Bad ED25519 sk '$1' with len '$sklen'"
498
return 1
499
- fi
499
+ }
500
}
501
502
convert_tcp_maddr() {
test/sharness/t0165-keystore.sh
+4
-21
@@ -11,22 +11,6 @@ test_description="Test keystore commands"
11
test_init_ipfs
12
13
test_key_cmd() {
14
- test_expect_success "export with no store doesn't store" '
15
- ipfs key gen -n -e && echo self > list_exp &&
16
- ipfs key list > list_out &&
17
- test_sort_cmp list_exp list_out
18
- '
19
-
20
- test_expect_success "no store without export is an error" '
21
- test_must_fail ipfs key gen -n 2>&1 | tee key_gen_out &&
22
- grep -q "you must export key" key_gen_out
23
- '
24
-
25
- test_expect_success "key gen without name is an error" '
26
- test_must_fail ipfs key gen 2>&1 | tee key_gen_out &&
27
- grep -q "you must specify a key name" key_gen_out
28
- '
29
-
14
# test key output format
15
test_expect_success "create an RSA key and test B58MH multihash output" '
16
PEERID=$(ipfs key gen -f=b58mh --type=rsa --size=2048 key_rsa) &&
@@ -49,10 +33,9 @@ PEERID=$(ipfs key gen -f=b36cid --type=ed25519 key_ed25519) &&
33
test_check_ed25519_b36cid_peerid $PEERID
34
'
35
52
-test_expect_success "create and export an ED25519 key" '
53
-SK=$(ipfs key gen -e key_ed25519_2) &&
54
-test_check_ed25519_sk $SK &&
55
-ipfs key rm key_ed25519_2
36
+test_expect_success "test ED25519 key sk export format" '
37
+SK=$(ipfs key export key_ed25519) &&
38
+test_check_ed25519_sk $SK
39
'
40
41
test_expect_success "test ED25519 key B36CID multihash format" '
@@ -76,7 +59,7 @@ ipfs key rm key_ed25519
59
imphash=$(ipfs key import -f=b58mh quxel $(cat importkey))
60
'
61
79
- test_expect_success "export an rsa key" '
62
+ test_expect_success "exported key matches imported" '
63
ipfs key export quxel >> exportkey &&
64
test_cmp importkey exportkey
65
'