Skip to content

Commit

Permalink
Merge PR cosmos#3593: hide dbKeybase's constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio authored and jackzampolin committed Feb 12, 2019
1 parent 9c23fe6 commit fe548c0
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 87 deletions.
5 changes: 5 additions & 0 deletions 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
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 Down Expand Up @@ -101,8 +84,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 @@ -180,13 +162,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 @@ -268,12 +249,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 @@ -301,10 +278,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 @@ -342,11 +316,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 @@ -394,9 +365,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 @@ -425,9 +394,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

0 comments on commit fe548c0

Please sign in to comment.