Skip to content

Commit

Permalink
hide dbKeybase's constructor
Browse files Browse the repository at this point in the history
crypto/keys.New(string, string) now returns a lazy keybase
instance. Function's signature has changed, this this is
an SDK Go API breaking change.

Remove client.MockKeyBase, superseded by
crypto/keys.NewInMemory()

Closes: #3592
  • Loading branch information
Alessio Treglia committed Feb 11, 2019
1 parent ed2b427 commit 79a4aba
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 88 deletions.
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.
5 changes: 2 additions & 3 deletions client/keys/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ 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"
)
Expand Down Expand Up @@ -87,7 +86,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(KeyDBName, 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

0 comments on commit 79a4aba

Please sign in to comment.