1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 02:23:18 +08:00

coreapi: key: some changes to match command functionality

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera
2018-08-03 16:46:25 +02:00
parent 8d3a350c20
commit c616b7ce20
3 changed files with 25 additions and 14 deletions

View File

@ -4,14 +4,20 @@ import (
"context" "context"
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options" 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 // Key specifies the interface to Keys in KeyAPI Keystore
type Key interface { type Key interface {
// Key returns key name // Key returns key name
Name() string Name() string
// Path returns key path // Path returns key path
Path() Path Path() Path
// Id returns key PeerID
Id() peer.ID
} }
// KeyAPI specifies the interface to Keystore // KeyAPI specifies the interface to Keystore
@ -28,5 +34,5 @@ type KeyAPI interface {
List(ctx context.Context) ([]Key, error) List(ctx context.Context) ([]Key, error)
// Remove removes keys from keystore. Returns ipns path of the removed key // 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)
} }

View File

@ -18,7 +18,7 @@ type KeyAPI CoreAPI
type key struct { type key struct {
name string name string
peerId string peerId peer.ID
} }
// Name returns the key name // Name returns the key name
@ -28,7 +28,7 @@ func (k *key) Name() string {
// Path returns the path of the key. // Path returns the path of the key.
func (k *key) Path() coreiface.Path { 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 { if err != nil {
panic("error parsing path: " + err.Error()) panic("error parsing path: " + err.Error())
} }
@ -36,6 +36,11 @@ func (k *key) Path() coreiface.Path {
return 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 // Generate generates new key, stores it in the keystore under the specified
// name and returns a base58 encoded multihash of its public key. // 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) { 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" { 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) _, 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 nil, err
} }
return &key{name, pid.Pretty()}, nil return &key{name, pid}, nil
} }
// List returns a list keys stored in keystore. // 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) sort.Strings(keys)
out := make([]coreiface.Key, len(keys)+1) 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 { for n, k := range keys {
privKey, err := api.node.Repo.Keystore().Get(k) 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 return nil, err
} }
out[n+1] = &key{k, pid.Pretty()} out[n+1] = &key{k, pid}
} }
return out, nil return out, nil
} }
@ -175,11 +180,11 @@ func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, o
return nil, false, err 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. // 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() ks := api.node.Repo.Keystore()
if name == "self" { if name == "self" {
@ -203,5 +208,5 @@ func (api *KeyAPI) Remove(ctx context.Context, name string) (coreiface.Path, err
return nil, err return nil, err
} }
return (&key{"", pid.Pretty()}).Path(), nil return &key{"", pid}, nil
} }

View File

@ -174,8 +174,8 @@ func TestGenerateExisting(t *testing.T) {
if err == nil { if err == nil {
t.Error("expected error to not be nil") t.Error("expected error to not be nil")
} else { } else {
if err.Error() != "cannot overwrite key with name 'self'" { if err.Error() != "cannot create key with name 'self'" {
t.Fatalf("expected error 'cannot overwrite key with name 'self'', got '%s'", err.Error()) t.Fatalf("expected error 'cannot create key with name 'self'', got '%s'", err.Error())
} }
} }
} }
@ -396,8 +396,8 @@ func TestRemove(t *testing.T) {
return return
} }
if 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.String()) t.Errorf("k and p should have equal paths, '%s'!='%s'", k.Path().String(), p.Path().String())
} }
l, err = api.Key().List(ctx) l, err = api.Key().List(ctx)