mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 02:30:39 +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:
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user