From c616b7ce20cb1316505b8cb9fe7f90a2529ac357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 3 Aug 2018 16:46:25 +0200 Subject: [PATCH] coreapi: key: some changes to match command functionality MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/coreapi/interface/key.go | 8 +++++++- core/coreapi/key.go | 23 ++++++++++++++--------- core/coreapi/key_test.go | 8 ++++---- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/core/coreapi/interface/key.go b/core/coreapi/interface/key.go index 928aa265f..9e9c7e400 100644 --- a/core/coreapi/interface/key.go +++ b/core/coreapi/interface/key.go @@ -4,14 +4,20 @@ import ( "context" options "github.com/ipfs/go-ipfs/core/coreapi/interface/options" + + "gx/ipfs/QmdVrMn1LhB4ybb8hMVaMLXnA8XRSewMnK6YqXKXoTcRvN/go-libp2p-peer" ) // Key specifies the interface to Keys in KeyAPI Keystore type Key interface { // Key returns key name Name() string + // Path returns key path Path() Path + + // Id returns key PeerID + Id() peer.ID } // KeyAPI specifies the interface to Keystore @@ -28,5 +34,5 @@ type KeyAPI interface { List(ctx context.Context) ([]Key, error) // Remove removes keys from keystore. Returns ipns path of the removed key - Remove(ctx context.Context, name string) (Path, error) + Remove(ctx context.Context, name string) (Key, error) } diff --git a/core/coreapi/key.go b/core/coreapi/key.go index 2767a3b07..09fa4fd36 100644 --- a/core/coreapi/key.go +++ b/core/coreapi/key.go @@ -18,7 +18,7 @@ type KeyAPI CoreAPI type key struct { name string - peerId string + peerId peer.ID } // Name returns the key name @@ -28,7 +28,7 @@ func (k *key) Name() string { // Path returns the path of the key. func (k *key) Path() coreiface.Path { - path, err := coreiface.ParsePath(ipfspath.Join([]string{"/ipns", k.peerId})) + path, err := coreiface.ParsePath(ipfspath.Join([]string{"/ipns", k.peerId.Pretty()})) if err != nil { panic("error parsing path: " + err.Error()) } @@ -36,6 +36,11 @@ func (k *key) Path() coreiface.Path { return path } +// Id returns key PeerID +func (k *key) Id() peer.ID { + return k.peerId +} + // Generate generates new key, stores it in the keystore under the specified // name and returns a base58 encoded multihash of its public key. 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 } if name == "self" { - return nil, fmt.Errorf("cannot overwrite key with name 'self'") + return nil, fmt.Errorf("cannot create key with name 'self'") } _, err = api.node.Repo.Keystore().Get(name) @@ -91,7 +96,7 @@ func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.Key return nil, err } - return &key{name, pid.Pretty()}, nil + return &key{name, pid}, nil } // List returns a list keys stored in keystore. @@ -104,7 +109,7 @@ func (api *KeyAPI) List(ctx context.Context) ([]coreiface.Key, error) { sort.Strings(keys) out := make([]coreiface.Key, len(keys)+1) - out[0] = &key{"self", api.node.Identity.Pretty()} + out[0] = &key{"self", api.node.Identity} for n, k := range keys { privKey, err := api.node.Repo.Keystore().Get(k) @@ -119,7 +124,7 @@ func (api *KeyAPI) List(ctx context.Context) ([]coreiface.Key, error) { return nil, err } - out[n+1] = &key{k, pid.Pretty()} + out[n+1] = &key{k, pid} } return out, nil } @@ -175,11 +180,11 @@ func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, o return nil, false, err } - return &key{newName, pid.Pretty()}, overwrite, ks.Delete(oldName) + return &key{newName, pid}, overwrite, ks.Delete(oldName) } // Remove removes keys from keystore. Returns ipns path of the removed key. -func (api *KeyAPI) Remove(ctx context.Context, name string) (coreiface.Path, error) { +func (api *KeyAPI) Remove(ctx context.Context, name string) (coreiface.Key, error) { ks := api.node.Repo.Keystore() if name == "self" { @@ -203,5 +208,5 @@ func (api *KeyAPI) Remove(ctx context.Context, name string) (coreiface.Path, err return nil, err } - return (&key{"", pid.Pretty()}).Path(), nil + return &key{"", pid}, nil } diff --git a/core/coreapi/key_test.go b/core/coreapi/key_test.go index 8feb2ee92..55bc3b6b9 100644 --- a/core/coreapi/key_test.go +++ b/core/coreapi/key_test.go @@ -174,8 +174,8 @@ func TestGenerateExisting(t *testing.T) { if err == nil { t.Error("expected error to not be nil") } else { - if err.Error() != "cannot overwrite key with name 'self'" { - t.Fatalf("expected error 'cannot overwrite key with name 'self'', got '%s'", err.Error()) + if err.Error() != "cannot create key with name 'self'" { + t.Fatalf("expected error 'cannot create key with name 'self'', got '%s'", err.Error()) } } } @@ -396,8 +396,8 @@ func TestRemove(t *testing.T) { return } - if k.Path().String() != p.String() { - t.Errorf("k and p should have equal paths, '%s'!='%s'", k.Path().String(), p.String()) + if k.Path().String() != p.Path().String() { + t.Errorf("k and p should have equal paths, '%s'!='%s'", k.Path().String(), p.Path().String()) } l, err = api.Key().List(ctx)