Skip to content

Commit

Permalink
chore: move server.GenerateCoinKey and server.GenerateSaveCoinKey to …
Browse files Browse the repository at this point in the history
…testutil (cosmos#10956)

* chore: move server.GenerateCoinKey and server.GenerateSaveCoinKey to testutil

* add changelog

* move testutil/known_values.go to testutil/testdata

* changelog
  • Loading branch information
robert-zaremba authored and JeancarloBarrios committed Sep 28, 2024
1 parent 5a986d9 commit bd8cca4
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 107 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### API Breaking Changes

* [\#10561](https://github.com/cosmos/cosmos-sdk/pull/10561) The `CommitMultiStore` interface contains a new `SetIAVLCacheSize` method
* [\#10922](https://github.com/cosmos/cosmos-sdk/pull/10922), [/#10956](https://github.com/cosmos/cosmos-sdk/pull/10956) Deprecate key `server.Generate*` functions and move them to `testutil` and support custom mnemonics in in-process testing network. Moved `TestMnemonic` from `testutil` package to `testdata`.

### Features

Expand Down
2 changes: 1 addition & 1 deletion client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func Test_runAddCmdDryRun(t *testing.T) {
WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons"))
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)

path := sdk.GetFullBIP44Path()
path := sdk.GetConfig().GetFullBIP44Path()
_, err = kb.NewAccount("subkey", testdata.TestMnemonic, "", path, hd.Secp256k1)
require.NoError(t, err)

Expand Down
6 changes: 5 additions & 1 deletion client/keys/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/stretchr/testify/require"

"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"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/crypto/hd"
Expand Down Expand Up @@ -93,7 +97,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{}.
Expand Down
5 changes: 3 additions & 2 deletions client/keys/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
Expand Down Expand Up @@ -50,7 +49,9 @@ func Test_runListCmd(t *testing.T) {
WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")).
WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons"))

ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
path := "" //sdk.GetConfig().GetFullBIP44Path()
_, err = kb.NewAccount("something", testdata.TestMnemonic, "", path, hd.Secp256k1)
require.NoError(t, err)

path := "" // sdk.GetConfig().GetFullBIP44Path()
_, err = kb.NewAccount("something", testdata.TestMnemonic, "", path, hd.Secp256k1)
Expand Down
5 changes: 5 additions & 0 deletions crypto/ledger/ledger_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/hd"
csecp256k1 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// If ledger support (build tag) has been enabled, which implies a CGO dependency,
Expand All @@ -42,6 +43,10 @@ func (mock LedgerSECP256K1Mock) GetPublicKeySECP256K1(derivationPath []uint32) (
return nil, errors.New("invalid derivation path")
}

if derivationPath[1] != sdk.GetConfig().GetCoinType() {
return nil, errors.New("Invalid derivation path")
}

seed, err := bip39.NewSeedWithErrorChecking(testdata.TestMnemonic, "")
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"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"
"github.com/cosmos/cosmos-sdk/version"
Expand Down Expand Up @@ -311,7 +310,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
Expand Down
50 changes: 26 additions & 24 deletions testutil/key.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
package testutil

import (
"errors"
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// GenerateCoinKey generates a new key mnemonic along with its address.
func GenerateCoinKey(algo keyring.SignatureAlgo, cdc codec.Codec) (sdk.AccAddress, string, error) {
// GenerateCoinKey generates a new key mnemonic along with its addrress.
func GenerateCoinKey(algo keyring.SignatureAlgo) (sdk.AccAddress, string, error) {
// generate a private key, with mnemonic
info, secret, err := keyring.NewInMemory(cdc).NewMnemonic(
info, secret, err := keyring.NewInMemory().NewMnemonic(
"name",
keyring.English,
sdk.GetFullBIP44Path(),
sdk.GetConfig().GetFullBIP44Path(),
keyring.DefaultBIP39Passphrase,
algo,
)
if err != nil {
return sdk.AccAddress{}, "", err
}
addr, err := info.GetAddress()
if err != nil {
return sdk.AccAddress{}, "", err
}
return addr, secret, nil

return sdk.AccAddress(info.GetPubKey().Address()), secret, nil
}

// GenerateSaveCoinKey generates a new key mnemonic with its address.
// 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.
Expand All @@ -37,7 +33,6 @@ func GenerateSaveCoinKey(
keyName, mnemonic string,
overwrite bool,
algo keyring.SignatureAlgo,
hdPath string,
) (sdk.AccAddress, string, error) {
exists := false
_, err := keybase.Key(keyName)
Expand All @@ -47,34 +42,41 @@ func GenerateSaveCoinKey(

// ensure no overwrite
if !overwrite && exists {
return sdk.AccAddress{}, "", errors.New("key already exists, overwrite is disabled")
return sdk.AccAddress{}, "", fmt.Errorf("key already exists, overwrite is disabled")
}

if exists {
if err := keybase.Delete(keyName); err != nil {
return sdk.AccAddress{}, "", errors.New("failed to overwrite key")
return sdk.AccAddress{}, "", fmt.Errorf("failed to overwrite key")
}
}

var (
record *keyring.Record
info keyring.Info
secret string
)

// generate or recover a new account
if mnemonic != "" {
secret = mnemonic
record, err = keybase.NewAccount(keyName, mnemonic, keyring.DefaultBIP39Passphrase, hdPath, algo)
info, err = keybase.NewAccount(
keyName,
mnemonic,
keyring.DefaultBIP39Passphrase,
sdk.GetConfig().GetFullBIP44Path(),
algo,
)
} else {
record, secret, err = keybase.NewMnemonic(keyName, keyring.English, hdPath, keyring.DefaultBIP39Passphrase, algo)
info, secret, err = keybase.NewMnemonic(
keyName,
keyring.English,
sdk.GetConfig().GetFullBIP44Path(),
keyring.DefaultBIP39Passphrase,
algo,
)
}
if err != nil {
return sdk.AccAddress{}, "", err
}

addr, err := record.GetAddress()
if err != nil {
return nil, "", err
}
return addr, secret, nil
return sdk.AccAddress(info.GetAddress()), secret, nil
}
74 changes: 0 additions & 74 deletions testutil/key_test.go

This file was deleted.

7 changes: 4 additions & 3 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ import (
"github.com/cosmos/cosmos-sdk/runtime"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"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"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -452,7 +453,7 @@ func New(l Logger, baseDir string, cfg Config) (NetworkI, 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)
require.NoError(t, err)

info := map[string]string{"secret": secret}
Expand Down

0 comments on commit bd8cca4

Please sign in to comment.