From 54455338794127e7bff18bb4a00868094048e2cf Mon Sep 17 00:00:00 2001 From: bizk Date: Mon, 6 Nov 2023 08:49:29 -0300 Subject: [PATCH 01/16] moved cointype out of config --- client/keys/add.go | 2 +- crypto/ledger/ledger_mock.go | 2 +- testutil/key_test.go | 4 ++-- types/config.go | 5 ----- types/config_test.go | 4 ++-- 5 files changed, 6 insertions(+), 11 deletions(-) diff --git a/client/keys/add.go b/client/keys/add.go index 4ff3b5af2819..d738d93aaa11 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/crypto/ledger/ledger_mock.go b/crypto/ledger/ledger_mock.go index 93b9903e138a..d75694ea7385 100644 --- a/crypto/ledger/ledger_mock.go +++ b/crypto/ledger/ledger_mock.go @@ -42,7 +42,7 @@ func (mock LedgerSECP256K1Mock) GetPublicKeySECP256K1(derivationPath []uint32) ( return nil, errors.New("invalid derivation path") } - if derivationPath[1] != sdk.GetConfig().GetCoinType() { + if derivationPath[1] != sdk.CoinType { return nil, errors.New("invalid derivation path") } diff --git a/testutil/key_test.go b/testutil/key_test.go index 799e2a0629f9..10d427652c9a 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) @@ -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) diff --git a/types/config.go b/types/config.go index 826e6652bc44..fd74784b5a23 100644 --- a/types/config.go +++ b/types/config.go @@ -202,11 +202,6 @@ 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. diff --git a/types/config_test.go b/types/config_test.go index df281b1f852a..3cf9404464f6 100644 --- a/types/config_test.go +++ b/types/config_test.go @@ -32,9 +32,9 @@ func (s *contextTestSuite) TestConfig_SetPurpose() { func (s *configTestSuite) TestConfig_SetCoinType() { config := sdk.NewConfig() config.SetCoinType(1) - s.Require().Equal(uint32(1), config.GetCoinType()) + s.Require().Equal(uint32(1), sdk.CoinType) config.SetCoinType(99) - s.Require().Equal(uint32(99), config.GetCoinType()) + s.Require().Equal(uint32(99), sdk.CoinType) config.Seal() s.Require().Panics(func() { config.SetCoinType(99) }) From bbb175c33dc962797a3569783a1c6724e4fd98d8 Mon Sep 17 00:00:00 2001 From: bizk Date: Wed, 8 Nov 2023 13:13:05 -0300 Subject: [PATCH 02/16] small rollback --- client/keys/add.go | 2 +- crypto/ledger/ledger_mock.go | 2 +- testutil/key_test.go | 4 ++-- types/config.go | 5 +++++ types/config_test.go | 4 ++-- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/client/keys/add.go b/client/keys/add.go index d738d93aaa11..4ff3b5af2819 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.CoinType, "coin type number for HD derivation") + f.Uint32(flagCoinType, sdk.GetConfig().GetCoinType(), "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/crypto/ledger/ledger_mock.go b/crypto/ledger/ledger_mock.go index d75694ea7385..93b9903e138a 100644 --- a/crypto/ledger/ledger_mock.go +++ b/crypto/ledger/ledger_mock.go @@ -42,7 +42,7 @@ func (mock LedgerSECP256K1Mock) GetPublicKeySECP256K1(derivationPath []uint32) ( return nil, errors.New("invalid derivation path") } - if derivationPath[1] != sdk.CoinType { + if derivationPath[1] != sdk.GetConfig().GetCoinType() { return nil, errors.New("invalid derivation path") } diff --git a/testutil/key_test.go b/testutil/key_test.go index 10d427652c9a..799e2a0629f9 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.CoinType, 0).String(), hd.Secp256k1) + k, err := keyring.NewInMemory(cdc).NewAccount("xxx", mnemonic, "", hd.NewFundraiserParams(0, types.GetConfig().GetCoinType(), 0).String(), hd.Secp256k1) require.NoError(t, err) addr1, err := k.GetAddress() require.NoError(t, err) @@ -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.CoinType, 0).String(), hd.Secp256k1) + k, err = keyring.NewInMemory(encCfg.Codec).NewAccount("xxx", mnemonic, "", hd.NewFundraiserParams(0, types.GetConfig().GetCoinType(), 0).String(), hd.Secp256k1) require.NoError(t, err) addr1, err = k.GetAddress() require.NoError(t, err) diff --git a/types/config.go b/types/config.go index fd74784b5a23..826e6652bc44 100644 --- a/types/config.go +++ b/types/config.go @@ -202,6 +202,11 @@ 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. diff --git a/types/config_test.go b/types/config_test.go index 3cf9404464f6..df281b1f852a 100644 --- a/types/config_test.go +++ b/types/config_test.go @@ -32,9 +32,9 @@ func (s *contextTestSuite) TestConfig_SetPurpose() { func (s *configTestSuite) TestConfig_SetCoinType() { config := sdk.NewConfig() config.SetCoinType(1) - s.Require().Equal(uint32(1), sdk.CoinType) + s.Require().Equal(uint32(1), config.GetCoinType()) config.SetCoinType(99) - s.Require().Equal(uint32(99), sdk.CoinType) + s.Require().Equal(uint32(99), config.GetCoinType()) config.Seal() s.Require().Panics(func() { config.SetCoinType(99) }) From 42213bc3bff250789178f7c834ca61be8a2fd286 Mon Sep 17 00:00:00 2001 From: bizk Date: Wed, 15 Nov 2023 19:25:43 -0300 Subject: [PATCH 03/16] moved address config to address file --- client/keys/add.go | 2 +- client/keys/add_ledger_test.go | 4 ++-- crypto/ledger/ledger_mock.go | 2 +- testutil/key_test.go | 4 ++-- types/address.go | 35 ++++++++++++++++++++++++++++++++++ types/config.go | 11 ----------- types/config_test.go | 5 +---- 7 files changed, 42 insertions(+), 21 deletions(-) diff --git a/client/keys/add.go b/client/keys/add.go index 4ff3b5af2819..137fa651e138 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.GetAddresConfig().GetCoinType(), "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 a003b20d6ddc..d3c777aa7bb1 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -33,7 +33,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { bech32PrefixConsPub := "terravalconspub" config.SetPurpose(44) - config.SetCoinType(330) + sdk.GetAddresConfig().SetCoinType(330) config.SetBech32PrefixForAccount(bech32PrefixAccAddr, bech32PrefixAccPub) config.SetBech32PrefixForValidator(bech32PrefixValAddr, bech32PrefixValPub) config.SetBech32PrefixForConsensusNode(bech32PrefixConsAddr, bech32PrefixConsPub) @@ -90,7 +90,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { pub.String()) config.SetPurpose(44) - config.SetCoinType(118) + sdk.GetAddresConfig().SetCoinType(118) config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub) diff --git a/crypto/ledger/ledger_mock.go b/crypto/ledger/ledger_mock.go index 93b9903e138a..aff2cbe3d7dc 100644 --- a/crypto/ledger/ledger_mock.go +++ b/crypto/ledger/ledger_mock.go @@ -42,7 +42,7 @@ func (mock LedgerSECP256K1Mock) GetPublicKeySECP256K1(derivationPath []uint32) ( return nil, errors.New("invalid derivation path") } - if derivationPath[1] != sdk.GetConfig().GetCoinType() { + if derivationPath[1] != sdk.GetAddresConfig().GetCoinType() { return nil, errors.New("invalid derivation path") } diff --git a/testutil/key_test.go b/testutil/key_test.go index 799e2a0629f9..3cde9cdb3a40 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.GetAddresConfig().GetCoinType(), 0).String(), hd.Secp256k1) require.NoError(t, err) addr1, err := k.GetAddress() require.NoError(t, err) @@ -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.GetAddresConfig().GetCoinType(), 0).String(), hd.Secp256k1) require.NoError(t, err) addr1, err = k.GetAddress() require.NoError(t, err) diff --git a/types/address.go b/types/address.go index cd913d493e63..a0f99264a976 100644 --- a/types/address.go +++ b/types/address.go @@ -709,3 +709,38 @@ func cacheBech32Addr(prefix string, addr []byte, cache *simplelru.LRU, cacheKey } return bech32Addr } + +type AddressConfig struct { + coinType uint32 +} + +var ( + sdkAddressConfig *AddressConfig + initAddressConfig sync.Once +) + +// GetAddressConfig returns theaddres instance for the SDK. +func GetAddresConfig() *AddressConfig { + fmt.Println() + initAddressConfig.Do(func() { + sdkAddressConfig = NewAddressConfig() + }) + return sdkAddressConfig +} + +// New returns a new Config with default values. +func NewAddressConfig() *AddressConfig { + return &AddressConfig{ + coinType: CoinType, + } +} + +// Set the BIP-0044 CoinType code on the config +func (config *AddressConfig) SetCoinType(coinType uint32) { + config.coinType = coinType +} + +// GetCoinType returns the BIP-0044 CoinType code on the config. +func (config *AddressConfig) GetCoinType() uint32 { + return config.coinType +} diff --git a/types/config.go b/types/config.go index 826e6652bc44..a8e9bb4e4c29 100644 --- a/types/config.go +++ b/types/config.go @@ -134,12 +134,6 @@ func (config *Config) SetPurpose(purpose uint32) { 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() @@ -202,11 +196,6 @@ 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. diff --git a/types/config_test.go b/types/config_test.go index df281b1f852a..9b9c1789f340 100644 --- a/types/config_test.go +++ b/types/config_test.go @@ -30,14 +30,11 @@ func (s *contextTestSuite) TestConfig_SetPurpose() { } func (s *configTestSuite) TestConfig_SetCoinType() { - config := sdk.NewConfig() + config := sdk.NewAddressConfig() 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() { From 99deec0fab6f8f8da4f525ee61776c4e06abd156 Mon Sep 17 00:00:00 2001 From: bizk Date: Wed, 15 Nov 2023 20:14:37 -0300 Subject: [PATCH 04/16] moved out purpose to address global config --- types/address.go | 12 ++++++++++++ types/config.go | 11 ----------- types/config_test.go | 5 +---- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/types/address.go b/types/address.go index a0f99264a976..15907e2b0d0e 100644 --- a/types/address.go +++ b/types/address.go @@ -712,6 +712,7 @@ func cacheBech32Addr(prefix string, addr []byte, cache *simplelru.LRU, cacheKey type AddressConfig struct { coinType uint32 + purpose uint32 } var ( @@ -732,6 +733,7 @@ func GetAddresConfig() *AddressConfig { func NewAddressConfig() *AddressConfig { return &AddressConfig{ coinType: CoinType, + purpose: Purpose, } } @@ -744,3 +746,13 @@ func (config *AddressConfig) SetCoinType(coinType uint32) { func (config *AddressConfig) GetCoinType() uint32 { return config.coinType } + +// Set the BIP-0044 Purpose code on the config +func (config *AddressConfig) SetPurpose(purpose uint32) { + config.purpose = purpose +} + +// GetPurpose returns the BIP-0044 Purpose code on the config. +func (config *AddressConfig) GetPurpose() uint32 { + return config.purpose +} diff --git a/types/config.go b/types/config.go index a8e9bb4e4c29..7458da9bc766 100644 --- a/types/config.go +++ b/types/config.go @@ -128,12 +128,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 -} - // Seal seals the config such that the config state could not be modified further func (config *Config) Seal() *Config { config.mtx.Lock() @@ -191,11 +185,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 -} - // GetFullFundraiserPath returns the BIP44Prefix. // // Deprecated: This method is supported for backward compatibility only and will be removed in a future release. Use GetFullBIP44Path instead. diff --git a/types/config_test.go b/types/config_test.go index 9b9c1789f340..b4735035562e 100644 --- a/types/config_test.go +++ b/types/config_test.go @@ -18,15 +18,12 @@ func TestConfigTestSuite(t *testing.T) { } func (s *contextTestSuite) TestConfig_SetPurpose() { - config := sdk.NewConfig() + config := sdk.NewAddressConfig() 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() { From 27e62ed97e07a6d7f18f5aaafbe6cd6f6e071543 Mon Sep 17 00:00:00 2001 From: bizk Date: Tue, 21 Nov 2023 19:46:47 -0300 Subject: [PATCH 05/16] moving configs out of global to context --- client/context.go | 8 ++++++++ client/keys/add.go | 4 ++-- client/keys/add_ledger_test.go | 29 ++++++++++++++++++++++------- client/keys/add_test.go | 15 ++++++++++++--- client/keys/root.go | 8 +++++++- crypto/ledger/ledger_mock.go | 2 +- testutil/key_test.go | 4 ++-- types/address.go | 3 +-- 8 files changed, 55 insertions(+), 18 deletions(-) diff --git a/client/context.go b/client/context.go index d75ea6771b24..11cb0ec3656d 100644 --- a/client/context.go +++ b/client/context.go @@ -73,6 +73,8 @@ type Context struct { AddressCodec address.Codec ValidatorAddressCodec address.Codec ConsensusAddressCodec address.Codec + + AddressConfigs sdk.AddressConfig } // WithCmdContext returns a copy of the context with an updated context.Context, @@ -324,6 +326,12 @@ func (ctx Context) WithConsensusAddressCodec(consensusAddressCodec address.Codec return ctx } +// WithAddressConfig returns the context with the provided address config. +func (ctx Context) WithAddressConfig(addressConfig sdk.AddressConfig) Context { + ctx.AddressConfigs = addressConfig + return ctx +} + // PrintString prints the raw string to ctx.Output if it's defined, otherwise to os.Stdout func (ctx Context) PrintString(str string) error { return ctx.PrintBytes([]byte(str)) diff --git a/client/keys/add.go b/client/keys/add.go index 137fa651e138..476c89c2059e 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -41,7 +41,7 @@ const ( ) // AddKeyCommand defines a keys command to add a generated or recovered private key to keybase. -func AddKeyCommand() *cobra.Command { +func AddKeyCommand(ctx client.Context) *cobra.Command { cmd := &cobra.Command{ Use: "add ", Short: "Add an encrypted private key (either newly generated or recovered), encrypt it, and save to file", @@ -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.GetAddresConfig().GetCoinType(), "coin type number for HD derivation") + f.Uint32(flagCoinType, ctx.AddressConfigs.GetCoinType(), "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 d3c777aa7bb1..350a6efe7b0a 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -32,13 +32,17 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { bech32PrefixConsAddr := "terravalcons" bech32PrefixConsPub := "terravalconspub" - config.SetPurpose(44) - sdk.GetAddresConfig().SetCoinType(330) config.SetBech32PrefixForAccount(bech32PrefixAccAddr, bech32PrefixAccPub) config.SetBech32PrefixForValidator(bech32PrefixValAddr, bech32PrefixValPub) config.SetBech32PrefixForConsensusNode(bech32PrefixConsAddr, bech32PrefixConsPub) - cmd := AddKeyCommand() + clientContext := client.Context{} + addressConfig := sdk.NewAddressConfig() + addressConfig.SetPurpose(44) + addressConfig.SetCoinType(330) + + clientContext = clientContext.WithAddressConfig(*addressConfig) + cmd := AddKeyCommand(clientContext) cmd.Flags().AddFlagSet(Commands().PersistentFlags()) // Prepare a keybase @@ -89,15 +93,22 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { "PubKeySecp256k1{03028F0D5A9FD41600191CDEFDEA05E77A68DFBCE286241C0190805B9346667D07}", pub.String()) - config.SetPurpose(44) - sdk.GetAddresConfig().SetCoinType(118) + addressConfig.SetPurpose(44) + addressConfig.SetCoinType(330) + clientContext = clientContext.WithAddressConfig(*addressConfig) + + cmd = AddKeyCommand(clientContext) config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub) } func Test_runAddCmdLedger(t *testing.T) { - cmd := AddKeyCommand() + clientContext := client.Context{} + addressConfig := sdk.NewAddressConfig() + clientContext = clientContext.WithAddressConfig(*addressConfig) + + cmd := AddKeyCommand(clientContext) cmd.Flags().AddFlagSet(Commands().PersistentFlags()) mockIn := testutil.ApplyMockIODiscardOutErr(cmd) @@ -178,7 +189,11 @@ func Test_runAddCmdLedgerDryRun(t *testing.T) { for _, tt := range testData { tt := tt t.Run(tt.name, func(t *testing.T) { - cmd := AddKeyCommand() + clientContext := client.Context{} + addressConfig := sdk.NewAddressConfig() + clientContext = clientContext.WithAddressConfig(*addressConfig) + + cmd := AddKeyCommand(clientContext) cmd.Flags().AddFlagSet(Commands().PersistentFlags()) kbHome := t.TempDir() diff --git a/client/keys/add_test.go b/client/keys/add_test.go index a10da1c5a408..7dbb2084d409 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -22,7 +22,10 @@ import ( ) func Test_runAddCmdBasic(t *testing.T) { - cmd := AddKeyCommand() + clientContext := client.Context{} + addressConfig := sdk.NewAddressConfig() + clientContext = clientContext.WithAddressConfig(*addressConfig) + cmd := AddKeyCommand(clientContext) cmd.Flags().AddFlagSet(Commands().PersistentFlags()) mockIn := testutil.ApplyMockIODiscardOutErr(cmd) @@ -212,7 +215,10 @@ func Test_runAddCmdDryRun(t *testing.T) { for _, tt := range testData { tt := tt t.Run(tt.name, func(t *testing.T) { - cmd := AddKeyCommand() + clientContext := client.Context{} + addressConfig := sdk.NewAddressConfig() + clientContext = clientContext.WithAddressConfig(*addressConfig) + cmd := AddKeyCommand(clientContext) cmd.Flags().AddFlagSet(Commands().PersistentFlags()) kbHome := t.TempDir() @@ -261,7 +267,10 @@ func Test_runAddCmdDryRun(t *testing.T) { } func TestAddRecoverFileBackend(t *testing.T) { - cmd := AddKeyCommand() + clientContext := client.Context{} + addressConfig := sdk.NewAddressConfig() + clientContext.WithAddressConfig(*addressConfig) + cmd := AddKeyCommand(clientContext) cmd.Flags().AddFlagSet(Commands().PersistentFlags()) cdc := moduletestutil.MakeTestEncodingConfig().Codec diff --git a/client/keys/root.go b/client/keys/root.go index 9dee7bd48e3a..f330dfd9a7b5 100644 --- a/client/keys/root.go +++ b/client/keys/root.go @@ -1,6 +1,8 @@ package keys import ( + "github.com/cosmos/cosmos-sdk/client" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client/flags" @@ -36,9 +38,13 @@ The pass backend requires GnuPG: https://gnupg.org/ `, } + clientContext := client.Context{} + addressConfig := sdk.NewAddressConfig() + clientContext = clientContext.WithAddressConfig(*addressConfig) + cmd.AddCommand( MnemonicKeyCommand(), - AddKeyCommand(), + AddKeyCommand(clientContext), ExportKeyCommand(), ImportKeyCommand(), ImportKeyHexCommand(), diff --git a/crypto/ledger/ledger_mock.go b/crypto/ledger/ledger_mock.go index aff2cbe3d7dc..b7b484863a0e 100644 --- a/crypto/ledger/ledger_mock.go +++ b/crypto/ledger/ledger_mock.go @@ -42,7 +42,7 @@ func (mock LedgerSECP256K1Mock) GetPublicKeySECP256K1(derivationPath []uint32) ( return nil, errors.New("invalid derivation path") } - if derivationPath[1] != sdk.GetAddresConfig().GetCoinType() { + if derivationPath[1] != sdk.GetAddressConfig().GetCoinType() { return nil, errors.New("invalid derivation path") } diff --git a/testutil/key_test.go b/testutil/key_test.go index 3cde9cdb3a40..a77cb75d3593 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.GetAddresConfig().GetCoinType(), 0).String(), hd.Secp256k1) + k, err := keyring.NewInMemory(cdc).NewAccount("xxx", mnemonic, "", hd.NewFundraiserParams(0, types.GetAddressConfig().GetCoinType(), 0).String(), hd.Secp256k1) require.NoError(t, err) addr1, err := k.GetAddress() require.NoError(t, err) @@ -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.GetAddresConfig().GetCoinType(), 0).String(), hd.Secp256k1) + k, err = keyring.NewInMemory(encCfg.Codec).NewAccount("xxx", mnemonic, "", hd.NewFundraiserParams(0, types.GetAddressConfig().GetCoinType(), 0).String(), hd.Secp256k1) require.NoError(t, err) addr1, err = k.GetAddress() require.NoError(t, err) diff --git a/types/address.go b/types/address.go index 15907e2b0d0e..e42fa74a6aee 100644 --- a/types/address.go +++ b/types/address.go @@ -721,8 +721,7 @@ var ( ) // GetAddressConfig returns theaddres instance for the SDK. -func GetAddresConfig() *AddressConfig { - fmt.Println() +func GetAddressConfig() *AddressConfig { initAddressConfig.Do(func() { sdkAddressConfig = NewAddressConfig() }) From f190645efccadf4dc4605e88bd9f44b1d87c3093 Mon Sep 17 00:00:00 2001 From: bizk Date: Thu, 23 Nov 2023 12:58:25 -0300 Subject: [PATCH 06/16] Resolved address config issue --- client/keys/add.go | 4 ++-- client/keys/add_ledger_test.go | 30 ++++++++++++++---------------- client/keys/add_test.go | 8 ++++---- client/keys/delete_test.go | 2 +- client/keys/export_test.go | 3 +-- client/keys/rename_test.go | 2 +- client/keys/root.go | 2 +- testutil/key.go | 7 +++---- types/address.go | 5 +++++ types/config.go | 6 ------ 10 files changed, 32 insertions(+), 37 deletions(-) diff --git a/client/keys/add.go b/client/keys/add.go index 476c89c2059e..4600a8e6d32d 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -41,7 +41,7 @@ const ( ) // AddKeyCommand defines a keys command to add a generated or recovered private key to keybase. -func AddKeyCommand(ctx client.Context) *cobra.Command { +func AddKeyCommand() *cobra.Command { cmd := &cobra.Command{ Use: "add ", Short: "Add an encrypted private key (either newly generated or recovered), encrypt it, and save to file", @@ -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, ctx.AddressConfigs.GetCoinType(), "coin type number for HD derivation") + f.Uint32(flagCoinType, sdk.GetAddressConfig().GetCoinType(), "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 350a6efe7b0a..fc74091dcaba 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -36,15 +36,10 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { config.SetBech32PrefixForValidator(bech32PrefixValAddr, bech32PrefixValPub) config.SetBech32PrefixForConsensusNode(bech32PrefixConsAddr, bech32PrefixConsPub) - clientContext := client.Context{} - addressConfig := sdk.NewAddressConfig() + addressConfig := sdk.GetAddressConfig() addressConfig.SetPurpose(44) addressConfig.SetCoinType(330) - clientContext = clientContext.WithAddressConfig(*addressConfig) - cmd := AddKeyCommand(clientContext) - cmd.Flags().AddFlagSet(Commands().PersistentFlags()) - // Prepare a keybase kbHome := t.TempDir() @@ -54,10 +49,13 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { WithCodec(cdc). WithAddressCodec(addresscodec.NewBech32Codec("cosmos")). WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")). - WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons")) + WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons")). + WithAddressConfig(*addressConfig) 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), @@ -95,32 +93,32 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { addressConfig.SetPurpose(44) addressConfig.SetCoinType(330) - clientContext = clientContext.WithAddressConfig(*addressConfig) + clientCtx = clientCtx.WithAddressConfig(*addressConfig) - cmd = AddKeyCommand(clientContext) + cmd = AddKeyCommand() config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub) } func Test_runAddCmdLedger(t *testing.T) { - clientContext := client.Context{} - addressConfig := sdk.NewAddressConfig() - clientContext = clientContext.WithAddressConfig(*addressConfig) - - cmd := AddKeyCommand(clientContext) + cmd := AddKeyCommand() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) mockIn := testutil.ApplyMockIODiscardOutErr(cmd) kbHome := t.TempDir() cdc := moduletestutil.MakeTestEncodingConfig().Codec + addressConfig := sdk.GetAddressConfig() + addressConfig.SetCoinType(sdk.CoinType) + clientCtx := client.Context{}. WithKeyringDir(kbHome). WithCodec(cdc). WithAddressCodec(addresscodec.NewBech32Codec("cosmos")). WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")). - WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons")) + WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons")). + WithAddressConfig(*addressConfig) ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) @@ -193,7 +191,7 @@ func Test_runAddCmdLedgerDryRun(t *testing.T) { addressConfig := sdk.NewAddressConfig() clientContext = clientContext.WithAddressConfig(*addressConfig) - cmd := AddKeyCommand(clientContext) + cmd := AddKeyCommand() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) kbHome := t.TempDir() diff --git a/client/keys/add_test.go b/client/keys/add_test.go index 7dbb2084d409..1b32112dfd97 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -25,7 +25,7 @@ func Test_runAddCmdBasic(t *testing.T) { clientContext := client.Context{} addressConfig := sdk.NewAddressConfig() clientContext = clientContext.WithAddressConfig(*addressConfig) - cmd := AddKeyCommand(clientContext) + cmd := AddKeyCommand() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) mockIn := testutil.ApplyMockIODiscardOutErr(cmd) @@ -218,7 +218,7 @@ func Test_runAddCmdDryRun(t *testing.T) { clientContext := client.Context{} addressConfig := sdk.NewAddressConfig() clientContext = clientContext.WithAddressConfig(*addressConfig) - cmd := AddKeyCommand(clientContext) + cmd := AddKeyCommand() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) kbHome := t.TempDir() @@ -236,7 +236,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.GetAddressConfig().GetFullBIP44Path() _, err = kb.NewAccount("subkey", testdata.TestMnemonic, "", path, hd.Secp256k1) require.NoError(t, err) @@ -270,7 +270,7 @@ func TestAddRecoverFileBackend(t *testing.T) { clientContext := client.Context{} addressConfig := sdk.NewAddressConfig() clientContext.WithAddressConfig(*addressConfig) - cmd := AddKeyCommand(clientContext) + cmd := AddKeyCommand() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) cdc := moduletestutil.MakeTestEncodingConfig().Codec diff --git a/client/keys/delete_test.go b/client/keys/delete_test.go index 29246b1095dd..0d11a17af36f 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.GetAddressConfig().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 d7e4126f6a7c..ea99086e35ad 100644 --- a/client/keys/export_test.go +++ b/client/keys/export_test.go @@ -90,8 +90,7 @@ func Test_runExportCmd(t *testing.T) { require.NoError(t, err) t.Cleanup(cleanupKeys(t, kb, "keyname1")) - path := sdk.GetConfig().GetFullBIP44Path() - _, err = kb.NewAccount("keyname1", testdata.TestMnemonic, "", path, hd.Secp256k1) + _, err = kb.NewAccount("keyname1", testdata.TestMnemonic, "", sdk.GetAddressConfig().GetFullBIP44Path(), hd.Secp256k1) require.NoError(t, err) clientCtx := client.Context{}. diff --git a/client/keys/rename_test.go b/client/keys/rename_test.go index 8df5d641ba05..fac331df5932 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.GetAddressConfig().GetFullBIP44Path() cdc := moduletestutil.MakeTestEncodingConfig().Codec kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc) diff --git a/client/keys/root.go b/client/keys/root.go index f330dfd9a7b5..65cf0d705398 100644 --- a/client/keys/root.go +++ b/client/keys/root.go @@ -44,7 +44,7 @@ The pass backend requires GnuPG: https://gnupg.org/ cmd.AddCommand( MnemonicKeyCommand(), - AddKeyCommand(clientContext), + AddKeyCommand(), ExportKeyCommand(), ImportKeyCommand(), ImportKeyHexCommand(), diff --git a/testutil/key.go b/testutil/key.go index a82a822567f3..0842a15c70a9 100644 --- a/testutil/key.go +++ b/testutil/key.go @@ -2,7 +2,6 @@ package testutil import ( "fmt" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keyring" sdk "github.com/cosmos/cosmos-sdk/types" @@ -14,7 +13,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.NewAddressConfig().GetFullBIP44Path(), keyring.DefaultBIP39Passphrase, algo, ) @@ -63,9 +62,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, sdk.NewAddressConfig().GetFullBIP44Path(), algo) } else { - record, secret, err = keybase.NewMnemonic(keyName, keyring.English, sdk.GetConfig().GetFullBIP44Path(), keyring.DefaultBIP39Passphrase, algo) + record, secret, err = keybase.NewMnemonic(keyName, keyring.English, sdk.NewAddressConfig().GetFullBIP44Path(), keyring.DefaultBIP39Passphrase, algo) } if err != nil { return sdk.AccAddress{}, "", err diff --git a/types/address.go b/types/address.go index e42fa74a6aee..ea2ede8cd495 100644 --- a/types/address.go +++ b/types/address.go @@ -755,3 +755,8 @@ func (config *AddressConfig) SetPurpose(purpose uint32) { func (config *AddressConfig) GetPurpose() uint32 { return config.purpose } + +// GetFullBIP44Path returns the BIP44Prefix. +func (config *AddressConfig) GetFullBIP44Path() string { + return fmt.Sprintf("m/%d'/%d'/0'/0/0", config.purpose, config.coinType) +} diff --git a/types/config.go b/types/config.go index 7458da9bc766..8ff1ad5eb87f 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" @@ -192,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 From 272083d603ca6dd357c3eeadc3115b7e59008261 Mon Sep 17 00:00:00 2001 From: bizk Date: Thu, 23 Nov 2023 13:05:43 -0300 Subject: [PATCH 07/16] solved lint issues --- client/keys/add_ledger_test.go | 5 ----- client/keys/add_test.go | 6 ------ client/keys/root.go | 6 ------ 3 files changed, 17 deletions(-) diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index fc74091dcaba..00c08799d382 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -95,7 +95,6 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { addressConfig.SetCoinType(330) clientCtx = clientCtx.WithAddressConfig(*addressConfig) - cmd = AddKeyCommand() config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub) @@ -187,10 +186,6 @@ func Test_runAddCmdLedgerDryRun(t *testing.T) { for _, tt := range testData { tt := tt t.Run(tt.name, func(t *testing.T) { - clientContext := client.Context{} - addressConfig := sdk.NewAddressConfig() - clientContext = clientContext.WithAddressConfig(*addressConfig) - cmd := AddKeyCommand() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) diff --git a/client/keys/add_test.go b/client/keys/add_test.go index 1b32112dfd97..dc62fc609d48 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -22,9 +22,6 @@ import ( ) func Test_runAddCmdBasic(t *testing.T) { - clientContext := client.Context{} - addressConfig := sdk.NewAddressConfig() - clientContext = clientContext.WithAddressConfig(*addressConfig) cmd := AddKeyCommand() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) @@ -215,9 +212,6 @@ func Test_runAddCmdDryRun(t *testing.T) { for _, tt := range testData { tt := tt t.Run(tt.name, func(t *testing.T) { - clientContext := client.Context{} - addressConfig := sdk.NewAddressConfig() - clientContext = clientContext.WithAddressConfig(*addressConfig) cmd := AddKeyCommand() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) diff --git a/client/keys/root.go b/client/keys/root.go index 65cf0d705398..9dee7bd48e3a 100644 --- a/client/keys/root.go +++ b/client/keys/root.go @@ -1,8 +1,6 @@ package keys import ( - "github.com/cosmos/cosmos-sdk/client" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client/flags" @@ -38,10 +36,6 @@ The pass backend requires GnuPG: https://gnupg.org/ `, } - clientContext := client.Context{} - addressConfig := sdk.NewAddressConfig() - clientContext = clientContext.WithAddressConfig(*addressConfig) - cmd.AddCommand( MnemonicKeyCommand(), AddKeyCommand(), From a3070eb47491cd58e259ac3bef2f66b441ac5edb Mon Sep 17 00:00:00 2001 From: bizk Date: Thu, 23 Nov 2023 13:33:41 -0300 Subject: [PATCH 08/16] fixed lint --- tests/integration/runtime/query_test.go | 4 +--- testutil/key.go | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/integration/runtime/query_test.go b/tests/integration/runtime/query_test.go index e2385dc87473..7d40ae80aff8 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 0842a15c70a9..69cb4fac6248 100644 --- a/testutil/key.go +++ b/testutil/key.go @@ -2,6 +2,7 @@ package testutil import ( "fmt" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keyring" sdk "github.com/cosmos/cosmos-sdk/types" From 016259c295db6be7d7b4e6ec6a0300e27386118c Mon Sep 17 00:00:00 2001 From: bizk Date: Thu, 23 Nov 2023 13:43:52 -0300 Subject: [PATCH 09/16] rolled back unnecesary change --- client/keys/export_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/keys/export_test.go b/client/keys/export_test.go index ea99086e35ad..84842ea42c88 100644 --- a/client/keys/export_test.go +++ b/client/keys/export_test.go @@ -90,7 +90,8 @@ func Test_runExportCmd(t *testing.T) { require.NoError(t, err) t.Cleanup(cleanupKeys(t, kb, "keyname1")) - _, err = kb.NewAccount("keyname1", testdata.TestMnemonic, "", sdk.GetAddressConfig().GetFullBIP44Path(), hd.Secp256k1) + path := sdk.GetAddressConfig().GetFullBIP44Path() + _, err = kb.NewAccount("keyname1", testdata.TestMnemonic, "", path, hd.Secp256k1) require.NoError(t, err) clientCtx := client.Context{}. From ae0dbc8614e900d949e518e816ce26abdebd1397 Mon Sep 17 00:00:00 2001 From: bizk Date: Mon, 27 Nov 2023 19:48:39 -0300 Subject: [PATCH 10/16] removed getCoinType --- client/keys/add.go | 2 +- client/keys/add_ledger_test.go | 3 --- client/keys/add_test.go | 10 +++++----- client/keys/delete_test.go | 1 + crypto/ledger/ledger_mock.go | 4 ---- crypto/ledger/ledger_test.go | 8 -------- simapp/simd/cmd/testnet.go | 2 +- testutil/key.go | 5 +++-- testutil/key_test.go | 12 ++++++------ testutil/network/network.go | 5 ++++- types/address.go | 11 ----------- types/config_test.go | 8 -------- 12 files changed, 21 insertions(+), 50 deletions(-) diff --git a/client/keys/add.go b/client/keys/add.go index 4600a8e6d32d..d738d93aaa11 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.GetAddressConfig().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 00c08799d382..c7ed0e8bb74a 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -38,7 +38,6 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { addressConfig := sdk.GetAddressConfig() addressConfig.SetPurpose(44) - addressConfig.SetCoinType(330) // Prepare a keybase kbHome := t.TempDir() @@ -92,7 +91,6 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { pub.String()) addressConfig.SetPurpose(44) - addressConfig.SetCoinType(330) clientCtx = clientCtx.WithAddressConfig(*addressConfig) config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) @@ -109,7 +107,6 @@ func Test_runAddCmdLedger(t *testing.T) { cdc := moduletestutil.MakeTestEncodingConfig().Codec addressConfig := sdk.GetAddressConfig() - addressConfig.SetCoinType(sdk.CoinType) clientCtx := client.Context{}. WithKeyringDir(kbHome). diff --git a/client/keys/add_test.go b/client/keys/add_test.go index dc62fc609d48..b614f5ac7acf 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -221,16 +221,19 @@ func Test_runAddCmdDryRun(t *testing.T) { kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc) require.NoError(t, err) + addressConfig := sdk.NewAddressConfig() + clientCtx := client.Context{}. WithCodec(cdc). WithKeyringDir(kbHome). WithKeyring(kb). WithAddressCodec(addresscodec.NewBech32Codec("cosmos")). WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")). - WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons")) + WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons")). + WithAddressConfig(*addressConfig) ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) - path := sdk.GetAddressConfig().GetFullBIP44Path() + path := clientCtx.AddressConfigs.GetFullBIP44Path() _, err = kb.NewAccount("subkey", testdata.TestMnemonic, "", path, hd.Secp256k1) require.NoError(t, err) @@ -261,9 +264,6 @@ func Test_runAddCmdDryRun(t *testing.T) { } func TestAddRecoverFileBackend(t *testing.T) { - clientContext := client.Context{} - addressConfig := sdk.NewAddressConfig() - clientContext.WithAddressConfig(*addressConfig) cmd := AddKeyCommand() cmd.Flags().AddFlagSet(Commands().PersistentFlags()) cdc := moduletestutil.MakeTestEncodingConfig().Codec diff --git a/client/keys/delete_test.go b/client/keys/delete_test.go index 0d11a17af36f..c67fe0fd2760 100644 --- a/client/keys/delete_test.go +++ b/client/keys/delete_test.go @@ -34,6 +34,7 @@ func Test_runDeleteCmd(t *testing.T) { fakeKeyName2 := "runDeleteCmd_Key2" path := sdk.GetAddressConfig().GetFullBIP44Path() + fmt.Println(sdk.GetAddressConfig().GetFullBIP44Path()) cdc := moduletestutil.MakeTestEncodingConfig().Codec cmd.SetArgs([]string{"blah", fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome)}) diff --git a/crypto/ledger/ledger_mock.go b/crypto/ledger/ledger_mock.go index b7b484863a0e..94e5063bc54f 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.GetAddressConfig().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 368c86ac419e..1c742791a35f 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 d0de26b57c77..fa9198ccc1a1 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.NewAddressConfig().GetFullBIP44Path()) if err != nil { _ = os.RemoveAll(args.outputDir) return err diff --git a/testutil/key.go b/testutil/key.go index 69cb4fac6248..2ed862151977 100644 --- a/testutil/key.go +++ b/testutil/key.go @@ -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.NewAddressConfig().GetFullBIP44Path(), algo) + record, err = keybase.NewAccount(keyName, mnemonic, keyring.DefaultBIP39Passphrase, hdPath, algo) } else { - record, secret, err = keybase.NewMnemonic(keyName, keyring.English, sdk.NewAddressConfig().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 a77cb75d3593..6e1d6c833e9b 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.GetAddressConfig().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.NewAddressConfig().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.GetAddressConfig().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.NewAddressConfig().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.NewAddressConfig().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.NewAddressConfig().GetFullBIP44Path()) require.NoError(t, err) require.NotEqual(t, addr1, addr2) diff --git a/testutil/network/network.go b/testutil/network/network.go index 24a8da27f99a..d60a8611218a 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -137,6 +137,8 @@ type Config struct { AddressCodec address.Codec // address codec ValidatorAddressCodec runtime.ValidatorAddressCodec // validator address codec ConsensusAddressCodec runtime.ConsensusAddressCodec // consensus address codec + + AddressConfig sdk.AddressConfig } // DefaultConfig returns a sane default configuration suitable for nearly all @@ -168,6 +170,7 @@ func DefaultConfig(factory TestFixtureFactory) Config { AddressCodec: addresscodec.NewBech32Codec("cosmos"), ValidatorAddressCodec: addresscodec.NewBech32Codec("cosmosvaloper"), ConsensusAddressCodec: addresscodec.NewBech32Codec("cosmosvalcons"), + AddressConfig: *sdk.NewAddressConfig(), } } @@ -503,7 +506,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) { return nil, err } - addr, secret, err := testutil.GenerateSaveCoinKey(kb, nodeDirName, mnemonic, true, algo) + addr, secret, err := testutil.GenerateSaveCoinKey(kb, nodeDirName, mnemonic, true, algo, cfg.AddressConfig.GetFullBIP44Path()) if err != nil { return nil, err } diff --git a/types/address.go b/types/address.go index ea2ede8cd495..8409ffd144d6 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 @@ -736,16 +735,6 @@ func NewAddressConfig() *AddressConfig { } } -// Set the BIP-0044 CoinType code on the config -func (config *AddressConfig) SetCoinType(coinType uint32) { - config.coinType = coinType -} - -// GetCoinType returns the BIP-0044 CoinType code on the config. -func (config *AddressConfig) GetCoinType() uint32 { - return config.coinType -} - // Set the BIP-0044 Purpose code on the config func (config *AddressConfig) SetPurpose(purpose uint32) { config.purpose = purpose diff --git a/types/config_test.go b/types/config_test.go index b4735035562e..0d2a7204cfe5 100644 --- a/types/config_test.go +++ b/types/config_test.go @@ -26,14 +26,6 @@ func (s *contextTestSuite) TestConfig_SetPurpose() { s.Require().Equal(uint32(0), config.GetPurpose()) } -func (s *configTestSuite) TestConfig_SetCoinType() { - config := sdk.NewAddressConfig() - config.SetCoinType(1) - s.Require().Equal(uint32(1), config.GetCoinType()) - config.SetCoinType(99) - s.Require().Equal(uint32(99), config.GetCoinType()) -} - func (s *configTestSuite) TestConfig_SetTxEncoder() { mockErr := errors.New("test") config := sdk.NewConfig() From e3bd0d23908fdd558a4c82a306ec2d8bdfa8d2c9 Mon Sep 17 00:00:00 2001 From: bizk Date: Tue, 28 Nov 2023 11:47:19 -0300 Subject: [PATCH 11/16] removed pupose from config --- types/address.go | 5 ----- types/config_test.go | 9 --------- 2 files changed, 14 deletions(-) diff --git a/types/address.go b/types/address.go index 8409ffd144d6..9a2b5998725c 100644 --- a/types/address.go +++ b/types/address.go @@ -740,11 +740,6 @@ func (config *AddressConfig) SetPurpose(purpose uint32) { config.purpose = purpose } -// GetPurpose returns the BIP-0044 Purpose code on the config. -func (config *AddressConfig) GetPurpose() uint32 { - return config.purpose -} - // GetFullBIP44Path returns the BIP44Prefix. func (config *AddressConfig) GetFullBIP44Path() string { return fmt.Sprintf("m/%d'/%d'/0'/0/0", config.purpose, config.coinType) diff --git a/types/config_test.go b/types/config_test.go index 0d2a7204cfe5..da9383ef3a3f 100644 --- a/types/config_test.go +++ b/types/config_test.go @@ -17,15 +17,6 @@ func TestConfigTestSuite(t *testing.T) { suite.Run(t, new(configTestSuite)) } -func (s *contextTestSuite) TestConfig_SetPurpose() { - config := sdk.NewAddressConfig() - config.SetPurpose(44) - s.Require().Equal(uint32(44), config.GetPurpose()) - - config.SetPurpose(0) - s.Require().Equal(uint32(0), config.GetPurpose()) -} - func (s *configTestSuite) TestConfig_SetTxEncoder() { mockErr := errors.New("test") config := sdk.NewConfig() From 999633cd63c1b0e6262b69be2f0c2f6f542eadae Mon Sep 17 00:00:00 2001 From: bizk Date: Tue, 28 Nov 2023 12:52:06 -0300 Subject: [PATCH 12/16] removed setPurpose --- client/keys/add_ledger_test.go | 2 -- types/address.go | 5 ----- 2 files changed, 7 deletions(-) diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index c7ed0e8bb74a..045f910e73f0 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -37,7 +37,6 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { config.SetBech32PrefixForConsensusNode(bech32PrefixConsAddr, bech32PrefixConsPub) addressConfig := sdk.GetAddressConfig() - addressConfig.SetPurpose(44) // Prepare a keybase kbHome := t.TempDir() @@ -90,7 +89,6 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { "PubKeySecp256k1{03028F0D5A9FD41600191CDEFDEA05E77A68DFBCE286241C0190805B9346667D07}", pub.String()) - addressConfig.SetPurpose(44) clientCtx = clientCtx.WithAddressConfig(*addressConfig) config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) diff --git a/types/address.go b/types/address.go index 9a2b5998725c..2ce4d8c517ae 100644 --- a/types/address.go +++ b/types/address.go @@ -735,11 +735,6 @@ func NewAddressConfig() *AddressConfig { } } -// Set the BIP-0044 Purpose code on the config -func (config *AddressConfig) SetPurpose(purpose uint32) { - config.purpose = purpose -} - // GetFullBIP44Path returns the BIP44Prefix. func (config *AddressConfig) GetFullBIP44Path() string { return fmt.Sprintf("m/%d'/%d'/0'/0/0", config.purpose, config.coinType) From 545c0536255b278cd2bfa7687241774dac719667 Mon Sep 17 00:00:00 2001 From: bizk Date: Tue, 28 Nov 2023 14:30:04 -0300 Subject: [PATCH 13/16] removed purpose and addressconfig --- client/context.go | 8 -------- client/keys/add_ledger_test.go | 12 ++---------- client/keys/add_test.go | 7 ++----- client/keys/delete_test.go | 4 ++-- client/keys/export_test.go | 2 +- client/keys/rename_test.go | 2 +- simapp/simd/cmd/testnet.go | 2 +- testutil/key.go | 2 +- testutil/key_test.go | 8 ++++---- testutil/network/network.go | 5 +---- types/address.go | 30 ++---------------------------- 11 files changed, 17 insertions(+), 65 deletions(-) diff --git a/client/context.go b/client/context.go index 6e5ee7eab229..cafc180dc20b 100644 --- a/client/context.go +++ b/client/context.go @@ -75,8 +75,6 @@ type Context struct { AddressCodec address.Codec ValidatorAddressCodec address.Codec ConsensusAddressCodec address.Codec - - AddressConfigs sdk.AddressConfig } // WithCmdContext returns a copy of the context with an updated context.Context, @@ -335,12 +333,6 @@ func (ctx Context) WithConsensusAddressCodec(consensusAddressCodec address.Codec return ctx } -// WithAddressConfig returns the context with the provided address config. -func (ctx Context) WithAddressConfig(addressConfig sdk.AddressConfig) Context { - ctx.AddressConfigs = addressConfig - return ctx -} - // PrintString prints the raw string to ctx.Output if it's defined, otherwise to os.Stdout func (ctx Context) PrintString(str string) error { return ctx.PrintBytes([]byte(str)) diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index 045f910e73f0..2e1865823c30 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -36,8 +36,6 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { config.SetBech32PrefixForValidator(bech32PrefixValAddr, bech32PrefixValPub) config.SetBech32PrefixForConsensusNode(bech32PrefixConsAddr, bech32PrefixConsPub) - addressConfig := sdk.GetAddressConfig() - // Prepare a keybase kbHome := t.TempDir() @@ -47,8 +45,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { WithCodec(cdc). WithAddressCodec(addresscodec.NewBech32Codec("cosmos")). WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")). - WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons")). - WithAddressConfig(*addressConfig) + WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons")) ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) @@ -89,8 +86,6 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { "PubKeySecp256k1{03028F0D5A9FD41600191CDEFDEA05E77A68DFBCE286241C0190805B9346667D07}", pub.String()) - clientCtx = clientCtx.WithAddressConfig(*addressConfig) - config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub) @@ -104,15 +99,12 @@ func Test_runAddCmdLedger(t *testing.T) { kbHome := t.TempDir() cdc := moduletestutil.MakeTestEncodingConfig().Codec - addressConfig := sdk.GetAddressConfig() - clientCtx := client.Context{}. WithKeyringDir(kbHome). WithCodec(cdc). WithAddressCodec(addresscodec.NewBech32Codec("cosmos")). WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")). - WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons")). - WithAddressConfig(*addressConfig) + WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons")) ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) diff --git a/client/keys/add_test.go b/client/keys/add_test.go index b614f5ac7acf..84b81833bbb7 100644 --- a/client/keys/add_test.go +++ b/client/keys/add_test.go @@ -221,19 +221,16 @@ func Test_runAddCmdDryRun(t *testing.T) { kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc) require.NoError(t, err) - addressConfig := sdk.NewAddressConfig() - clientCtx := client.Context{}. WithCodec(cdc). WithKeyringDir(kbHome). WithKeyring(kb). WithAddressCodec(addresscodec.NewBech32Codec("cosmos")). WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")). - WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons")). - WithAddressConfig(*addressConfig) + WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons")) ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) - path := clientCtx.AddressConfigs.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 9f09ebe2a629..93974c3f2d41 100644 --- a/client/keys/delete_test.go +++ b/client/keys/delete_test.go @@ -33,8 +33,8 @@ func Test_runDeleteCmd(t *testing.T) { fakeKeyName1 := "runDeleteCmd_Key1" fakeKeyName2 := "runDeleteCmd_Key2" - path := sdk.GetAddressConfig().GetFullBIP44Path() - fmt.Println(sdk.GetAddressConfig().GetFullBIP44Path()) + path := sdk.GetFullBIP44Path() + fmt.Println(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 84842ea42c88..96d1048e90b5 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.GetAddressConfig().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 fac331df5932..dad91168f07b 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.GetAddressConfig().GetFullBIP44Path() + path := sdk.GetFullBIP44Path() cdc := moduletestutil.MakeTestEncodingConfig().Codec kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc) diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index 40dbd7ba3d01..4ae56e28c4fb 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, sdk.NewAddressConfig().GetFullBIP44Path()) + addr, secret, err := testutil.GenerateSaveCoinKey(kb, nodeDirName, "", true, algo, sdk.GetFullBIP44Path()) if err != nil { _ = os.RemoveAll(args.outputDir) return err diff --git a/testutil/key.go b/testutil/key.go index 2ed862151977..5ae8926d1088 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.NewAddressConfig().GetFullBIP44Path(), + sdk.GetFullBIP44Path(), keyring.DefaultBIP39Passphrase, algo, ) diff --git a/testutil/key_test.go b/testutil/key_test.go index 6e1d6c833e9b..26847d764752 100644 --- a/testutil/key_test.go +++ b/testutil/key_test.go @@ -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, types.NewAddressConfig().GetFullBIP44Path()) + addr, mnemonic, err := GenerateSaveCoinKey(kb, "keyname", "", false, hd.Secp256k1, types.GetFullBIP44Path()) require.NoError(t, err) // Test key was actually saved @@ -58,15 +58,15 @@ func TestGenerateSaveCoinKeyOverwriteFlag(t *testing.T) { require.NoError(t, err) keyname := "justakey" - addr1, _, err := GenerateSaveCoinKey(kb, keyname, "", false, hd.Secp256k1, types.NewAddressConfig().GetFullBIP44Path()) + 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, types.NewAddressConfig().GetFullBIP44Path()) + _, _, 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, types.NewAddressConfig().GetFullBIP44Path()) + 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 348990b856b9..86e964ee449c 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -131,8 +131,6 @@ type Config struct { AddressCodec address.Codec // address codec ValidatorAddressCodec runtime.ValidatorAddressCodec // validator address codec ConsensusAddressCodec runtime.ConsensusAddressCodec // consensus address codec - - AddressConfig sdk.AddressConfig } // DefaultConfig returns a sane default configuration suitable for nearly all @@ -164,7 +162,6 @@ func DefaultConfig(factory TestFixtureFactory) Config { AddressCodec: addresscodec.NewBech32Codec("cosmos"), ValidatorAddressCodec: addresscodec.NewBech32Codec("cosmosvaloper"), ConsensusAddressCodec: addresscodec.NewBech32Codec("cosmosvalcons"), - AddressConfig: *sdk.NewAddressConfig(), } } @@ -458,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, cfg.AddressConfig.GetFullBIP44Path()) + 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 2ce4d8c517ae..e3ac48e31a65 100644 --- a/types/address.go +++ b/types/address.go @@ -709,33 +709,7 @@ func cacheBech32Addr(prefix string, addr []byte, cache *simplelru.LRU, cacheKey return bech32Addr } -type AddressConfig struct { - coinType uint32 - purpose uint32 -} - -var ( - sdkAddressConfig *AddressConfig - initAddressConfig sync.Once -) - -// GetAddressConfig returns theaddres instance for the SDK. -func GetAddressConfig() *AddressConfig { - initAddressConfig.Do(func() { - sdkAddressConfig = NewAddressConfig() - }) - return sdkAddressConfig -} - -// New returns a new Config with default values. -func NewAddressConfig() *AddressConfig { - return &AddressConfig{ - coinType: CoinType, - purpose: Purpose, - } -} - // GetFullBIP44Path returns the BIP44Prefix. -func (config *AddressConfig) GetFullBIP44Path() string { - return fmt.Sprintf("m/%d'/%d'/0'/0/0", config.purpose, config.coinType) +func GetFullBIP44Path() string { + return fmt.Sprintf("m/%d'/%d'/0'/0/0", Purpose, CoinType) } From d906067bbb7edea0d85840f020825d74cb7e06b1 Mon Sep 17 00:00:00 2001 From: bizk Date: Tue, 28 Nov 2023 14:54:31 -0300 Subject: [PATCH 14/16] removed missing fmt --- client/keys/delete_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/client/keys/delete_test.go b/client/keys/delete_test.go index 93974c3f2d41..dd1a38bd875b 100644 --- a/client/keys/delete_test.go +++ b/client/keys/delete_test.go @@ -34,7 +34,6 @@ func Test_runDeleteCmd(t *testing.T) { fakeKeyName2 := "runDeleteCmd_Key2" path := sdk.GetFullBIP44Path() - fmt.Println(sdk.GetFullBIP44Path()) cdc := moduletestutil.MakeTestEncodingConfig().Codec cmd.SetArgs([]string{"blah", fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome)}) From 246aedbd8ddd61a340350f0e78388f9fab6aa01e Mon Sep 17 00:00:00 2001 From: bizk Date: Tue, 28 Nov 2023 15:39:44 -0300 Subject: [PATCH 15/16] added changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38c8fd49cefa..8753c96293b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (version) [#18063](https://github.com/cosmos/cosmos-sdk/pull/18063) Include additional information in the Info struct. This change enhances the Info struct by adding support for additional information through the ExtraInfo field * (crypto | x/auth) [#14372](https://github.com/cosmos/cosmos-sdk/pull/18194) Key checks on signatures antehandle. * (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 address coin type and purpose ### Bug Fixes From 21d6c7df2b486ce2a428d45a3fff692448d866b8 Mon Sep 17 00:00:00 2001 From: bizk Date: Wed, 29 Nov 2023 12:12:12 -0300 Subject: [PATCH 16/16] improved changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8753c96293b1..aa950766105b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,7 +66,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (version) [#18063](https://github.com/cosmos/cosmos-sdk/pull/18063) Include additional information in the Info struct. This change enhances the Info struct by adding support for additional information through the ExtraInfo field * (crypto | x/auth) [#14372](https://github.com/cosmos/cosmos-sdk/pull/18194) Key checks on signatures antehandle. * (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 address coin type and purpose +* (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