From bb74e84b29b3461ed9de2e3536aa7e1c33991570 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 28 Feb 2018 15:36:04 +0100 Subject: [PATCH] Clean up keys/keybase and comments --- client/keys.go | 16 +++------------- client/keys/README.md | 2 ++ client/keys/add.go | 14 -------------- client/keys/delete.go | 14 -------------- client/keys/list.go | 14 -------------- client/keys/root.go | 14 -------------- client/keys/show.go | 14 -------------- client/keys/update.go | 14 -------------- client/keys/utils.go | 8 ++++++-- x/bank/commands/sendtx.go | 7 +++---- 10 files changed, 14 insertions(+), 103 deletions(-) diff --git a/client/keys.go b/client/keys.go index f64ba405f6..42eb00b7eb 100644 --- a/client/keys.go +++ b/client/keys.go @@ -6,28 +6,18 @@ import ( dbm "github.com/tendermint/tmlibs/db" ) -// KeyDBName is the directory under root where we store the keys -const KeyDBName = "keys" - // GetKeyBase initializes a keybase based on the configuration -func GetKeyBase(rootDir string) (keys.Keybase, error) { - db, err := dbm.NewGoLevelDB(KeyDBName, rootDir) - if err != nil { - return nil, err - } +func GetKeyBase(db dbm.DB) keys.Keybase { keybase := keys.New( db, words.MustLoadCodec("english"), ) - return keybase, nil + return keybase } // MockKeyBase generates an in-memory keybase that will be discarded // useful for --dry-run to generate a seed phrase without // storing the key func MockKeyBase() keys.Keybase { - return keys.New( - dbm.NewMemDB(), - words.MustLoadCodec("english"), - ) + return GetKeyBase(dbm.NewMemDB()) } diff --git a/client/keys/README.md b/client/keys/README.md index 8bf9ca73b6..029508ad5f 100644 --- a/client/keys/README.md +++ b/client/keys/README.md @@ -1,5 +1,7 @@ # Keys CLI +**WARNING: out-of-date and parts are wrong.... please update** + This is as much an example how to expose cobra/viper, as for a cli itself (I think this code is overkill for what go-keys needs). But please look at the commands, and give feedback and changes. diff --git a/client/keys/add.go b/client/keys/add.go index cef0b62601..40bb0d103e 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -1,17 +1,3 @@ -// Copyright © 2017 Ethan Frey -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - package keys import ( diff --git a/client/keys/delete.go b/client/keys/delete.go index 3477ed6d40..65a9513272 100644 --- a/client/keys/delete.go +++ b/client/keys/delete.go @@ -1,17 +1,3 @@ -// Copyright © 2017 Ethan Frey -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - package keys import ( diff --git a/client/keys/list.go b/client/keys/list.go index 8e11d36afc..14c4408781 100644 --- a/client/keys/list.go +++ b/client/keys/list.go @@ -1,17 +1,3 @@ -// Copyright © 2017 Ethan Frey -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - package keys import "github.com/spf13/cobra" diff --git a/client/keys/root.go b/client/keys/root.go index d067d15159..962986c91a 100644 --- a/client/keys/root.go +++ b/client/keys/root.go @@ -1,17 +1,3 @@ -// Copyright © 2017 Ethan Frey -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - package keys import ( diff --git a/client/keys/show.go b/client/keys/show.go index e569f265de..a22cb4bc88 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -1,17 +1,3 @@ -// Copyright © 2017 Ethan Frey -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - package keys import ( diff --git a/client/keys/update.go b/client/keys/update.go index c809b988ee..0e0f881c6e 100644 --- a/client/keys/update.go +++ b/client/keys/update.go @@ -1,17 +1,3 @@ -// Copyright © 2017 Ethan Frey -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - package keys import ( diff --git a/client/keys/utils.go b/client/keys/utils.go index 5c156cb748..19e63d7482 100644 --- a/client/keys/utils.go +++ b/client/keys/utils.go @@ -7,10 +7,14 @@ import ( keys "github.com/tendermint/go-crypto/keys" "github.com/tendermint/tmlibs/cli" + dbm "github.com/tendermint/tmlibs/db" "github.com/cosmos/cosmos-sdk/client" ) +// KeyDBName is the directory under root where we store the keys +const KeyDBName = "keys" + var ( // keybase is used to make GetKeyBase a singleton keybase keys.Keybase @@ -20,11 +24,11 @@ var ( func GetKeyBase() (keys.Keybase, error) { if keybase == nil { rootDir := viper.GetString(cli.HomeFlag) - kb, err := client.GetKeyBase(rootDir) + db, err := dbm.NewGoLevelDB(KeyDBName, rootDir) if err != nil { return nil, err } - keybase = kb + keybase = client.GetKeyBase(db) } return keybase, nil } diff --git a/x/bank/commands/sendtx.go b/x/bank/commands/sendtx.go index 6293661e6d..b6d3ed31fc 100644 --- a/x/bank/commands/sendtx.go +++ b/x/bank/commands/sendtx.go @@ -9,11 +9,11 @@ import ( "github.com/spf13/viper" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/examples/basecoin/app" // XXX: not good sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank" crypto "github.com/tendermint/go-crypto" - "github.com/tendermint/tmlibs/cli" ) const ( @@ -70,8 +70,7 @@ func sendTx(cmd *cobra.Command, args []string) error { } func buildTx() ([]byte, error) { - rootDir := viper.GetString(cli.HomeFlag) - keybase, err := client.GetKeyBase(rootDir) + keybase, err := keys.GetKeyBase() if err != nil { return nil, err } @@ -79,7 +78,7 @@ func buildTx() ([]byte, error) { name := viper.GetString(client.FlagName) info, err := keybase.Get(name) if err != nil { - return nil, errors.WithMessage(err, "No key for name") + return nil, errors.Errorf("No key for: %s", name) } from := info.PubKey.Address()