Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix default coin type #1134

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ $ %s k a cosmoshub testkey`, appName, appName, appName)),
}

if coinType < 0 {
if ccp, ok := chain.ChainProvider.(*cosmos.CosmosProvider); ok {
coinType = int32(ccp.PCfg.Slip44)
if ccp, ok := chain.ChainProvider.(*cosmos.CosmosProvider); ok && ccp.PCfg.Slip44 != nil {
coinType = int32(*ccp.PCfg.Slip44)
} else {
coinType = int32(defaultCoinType)
}
Expand Down Expand Up @@ -135,8 +135,8 @@ $ %s k r cosmoshub faucet-key "[mnemonic-words]"`, appName, appName)),
}

if coinType < 0 {
if ccp, ok := chain.ChainProvider.(*cosmos.CosmosProvider); ok {
coinType = int32(ccp.PCfg.Slip44)
if ccp, ok := chain.ChainProvider.(*cosmos.CosmosProvider); ok && ccp.PCfg.Slip44 != nil {
coinType = int32(*ccp.PCfg.Slip44)
} else {
coinType = int32(defaultCoinType)
}
Expand Down
73 changes: 71 additions & 2 deletions cmd/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ func TestKeysRestore_Delete(t *testing.T) {

_ = sys.MustRun(t, "config", "init")

slip44 := 118

sys.MustAddChain(t, "testChain", cmd.ProviderConfigWrapper{
Type: "cosmos",
Value: cosmos.CosmosProviderConfig{
AccountPrefix: "cosmos",
ChainID: "testcosmos",
KeyringBackend: "test",
Timeout: "10s",
Slip44: 118,
Slip44: &slip44,
},
})

Expand Down Expand Up @@ -82,14 +84,16 @@ func TestKeysExport(t *testing.T) {

_ = sys.MustRun(t, "config", "init")

slip44 := 118

sys.MustAddChain(t, "testChain", cmd.ProviderConfigWrapper{
Type: "cosmos",
Value: cosmos.CosmosProviderConfig{
AccountPrefix: "cosmos",
ChainID: "testcosmos",
KeyringBackend: "test",
Timeout: "10s",
Slip44: 118,
Slip44: &slip44,
},
})

Expand All @@ -113,3 +117,68 @@ func TestKeysExport(t *testing.T) {

// TODO: confirm the imported address matches?
}

func TestKeysDefaultCoinType(t *testing.T) {
t.Parallel()

sys := relayertest.NewSystem(t)

_ = sys.MustRun(t, "config", "init")

slip44 := 118

sys.MustAddChain(t, "testChain", cmd.ProviderConfigWrapper{
Type: "cosmos",
Value: cosmos.CosmosProviderConfig{
AccountPrefix: "cosmos",
ChainID: "testcosmos-1",
KeyringBackend: "test",
Timeout: "10s",
Slip44: &slip44,
},
})

sys.MustAddChain(t, "testChain2", cmd.ProviderConfigWrapper{
Type: "cosmos",
Value: cosmos.CosmosProviderConfig{
AccountPrefix: "cosmos",
ChainID: "testcosmos-2",
KeyringBackend: "test",
Timeout: "10s",
},
})

// Restore a key with mnemonic to the chain.
res := sys.MustRun(t, "keys", "restore", "testChain", "default", relayertest.ZeroMnemonic)
require.Equal(t, res.Stdout.String(), relayertest.ZeroCosmosAddr+"\n")
require.Empty(t, res.Stderr.String())

// Restore a key with mnemonic to the chain.
res = sys.MustRun(t, "keys", "restore", "testChain2", "default", relayertest.ZeroMnemonic)
require.Equal(t, res.Stdout.String(), relayertest.ZeroCosmosAddr+"\n")
require.Empty(t, res.Stderr.String())

// Export the key.
res = sys.MustRun(t, "keys", "export", "testChain", "default")
armorOut := res.Stdout.String()
require.Contains(t, armorOut, "BEGIN TENDERMINT PRIVATE KEY")
require.Empty(t, res.Stderr.String())

// Export the key.
res = sys.MustRun(t, "keys", "export", "testChain2", "default")
armorOut2 := res.Stdout.String()
require.Contains(t, armorOut, "BEGIN TENDERMINT PRIVATE KEY")
require.Empty(t, res.Stderr.String())

// Import the key to a temporary keyring.
registry := codectypes.NewInterfaceRegistry()
cryptocodec.RegisterInterfaces(registry)
cdc := codec.NewProtoCodec(registry)
kr := keyring.NewInMemory(cdc)
require.NoError(t, kr.ImportPrivKey("temp", armorOut, keys.DefaultKeyPass))

// This should fail due to same key
err := kr.ImportPrivKey("temp", armorOut2, keys.DefaultKeyPass)
require.Error(t, err, "same key was able to be imported twice")
require.Contains(t, err.Error(), "cannot overwrite key")
}
2 changes: 1 addition & 1 deletion cregistry/chain_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type ChainInfo struct {
Genesis struct {
GenesisURL string `json:"genesis_url"`
} `json:"genesis"`
Slip44 int `json:"slip44"`
Slip44 *int `json:"slip44"`
Codebase struct {
GitRepo string `json:"git_repo"`
RecommendedVersion string `json:"recommended_version"`
Expand Down
2 changes: 1 addition & 1 deletion relayer/chains/cosmos/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type CosmosProviderConfig struct {
SignModeStr string `json:"sign-mode" yaml:"sign-mode"`
ExtraCodecs []string `json:"extra-codecs" yaml:"extra-codecs"`
Modules []module.AppModuleBasic `json:"-" yaml:"-"`
Slip44 int `json:"coin-type" yaml:"coin-type"`
Slip44 *int `json:"coin-type" yaml:"coin-type"`
Broadcast provider.BroadcastMode `json:"broadcast-mode" yaml:"broadcast-mode"`
}

Expand Down