Skip to content

Commit

Permalink
Harry/wallet init (#1190)
Browse files Browse the repository at this point in the history
* made a new method "logChannelOpenMessage" to log the newly opened channel into info level

* added fields

* some changes

* recreated issue 1151, added logs

* Added new flag to restore all keys based on single mnemonic

* minor fixes

* some nicks

* nicks

* made some changes based on the suggestions and mistakes

* added a separate test for restore-all

* missed a short declaration for res

* small nick failing the build

* fixed test

* added keys list command for techChain and testChain2 as well in the test cmd

* nicks

---------

Co-authored-by: Harry <[email protected]>
Co-authored-by: Justin Tieri <[email protected]>
  • Loading branch information
3 people authored Oct 20, 2023
1 parent cc75c04 commit d0deac5
Show file tree
Hide file tree
Showing 3 changed files with 1,270 additions and 34 deletions.
124 changes: 91 additions & 33 deletions cmd/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
const (
flagCoinType = "coin-type"
flagAlgo = "signing-algorithm"
flagRestoreAll = "restore-all"
defaultCoinType uint32 = sdk.CoinType
)

Expand Down Expand Up @@ -148,61 +149,118 @@ func keysRestoreCmd(a *appState) *cobra.Command {
Use: "restore chain_name key_name mnemonic",
Aliases: []string{"r"},
Short: "Restores a mnemonic to the keychain associated with a particular chain",
Args: withUsage(cobra.ExactArgs(3)),
Args: withUsage(cobra.RangeArgs(2, 3)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys restore ibc-0 testkey "[mnemonic-words]"
$ %s k r cosmoshub faucet-key "[mnemonic-words]"`, appName, appName)),
$ %s keys restore ibc-0 testkey "[mnemonic-words]"
$ %s k r cosmoshub faucet-key "[mnemonic-words]"
$ %s k r demo-key "[mnemonic-words]" --restore-all`, appName, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
keyName := args[1]

chain, ok := a.config.Chains[args[0]]
if !ok {
return errChainNotFound(args[0])
}

if chain.ChainProvider.KeyExists(keyName) {
return errKeyExists(keyName)
}

cmdFlags := cmd.Flags()

coinType, err := cmdFlags.GetInt32(flagCoinType)
restoreAll, err := cmdFlags.GetBool(flagRestoreAll)
if err != nil {
return err
}

if coinType < 0 {
if ccp, ok := chain.ChainProvider.(*cosmos.CosmosProvider); ok && ccp.PCfg.Slip44 != nil {
coinType = int32(*ccp.PCfg.Slip44)
} else {
coinType = int32(defaultCoinType)
if !restoreAll {

keyName := args[1]
chain, ok := a.config.Chains[args[0]]
if !ok {
return errChainNotFound(args[0])
}
}

algo, err := cmdFlags.GetString(flagAlgo)
if err != nil {
return err
}
if chain.ChainProvider.KeyExists(keyName) {
return errKeyExists(keyName)
}

if algo == "" {
if ccp, ok := chain.ChainProvider.(*cosmos.CosmosProvider); ok {
algo = ccp.PCfg.SigningAlgorithm
} else {
algo = string(hd.Secp256k1Type)
coinType, err := cmdFlags.GetInt32(flagCoinType)
if err != nil {
return err
}

if coinType < 0 {
if ccp, ok := chain.ChainProvider.(*cosmos.CosmosProvider); ok && ccp.PCfg.Slip44 != nil {
coinType = int32(*ccp.PCfg.Slip44)
} else {
coinType = int32(defaultCoinType)
}
}

algo, err := cmdFlags.GetString(flagAlgo)
if err != nil {
return err
}

if algo == "" {
if ccp, ok := chain.ChainProvider.(*cosmos.CosmosProvider); ok {
algo = ccp.PCfg.SigningAlgorithm
} else {
algo = string(hd.Secp256k1Type)
}
}

address, err := chain.ChainProvider.RestoreKey(keyName, args[2], uint32(coinType), algo)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), address)
return nil

}

address, err := chain.ChainProvider.RestoreKey(keyName, args[2], uint32(coinType), algo)
if err != nil {
return err
chains := a.config.Chains
keyName := args[0]

for i, c := range chains {

chain := a.config.Chains[i]

if chain.ChainProvider.KeyExists(keyName) {
return errKeyExists(keyName)
}

coinType, err := cmdFlags.GetInt32(flagCoinType)
if err != nil {
return err
}

if coinType < 0 {
if ccp, ok := c.ChainProvider.(*cosmos.CosmosProvider); ok && ccp.PCfg.Slip44 != nil {
coinType = int32(*ccp.PCfg.Slip44)
} else {
coinType = int32(defaultCoinType)
}
}

algo, err := cmdFlags.GetString(flagAlgo)
if err != nil {
return err
}

if algo == "" {
if ccp, ok := c.ChainProvider.(*cosmos.CosmosProvider); ok {
algo = ccp.PCfg.SigningAlgorithm
} else {
algo = string(hd.Secp256k1Type)
}
}

addresses, err := c.ChainProvider.RestoreKey(keyName, args[1], uint32(coinType), algo)
if err != nil {
return err
}

fmt.Fprintln(cmd.OutOrStdout(), addresses)
}

fmt.Fprintln(cmd.OutOrStdout(), address)
return nil
},
}
cmd.Flags().Int32(flagCoinType, -1, "coin type number for HD derivation")
cmd.Flags().String(flagAlgo, "", "signing algorithm for key (secp256k1, sr25519)")
cmd.Flags().Bool(flagRestoreAll, false, "restores keys for all configured chains with a single mnemonic")

return cmd
}
Expand Down
86 changes: 85 additions & 1 deletion cmd/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func TestKeysRestore_Delete(t *testing.T) {
})

// 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())
Expand Down Expand Up @@ -182,3 +181,88 @@ func TestKeysDefaultCoinType(t *testing.T) {
require.Error(t, err, "same key was able to be imported twice")
require.Contains(t, err.Error(), "cannot overwrite key")
}

func TestKeysRestoreAll_Delete(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",
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",
Slip44: &slip44,
},
})
sys.MustAddChain(t, "testChain3", cmd.ProviderConfigWrapper{
Type: "cosmos",
Value: cosmos.CosmosProviderConfig{
AccountPrefix: "cosmos",
ChainID: "testcosmos-3",
KeyringBackend: "test",
Timeout: "10s",
Slip44: &slip44,
},
})

// Restore keys for all configured chains with a single mnemonic.
res := sys.MustRun(t, "keys", "restore", "default", relayertest.ZeroMnemonic, "--restore-all")
require.Equal(t, res.Stdout.String(), relayertest.ZeroCosmosAddr+"\n"+relayertest.ZeroCosmosAddr+"\n"+relayertest.ZeroCosmosAddr+"\n")
require.Empty(t, res.Stderr.String())

// Restored key must show up in list.
res = sys.MustRun(t, "keys", "list", "testChain")
require.Equal(t, res.Stdout.String(), "key(default) -> "+relayertest.ZeroCosmosAddr+"\n")
require.Empty(t, res.Stderr.String())

res = sys.MustRun(t, "keys", "list", "testChain2")
require.Equal(t, res.Stdout.String(), "key(default) -> "+relayertest.ZeroCosmosAddr+"\n")
require.Empty(t, res.Stderr.String())

res = sys.MustRun(t, "keys", "list", "testChain3")
require.Equal(t, res.Stdout.String(), "key(default) -> "+relayertest.ZeroCosmosAddr+"\n")
require.Empty(t, res.Stderr.String())

// Deleting the key must succeed.
res = sys.MustRun(t, "keys", "delete", "testChain", "default", "-y")
require.Empty(t, res.Stdout.String())
require.Equal(t, res.Stderr.String(), "key default deleted\n")

res = sys.MustRun(t, "keys", "delete", "testChain2", "default", "-y")
require.Empty(t, res.Stdout.String())
require.Equal(t, res.Stderr.String(), "key default deleted\n")

res = sys.MustRun(t, "keys", "delete", "testChain3", "default", "-y")
require.Empty(t, res.Stdout.String())
require.Equal(t, res.Stderr.String(), "key default deleted\n")

// Listing the keys again gives the no keys warning.
res = sys.MustRun(t, "keys", "list", "testChain")
require.Empty(t, res.Stdout.String())
require.Contains(t, res.Stderr.String(), "no keys found for chain testChain")

res = sys.MustRun(t, "keys", "list", "testChain2")
require.Empty(t, res.Stdout.String())
require.Contains(t, res.Stderr.String(), "no keys found for chain testChain2")

res = sys.MustRun(t, "keys", "list", "testChain3")
require.Empty(t, res.Stdout.String())
require.Contains(t, res.Stderr.String(), "no keys found for chain testChain3")
}
Loading

0 comments on commit d0deac5

Please sign in to comment.