From 4d4d7b8e089d3f6ee532d75c16bec738dfd51110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Mon, 1 May 2017 15:14:30 +0900 Subject: [PATCH 01/10] Implement ipfs key rm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Michael Muré --- core/commands/keystore.go | 56 +++++++++++++++++++++++++++++++++ test/sharness/t0165-keystore.sh | 8 +++++ 2 files changed, 64 insertions(+) diff --git a/core/commands/keystore.go b/core/commands/keystore.go index 7ab6cd91495..b47faf02b9c 100644 --- a/core/commands/keystore.go +++ b/core/commands/keystore.go @@ -35,6 +35,7 @@ var KeyCmd = &cmds.Command{ Subcommands: map[string]*cmds.Command{ "gen": KeyGenCmd, "list": KeyListCmd, + "rm": KeyRmCmd, }, } @@ -202,6 +203,61 @@ var KeyListCmd = &cmds.Command{ Type: KeyOutputList{}, } +var KeyRmCmd = &cmds.Command{ + Helptext: cmds.HelpText{ + Tagline: "Remove a keypair", + }, + Arguments: []cmds.Argument{ + cmds.StringArg("name", true, false, "name of key to remove"), + }, + Run: func(req cmds.Request, res cmds.Response) { + n, err := req.InvocContext().GetNode() + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + name := req.Arguments()[0] + if name == "self" { + res.SetError(fmt.Errorf("cannot remove key with name 'self'"), cmds.ErrNormal) + return + } + + removed, err := n.Repo.Keystore().Get(name) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + err = n.Repo.Keystore().Delete(name) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + pubKey := removed.GetPublic() + + pid, err := peer.IDFromPublicKey(pubKey) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + res.SetOutput(&KeyOutput{ + Name: name, + Id: pid.Pretty(), + }) + }, + Marshalers: cmds.MarshalerMap{ + cmds.Text: func(res cmds.Response) (io.Reader, error) { + v := res.Output().(*KeyOutput) + s := fmt.Sprintf("Removed key %s with Id: %s\n", v.Name, v.Id) + return strings.NewReader(s), nil + }, + }, + Type: KeyOutput{}, +} + func keyOutputListMarshaler(res cmds.Response) (io.Reader, error) { withId, _, _ := res.Request().Option("l").Bool() diff --git a/test/sharness/t0165-keystore.sh b/test/sharness/t0165-keystore.sh index ec9726a8a84..78bc9d6463f 100755 --- a/test/sharness/t0165-keystore.sh +++ b/test/sharness/t0165-keystore.sh @@ -36,6 +36,14 @@ test_key_cmd() { PeerID="$(ipfs config Identity.PeerID)" ipfs key list -l | grep "$PeerID self" ' + + test_expect_success "key rm remove a key" ' + ipfs key rm foobarsa + echo bazed > list_exp && + echo self >> list_exp + ipfs key list | sort > list_out && + test_cmp list_exp list_out + ' } test_key_cmd From 730ab6115cffb3849694de9263becf35b08aba60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Mon, 1 May 2017 21:54:24 +0900 Subject: [PATCH 02/10] fix fmt and exported var that shouldn't be MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Michael Muré --- core/commands/keystore.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/commands/keystore.go b/core/commands/keystore.go index b47faf02b9c..6e8ad59fa9e 100644 --- a/core/commands/keystore.go +++ b/core/commands/keystore.go @@ -33,9 +33,9 @@ var KeyCmd = &cmds.Command{ `, }, Subcommands: map[string]*cmds.Command{ - "gen": KeyGenCmd, - "list": KeyListCmd, - "rm": KeyRmCmd, + "gen": keyGenCmd, + "list": keyListCmd, + "rm": keyRmCmd, }, } @@ -48,7 +48,7 @@ type KeyOutputList struct { Keys []KeyOutput } -var KeyGenCmd = &cmds.Command{ +var keyGenCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Create a new keypair", }, @@ -151,7 +151,7 @@ var KeyGenCmd = &cmds.Command{ Type: KeyOutput{}, } -var KeyListCmd = &cmds.Command{ +var keyListCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "List all local keypairs", }, @@ -203,7 +203,7 @@ var KeyListCmd = &cmds.Command{ Type: KeyOutputList{}, } -var KeyRmCmd = &cmds.Command{ +var keyRmCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Remove a keypair", }, From bd96f71d2c884e298d608963cd3e368287f2e852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Tue, 2 May 2017 01:41:30 +0900 Subject: [PATCH 03/10] Make ipfs key rm variadic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Michael Muré --- core/commands/keystore.go | 66 +++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/core/commands/keystore.go b/core/commands/keystore.go index 6e8ad59fa9e..e3cbe4449d5 100644 --- a/core/commands/keystore.go +++ b/core/commands/keystore.go @@ -208,7 +208,10 @@ var keyRmCmd = &cmds.Command{ Tagline: "Remove a keypair", }, Arguments: []cmds.Argument{ - cmds.StringArg("name", true, false, "name of key to remove"), + cmds.StringArg("name", true, true, "names of keys to remove"), + }, + Options: []cmds.Option{ + cmds.BoolOption("l", "Show extra information about keys."), }, Run: func(req cmds.Request, res cmds.Response) { n, err := req.InvocContext().GetNode() @@ -217,45 +220,46 @@ var keyRmCmd = &cmds.Command{ return } - name := req.Arguments()[0] - if name == "self" { - res.SetError(fmt.Errorf("cannot remove key with name 'self'"), cmds.ErrNormal) - return - } + names := req.Arguments() - removed, err := n.Repo.Keystore().Get(name) - if err != nil { - res.SetError(err, cmds.ErrNormal) - return - } + list := make([]KeyOutput, 0, len(names)) + for _, name := range names { + if name == "self" { + res.SetError(fmt.Errorf("cannot remove key with name 'self'"), cmds.ErrNormal) + return + } - err = n.Repo.Keystore().Delete(name) - if err != nil { - res.SetError(err, cmds.ErrNormal) - return - } + removed, err := n.Repo.Keystore().Get(name) + if err != nil { + res.SetError(fmt.Errorf("no key named %s was found", name), cmds.ErrNormal) + return + } - pubKey := removed.GetPublic() + pubKey := removed.GetPublic() - pid, err := peer.IDFromPublicKey(pubKey) - if err != nil { - res.SetError(err, cmds.ErrNormal) - return + pid, err := peer.IDFromPublicKey(pubKey) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + list = append(list, KeyOutput{Name: name, Id: pid.Pretty()}) } - res.SetOutput(&KeyOutput{ - Name: name, - Id: pid.Pretty(), - }) + for _, name := range names { + err = n.Repo.Keystore().Delete(name) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + } + + res.SetOutput(&KeyOutputList{list}) }, Marshalers: cmds.MarshalerMap{ - cmds.Text: func(res cmds.Response) (io.Reader, error) { - v := res.Output().(*KeyOutput) - s := fmt.Sprintf("Removed key %s with Id: %s\n", v.Name, v.Id) - return strings.NewReader(s), nil - }, + cmds.Text: keyOutputListMarshaler, }, - Type: KeyOutput{}, + Type: KeyOutputList{}, } func keyOutputListMarshaler(res cmds.Response) (io.Reader, error) { From a813830657c4d7838a3f469292841a61881c68e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Tue, 2 May 2017 12:27:15 +0900 Subject: [PATCH 04/10] Enable stdin for ipfs key rm's name argument MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Michael Muré --- core/commands/keystore.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/commands/keystore.go b/core/commands/keystore.go index e3cbe4449d5..ab376c8fdbf 100644 --- a/core/commands/keystore.go +++ b/core/commands/keystore.go @@ -208,7 +208,7 @@ var keyRmCmd = &cmds.Command{ Tagline: "Remove a keypair", }, Arguments: []cmds.Argument{ - cmds.StringArg("name", true, true, "names of keys to remove"), + cmds.StringArg("name", true, true, "names of keys to remove").EnableStdin(), }, Options: []cmds.Option{ cmds.BoolOption("l", "Show extra information about keys."), From 6f815b15688f189e8f39d6baa3640982b6fb3f28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Thu, 4 May 2017 17:29:29 +0900 Subject: [PATCH 05/10] Add a Has(name) method to the keystore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Michael Muré --- keystore/keystore.go | 9 +++++++++ keystore/keystore_test.go | 8 ++++++++ keystore/memkeystore.go | 5 +++++ keystore/memkeystore_test.go | 9 +++++++++ 4 files changed, 31 insertions(+) diff --git a/keystore/keystore.go b/keystore/keystore.go index b69e3e940b1..de0b62dc61b 100644 --- a/keystore/keystore.go +++ b/keystore/keystore.go @@ -11,6 +11,7 @@ import ( ) type Keystore interface { + Has(string) bool Put(string, ci.PrivKey) error Get(string) (ci.PrivKey, error) Delete(string) error @@ -54,6 +55,14 @@ func NewFSKeystore(dir string) (*FSKeystore, error) { return &FSKeystore{dir}, nil } +func (ks *FSKeystore) Has(name string) bool { + kp := filepath.Join(ks.dir, name) + + _, err := os.Stat(kp) + + return err == nil +} + func (ks *FSKeystore) Put(name string, k ci.PrivKey) error { if err := validateName(name); err != nil { return err diff --git a/keystore/keystore_test.go b/keystore/keystore_test.go index 4840069bca0..505d9119d6c 100644 --- a/keystore/keystore_test.go +++ b/keystore/keystore_test.go @@ -82,6 +82,14 @@ func TestKeystoreBasics(t *testing.T) { t.Fatal(err) } + if !ks.Has("foo") { + t.Fatal("should know it has a key named foo") + } + + if ks.Has("nonexistingkey") { + t.Fatal("should know it doesn't have a key named nonexistingkey") + } + if err := ks.Delete("bar"); err != nil { t.Fatal(err) } diff --git a/keystore/memkeystore.go b/keystore/memkeystore.go index ae45ecf2193..0018ade4d34 100644 --- a/keystore/memkeystore.go +++ b/keystore/memkeystore.go @@ -10,6 +10,11 @@ func NewMemKeystore() *MemKeystore { return &MemKeystore{make(map[string]ci.PrivKey)} } +func (mk *MemKeystore) Has(name string) bool { + _, ok := mk.keys[name] + return ok +} + func (mk *MemKeystore) Put(name string, k ci.PrivKey) error { if err := validateName(name); err != nil { return err diff --git a/keystore/memkeystore_test.go b/keystore/memkeystore_test.go index 7f4362795d5..913f14b83ca 100644 --- a/keystore/memkeystore_test.go +++ b/keystore/memkeystore_test.go @@ -46,6 +46,15 @@ func TestMemKeyStoreBasics(t *testing.T) { if err == nil { t.Fatal("should not be able to overwrite key") } + + if !ks.Has("foo") { + t.Fatal("should know it has a key named foo") + } + + if ks.Has("nonexistingkey") { + t.Fatal("should know it doesn't have a key named nonexistingkey") + } + if err := ks.Delete("bar"); err != nil { t.Fatal(err) } From 3aa6d78ee1e6a3ab56595517434f55d31f5ca913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Thu, 4 May 2017 17:30:10 +0900 Subject: [PATCH 06/10] Implement ipfs key rename MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Michael Muré --- core/commands/keystore.go | 111 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 108 insertions(+), 3 deletions(-) diff --git a/core/commands/keystore.go b/core/commands/keystore.go index ab376c8fdbf..abd71ed89ab 100644 --- a/core/commands/keystore.go +++ b/core/commands/keystore.go @@ -33,9 +33,10 @@ var KeyCmd = &cmds.Command{ `, }, Subcommands: map[string]*cmds.Command{ - "gen": keyGenCmd, - "list": keyListCmd, - "rm": keyRmCmd, + "gen": keyGenCmd, + "list": keyListCmd, + "rename": keyRenameCmd, + "rm": keyRmCmd, }, } @@ -48,6 +49,13 @@ type KeyOutputList struct { Keys []KeyOutput } +type KeyRenameOutput struct { + Was string + Now string + Id string + Overwrite bool +} + var keyGenCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Create a new keypair", @@ -203,6 +211,103 @@ var keyListCmd = &cmds.Command{ Type: KeyOutputList{}, } +var keyRenameCmd = &cmds.Command{ + Helptext: cmds.HelpText{ + Tagline: "Rename a keypair", + }, + Arguments: []cmds.Argument{ + cmds.StringArg("name", true, false, "name of key to rename"), + cmds.StringArg("newName", true, false, "new name of the key"), + }, + Options: []cmds.Option{ + cmds.BoolOption("force", "f", "Allow to overwrite an existing key."), + }, + Run: func(req cmds.Request, res cmds.Response) { + n, err := req.InvocContext().GetNode() + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + ks := n.Repo.Keystore() + + name := req.Arguments()[0] + newName := req.Arguments()[1] + + if name == "self" { + res.SetError(fmt.Errorf("cannot rename key with name 'self'"), cmds.ErrNormal) + return + } + + if newName == "self" { + res.SetError(fmt.Errorf("cannot overwrite key with name 'self'"), cmds.ErrNormal) + return + } + + oldKey, err := ks.Get(name) + if err != nil { + res.SetError(fmt.Errorf("no key named %s was found", name), cmds.ErrNormal) + return + } + + pubKey := oldKey.GetPublic() + + pid, err := peer.IDFromPublicKey(pubKey) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + overwrite := false + force, _, _ := res.Request().Option("f").Bool() + if force && ks.Has(newName) { + overwrite = true + err := ks.Delete(newName) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + } + + err = ks.Put(newName, oldKey) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + err = ks.Delete(name) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + + res.SetOutput(&KeyRenameOutput{ + Was: name, + Now: newName, + Id: pid.Pretty(), + Overwrite: overwrite, + }) + }, + Marshalers: cmds.MarshalerMap{ + cmds.Text: func(res cmds.Response) (io.Reader, error) { + k, ok := res.Output().(*KeyRenameOutput) + if !ok { + return nil, fmt.Errorf("expected a KeyRenameOutput as command result") + } + + buf := new(bytes.Buffer) + + if k.Overwrite { + fmt.Fprintf(buf, "Key %s renamed to %s with overwriting\n", k.Id, k.Now) + } else { + fmt.Fprintf(buf, "Key %s renamed to %s\n", k.Id, k.Now) + } + return buf, nil + }, + }, + Type: KeyRenameOutput{}, +} + var keyRmCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Remove a keypair", From c0a04a06c39064f6bf40677e43d12ec6183ad578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Thu, 4 May 2017 21:56:58 +0900 Subject: [PATCH 07/10] add sharness test for ipfs key rename MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Michael Muré --- test/sharness/t0165-keystore.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/sharness/t0165-keystore.sh b/test/sharness/t0165-keystore.sh index 78bc9d6463f..8ba5f61381f 100755 --- a/test/sharness/t0165-keystore.sh +++ b/test/sharness/t0165-keystore.sh @@ -44,6 +44,24 @@ test_key_cmd() { ipfs key list | sort > list_out && test_cmp list_exp list_out ' + + test_expect_success "key rename rename a key" ' + ipfs key rename bazed fooed + echo fooed > list_exp && + echo self >> list_exp + ipfs key list | sort > list_out && + test_cmp list_exp list_out + ' + + test_expect_success "key rename can't remove self" ' + test_must_fail ipfs key rename self bar 2>&1 | tee key_rename_out && + grep -q "Error: cannot rename key with name" key_rename_out + ' + + test_expect_success "key rename can't overwrite self, even with force" ' + test_must_fail ipfs key rename -f fooed self 2>&1 | tee key_rename_out && + grep -q "Error: cannot overwrite key with name" key_rename_out + ' } test_key_cmd From 7e91da20673f97cac2ff0729402d678751f5cae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Mon, 8 May 2017 17:00:00 +0900 Subject: [PATCH 08/10] Future-proof keystore.Has by returning an error as well MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Michael Muré --- core/commands/keystore.go | 14 +++++++++++--- keystore/keystore.go | 14 +++++++++++--- keystore/keystore_test.go | 12 ++++++++++-- keystore/memkeystore.go | 4 ++-- keystore/memkeystore_test.go | 12 ++++++++++-- 5 files changed, 44 insertions(+), 12 deletions(-) diff --git a/core/commands/keystore.go b/core/commands/keystore.go index abd71ed89ab..e0d9b5d101f 100644 --- a/core/commands/keystore.go +++ b/core/commands/keystore.go @@ -260,13 +260,21 @@ var keyRenameCmd = &cmds.Command{ overwrite := false force, _, _ := res.Request().Option("f").Bool() - if force && ks.Has(newName) { - overwrite = true - err := ks.Delete(newName) + if force { + exist, err := ks.Has(newName) if err != nil { res.SetError(err, cmds.ErrNormal) return } + + if exist { + overwrite = true + err := ks.Delete(newName) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + } } err = ks.Put(newName, oldKey) diff --git a/keystore/keystore.go b/keystore/keystore.go index de0b62dc61b..e38211480ea 100644 --- a/keystore/keystore.go +++ b/keystore/keystore.go @@ -11,7 +11,7 @@ import ( ) type Keystore interface { - Has(string) bool + Has(string) (bool, error) Put(string, ci.PrivKey) error Get(string) (ci.PrivKey, error) Delete(string) error @@ -55,12 +55,20 @@ func NewFSKeystore(dir string) (*FSKeystore, error) { return &FSKeystore{dir}, nil } -func (ks *FSKeystore) Has(name string) bool { +func (ks *FSKeystore) Has(name string) (bool, error) { kp := filepath.Join(ks.dir, name) _, err := os.Stat(kp) - return err == nil + if os.IsNotExist(err) { + return false, nil + } + + if err != nil { + return false, err + } + + return true, nil } func (ks *FSKeystore) Put(name string, k ci.PrivKey) error { diff --git a/keystore/keystore_test.go b/keystore/keystore_test.go index 505d9119d6c..53c30b0d7d1 100644 --- a/keystore/keystore_test.go +++ b/keystore/keystore_test.go @@ -82,13 +82,21 @@ func TestKeystoreBasics(t *testing.T) { t.Fatal(err) } - if !ks.Has("foo") { + exist, err := ks.Has("foo") + if !exist { t.Fatal("should know it has a key named foo") } + if err != nil { + t.Fatal(err) + } - if ks.Has("nonexistingkey") { + exist, err = ks.Has("nonexistingkey") + if exist { t.Fatal("should know it doesn't have a key named nonexistingkey") } + if err != nil { + t.Fatal(err) + } if err := ks.Delete("bar"); err != nil { t.Fatal(err) diff --git a/keystore/memkeystore.go b/keystore/memkeystore.go index 0018ade4d34..626ad8bc078 100644 --- a/keystore/memkeystore.go +++ b/keystore/memkeystore.go @@ -10,9 +10,9 @@ func NewMemKeystore() *MemKeystore { return &MemKeystore{make(map[string]ci.PrivKey)} } -func (mk *MemKeystore) Has(name string) bool { +func (mk *MemKeystore) Has(name string) (bool, error) { _, ok := mk.keys[name] - return ok + return ok, nil } func (mk *MemKeystore) Put(name string, k ci.PrivKey) error { diff --git a/keystore/memkeystore_test.go b/keystore/memkeystore_test.go index 913f14b83ca..62533d54b92 100644 --- a/keystore/memkeystore_test.go +++ b/keystore/memkeystore_test.go @@ -47,13 +47,21 @@ func TestMemKeyStoreBasics(t *testing.T) { t.Fatal("should not be able to overwrite key") } - if !ks.Has("foo") { + exist, err := ks.Has("foo") + if !exist { t.Fatal("should know it has a key named foo") } + if err != nil { + t.Fatal(err) + } - if ks.Has("nonexistingkey") { + exist, err = ks.Has("nonexistingkey") + if exist { t.Fatal("should know it doesn't have a key named nonexistingkey") } + if err != nil { + t.Fatal(err) + } if err := ks.Delete("bar"); err != nil { t.Fatal(err) From cb8d5eb2458f5aa122c22e5038b8ad333b796145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Sun, 14 May 2017 21:01:18 +0900 Subject: [PATCH 09/10] Add a test for failure of 'ipfs key rm self' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Michael Muré --- test/sharness/t0165-keystore.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/sharness/t0165-keystore.sh b/test/sharness/t0165-keystore.sh index 8ba5f61381f..305ab1e38c1 100755 --- a/test/sharness/t0165-keystore.sh +++ b/test/sharness/t0165-keystore.sh @@ -45,6 +45,11 @@ test_key_cmd() { test_cmp list_exp list_out ' + test_expect_success "key rm can't remove self" ' + test_must_fail ipfs key rm self 2>&1 | tee key_rm_out && + grep -q "Error: cannot remove key with name" key_rm_out + ' + test_expect_success "key rename rename a key" ' ipfs key rename bazed fooed echo fooed > list_exp && @@ -53,7 +58,7 @@ test_key_cmd() { test_cmp list_exp list_out ' - test_expect_success "key rename can't remove self" ' + test_expect_success "key rename can't rename self" ' test_must_fail ipfs key rename self bar 2>&1 | tee key_rename_out && grep -q "Error: cannot rename key with name" key_rename_out ' From 2593495311d57007fac382cb7a6667e8f1f6b58f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Sun, 14 May 2017 21:02:01 +0900 Subject: [PATCH 10/10] Document exported symbols MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Michael Muré --- core/commands/keystore.go | 1 + keystore/keystore.go | 10 ++++++++++ keystore/memkeystore.go | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/core/commands/keystore.go b/core/commands/keystore.go index e0d9b5d101f..522552815d9 100644 --- a/core/commands/keystore.go +++ b/core/commands/keystore.go @@ -49,6 +49,7 @@ type KeyOutputList struct { Keys []KeyOutput } +// KeyRenameOutput define the output type of keyRenameCmd type KeyRenameOutput struct { Was string Now string diff --git a/keystore/keystore.go b/keystore/keystore.go index e38211480ea..8424dbafa58 100644 --- a/keystore/keystore.go +++ b/keystore/keystore.go @@ -11,10 +11,15 @@ import ( ) type Keystore interface { + // Has return whether or not a key exist in the Keystore Has(string) (bool, error) + // Put store a key in the Keystore Put(string, ci.PrivKey) error + // Get retrieve a key from the Keystore Get(string) (ci.PrivKey, error) + // Delete remove a key from the Keystore Delete(string) error + // List return a list of key identifier List() ([]string, error) } @@ -55,6 +60,7 @@ func NewFSKeystore(dir string) (*FSKeystore, error) { return &FSKeystore{dir}, nil } +// Has return whether or not a key exist in the Keystore func (ks *FSKeystore) Has(name string) (bool, error) { kp := filepath.Join(ks.dir, name) @@ -71,6 +77,7 @@ func (ks *FSKeystore) Has(name string) (bool, error) { return true, nil } +// Put store a key in the Keystore func (ks *FSKeystore) Put(name string, k ci.PrivKey) error { if err := validateName(name); err != nil { return err @@ -104,6 +111,7 @@ func (ks *FSKeystore) Put(name string, k ci.PrivKey) error { return nil } +// Get retrieve a key from the Keystore func (ks *FSKeystore) Get(name string) (ci.PrivKey, error) { if err := validateName(name); err != nil { return nil, err @@ -122,6 +130,7 @@ func (ks *FSKeystore) Get(name string) (ci.PrivKey, error) { return ci.UnmarshalPrivateKey(data) } +// Delete remove a key from the Keystore func (ks *FSKeystore) Delete(name string) error { if err := validateName(name); err != nil { return err @@ -132,6 +141,7 @@ func (ks *FSKeystore) Delete(name string) error { return os.Remove(kp) } +// List return a list of key identifier func (ks *FSKeystore) List() ([]string, error) { dir, err := os.Open(ks.dir) if err != nil { diff --git a/keystore/memkeystore.go b/keystore/memkeystore.go index 626ad8bc078..068c5e18917 100644 --- a/keystore/memkeystore.go +++ b/keystore/memkeystore.go @@ -10,11 +10,13 @@ func NewMemKeystore() *MemKeystore { return &MemKeystore{make(map[string]ci.PrivKey)} } +// Has return whether or not a key exist in the Keystore func (mk *MemKeystore) Has(name string) (bool, error) { _, ok := mk.keys[name] return ok, nil } +// Put store a key in the Keystore func (mk *MemKeystore) Put(name string, k ci.PrivKey) error { if err := validateName(name); err != nil { return err @@ -29,6 +31,7 @@ func (mk *MemKeystore) Put(name string, k ci.PrivKey) error { return nil } +// Get retrieve a key from the Keystore func (mk *MemKeystore) Get(name string) (ci.PrivKey, error) { if err := validateName(name); err != nil { return nil, err @@ -42,6 +45,7 @@ func (mk *MemKeystore) Get(name string) (ci.PrivKey, error) { return k, nil } +// Delete remove a key from the Keystore func (mk *MemKeystore) Delete(name string) error { if err := validateName(name); err != nil { return err @@ -51,6 +55,7 @@ func (mk *MemKeystore) Delete(name string) error { return nil } +// List return a list of key identifier func (mk *MemKeystore) List() ([]string, error) { out := make([]string, 0, len(mk.keys)) for k, _ := range mk.keys {