coreapi: key: some changes to match command functionality
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Aug 3, 2018 at 16:46 UTC
c616b7ce20cb1316505b8cb9fe7f90a2529ac357
3 files changed
+25
-14
core/coreapi/interface/key.go
+7
-1
@@ -4,14 +4,20 @@ import (
4
"context"
5
6
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
7
+
8
+ "gx/ipfs/QmdVrMn1LhB4ybb8hMVaMLXnA8XRSewMnK6YqXKXoTcRvN/go-libp2p-peer"
9
)
10
11
// Key specifies the interface to Keys in KeyAPI Keystore
12
type Key interface {
13
// Key returns key name
14
Name() string
15
+
16
// Path returns key path
17
Path() Path
18
+
19
+ // Id returns key PeerID
20
+ Id() peer.ID
21
}
22
23
// KeyAPI specifies the interface to Keystore
@@ -28,5 +34,5 @@ type KeyAPI interface {
34
List(ctx context.Context) ([]Key, error)
35
36
// Remove removes keys from keystore. Returns ipns path of the removed key
31
- Remove(ctx context.Context, name string) (Path, error)
37
+ Remove(ctx context.Context, name string) (Key, error)
38
}
core/coreapi/key.go
+14
-9
@@ -18,7 +18,7 @@ type KeyAPI CoreAPI
18
19
type key struct {
20
name string
21
- peerId string
21
+ peerId peer.ID
22
}
23
24
// Name returns the key name
@@ -28,7 +28,7 @@ func (k *key) Name() string {
28
29
// Path returns the path of the key.
30
func (k *key) Path() coreiface.Path {
31
- path, err := coreiface.ParsePath(ipfspath.Join([]string{"/ipns", k.peerId}))
31
+ path, err := coreiface.ParsePath(ipfspath.Join([]string{"/ipns", k.peerId.Pretty()}))
32
if err != nil {
33
panic("error parsing path: " + err.Error())
34
}
@@ -36,6 +36,11 @@ func (k *key) Path() coreiface.Path {
36
return path
37
}
38
39
+// Id returns key PeerID
40
+func (k *key) Id() peer.ID {
41
+ return k.peerId
42
+}
43
+
44
// Generate generates new key, stores it in the keystore under the specified
45
// name and returns a base58 encoded multihash of its public key.
46
func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.KeyGenerateOption) (coreiface.Key, error) {
@@ -45,7 +50,7 @@ func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.Key
50
}
51
52
if name == "self" {
48
- return nil, fmt.Errorf("cannot overwrite key with name 'self'")
53
+ return nil, fmt.Errorf("cannot create key with name 'self'")
54
}
55
56
_, err = api.node.Repo.Keystore().Get(name)
@@ -91,7 +96,7 @@ func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.Key
96
return nil, err
97
}
98
94
- return &key{name, pid.Pretty()}, nil
99
+ return &key{name, pid}, nil
100
}
101
102
// List returns a list keys stored in keystore.
@@ -104,7 +109,7 @@ func (api *KeyAPI) List(ctx context.Context) ([]coreiface.Key, error) {
109
sort.Strings(keys)
110
111
out := make([]coreiface.Key, len(keys)+1)
107
- out[0] = &key{"self", api.node.Identity.Pretty()}
112
+ out[0] = &key{"self", api.node.Identity}
113
114
for n, k := range keys {
115
privKey, err := api.node.Repo.Keystore().Get(k)
@@ -119,7 +124,7 @@ func (api *KeyAPI) List(ctx context.Context) ([]coreiface.Key, error) {
124
return nil, err
125
}
126
122
- out[n+1] = &key{k, pid.Pretty()}
127
+ out[n+1] = &key{k, pid}
128
}
129
return out, nil
130
}
@@ -175,11 +180,11 @@ func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, o
180
return nil, false, err
181
}
182
178
- return &key{newName, pid.Pretty()}, overwrite, ks.Delete(oldName)
183
+ return &key{newName, pid}, overwrite, ks.Delete(oldName)
184
}
185
186
// Remove removes keys from keystore. Returns ipns path of the removed key.
182
-func (api *KeyAPI) Remove(ctx context.Context, name string) (coreiface.Path, error) {
187
+func (api *KeyAPI) Remove(ctx context.Context, name string) (coreiface.Key, error) {
188
ks := api.node.Repo.Keystore()
189
190
if name == "self" {
@@ -203,5 +208,5 @@ func (api *KeyAPI) Remove(ctx context.Context, name string) (coreiface.Path, err
208
return nil, err
209
}
210
206
- return (&key{"", pid.Pretty()}).Path(), nil
211
+ return &key{"", pid}, nil
212
}
core/coreapi/key_test.go
+4
-4
@@ -174,8 +174,8 @@ func TestGenerateExisting(t *testing.T) {
174
if err == nil {
175
t.Error("expected error to not be nil")
176
} else {
177
- if err.Error() != "cannot overwrite key with name 'self'" {
178
- t.Fatalf("expected error 'cannot overwrite key with name 'self'', got '%s'", err.Error())
177
+ if err.Error() != "cannot create key with name 'self'" {
178
+ t.Fatalf("expected error 'cannot create key with name 'self'', got '%s'", err.Error())
179
}
180
}
181
}
@@ -396,8 +396,8 @@ func TestRemove(t *testing.T) {
396
return
397
}
398
399
- if k.Path().String() != p.String() {
400
- t.Errorf("k and p should have equal paths, '%s'!='%s'", k.Path().String(), p.String())
399
+ if k.Path().String() != p.Path().String() {
400
+ t.Errorf("k and p should have equal paths, '%s'!='%s'", k.Path().String(), p.Path().String())
401
}
402
403
l, err = api.Key().List(ctx)