Skip to content

Commit

Permalink
feat: Add ability to list roller addresses with roller keys list co…
Browse files Browse the repository at this point in the history
…mmand (#83)
  • Loading branch information
ItayLevyOfficial authored Jun 25, 2023
1 parent cada290 commit b4c875e
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 82 deletions.
6 changes: 2 additions & 4 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions cmd/config/init/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 2 additions & 21 deletions cmd/config/init/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
3 changes: 2 additions & 1 deletion cmd/config/init/rollapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions cmd/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions cmd/keys/keys.go
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 45 additions & 0 deletions cmd/keys/list/list.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion cmd/register/bash_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
11 changes: 5 additions & 6 deletions cmd/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -34,4 +35,5 @@ func init() {
rootCmd.AddCommand(da_light_client.DALightClientCmd())
rootCmd.AddCommand(sequencer.SequencerCmd())
rootCmd.AddCommand(relayer.Cmd())
rootCmd.AddCommand(keys.Cmd())
}
4 changes: 3 additions & 1 deletion cmd/sequencer/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions cmd/utils/error_handling.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package utils

import (
"os"

"github.com/fatih/color"
"os"
)

func PrettifyErrorIfExists(err error) {
Expand Down
80 changes: 48 additions & 32 deletions cmd/utils/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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()
}
2 changes: 1 addition & 1 deletion test/config/init/testutils/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit b4c875e

Please sign in to comment.