Skip to content

Commit

Permalink
removed purpose and addressconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
bizk committed Nov 28, 2023
1 parent 04b2a7a commit 545c053
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 65 deletions.
8 changes: 0 additions & 8 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand Down
12 changes: 2 additions & 10 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down
7 changes: 2 additions & 5 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions client/keys/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)})
Expand Down
2 changes: 1 addition & 1 deletion client/keys/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion client/keys/rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion testutil/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
8 changes: 4 additions & 4 deletions testutil/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -164,7 +162,6 @@ func DefaultConfig(factory TestFixtureFactory) Config {
AddressCodec: addresscodec.NewBech32Codec("cosmos"),
ValidatorAddressCodec: addresscodec.NewBech32Codec("cosmosvaloper"),
ConsensusAddressCodec: addresscodec.NewBech32Codec("cosmosvalcons"),
AddressConfig: *sdk.NewAddressConfig(),
}
}

Expand Down Expand Up @@ -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
}
Expand Down
30 changes: 2 additions & 28 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 545c053

Please sign in to comment.