Skip to content

Commit

Permalink
feat: add the ability to manage rollapp keys (#1054)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Sep 29, 2024
1 parent b560aa9 commit 3d1b220
Show file tree
Hide file tree
Showing 13 changed files with 284 additions and 228 deletions.
8 changes: 4 additions & 4 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func GenerateSequencersKeys(initConfig config.RollappConfig) ([]utils.KeyInfo, error) {
keys := getSequencerKeysConfig()
keys := GetSequencerKeysConfig()
addresses := make([]utils.KeyInfo, 0)
for _, key := range keys {
var address *utils.KeyInfo
Expand All @@ -35,7 +35,7 @@ func GenerateSequencersKeys(initConfig config.RollappConfig) ([]utils.KeyInfo, e
}

func GenerateMockSequencerKeys(initConfig config.RollappConfig) ([]utils.KeyInfo, error) {
keys := getMockSequencerKeyConfig(initConfig)
keys := GetMockSequencerKeyConfig(initConfig)
addresses := make([]utils.KeyInfo, 0)
for _, key := range keys {
var address *utils.KeyInfo
Expand All @@ -55,7 +55,7 @@ func GenerateMockSequencerKeys(initConfig config.RollappConfig) ([]utils.KeyInfo
return addresses, nil
}

func getSequencerKeysConfig() []utils.KeyConfig {
func GetSequencerKeysConfig() []utils.KeyConfig {
return []utils.KeyConfig{
{
Dir: consts.ConfigDirName.HubKeys,
Expand All @@ -67,7 +67,7 @@ func getSequencerKeysConfig() []utils.KeyConfig {
}
}

func getMockSequencerKeyConfig(rollappConfig config.RollappConfig) []utils.KeyConfig {
func GetMockSequencerKeyConfig(rollappConfig config.RollappConfig) []utils.KeyConfig {
return []utils.KeyConfig{
{
Dir: consts.ConfigDirName.Rollapp,
Expand Down
81 changes: 0 additions & 81 deletions cmd/keys/export/export.go

This file was deleted.

18 changes: 0 additions & 18 deletions cmd/keys/keys.go

This file was deleted.

118 changes: 0 additions & 118 deletions cmd/keys/list/list.go

This file was deleted.

54 changes: 54 additions & 0 deletions cmd/rollapp/keys/export/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package export

import (
"path/filepath"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/utils/bash"
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
"github.com/dymensionxyz/roller/utils/errorhandling"
)

func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "export",
Short: "Exports the private key of the sequencer key.",
Run: func(cmd *cobra.Command, args []string) {
home := cmd.Flag(utils.FlagNames.Home).Value.String()
rollerData, err := tomlconfig.LoadRollerConfig(home)
errorhandling.PrettifyErrorIfExists(err)

var kcs []utils.KeyConfig
if rollerData.HubData.ID != "mock" {
kcs = initconfig.GetSequencerKeysConfig()
} else {
kcs = initconfig.GetMockSequencerKeyConfig(rollerData)
}

kc := kcs[0]

expKeyArgs := []string{
"keys",
"export",
kc.ID,
"--keyring-backend",
"test",
"--keyring-dir",
filepath.Join(home, consts.ConfigDirName.HubKeys),
}

err = bash.ExecCommandWithInteractions(kc.ChainBinary, expKeyArgs...)
if err != nil {
pterm.Error.Println("failed to export private key: ", err)
return
}
},
}

return cmd
}
51 changes: 51 additions & 0 deletions cmd/rollapp/keys/import/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package importkeys

import (
"path/filepath"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/utils/bash"
)

func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "import <key-name> <priv-key-file-path>",
Args: cobra.ExactArgs(2),
Short: "Exports the private key of the sequencer key.",
Run: func(cmd *cobra.Command, args []string) {
home := cmd.Flag(utils.FlagNames.Home).Value.String()

keyName := args[0]
privKeyFilePath := args[1]
keyringDir := filepath.Join(home, consts.ConfigDirName.HubKeys)

expKeyArgs := []string{
"keys",
"import",
keyName,
privKeyFilePath,
"--keyring-backend",
"test",
"--keyring-dir",
keyringDir,
}

err := bash.ExecCommandWithInteractions(consts.Executables.Dymension, expKeyArgs...)
if err != nil {
pterm.Error.Println("failed to export private key: ", err)
return
}

pterm.Info.Printf(
"keys were imported into the test keyring, keyring-dir: %s",
keyringDir,
)
},
}

return cmd
}
23 changes: 23 additions & 0 deletions cmd/rollapp/keys/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package keys

import (
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/rollapp/keys/export"
importkeys "github.com/dymensionxyz/roller/cmd/rollapp/keys/import"
"github.com/dymensionxyz/roller/cmd/rollapp/keys/list"
"github.com/dymensionxyz/roller/cmd/rollapp/keys/showunarmoredprivkey"
)

func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "keys",
Short: "Commands for managing the roller different keys.",
}
cmd.AddCommand(list.Cmd())
cmd.AddCommand(showunarmoredprivkey.Cmd())
cmd.AddCommand(export.Cmd())
cmd.AddCommand(importkeys.Cmd())

return cmd
}
Loading

0 comments on commit 3d1b220

Please sign in to comment.