diff --git a/CHANGELOG.md b/CHANGELOG.md index 77e819e841d..ad4c48a0c46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -116,6 +116,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#10748](https://github.com/cosmos/cosmos-sdk/pull/10748) Move legacy `x/gov` api to `v1beta1` directory. * [\#10816](https://github.com/cosmos/cosmos-sdk/pull/10816) Reuse blocked addresses from the bank module. No need to pass them to distribution. * [\#10852](https://github.com/cosmos/cosmos-sdk/pull/10852) Move `x/gov/types` to `x/gov/types/v1beta2`. +* [\#10922](https://github.com/cosmos/cosmos-sdk/pull/10922), [/#10957](https://github.com/cosmos/cosmos-sdk/pull/10957) Move key `server.Generate*` functions to testutil and support custom mnemonics in in-process testing network. Moved `TestMnemonic` from `testutil` package to `testdata`. ### Client Breaking Changes diff --git a/client/keys/add_test.go b/client/keys/add_test.go index edc74912225..53f10ecb632 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -16,6 +16,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/go-bip39" ) @@ -202,7 +203,7 @@ func Test_runAddCmdDryRun(t *testing.T) { ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) path := sdk.GetConfig().GetFullBIP44Path() - _, err = kb.NewAccount("subkey", testutil.TestMnemonic, "", path, hd.Secp256k1) + _, err = kb.NewAccount("subkey", testdata.TestMnemonic, "", path, hd.Secp256k1) require.NoError(t, err) t.Cleanup(func() { diff --git a/client/keys/delete_test.go b/client/keys/delete_test.go index b87a98da5a8..e38329ee1ad 100644 --- a/client/keys/delete_test.go +++ b/client/keys/delete_test.go @@ -13,6 +13,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -39,7 +40,7 @@ func Test_runDeleteCmd(t *testing.T) { kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc) require.NoError(t, err) - _, err = kb.NewAccount(fakeKeyName1, testutil.TestMnemonic, "", path, hd.Secp256k1) + _, err = kb.NewAccount(fakeKeyName1, testdata.TestMnemonic, "", path, hd.Secp256k1) require.NoError(t, err) _, _, err = kb.NewMnemonic(fakeKeyName2, keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1) diff --git a/client/keys/export_test.go b/client/keys/export_test.go index 2dff05dd5ed..1bc6d695b6b 100644 --- a/client/keys/export_test.go +++ b/client/keys/export_test.go @@ -11,6 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/testutil/testdata" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/crypto/keyring" @@ -93,7 +94,7 @@ func Test_runExportCmd(t *testing.T) { }) path := sdk.GetConfig().GetFullBIP44Path() - _, err = kb.NewAccount("keyname1", testutil.TestMnemonic, "", path, hd.Secp256k1) + _, err = kb.NewAccount("keyname1", testdata.TestMnemonic, "", path, hd.Secp256k1) require.NoError(t, err) clientCtx := client.Context{}. diff --git a/client/keys/list_test.go b/client/keys/list_test.go index 2d728dbfd1b..e71a4fbe218 100644 --- a/client/keys/list_test.go +++ b/client/keys/list_test.go @@ -14,6 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -33,7 +34,7 @@ func Test_runListCmd(t *testing.T) { ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) path := "" //sdk.GetConfig().GetFullBIP44Path() - _, err = kb.NewAccount("something", testutil.TestMnemonic, "", path, hd.Secp256k1) + _, err = kb.NewAccount("something", testdata.TestMnemonic, "", path, hd.Secp256k1) require.NoError(t, err) t.Cleanup(func() { diff --git a/client/keys/rename_test.go b/client/keys/rename_test.go index b32e08c096f..a21d33df282 100644 --- a/client/keys/rename_test.go +++ b/client/keys/rename_test.go @@ -13,6 +13,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -36,7 +37,7 @@ func Test_runRenameCmd(t *testing.T) { require.NoError(t, err) // put fakeKeyName1 in keyring - _, err = kb.NewAccount(fakeKeyName1, testutil.TestMnemonic, "", path, hd.Secp256k1) + _, err = kb.NewAccount(fakeKeyName1, testdata.TestMnemonic, "", path, hd.Secp256k1) require.NoError(t, err) clientCtx := client.Context{}. diff --git a/client/keys/show_test.go b/client/keys/show_test.go index af3284221cb..ad86ecac525 100644 --- a/client/keys/show_test.go +++ b/client/keys/show_test.go @@ -16,6 +16,7 @@ import ( cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -76,11 +77,11 @@ func Test_runShowCmd(t *testing.T) { }) path := hd.NewFundraiserParams(1, sdk.CoinType, 0).String() - _, err = kb.NewAccount(fakeKeyName1, testutil.TestMnemonic, "", path, hd.Secp256k1) + _, err = kb.NewAccount(fakeKeyName1, testdata.TestMnemonic, "", path, hd.Secp256k1) require.NoError(t, err) path2 := hd.NewFundraiserParams(1, sdk.CoinType, 1).String() - _, err = kb.NewAccount(fakeKeyName2, testutil.TestMnemonic, "", path2, hd.Secp256k1) + _, err = kb.NewAccount(fakeKeyName2, testdata.TestMnemonic, "", path2, hd.Secp256k1) require.NoError(t, err) // Now try single key diff --git a/crypto/ledger/ledger_mock.go b/crypto/ledger/ledger_mock.go index 93068474710..868815982e1 100644 --- a/crypto/ledger/ledger_mock.go +++ b/crypto/ledger/ledger_mock.go @@ -1,3 +1,4 @@ +//go:build ledger && test_ledger_mock // +build ledger,test_ledger_mock package ledger @@ -14,7 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/hd" csecp256k1 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" - "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -45,7 +46,7 @@ func (mock LedgerSECP256K1Mock) GetPublicKeySECP256K1(derivationPath []uint32) ( return nil, errors.New("Invalid derivation path") } - seed, err := bip39.NewSeedWithErrorChecking(testutil.TestMnemonic, "") + seed, err := bip39.NewSeedWithErrorChecking(testdata.TestMnemonic, "") if err != nil { return nil, err } @@ -87,7 +88,7 @@ func (mock LedgerSECP256K1Mock) GetAddressPubKeySECP256K1(derivationPath []uint3 func (mock LedgerSECP256K1Mock) SignSECP256K1(derivationPath []uint32, message []byte) ([]byte, error) { path := hd.NewParams(derivationPath[0], derivationPath[1], derivationPath[2], derivationPath[3] != 0, derivationPath[4]) - seed, err := bip39.NewSeedWithErrorChecking(testutil.TestMnemonic, "") + seed, err := bip39.NewSeedWithErrorChecking(testdata.TestMnemonic, "") if err != nil { return nil, err } diff --git a/crypto/ledger/ledger_test.go b/crypto/ledger/ledger_test.go index fbc3b9e702b..5e65de66e2b 100644 --- a/crypto/ledger/ledger_test.go +++ b/crypto/ledger/ledger_test.go @@ -9,7 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec/legacy" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -33,11 +33,11 @@ func checkDefaultPubKey(t *testing.T, priv types.LedgerPrivKey) { expectedPkStr := "PubKeySecp256k1{034FEF9CD7C4C63588D3B03FEB5281B9D232CBA34D6F3D71AEE59211FFBFE1FE87}" require.Equal(t, "eb5ae98721034fef9cd7c4c63588d3b03feb5281b9d232cba34d6f3d71aee59211ffbfe1fe87", fmt.Sprintf("%x", cdc.Amino.MustMarshalBinaryBare(priv.PubKey())), - "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) + "Is your device using test mnemonic: %s ?", testdata.TestMnemonic) require.Equal(t, expectedPkStr, priv.PubKey().String()) addr := sdk.AccAddress(priv.PubKey().Address()).String() require.Equal(t, "cosmos1w34k53py5v5xyluazqpq65agyajavep2rflq6h", - addr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) + addr, "Is your device using test mnemonic: %s ?", testdata.TestMnemonic) } func TestPublicKeyUnsafeHDPath(t *testing.T) { @@ -74,7 +74,7 @@ func TestPublicKeyUnsafeHDPath(t *testing.T) { // in this test we are chekcking if the generated keys are correct. require.Equal(t, expectedAnswers[i], priv.PubKey().String(), - "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) + "Is your device using test mnemonic: %s ?", testdata.TestMnemonic) // Store and restore serializedPk := priv.Bytes() @@ -151,7 +151,7 @@ func TestPublicKeyHDPath(t *testing.T) { require.Equal(t, addr2, addr) require.Equal(t, expectedAddrs[i], addr, - "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) + "Is your device using test mnemonic: %s ?", testdata.TestMnemonic) // Check other methods tmp := priv.(PrivKeyLedgerSecp256k1) @@ -161,7 +161,7 @@ func TestPublicKeyHDPath(t *testing.T) { // in this test we are chekcking if the generated keys are correct and stored in a right path. require.Equal(t, expectedPubKeys[i], priv.PubKey().String(), - "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) + "Is your device using test mnemonic: %s ?", testdata.TestMnemonic) // Store and restore serializedPk := priv.Bytes() @@ -203,7 +203,7 @@ func TestSignaturesHD(t *testing.T) { require.NoError(t, err) valid := pub.VerifySignature(msg, sig) - require.True(t, valid, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) + require.True(t, valid, "Is your device using test mnemonic: %s ?", testdata.TestMnemonic) } } diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index 8f90899f328..20ded76a92e 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -26,6 +26,7 @@ import ( cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/server" srvconfig "github.com/cosmos/cosmos-sdk/server/config" + "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" @@ -263,7 +264,7 @@ func initTestnetFiles( return err } - addr, secret, err := server.GenerateSaveCoinKey(kb, nodeDirName, "", true, algo) + addr, secret, err := testutil.GenerateSaveCoinKey(kb, nodeDirName, "", true, algo) if err != nil { _ = os.RemoveAll(args.outputDir) return err diff --git a/server/init.go b/testutil/key.go similarity index 67% rename from server/init.go rename to testutil/key.go index 747cbba19c3..a82a822567f 100644 --- a/server/init.go +++ b/testutil/key.go @@ -1,4 +1,4 @@ -package server +package testutil import ( "fmt" @@ -8,25 +8,30 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -// GenerateCoinKey returns the address of a public key, along with the secret -// phrase to recover the private key. +// GenerateCoinKey generates a new key mnemonic along with its addrress. func GenerateCoinKey(algo keyring.SignatureAlgo, cdc codec.Codec) (sdk.AccAddress, string, error) { - // generate a private key, with recovery phrase - k, secret, err := keyring.NewInMemory(cdc).NewMnemonic("name", keyring.English, sdk.GetConfig().GetFullBIP44Path(), keyring.DefaultBIP39Passphrase, algo) + // generate a private key, with mnemonic + info, secret, err := keyring.NewInMemory(cdc).NewMnemonic( + "name", + keyring.English, + sdk.GetConfig().GetFullBIP44Path(), + keyring.DefaultBIP39Passphrase, + algo, + ) if err != nil { return sdk.AccAddress{}, "", err } - - addr, err := k.GetAddress() + addr, err := info.GetAddress() if err != nil { - return nil, "", err + return sdk.AccAddress{}, "", err } - return addr, secret, nil } -// GenerateSaveCoinKey returns the address of a public key, along with the secret -// phrase to recover the private key. +// GenerateSaveCoinKey generates a new key mnemonic with its addrress. +// If mnemonic is provided then it's used for key generation. +// The key is saved in the keyring. The function returns error if overwrite=true and the key +// already exists. func GenerateSaveCoinKey( keybase keyring.Keyring, keyName, mnemonic string, @@ -44,9 +49,8 @@ func GenerateSaveCoinKey( return sdk.AccAddress{}, "", fmt.Errorf("key already exists, overwrite is disabled") } - // remove the old key by name if it exists if exists { - if err = keybase.Delete(keyName); err != nil { + if err := keybase.Delete(keyName); err != nil { return sdk.AccAddress{}, "", fmt.Errorf("failed to overwrite key") } } @@ -71,6 +75,5 @@ func GenerateSaveCoinKey( if err != nil { return nil, "", err } - return addr, secret, nil } diff --git a/server/init_test.go b/testutil/key_test.go similarity index 79% rename from server/init_test.go rename to testutil/key_test.go index c3796e7434f..5cf9eeef631 100644 --- a/server/init_test.go +++ b/testutil/key_test.go @@ -1,4 +1,4 @@ -package server_test +package testutil import ( "testing" @@ -7,7 +7,6 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" - "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/types" ) @@ -15,7 +14,7 @@ import ( func TestGenerateCoinKey(t *testing.T) { t.Parallel() cdc := simapp.MakeTestEncodingConfig().Codec - addr, mnemonic, err := server.GenerateCoinKey(hd.Secp256k1, cdc) + addr, mnemonic, err := GenerateCoinKey(hd.Secp256k1, cdc) require.NoError(t, err) // Test creation @@ -33,7 +32,7 @@ func TestGenerateSaveCoinKey(t *testing.T) { kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil, encCfg.Codec) require.NoError(t, err) - addr, mnemonic, err := server.GenerateSaveCoinKey(kb, "keyname", "", false, hd.Secp256k1) + addr, mnemonic, err := GenerateSaveCoinKey(kb, "keyname", "", false, hd.Secp256k1) require.NoError(t, err) // Test key was actually saved @@ -59,15 +58,15 @@ func TestGenerateSaveCoinKeyOverwriteFlag(t *testing.T) { require.NoError(t, err) keyname := "justakey" - addr1, _, err := server.GenerateSaveCoinKey(kb, keyname, "", false, hd.Secp256k1) + addr1, _, err := GenerateSaveCoinKey(kb, keyname, "", false, hd.Secp256k1) require.NoError(t, err) // Test overwrite with overwrite=false - _, _, err = server.GenerateSaveCoinKey(kb, keyname, "", false, hd.Secp256k1) + _, _, err = GenerateSaveCoinKey(kb, keyname, "", false, hd.Secp256k1) require.Error(t, err) // Test overwrite with overwrite=true - addr2, _, err := server.GenerateSaveCoinKey(kb, keyname, "", true, hd.Secp256k1) + addr2, _, err := GenerateSaveCoinKey(kb, keyname, "", true, hd.Secp256k1) require.NoError(t, err) require.NotEqual(t, addr1, addr2) diff --git a/testutil/network/network.go b/testutil/network/network.go index 86d25290004..4de1c728814 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -39,6 +39,7 @@ import ( "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/simapp/params" storetypes "github.com/cosmos/cosmos-sdk/store/types" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -357,7 +358,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) { mnemonic = cfg.Mnemonics[i] } - addr, secret, err := server.GenerateSaveCoinKey(kb, nodeDirName, mnemonic, true, algo) + addr, secret, err := testutil.GenerateSaveCoinKey(kb, nodeDirName, mnemonic, true, algo) if err != nil { return nil, err } diff --git a/testutil/known_values.go b/testutil/testdata/known_values.go similarity index 90% rename from testutil/known_values.go rename to testutil/testdata/known_values.go index a87ccd2af0c..616a07e665d 100644 --- a/testutil/known_values.go +++ b/testutil/testdata/known_values.go @@ -1,4 +1,4 @@ -package testutil +package testdata const ( // Tests expect a ledger device initialized to the following mnemonic diff --git a/types/bech32/legacybech32/pk_test.go b/types/bech32/legacybech32/pk_test.go index 262784c0428..26183475bd7 100644 --- a/types/bech32/legacybech32/pk_test.go +++ b/types/bech32/legacybech32/pk_test.go @@ -7,7 +7,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/ledger" - "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -21,6 +21,5 @@ func TestBeach32ifPbKey(t *testing.T) { pubKeyAddr, err := MarshalPubKey(AccPK, priv.PubKey()) require.NoError(err) require.Equal("cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", - pubKeyAddr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) - + pubKeyAddr, "Is your device using test mnemonic: %s ?", testdata.TestMnemonic) }