diff --git a/CHANGELOG.md b/CHANGELOG.md index dbcc784dad..87949647a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (crypto | x/auth) [#14372](https://github.com/cosmos/cosmos-sdk/pull/18194) Key checks on signatures antehandle. * (codec/unknownproto)[#18541](https://github.com/cosmos/cosmos-sdk/pull/18541) Remove the use of "protoc-gen-gogo/descriptor" in favour of using the official protobuf descriptorpb types inside unknownproto. * (staking) [#18506](https://github.com/cosmos/cosmos-sdk/pull/18506) Detect the length of the ed25519 pubkey in CreateValidator to prevent panic. +* (types) [#18372](https://github.com/cosmos/cosmos-sdk/pull/18372) Removed global configuration for coin type and purpose. Setters and getters should be removed and access directly to defined types. ### Bug Fixes diff --git a/client/keys/add.go b/client/keys/add.go index 4ff3b5af28..d738d93aaa 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -79,7 +79,7 @@ Example: f.Bool(flagNoBackup, false, "Don't print out seed phrase (if others are watching the terminal)") f.Bool(flags.FlagDryRun, false, "Perform action, but don't add key to local keystore") f.String(flagHDPath, "", "Manual HD Path derivation (overrides BIP44 config)") - f.Uint32(flagCoinType, sdk.GetConfig().GetCoinType(), "coin type number for HD derivation") + f.Uint32(flagCoinType, sdk.CoinType, "coin type number for HD derivation") f.Uint32(flagAccount, 0, "Account number for HD derivation (less than equal 2147483647)") f.Uint32(flagIndex, 0, "Address index number for HD derivation (less than equal 2147483647)") f.String(flags.FlagKeyType, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for") diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index a003b20d6d..2e1865823c 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -32,15 +32,10 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { bech32PrefixConsAddr := "terravalcons" bech32PrefixConsPub := "terravalconspub" - config.SetPurpose(44) - config.SetCoinType(330) config.SetBech32PrefixForAccount(bech32PrefixAccAddr, bech32PrefixAccPub) config.SetBech32PrefixForValidator(bech32PrefixValAddr, bech32PrefixValPub) config.SetBech32PrefixForConsensusNode(bech32PrefixConsAddr, bech32PrefixConsPub) - cmd := AddKeyCommand() - cmd.Flags().AddFlagSet(Commands().PersistentFlags()) - // Prepare a keybase kbHome := t.TempDir() @@ -54,6 +49,8 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) + cmd := AddKeyCommand() + cmd.Flags().AddFlagSet(Commands().PersistentFlags()) cmd.SetArgs([]string{ "keyname1", fmt.Sprintf("--%s=true", flags.FlagUseLedger), @@ -89,8 +86,6 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { "PubKeySecp256k1{03028F0D5A9FD41600191CDEFDEA05E77A68DFBCE286241C0190805B9346667D07}", pub.String()) - config.SetPurpose(44) - config.SetCoinType(118) config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub) diff --git a/client/keys/add_test.go b/client/keys/add_test.go index a10da1c5a4..84b81833bb 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -230,7 +230,7 @@ func Test_runAddCmdDryRun(t *testing.T) { WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons")) ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) - path := sdk.GetConfig().GetFullBIP44Path() + path := sdk.GetFullBIP44Path() _, err = kb.NewAccount("subkey", testdata.TestMnemonic, "", path, hd.Secp256k1) require.NoError(t, err) diff --git a/client/keys/delete_test.go b/client/keys/delete_test.go index fc0d32ef90..dd1a38bd87 100644 --- a/client/keys/delete_test.go +++ b/client/keys/delete_test.go @@ -33,7 +33,7 @@ func Test_runDeleteCmd(t *testing.T) { fakeKeyName1 := "runDeleteCmd_Key1" fakeKeyName2 := "runDeleteCmd_Key2" - path := sdk.GetConfig().GetFullBIP44Path() + path := sdk.GetFullBIP44Path() cdc := moduletestutil.MakeTestEncodingConfig().Codec cmd.SetArgs([]string{"blah", fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome)}) diff --git a/client/keys/export_test.go b/client/keys/export_test.go index d7e4126f6a..96d1048e90 100644 --- a/client/keys/export_test.go +++ b/client/keys/export_test.go @@ -90,7 +90,7 @@ func Test_runExportCmd(t *testing.T) { require.NoError(t, err) t.Cleanup(cleanupKeys(t, kb, "keyname1")) - path := sdk.GetConfig().GetFullBIP44Path() + path := sdk.GetFullBIP44Path() _, err = kb.NewAccount("keyname1", testdata.TestMnemonic, "", path, hd.Secp256k1) require.NoError(t, err) diff --git a/client/keys/rename_test.go b/client/keys/rename_test.go index 8df5d641ba..dad91168f0 100644 --- a/client/keys/rename_test.go +++ b/client/keys/rename_test.go @@ -30,7 +30,7 @@ func Test_runRenameCmd(t *testing.T) { fakeKeyName1 := "runRenameCmd_Key1" fakeKeyName2 := "runRenameCmd_Key2" - path := sdk.GetConfig().GetFullBIP44Path() + path := sdk.GetFullBIP44Path() cdc := moduletestutil.MakeTestEncodingConfig().Codec kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc) diff --git a/crypto/ledger/ledger_mock.go b/crypto/ledger/ledger_mock.go index 93b9903e13..94e5063bc5 100644 --- a/crypto/ledger/ledger_mock.go +++ b/crypto/ledger/ledger_mock.go @@ -42,10 +42,6 @@ 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 diff --git a/crypto/ledger/ledger_test.go b/crypto/ledger/ledger_test.go index 368c86ac41..1c742791a3 100644 --- a/crypto/ledger/ledger_test.go +++ b/crypto/ledger/ledger_test.go @@ -16,14 +16,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -func TestErrorHandling(t *testing.T) { - // first, try to generate a key, must return an error - // (no panic) - path := *hd.NewParams(44, 555, 0, false, 0) - _, err := NewPrivKeySecp256k1Unsafe(path) - require.Error(t, err) -} - func TestPublicKeyUnsafe(t *testing.T) { path := *hd.NewFundraiserParams(0, sdk.CoinType, 0) priv, err := NewPrivKeySecp256k1Unsafe(path) diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index 46a9aadfcb..4ae56e28c4 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -273,7 +273,7 @@ func initTestnetFiles( return err } - addr, secret, err := testutil.GenerateSaveCoinKey(kb, nodeDirName, "", true, algo) + addr, secret, err := testutil.GenerateSaveCoinKey(kb, nodeDirName, "", true, algo, sdk.GetFullBIP44Path()) if err != nil { _ = os.RemoveAll(args.outputDir) return err diff --git a/tests/integration/runtime/query_test.go b/tests/integration/runtime/query_test.go index de6c8e91e8..f018c6ac19 100644 --- a/tests/integration/runtime/query_test.go +++ b/tests/integration/runtime/query_test.go @@ -83,9 +83,7 @@ func TestQueryAppConfig(t *testing.T) { // has all expected modules for _, modName := range []string{"auth", "bank", "tx", "consensus", "runtime", "staking"} { modConfig := moduleConfigs[modName] - if modConfig == nil { - t.Fatalf("missing %s", modName) - } + assert.Assert(t, modConfig != nil) assert.Assert(t, modConfig.Config != nil) } } diff --git a/testutil/key.go b/testutil/key.go index a82a822567..5ae8926d10 100644 --- a/testutil/key.go +++ b/testutil/key.go @@ -14,7 +14,7 @@ func GenerateCoinKey(algo keyring.SignatureAlgo, cdc codec.Codec) (sdk.AccAddres info, secret, err := keyring.NewInMemory(cdc).NewMnemonic( "name", keyring.English, - sdk.GetConfig().GetFullBIP44Path(), + sdk.GetFullBIP44Path(), keyring.DefaultBIP39Passphrase, algo, ) @@ -37,6 +37,7 @@ func GenerateSaveCoinKey( keyName, mnemonic string, overwrite bool, algo keyring.SignatureAlgo, + hdPath string, ) (sdk.AccAddress, string, error) { exists := false _, err := keybase.Key(keyName) @@ -63,9 +64,9 @@ func GenerateSaveCoinKey( // generate or recover a new account if mnemonic != "" { secret = mnemonic - record, err = keybase.NewAccount(keyName, mnemonic, keyring.DefaultBIP39Passphrase, sdk.GetConfig().GetFullBIP44Path(), algo) + record, err = keybase.NewAccount(keyName, mnemonic, keyring.DefaultBIP39Passphrase, hdPath, algo) } else { - record, secret, err = keybase.NewMnemonic(keyName, keyring.English, sdk.GetConfig().GetFullBIP44Path(), keyring.DefaultBIP39Passphrase, algo) + record, secret, err = keybase.NewMnemonic(keyName, keyring.English, hdPath, keyring.DefaultBIP39Passphrase, algo) } if err != nil { return sdk.AccAddress{}, "", err diff --git a/testutil/key_test.go b/testutil/key_test.go index 799e2a0629..26847d7647 100644 --- a/testutil/key_test.go +++ b/testutil/key_test.go @@ -18,7 +18,7 @@ func TestGenerateCoinKey(t *testing.T) { require.NoError(t, err) // Test creation - k, err := keyring.NewInMemory(cdc).NewAccount("xxx", mnemonic, "", hd.NewFundraiserParams(0, types.GetConfig().GetCoinType(), 0).String(), hd.Secp256k1) + k, err := keyring.NewInMemory(cdc).NewAccount("xxx", mnemonic, "", hd.NewFundraiserParams(0, types.CoinType, 0).String(), hd.Secp256k1) require.NoError(t, err) addr1, err := k.GetAddress() require.NoError(t, err) @@ -32,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 := GenerateSaveCoinKey(kb, "keyname", "", false, hd.Secp256k1) + addr, mnemonic, err := GenerateSaveCoinKey(kb, "keyname", "", false, hd.Secp256k1, types.GetFullBIP44Path()) require.NoError(t, err) // Test key was actually saved @@ -43,7 +43,7 @@ func TestGenerateSaveCoinKey(t *testing.T) { require.Equal(t, addr, addr1) // Test in-memory recovery - k, err = keyring.NewInMemory(encCfg.Codec).NewAccount("xxx", mnemonic, "", hd.NewFundraiserParams(0, types.GetConfig().GetCoinType(), 0).String(), hd.Secp256k1) + k, err = keyring.NewInMemory(encCfg.Codec).NewAccount("xxx", mnemonic, "", hd.NewFundraiserParams(0, types.CoinType, 0).String(), hd.Secp256k1) require.NoError(t, err) addr1, err = k.GetAddress() require.NoError(t, err) @@ -58,15 +58,15 @@ func TestGenerateSaveCoinKeyOverwriteFlag(t *testing.T) { require.NoError(t, err) keyname := "justakey" - addr1, _, err := GenerateSaveCoinKey(kb, keyname, "", false, hd.Secp256k1) + addr1, _, err := GenerateSaveCoinKey(kb, keyname, "", false, hd.Secp256k1, types.GetFullBIP44Path()) require.NoError(t, err) // Test overwrite with overwrite=false - _, _, err = GenerateSaveCoinKey(kb, keyname, "", false, hd.Secp256k1) + _, _, err = GenerateSaveCoinKey(kb, keyname, "", false, hd.Secp256k1, types.GetFullBIP44Path()) require.Error(t, err) // Test overwrite with overwrite=true - addr2, _, err := GenerateSaveCoinKey(kb, keyname, "", true, hd.Secp256k1) + addr2, _, err := GenerateSaveCoinKey(kb, keyname, "", true, hd.Secp256k1, types.GetFullBIP44Path()) require.NoError(t, err) require.NotEqual(t, addr1, addr2) diff --git a/testutil/network/network.go b/testutil/network/network.go index 86d0ff0f69..86e964ee44 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -455,7 +455,7 @@ func New(l Logger, baseDir string, cfg Config) (NetworkI, error) { return nil, err } - addr, secret, err := testutil.GenerateSaveCoinKey(kb, nodeDirName, mnemonic, true, algo) + addr, secret, err := testutil.GenerateSaveCoinKey(kb, nodeDirName, mnemonic, true, algo, sdk.GetFullBIP44Path()) if err != nil { return nil, err } diff --git a/types/address.go b/types/address.go index cd913d493e..e3ac48e31a 100644 --- a/types/address.go +++ b/types/address.go @@ -32,7 +32,6 @@ const ( // config.SetBech32PrefixForValidator(yourBech32PrefixValAddr, yourBech32PrefixValPub) // config.SetBech32PrefixForConsensusNode(yourBech32PrefixConsAddr, yourBech32PrefixConsPub) // config.SetPurpose(yourPurpose) - // config.SetCoinType(yourCoinType) // config.Seal() // Bech32MainPrefix defines the main SDK Bech32 prefix of an account's address @@ -709,3 +708,8 @@ func cacheBech32Addr(prefix string, addr []byte, cache *simplelru.LRU, cacheKey } return bech32Addr } + +// GetFullBIP44Path returns the BIP44Prefix. +func GetFullBIP44Path() string { + return fmt.Sprintf("m/%d'/%d'/0'/0/0", Purpose, CoinType) +} diff --git a/types/config.go b/types/config.go index 826e6652bc..8ff1ad5eb8 100644 --- a/types/config.go +++ b/types/config.go @@ -2,7 +2,6 @@ package types import ( "context" - "fmt" "sync" "github.com/cosmos/cosmos-sdk/version" @@ -128,18 +127,6 @@ func (config *Config) SetFullFundraiserPath(fullFundraiserPath string) { config.fullFundraiserPath = fullFundraiserPath } -// Set the BIP-0044 Purpose code on the config -func (config *Config) SetPurpose(purpose uint32) { - config.assertNotSealed() - config.purpose = purpose -} - -// Set the BIP-0044 CoinType code on the config -func (config *Config) SetCoinType(coinType uint32) { - config.assertNotSealed() - config.coinType = coinType -} - // Seal seals the config such that the config state could not be modified further func (config *Config) Seal() *Config { config.mtx.Lock() @@ -197,16 +184,6 @@ func (config *Config) GetAddressVerifier() func([]byte) error { return config.addressVerifier } -// GetPurpose returns the BIP-0044 Purpose code on the config. -func (config *Config) GetPurpose() uint32 { - return config.purpose -} - -// GetCoinType returns the BIP-0044 CoinType code on the config. -func (config *Config) GetCoinType() uint32 { - return config.coinType -} - // GetFullFundraiserPath returns the BIP44Prefix. // // Deprecated: This method is supported for backward compatibility only and will be removed in a future release. Use GetFullBIP44Path instead. @@ -214,11 +191,6 @@ func (config *Config) GetFullFundraiserPath() string { return config.fullFundraiserPath } -// GetFullBIP44Path returns the BIP44Prefix. -func (config *Config) GetFullBIP44Path() string { - return fmt.Sprintf("m/%d'/%d'/0'/0/0", config.purpose, config.coinType) -} - func KeyringServiceName() string { if len(version.Name) == 0 { return DefaultKeyringServiceName diff --git a/types/config_test.go b/types/config_test.go index df281b1f85..da9383ef3a 100644 --- a/types/config_test.go +++ b/types/config_test.go @@ -17,29 +17,6 @@ func TestConfigTestSuite(t *testing.T) { suite.Run(t, new(configTestSuite)) } -func (s *contextTestSuite) TestConfig_SetPurpose() { - config := sdk.NewConfig() - config.SetPurpose(44) - s.Require().Equal(uint32(44), config.GetPurpose()) - - config.SetPurpose(0) - s.Require().Equal(uint32(0), config.GetPurpose()) - - config.Seal() - s.Require().Panics(func() { config.SetPurpose(10) }) -} - -func (s *configTestSuite) TestConfig_SetCoinType() { - config := sdk.NewConfig() - config.SetCoinType(1) - s.Require().Equal(uint32(1), config.GetCoinType()) - config.SetCoinType(99) - s.Require().Equal(uint32(99), config.GetCoinType()) - - config.Seal() - s.Require().Panics(func() { config.SetCoinType(99) }) -} - func (s *configTestSuite) TestConfig_SetTxEncoder() { mockErr := errors.New("test") config := sdk.NewConfig()