Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: hide dbKeybase's constructor #3593

Merged
merged 3 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ BREAKING CHANGES
* Gaia

* SDK
* \#3592 Drop deprecated keybase implementation's
New constructor in favor of a new
crypto/keys.New(string, string) implementation that
returns a lazy keybase instance. Remove client.MockKeyBase,
superseded by crypto/keys.NewInMemory()

* Tendermint

Expand Down Expand Up @@ -48,4 +53,4 @@ BUG FIXES

* SDK

* Tendermint
* Tendermint
14 changes: 0 additions & 14 deletions client/keys.go

This file was deleted.

4 changes: 2 additions & 2 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func runAddCmd(_ *cobra.Command, args []string) error {
if viper.GetBool(flagDryRun) {
// we throw this away, so don't enforce args,
// we want to get a new random seed phrase quickly
kb = client.MockKeyBase()
kb = keys.NewInMemory()
encryptPassword = app.DefaultKeyPass
} else {
kb, err = NewKeyBaseFromHomeFlag()
Expand Down Expand Up @@ -309,7 +309,7 @@ func printCreate(info keys.Info, showMnemonic bool, mnemonic string) error {

// function to just create a new seed to display in the UI before actually persisting it in the keybase
func generateMnemonic(algo keys.SigningAlgo) string {
kb := client.MockKeyBase()
kb := keys.NewInMemory()
pass := app.DefaultKeyPass
name := "inmemorykey"
_, seed, _ := kb.CreateMnemonic(name, keys.English, pass, algo)
Expand Down
Empty file removed client/keys/keys/keys.db/LOCK
Empty file.
8 changes: 5 additions & 3 deletions client/keys/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// KeyDBName is the directory under root where we store the keys
// available output formats.
const (
KeyDBName = "keys"
OutputFormatText = "text"
OutputFormatJSON = "json"

// defaultKeyDBName is the client's subdirectory where keys are stored.
defaultKeyDBName = "keys"
)

type bechKeyOutFn func(keyInfo keys.Info) (KeyOutput, error)
Expand Down Expand Up @@ -87,7 +89,7 @@ func NewKeyBaseFromDir(rootDir string) (keys.Keybase, error) {
func NewInMemoryKeyBase() keys.Keybase { return keys.NewInMemory() }

func getLazyKeyBaseFromDir(rootDir string) (keys.Keybase, error) {
return keys.NewLazyKeybase(KeyDBName, filepath.Join(rootDir, "keys")), nil
return keys.New(defaultKeyDBName, filepath.Join(rootDir, "keys")), nil
}

// create a list of KeyOutput in bech32 format
Expand Down
4 changes: 2 additions & 2 deletions client/lcd/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ func doTransferWithGas(
) (res *http.Response, body string, receiveAddr sdk.AccAddress) {

// create receive address
kb := client.MockKeyBase()
kb := crkeys.NewInMemory()

receiveInfo, _, err := kb.CreateMnemonic(
"receive_address", crkeys.English, gapp.DefaultKeyPass, crkeys.SigningAlgo("secp256k1"),
Expand Down Expand Up @@ -724,7 +724,7 @@ func doTransferWithGasAccAuto(
) (res *http.Response, body string, receiveAddr sdk.AccAddress) {

// create receive address
kb := client.MockKeyBase()
kb := crkeys.NewInMemory()

receiveInfo, _, err := kb.CreateMnemonic(
"receive_address", crkeys.English, gapp.DefaultKeyPass, crkeys.SigningAlgo("secp256k1"),
Expand Down
7 changes: 4 additions & 3 deletions crypto/keys/keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ type dbKeybase struct {
db dbm.DB
}

// New creates a new keybase instance using the passed DB for reading and writing keys.
func New(db dbm.DB) Keybase {
// newDbKeybase creates a new keybase instance using the passed DB for reading and writing keys.
func newDbKeybase(db dbm.DB) Keybase {
return dbKeybase{
db: db,
}
}

// NewInMemory creates a new keybase on top of in-memory storage instance.
// NewInMemory creates a transient keybase on top of in-memory storage
// instance useful for testing purposes and on-the-fly key generation.
func NewInMemory() Keybase { return dbKeybase{dbm.NewMemDB()} }

// CreateMnemonic generates a new key and persists it to storage, encrypted
Expand Down
59 changes: 13 additions & 46 deletions crypto/keys/keybase_test.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,34 @@
package keys
package keys_test

import (
"fmt"
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

. "github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
"github.com/cosmos/cosmos-sdk/crypto/keys/mintkey"
"github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
dbm "github.com/tendermint/tendermint/libs/db"
)

func init() {
mintkey.BcryptSecurityParameter = 1
}

func TestKeybaseOpenClose(t *testing.T) {
dir, err := ioutil.TempDir("", "TestKeybaseOpenClose")
assert.Nil(t, err)

kb := New(dbm.NewDB("TestKeybaseOpenClose", dbm.LevelDBBackend, dir))
kb.CloseDB()

// The DB has been closed. At the moment, the expected behaviour is to panic
assert.Panics(t, func() {
_, _ = kb.CreateAccount(
"some_account",
"key pair crucial catch public canyon evil outer stage ten gym tornado",
"", "", 0, 1)
})
}

func TestLanguage(t *testing.T) {
kb := New(dbm.NewMemDB())
kb := NewInMemory()
_, _, err := kb.CreateMnemonic("something", Japanese, "no_pass", Secp256k1)
assert.Error(t, err)
assert.Equal(t, "unsupported language: only english is supported", err.Error())
}

func TestCreateAccountInvalidMnemonic(t *testing.T) {
kb := New(dbm.NewMemDB())
kb := NewInMemory()
_, err := kb.CreateAccount(
"some_account",
"malarkey pair crucial catch public canyon evil outer stage ten gym tornado",
Expand All @@ -55,14 +38,14 @@ func TestCreateAccountInvalidMnemonic(t *testing.T) {
}

func TestCreateLedgerUnsupportedAlgo(t *testing.T) {
kb := New(dbm.NewMemDB())
kb := NewInMemory()
_, err := kb.CreateLedger("some_account", Ed25519, 0, 1)
assert.Error(t, err)
assert.Equal(t, "unsupported signing algo: only secp256k1 is supported", err.Error())
}

func TestCreateLedger(t *testing.T) {
kb := New(dbm.NewMemDB())
kb := NewInMemory()

// test_cover and test_unit will result in different answers
// test_cover does not compile some dependencies so ledger is disabled
Expand All @@ -86,8 +69,7 @@ func TestCreateLedger(t *testing.T) {
// TestKeyManagement makes sure we can manipulate these keys well
func TestKeyManagement(t *testing.T) {
// make the storage with reasonable defaults
db := dbm.NewMemDB()
cstore := New(db)
cstore := NewInMemory()

algo := Secp256k1
n1, n2, n3 := "personal", "business", "other"
Expand Down Expand Up @@ -165,13 +147,12 @@ func TestKeyManagement(t *testing.T) {
// addr cache gets nuked - and test skip flag
err = cstore.Delete(n2, "", true)
require.NoError(t, err)
require.False(t, db.Has(addrKey(i2.GetAddress())))
}

// TestSignVerify does some detailed checks on how we sign and validate
// signatures
func TestSignVerify(t *testing.T) {
cstore := New(dbm.NewMemDB())
cstore := NewInMemory()
algo := Secp256k1

n1, n2, n3 := "some dude", "a dudette", "dude-ish"
Expand Down Expand Up @@ -253,12 +234,8 @@ func assertPassword(t *testing.T, cstore Keybase, name, pass, badpass string) {

// TestExportImport tests exporting and importing
func TestExportImport(t *testing.T) {

// make the storage with reasonable defaults
db := dbm.NewMemDB()
cstore := New(
db,
)
cstore := NewInMemory()

info, _, err := cstore.CreateMnemonic("john", English, "secretcpw", Secp256k1)
require.NoError(t, err)
Expand Down Expand Up @@ -286,10 +263,7 @@ func TestExportImport(t *testing.T) {
//
func TestExportImportPubKey(t *testing.T) {
// make the storage with reasonable defaults
db := dbm.NewMemDB()
cstore := New(
db,
)
cstore := NewInMemory()

// CreateMnemonic a private-public key pair and ensure consistency
notPasswd := "n9y25ah7"
Expand Down Expand Up @@ -327,11 +301,8 @@ func TestExportImportPubKey(t *testing.T) {

// TestAdvancedKeyManagement verifies update, import, export functionality
func TestAdvancedKeyManagement(t *testing.T) {

// make the storage with reasonable defaults
cstore := New(
dbm.NewMemDB(),
)
cstore := NewInMemory()

algo := Secp256k1
n1, n2 := "old-name", "new name"
Expand Down Expand Up @@ -379,9 +350,7 @@ func TestAdvancedKeyManagement(t *testing.T) {
func TestSeedPhrase(t *testing.T) {

// make the storage with reasonable defaults
cstore := New(
dbm.NewMemDB(),
)
cstore := NewInMemory()

algo := Secp256k1
n1, n2 := "lost-key", "found-again"
Expand Down Expand Up @@ -410,9 +379,7 @@ func TestSeedPhrase(t *testing.T) {

func ExampleNew() {
// Select the encryption and storage for your cryptostore
cstore := New(
dbm.NewMemDB(),
)
cstore := NewInMemory()

sec := Secp256k1

Expand Down
Loading