From b4c875e2508713204adf6c3507713903bc1258d9 Mon Sep 17 00:00:00 2001 From: Itay Date: Sun, 25 Jun 2023 10:55:09 +0200 Subject: [PATCH] feat: Add ability to list roller addresses with `roller keys list` command (#83) --- cmd/config/init/init.go | 6 +- cmd/config/init/keys.go | 14 ++--- cmd/config/init/output.go | 23 +------- cmd/config/init/rollapp.go | 3 +- cmd/consts/consts.go | 2 + cmd/keys/keys.go | 15 +++++ cmd/keys/list/list.go | 45 +++++++++++++++ cmd/register/bash_commands.go | 2 +- cmd/register/register.go | 11 ++-- cmd/root.go | 2 + cmd/sequencer/start/start.go | 4 +- cmd/utils/error_handling.go | 3 +- cmd/utils/keys.go | 80 ++++++++++++++++----------- test/config/init/testutils/keys.go | 2 +- test/config/init/testutils/rollapp.go | 19 +++++-- 15 files changed, 149 insertions(+), 82 deletions(-) create mode 100644 cmd/keys/keys.go create mode 100644 cmd/keys/list/list.go diff --git a/cmd/config/init/init.go b/cmd/config/init/init.go index 02ecd75e..fbde94a9 100644 --- a/cmd/config/init/init.go +++ b/cmd/config/init/init.go @@ -3,11 +3,9 @@ package initconfig import ( "fmt" "github.com/dymensionxyz/roller/cmd/consts" - "os" - "path/filepath" - "github.com/dymensionxyz/roller/cmd/utils" "github.com/spf13/cobra" + "os" ) func InitCmd() *cobra.Command { @@ -49,7 +47,7 @@ func InitCmd() *cobra.Command { addresses, err := generateKeys(initConfig) utils.PrettifyErrorIfExists(err) utils.PrettifyErrorIfExists(initializeLightNodeConfig(initConfig)) - daAddress, err := utils.GetCelestiaAddress(filepath.Join(initConfig.Home, consts.ConfigDirName.DALightNode, consts.KeysDirName)) + daAddress, err := utils.GetCelestiaAddress(initConfig.Home) utils.PrettifyErrorIfExists(err) addresses[consts.KeyNames.DALightNode] = daAddress initializeRollappConfig(initConfig) diff --git a/cmd/config/init/keys.go b/cmd/config/init/keys.go index 28bb3b62..9ea06844 100644 --- a/cmd/config/init/keys.go +++ b/cmd/config/init/keys.go @@ -41,10 +41,10 @@ func generateSequencersKeys(initConfig utils.RollappConfig) (map[string]string, return addresses, nil } -func getSequencerKeysConfig() []utils.KeyConfig { - return []utils.KeyConfig{ +func getSequencerKeysConfig() []utils.CreateKeyConfig { + return []utils.CreateKeyConfig{ { - Dir: consts.ConfigDirName.Rollapp, + Dir: consts.ConfigDirName.HubKeys, ID: consts.KeyNames.HubSequencer, CoinType: consts.CoinTypes.Cosmos, Prefix: consts.AddressPrefixes.Hub, @@ -58,8 +58,8 @@ func getSequencerKeysConfig() []utils.KeyConfig { } } -func getRelayerKeysConfig(rollappConfig utils.RollappConfig) map[string]utils.KeyConfig { - return map[string]utils.KeyConfig{ +func getRelayerKeysConfig(rollappConfig utils.RollappConfig) map[string]utils.CreateKeyConfig { + return map[string]utils.CreateKeyConfig{ consts.KeyNames.RollappRelayer: { Dir: path.Join(rollappConfig.Home, consts.ConfigDirName.Relayer), ID: consts.KeyNames.RollappRelayer, @@ -75,7 +75,7 @@ func getRelayerKeysConfig(rollappConfig utils.RollappConfig) map[string]utils.Ke } } -func createAddressBinary(keyConfig utils.KeyConfig, binaryPath string, home string) (string, error) { +func createAddressBinary(keyConfig utils.CreateKeyConfig, binaryPath string, home string) (string, error) { createKeyCommand := exec.Command(binaryPath, "keys", "add", keyConfig.ID, "--keyring-backend", "test", "--keyring-dir", filepath.Join(home, keyConfig.Dir), "--output", "json") out, err := utils.ExecBashCommand(createKeyCommand) @@ -111,7 +111,7 @@ func generateRelayerKeys(rollappConfig utils.RollappConfig) (map[string]string, return relayerAddresses, err } -func getAddRlyKeyCmd(keyConfig utils.KeyConfig, chainID string) *exec.Cmd { +func getAddRlyKeyCmd(keyConfig utils.CreateKeyConfig, chainID string) *exec.Cmd { return exec.Command( consts.Executables.Relayer, consts.KeysDirName, diff --git a/cmd/config/init/output.go b/cmd/config/init/output.go index bb87d86e..ae0f5745 100644 --- a/cmd/config/init/output.go +++ b/cmd/config/init/output.go @@ -2,30 +2,11 @@ package initconfig import ( "fmt" - "os" - - "github.com/dymensionxyz/roller/cmd/consts" - "github.com/olekukonko/tablewriter" + "github.com/dymensionxyz/roller/cmd/utils" ) func printInitOutput(addresses map[string]string, rollappId string) { fmt.Printf("šŸ’ˆ RollApp '%s' configuration files have been successfully generated on your local machine. Congratulations!\n\n", rollappId) - - fmt.Printf("šŸ”‘ Addresses:\n\n") - - data := [][]string{ - {"Celestia", addresses[consts.KeyNames.DALightNode]}, - {"Sequencer", addresses[consts.KeyNames.HubSequencer]}, - {"Relayer, Hub", addresses[consts.KeyNames.HubRelayer]}, - {"Relayer, RollApp", addresses[consts.KeyNames.RollappRelayer]}, - } - - table := tablewriter.NewWriter(os.Stdout) - table.SetAlignment(tablewriter.ALIGN_LEFT) - table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) - table.SetBorder(false) - table.AppendBulk(data) - table.Render() - + utils.PrintAddresses(addresses) fmt.Printf("\nšŸ”” Please fund these addresses to register and run the rollapp.\n") } diff --git a/cmd/config/init/rollapp.go b/cmd/config/init/rollapp.go index 10f1c3b3..9879c9d0 100644 --- a/cmd/config/init/rollapp.go +++ b/cmd/config/init/rollapp.go @@ -11,7 +11,8 @@ import ( ) func initializeRollappConfig(initConfig utils.RollappConfig) { - initRollappCmd := exec.Command(initConfig.RollappBinary, "init", consts.KeyNames.HubSequencer, "--chain-id", initConfig.RollappID, "--home", filepath.Join(initConfig.Home, consts.ConfigDirName.Rollapp)) + initRollappCmd := exec.Command(initConfig.RollappBinary, "init", consts.KeyNames.HubSequencer, "--chain-id", + initConfig.RollappID, "--home", filepath.Join(initConfig.Home, consts.ConfigDirName.Rollapp)) err := initRollappCmd.Run() if err != nil { panic(err) diff --git a/cmd/consts/consts.go b/cmd/consts/consts.go index 57d5ae59..0c3c685f 100644 --- a/cmd/consts/consts.go +++ b/cmd/consts/consts.go @@ -48,10 +48,12 @@ var ConfigDirName = struct { Rollapp string Relayer string DALightNode string + HubKeys string }{ Rollapp: "rollapp", Relayer: "relayer", DALightNode: "da-light-node", + HubKeys: "hub-keys", } var CoinTypes = struct { diff --git a/cmd/keys/keys.go b/cmd/keys/keys.go new file mode 100644 index 00000000..edf58bf8 --- /dev/null +++ b/cmd/keys/keys.go @@ -0,0 +1,15 @@ +package keys + +import ( + "github.com/dymensionxyz/roller/cmd/keys/list" + "github.com/spf13/cobra" +) + +func Cmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "keys", + Short: "Commands for managing the roller different keys.", + } + cmd.AddCommand(list.Cmd()) + return cmd +} diff --git a/cmd/keys/list/list.go b/cmd/keys/list/list.go new file mode 100644 index 00000000..a3d324d8 --- /dev/null +++ b/cmd/keys/list/list.go @@ -0,0 +1,45 @@ +package list + +import ( + "github.com/dymensionxyz/roller/cmd/consts" + "github.com/dymensionxyz/roller/cmd/utils" + "github.com/spf13/cobra" + "path/filepath" +) + +func Cmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List all the addresses of roller on the local machine.", + Run: func(cmd *cobra.Command, args []string) { + home := cmd.Flag(utils.FlagNames.Home).Value.String() + rollappConfig, err := utils.LoadConfigFromTOML(home) + utils.PrettifyErrorIfExists(err) + daAddr, err := utils.GetCelestiaAddress(rollappConfig.Home) + utils.PrettifyErrorIfExists(err) + addresses := map[string]string{} + addresses[consts.KeyNames.DALightNode] = daAddr + hubSeqAddr, err := utils.GetAddressBinary(utils.GetKeyConfig{ + Dir: filepath.Join(rollappConfig.Home, consts.ConfigDirName.HubKeys), + ID: consts.KeyNames.HubSequencer, + }, consts.Executables.Dymension) + utils.PrettifyErrorIfExists(err) + addresses[consts.KeyNames.HubSequencer] = hubSeqAddr + rollappSeqAddr, err := utils.GetAddressBinary(utils.GetKeyConfig{ + Dir: filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp), + ID: consts.KeyNames.RollappSequencer, + }, consts.Executables.RollappEVM) + utils.PrettifyErrorIfExists(err) + addresses[consts.KeyNames.RollappSequencer] = rollappSeqAddr + hubRlyAddr, err := utils.GetRelayerAddress(rollappConfig.Home, rollappConfig.HubData.ID) + utils.PrettifyErrorIfExists(err) + addresses[consts.KeyNames.HubRelayer] = hubRlyAddr + rollappRlyAddr, err := utils.GetRelayerAddress(rollappConfig.Home, rollappConfig.RollappID) + utils.PrettifyErrorIfExists(err) + addresses[consts.KeyNames.RollappRelayer] = rollappRlyAddr + utils.PrintAddresses(addresses) + }, + } + utils.AddGlobalFlags(cmd) + return cmd +} diff --git a/cmd/register/bash_commands.go b/cmd/register/bash_commands.go index 366e77da..f770de23 100644 --- a/cmd/register/bash_commands.go +++ b/cmd/register/bash_commands.go @@ -59,7 +59,7 @@ func getCommonDymdTxFlags(rollappConfig utils.RollappConfig) []string { txArgs := []string{ "--from", consts.KeyNames.HubSequencer, "--keyring-backend", "test", - "--keyring-dir", filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp), + "--keyring-dir", filepath.Join(rollappConfig.Home, consts.ConfigDirName.HubKeys), "--yes", "--broadcast-mode", "block", "--chain-id", rollappConfig.HubData.ID, } return append(commonFlags, txArgs...) diff --git a/cmd/register/register.go b/cmd/register/register.go index a7f3ce10..3c64e038 100644 --- a/cmd/register/register.go +++ b/cmd/register/register.go @@ -61,13 +61,12 @@ func handleStdErr(stderr bytes.Buffer, rollappConfig utils.RollappConfig) error stderrStr := stderr.String() if len(stderrStr) > 0 { if strings.Contains(stderrStr, "key not found") { - sequencerAddress, err := utils.GetAddress( - utils.KeyConfig{ - ID: consts.KeyNames.HubSequencer, - Prefix: consts.AddressPrefixes.Hub, - Dir: filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp), - CoinType: consts.CoinTypes.Cosmos, + sequencerAddress, err := utils.GetAddressBinary( + utils.GetKeyConfig{ + ID: consts.KeyNames.HubSequencer, + Dir: filepath.Join(rollappConfig.Home, consts.ConfigDirName.HubKeys), }, + consts.Executables.Dymension, ) if err != nil { return err diff --git a/cmd/root.go b/cmd/root.go index 7d7611c8..7d5b4a4c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,6 +5,7 @@ import ( "github.com/dymensionxyz/roller/cmd/config" da_light_client "github.com/dymensionxyz/roller/cmd/da-light-client" + "github.com/dymensionxyz/roller/cmd/keys" "github.com/dymensionxyz/roller/cmd/register" "github.com/dymensionxyz/roller/cmd/relayer" "github.com/dymensionxyz/roller/cmd/sequencer" @@ -34,4 +35,5 @@ func init() { rootCmd.AddCommand(da_light_client.DALightClientCmd()) rootCmd.AddCommand(sequencer.SequencerCmd()) rootCmd.AddCommand(relayer.Cmd()) + rootCmd.AddCommand(keys.Cmd()) } diff --git a/cmd/sequencer/start/start.go b/cmd/sequencer/start/start.go index 2cb3e0d5..9cd93516 100644 --- a/cmd/sequencer/start/start.go +++ b/cmd/sequencer/start/start.go @@ -55,9 +55,11 @@ func getStartRollapCmd(rollappConfig utils.RollappConfig, lightNodeEndpoint stri daConfig := fmt.Sprintf(`{"base_url": "%s", "timeout": 60000000000, "fee":20000, "gas_limit": 20000000, "namespace_id":[0,0,0,0,0,0,255,255]}`, lightNodeEndpoint) rollappConfigDir := filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp) + hubKeysDir := filepath.Join(rollappConfig.Home, consts.ConfigDirName.HubKeys) // TODO: Update the gas_fees to 2000000udym before 35-c launch. - settlementConfig := fmt.Sprintf(`{"node_address": "%s", "rollapp_id": "%s", "dym_account_name": "%s", "keyring_home_dir": "%s", "keyring_backend":"test", "gas_fees": "0udym"}`, rollappConfig.HubData.RPC_URL, rollappConfig.RollappID, consts.KeyNames.HubSequencer, rollappConfigDir) + settlementConfig := fmt.Sprintf(`{"node_address": "%s", "rollapp_id": "%s", "dym_account_name": "%s", "keyring_home_dir": "%s", "keyring_backend":"test", "gas_fees": "0udym"}`, + rollappConfig.HubData.RPC_URL, rollappConfig.RollappID, consts.KeyNames.HubSequencer, hubKeysDir) return exec.Command( rollappConfig.RollappBinary, "start", diff --git a/cmd/utils/error_handling.go b/cmd/utils/error_handling.go index 9eaa8ac0..b84c305c 100644 --- a/cmd/utils/error_handling.go +++ b/cmd/utils/error_handling.go @@ -1,9 +1,8 @@ package utils import ( - "os" - "github.com/fatih/color" + "os" ) func PrettifyErrorIfExists(err error) { diff --git a/cmd/utils/keys.go b/cmd/utils/keys.go index 4943a4d2..b673539b 100644 --- a/cmd/utils/keys.go +++ b/cmd/utils/keys.go @@ -3,11 +3,13 @@ package utils import ( "bytes" "encoding/json" + "fmt" + "github.com/olekukonko/tablewriter" + "os" "os/exec" + "path/filepath" + "strings" - "github.com/cosmos/cosmos-sdk/crypto/keyring" - "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/bech32" "github.com/dymensionxyz/roller/cmd/consts" ) @@ -24,10 +26,11 @@ func ParseAddressFromOutput(output bytes.Buffer) (string, error) { return key.Address, nil } -func GetCelestiaAddress(keyringDir string) (string, error) { +func GetCelestiaAddress(rollerRoot string) (string, error) { + daKeysDir := filepath.Join(rollerRoot, consts.ConfigDirName.DALightNode, consts.KeysDirName) cmd := exec.Command( - consts.Executables.CelKey, - "show", consts.KeyNames.DALightNode, "--node.type", "light", "--keyring-dir", keyringDir, "--keyring-backend", "test", "--output", "json", + consts.Executables.CelKey, "show", consts.KeyNames.DALightNode, "--node.type", "light", "--keyring-dir", + daKeysDir, "--keyring-backend", "test", "--output", "json", ) output, err := ExecBashCommand(cmd) if err != nil { @@ -37,42 +40,28 @@ func GetCelestiaAddress(keyringDir string) (string, error) { return address, err } -type KeyConfig struct { +type GetKeyConfig struct { + Dir string + ID string +} + +type CreateKeyConfig struct { Dir string ID string CoinType uint32 Prefix string } -func KeyInfoToBech32Address(info keyring.Info, prefix string) (string, error) { - pk := info.GetPubKey() - addr := types.AccAddress(pk.Address()) - bech32Address, err := bech32.ConvertAndEncode(prefix, addr.Bytes()) - if err != nil { - return "", err - } - return bech32Address, nil -} - -func GetAddress(keyConfig KeyConfig) (string, error) { - kr, err := keyring.New( - "", - keyring.BackendTest, - keyConfig.Dir, - nil, +func GetAddressBinary(keyConfig GetKeyConfig, binaryPath string) (string, error) { + showKeyCommand := exec.Command( + binaryPath, "keys", "show", keyConfig.ID, "--keyring-backend", "test", "--keyring-dir", keyConfig.Dir, + "--output", "json", ) + output, err := ExecBashCommand(showKeyCommand) if err != nil { return "", err } - keyInfo, err := kr.Key(keyConfig.ID) - if err != nil { - return "", err - } - formattedAddress, err := KeyInfoToBech32Address(keyInfo, keyConfig.Prefix) - if err != nil { - return "", err - } - return formattedAddress, nil + return ParseAddressFromOutput(output) } func MergeMaps(map1, map2 map[string]string) map[string]string { @@ -86,3 +75,30 @@ func MergeMaps(map1, map2 map[string]string) map[string]string { return result } + +func GetRelayerAddress(home string, chainID string) (string, error) { + showKeyCmd := exec.Command( + consts.Executables.Relayer, "keys", "show", chainID, "--home", filepath.Join(home, consts.ConfigDirName.Relayer), + ) + out, err := ExecBashCommand(showKeyCmd) + return strings.TrimSuffix(out.String(), "\n"), err +} + +func PrintAddresses(addresses map[string]string) { + fmt.Printf("šŸ”‘ Addresses:\n\n") + + data := [][]string{ + {"Celestia", addresses[consts.KeyNames.DALightNode]}, + {"Sequencer, Hub", addresses[consts.KeyNames.HubSequencer]}, + {"Sequencer, Rollapp", addresses[consts.KeyNames.RollappSequencer]}, + {"Relayer, Hub", addresses[consts.KeyNames.HubRelayer]}, + {"Relayer, RollApp", addresses[consts.KeyNames.RollappRelayer]}, + } + + table := tablewriter.NewWriter(os.Stdout) + table.SetAlignment(tablewriter.ALIGN_LEFT) + table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) + table.SetBorder(false) + table.AppendBulk(data) + table.Render() +} diff --git a/test/config/init/testutils/keys.go b/test/config/init/testutils/keys.go index fab6431d..ce07c41b 100644 --- a/test/config/init/testutils/keys.go +++ b/test/config/init/testutils/keys.go @@ -17,7 +17,7 @@ const innerKeysDirName = "keyring-test" const addressPattern = `.*\.address` func ClearKeys(root string) error { - keyDirs := []string{getLightNodeKeysDir(root), getRelayerKeysDir(root), getRollappKeysDir(root)} + keyDirs := []string{getLightNodeKeysDir(root), getRelayerKeysDir(root), getRollappKeysDir(root), getHubKeysDir(root)} for _, dir := range keyDirs { if err := os.RemoveAll(dir); err != nil { return err diff --git a/test/config/init/testutils/rollapp.go b/test/config/init/testutils/rollapp.go index 035db6b2..a848c0dd 100644 --- a/test/config/init/testutils/rollapp.go +++ b/test/config/init/testutils/rollapp.go @@ -14,21 +14,28 @@ func getRollappKeysDir(root string) string { return filepath.Join(root, consts.ConfigDirName.Rollapp, innerKeysDirName) } +func getHubKeysDir(root string) string { + return filepath.Join(root, consts.ConfigDirName.HubKeys, innerKeysDirName) +} + func VerifyRollappKeys(root string) error { rollappKeysDir := getRollappKeysDir(root) sequencerKeyInfoPath := filepath.Join(rollappKeysDir, consts.KeyNames.RollappSequencer+".info") if err := verifyFileExists(sequencerKeyInfoPath); err != nil { return err } - relayerKeyInfoPath := filepath.Join(rollappKeysDir, consts.KeyNames.HubSequencer+".info") + hubKeysDir := getHubKeysDir(root) + relayerKeyInfoPath := filepath.Join(hubKeysDir, consts.KeyNames.HubSequencer+".info") if err := verifyFileExists(relayerKeyInfoPath); err != nil { return err } - for i := 0; i < 2; i++ { - err := verifyAndRemoveFilePattern(addressPattern, rollappKeysDir) - if err != nil { - return err - } + err := verifyAndRemoveFilePattern(addressPattern, rollappKeysDir) + if err != nil { + return err + } + err = verifyAndRemoveFilePattern(addressPattern, hubKeysDir) + if err != nil { + return err } nodeKeyPath := getNodeKeyPath(root) if err := verifyFileExists(nodeKeyPath); err != nil {