1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-30 18:13:54 +08:00

keystore interface docs

License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
Jeromy
2018-01-22 11:20:07 -08:00
parent e0e856aa93
commit 76907bb402
2 changed files with 16 additions and 10 deletions

View File

@ -10,22 +10,25 @@ import (
ci "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto" ci "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
) )
// Keystore provides a key management interface
type Keystore interface { type Keystore interface {
// Has return whether or not a key exist in the Keystore // Has returns whether or not a key exist in the Keystore
Has(string) (bool, error) Has(string) (bool, error)
// Put store a key in the Keystore // Put stores a key in the Keystore, if a key with the same name already exists, returns ErrKeyExists
Put(string, ci.PrivKey) error Put(string, ci.PrivKey) error
// Get retrieve a key from the Keystore // Get retrieves a key from the Keystore if it exists, and returns ErrNoSuchKey
// otherwise.
Get(string) (ci.PrivKey, error) Get(string) (ci.PrivKey, error)
// Delete remove a key from the Keystore // Delete removes a key from the Keystore
Delete(string) error Delete(string) error
// List return a list of key identifier // List returns a list of key identifier
List() ([]string, error) List() ([]string, error)
} }
var ErrNoSuchKey = fmt.Errorf("no key by the given name was found") var ErrNoSuchKey = fmt.Errorf("no key by the given name was found")
var ErrKeyExists = fmt.Errorf("key by that name already exists, refusing to overwrite") var ErrKeyExists = fmt.Errorf("key by that name already exists, refusing to overwrite")
// FSKeystore is a keystore backed by files in a given directory stored on disk.
type FSKeystore struct { type FSKeystore struct {
dir string dir string
} }
@ -60,7 +63,7 @@ func NewFSKeystore(dir string) (*FSKeystore, error) {
return &FSKeystore{dir}, nil return &FSKeystore{dir}, nil
} }
// Has return whether or not a key exist in the Keystore // Has returns whether or not a key exist in the Keystore
func (ks *FSKeystore) Has(name string) (bool, error) { func (ks *FSKeystore) Has(name string) (bool, error) {
kp := filepath.Join(ks.dir, name) kp := filepath.Join(ks.dir, name)
@ -77,7 +80,7 @@ func (ks *FSKeystore) Has(name string) (bool, error) {
return true, nil return true, nil
} }
// Put store a key in the Keystore // Put stores a key in the Keystore, if a key with the same name already exists, returns ErrKeyExists
func (ks *FSKeystore) Put(name string, k ci.PrivKey) error { func (ks *FSKeystore) Put(name string, k ci.PrivKey) error {
if err := validateName(name); err != nil { if err := validateName(name); err != nil {
return err return err
@ -108,7 +111,8 @@ func (ks *FSKeystore) Put(name string, k ci.PrivKey) error {
return err return err
} }
// Get retrieve a key from the Keystore // Get retrieves a key from the Keystore if it exists, and returns ErrNoSuchKey
// otherwise.
func (ks *FSKeystore) Get(name string) (ci.PrivKey, error) { func (ks *FSKeystore) Get(name string) (ci.PrivKey, error) {
if err := validateName(name); err != nil { if err := validateName(name); err != nil {
return nil, err return nil, err
@ -127,7 +131,7 @@ func (ks *FSKeystore) Get(name string) (ci.PrivKey, error) {
return ci.UnmarshalPrivateKey(data) return ci.UnmarshalPrivateKey(data)
} }
// Delete remove a key from the Keystore // Delete removes a key from the Keystore
func (ks *FSKeystore) Delete(name string) error { func (ks *FSKeystore) Delete(name string) error {
if err := validateName(name); err != nil { if err := validateName(name); err != nil {
return err return err

View File

@ -2,6 +2,8 @@ package keystore
import ci "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto" import ci "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
// MemKeystore is an in memory keystore implementation that is not persisted to
// any backing storage.
type MemKeystore struct { type MemKeystore struct {
keys map[string]ci.PrivKey keys map[string]ci.PrivKey
} }
@ -58,7 +60,7 @@ func (mk *MemKeystore) Delete(name string) error {
// List return a list of key identifier // List return a list of key identifier
func (mk *MemKeystore) List() ([]string, error) { func (mk *MemKeystore) List() ([]string, error) {
out := make([]string, 0, len(mk.keys)) out := make([]string, 0, len(mk.keys))
for k, _ := range mk.keys { for k := range mk.keys {
out = append(out, k) out = append(out, k)
} }
return out, nil return out, nil