From f280df268c6d2648cb0a4e26876db9dfcf8866d0 Mon Sep 17 00:00:00 2001 From: Itay Levy Date: Tue, 11 Jul 2023 13:30:19 +0300 Subject: [PATCH] Added option to export celestia private key --- cmd/config/init/init.go | 2 +- cmd/config/init/output.go | 2 +- cmd/consts/consts.go | 5 +--- cmd/keys/export/export.go | 51 ++++++++++++++++----------------- cmd/keys/list/list.go | 2 +- cmd/utils/bash_commands.go | 12 ++++++++ cmd/utils/keys.go | 16 +++++++++++ data_layer/celestia/celestia.go | 13 +++++++-- data_layer/da_layer.go | 2 ++ data_layer/damock/damock.go | 8 ++++++ 10 files changed, 78 insertions(+), 35 deletions(-) diff --git a/cmd/config/init/init.go b/cmd/config/init/init.go index 40dbea61..034f8fcc 100644 --- a/cmd/config/init/init.go +++ b/cmd/config/init/init.go @@ -137,7 +137,7 @@ func runInit(cmd *cobra.Command, args []string) error { if daAddress != "" { addresses = append(addresses, utils.AddressData{ - Name: consts.KeysIds.DALightNode, + Name: damanager.GetKeyName(), Addr: daAddress, }) } diff --git a/cmd/config/init/output.go b/cmd/config/init/output.go index 6ec13298..285a4e29 100644 --- a/cmd/config/init/output.go +++ b/cmd/config/init/output.go @@ -23,7 +23,7 @@ func formatAddresses(rollappConfig config.RollappConfig, addresses []utils.Addre requireFundingKeys := map[string]string{ consts.KeysIds.HubSequencer: fmt.Sprintf("Sequencer, %s Hub", rollappConfig.HubData.ID), consts.KeysIds.HubRelayer: fmt.Sprintf("Relayer, %s Hub", rollappConfig.HubData.ID), - consts.KeysIds.DALightNode: fmt.Sprintf("DA, %s Network", damanager.GetNetworkName()), + damanager.GetKeyName(): fmt.Sprintf("DA, %s Network", damanager.GetNetworkName()), } filteredAddresses := make([]utils.AddressData, 0) for _, address := range addresses { diff --git a/cmd/consts/consts.go b/cmd/consts/consts.go index 6ffa7b66..e4e6b98a 100644 --- a/cmd/consts/consts.go +++ b/cmd/consts/consts.go @@ -29,15 +29,12 @@ var KeysIds = struct { HubSequencer string RollappSequencer string RollappRelayer string - DALightNode string HubRelayer string }{ HubSequencer: "hub_sequencer", RollappSequencer: "rollapp_sequencer", RollappRelayer: "relayer-rollapp-key", - //TODO(#76): da key name should be more generic - DALightNode: "my_celes_key", - HubRelayer: "relayer-hub-key", + HubRelayer: "relayer-hub-key", } var AddressPrefixes = struct { diff --git a/cmd/keys/export/export.go b/cmd/keys/export/export.go index b6c86514..ab6d5b82 100644 --- a/cmd/keys/export/export.go +++ b/cmd/keys/export/export.go @@ -2,7 +2,7 @@ package export import ( "fmt" - "os/exec" + datalayer "github.com/dymensionxyz/roller/data_layer" "path/filepath" "strings" @@ -12,34 +12,38 @@ import ( "github.com/spf13/cobra" ) -var supportedKeys = []string{ - consts.KeysIds.HubSequencer, - consts.KeysIds.RollappSequencer, -} - func Cmd() *cobra.Command { cmd := &cobra.Command{ - Use: "export ", - Short: fmt.Sprintf("Exports the private key of the given key id. The supported keys are %s", - strings.Join(supportedKeys, ", ")), + Use: "export ", + Short: "Exports the private key of the given key id.", Run: func(cmd *cobra.Command, args []string) { home := cmd.Flag(utils.FlagNames.Home).Value.String() - config, err := config.LoadConfigFromTOML(home) + rlpCfg, err := config.LoadConfigFromTOML(home) utils.PrettifyErrorIfExists(err) - + var supportedKeys = []string{ + consts.KeysIds.HubSequencer, + consts.KeysIds.RollappSequencer, + } + damanager := datalayer.NewDAManager(rlpCfg.DA, rlpCfg.Home) + if damanager.GetKeyName() != "" { + supportedKeys = append(supportedKeys, damanager.GetKeyName()) + } keyID := args[0] if keyID == consts.KeysIds.HubSequencer { - exportKeyCmd := getExportKeyCmdBinary(keyID, filepath.Join(home, consts.ConfigDirName.HubKeys), + exportKeyCmd := utils.GetExportKeyCmdBinary(keyID, filepath.Join(home, consts.ConfigDirName.HubKeys), consts.Executables.Dymension) out, err := utils.ExecBashCommand(exportKeyCmd) - utils.PrettifyErrorIfExists(err) - fmt.Println(out.String()) + printKeyOutput(out.String(), err) } else if keyID == consts.KeysIds.RollappSequencer { - exportKeyCmd := getExportKeyCmdBinary(keyID, filepath.Join(home, consts.ConfigDirName.Rollapp), - config.RollappBinary) + exportKeyCmd := utils.GetExportKeyCmdBinary(keyID, filepath.Join(home, consts.ConfigDirName.Rollapp), + rlpCfg.RollappBinary) out, err := utils.ExecBashCommand(exportKeyCmd) - utils.PrettifyErrorIfExists(err) - fmt.Println(out.String()) + printKeyOutput(out.String(), err) + } else if keyID != "" && keyID == damanager.GetKeyName() { + exportKeyCmd := damanager.GetExportKeyCmd() + // TODO: make more generic. need it because cel-key write the output to stderr for some reason + out, err := utils.ExecBashCommandWithStdErr(exportKeyCmd) + printKeyOutput(out.String(), err) } else { utils.PrettifyErrorIfExists(fmt.Errorf("invalid key id: %s. The supported keys are %s", keyID, strings.Join(supportedKeys, ", "))) @@ -51,12 +55,7 @@ func Cmd() *cobra.Command { return cmd } -func getExportKeyCmdBinary(keyID, keyringDir, binary string) *exec.Cmd { - flags := getExportKeyFlags(keyringDir) - cmdStr := fmt.Sprintf("yes | %s keys export %s %s", binary, keyID, flags) - return exec.Command("bash", "-c", cmdStr) -} - -func getExportKeyFlags(keyringDir string) string { - return fmt.Sprintf("--keyring-backend test --keyring-dir %s --unarmored-hex --unsafe", keyringDir) +func printKeyOutput(output string, err error) { + utils.PrettifyErrorIfExists(err) + fmt.Printf("🔑 Unarmored Hex Private Key: %s", output) } diff --git a/cmd/keys/list/list.go b/cmd/keys/list/list.go index e1f89721..65771ac3 100644 --- a/cmd/keys/list/list.go +++ b/cmd/keys/list/list.go @@ -26,7 +26,7 @@ func Cmd() *cobra.Command { if daAddr != "" { addresses = append(addresses, utils.AddressData{ Addr: daAddr, - Name: consts.KeysIds.DALightNode, + Name: damanager.GetKeyName(), }) } hubSeqAddr, err := utils.GetAddressBinary(utils.KeyConfig{ diff --git a/cmd/utils/bash_commands.go b/cmd/utils/bash_commands.go index aa340c89..5bf2b921 100644 --- a/cmd/utils/bash_commands.go +++ b/cmd/utils/bash_commands.go @@ -82,6 +82,18 @@ func ExecBashCommand(cmd *exec.Cmd) (bytes.Buffer, error) { return stdout, nil } +func ExecBashCommandWithStdErr(cmd *exec.Cmd) (bytes.Buffer, error) { + var stderr bytes.Buffer + var stdout bytes.Buffer + cmd.Stderr = &stderr + cmd.Stdout = &stdout + err := cmd.Run() + if err != nil { + return stdout, fmt.Errorf("command execution failed: %w, stderr: %s", err, stderr.String()) + } + return stderr, nil +} + func ExecBashCmdWithOSOutput(cmd *exec.Cmd, options ...CommandOption) error { for _, option := range options { option(cmd) diff --git a/cmd/utils/keys.go b/cmd/utils/keys.go index bd01d5be..cc86a1ac 100644 --- a/cmd/utils/keys.go +++ b/cmd/utils/keys.go @@ -103,3 +103,19 @@ func GetAddressPrefix(binaryPath string) (string, error) { } return "", fmt.Errorf("could not find address prefix in binary debug command output") } + +func GetExportKeyCmdBinary(keyID, keyringDir, binary string) *exec.Cmd { + flags := getExportKeyFlags(keyringDir) + var commandStr string + if binary == consts.Executables.CelKey { + commandStr = fmt.Sprintf("%s export %s %s", binary, keyID, flags) + } else { + commandStr = fmt.Sprintf("%s keys export %s %s", binary, keyID, flags) + } + cmdStr := fmt.Sprintf("yes | %s", commandStr) + return exec.Command("bash", "-c", cmdStr) +} + +func getExportKeyFlags(keyringDir string) string { + return fmt.Sprintf("--keyring-backend test --keyring-dir %s --unarmored-hex --unsafe", keyringDir) +} diff --git a/data_layer/celestia/celestia.go b/data_layer/celestia/celestia.go index 4a845f72..a0f00282 100644 --- a/data_layer/celestia/celestia.go +++ b/data_layer/celestia/celestia.go @@ -65,7 +65,7 @@ func (c *Celestia) GetLightNodeEndpoint() string { func (c *Celestia) GetDAAccountAddress() (string, error) { daKeysDir := filepath.Join(c.Root, consts.ConfigDirName.DALightNode, consts.KeysDirName) cmd := exec.Command( - consts.Executables.CelKey, "show", consts.KeysIds.DALightNode, "--node.type", "light", "--keyring-dir", + consts.Executables.CelKey, "show", c.GetKeyName(), "--node.type", "light", "--keyring-dir", daKeysDir, "--keyring-backend", "test", "--output", "json", ) output, err := utils.ExecBashCommand(cmd) @@ -121,6 +121,15 @@ func (c *Celestia) GetDAAccData(cfg config.RollappConfig) ([]utils.AccountData, return []utils.AccountData{*celAddress}, err } +func (c *Celestia) GetKeyName() string { + return "my_celes_key" +} + +func (c *Celestia) GetExportKeyCmd() *exec.Cmd { + return utils.GetExportKeyCmdBinary(c.GetKeyName(), filepath.Join(c.Root, consts.ConfigDirName.DALightNode, "keys"), + consts.Executables.CelKey) +} + func (c *Celestia) CheckDABalance() ([]utils.NotFundedAddressData, error) { accData, err := c.getDAAccData(config.RollappConfig{}) if err != nil { @@ -132,7 +141,7 @@ func (c *Celestia) CheckDABalance() ([]utils.NotFundedAddressData, error) { Address: accData.Address, CurrentBalance: accData.Balance.Amount, RequiredBalance: lcMinBalance, - KeyName: consts.KeysIds.DALightNode, + KeyName: c.GetKeyName(), Denom: consts.Denoms.Celestia, Network: DefaultCelestiaNetwork, }) diff --git a/data_layer/da_layer.go b/data_layer/da_layer.go index f503dbf8..7f27dd3c 100644 --- a/data_layer/da_layer.go +++ b/data_layer/da_layer.go @@ -19,6 +19,8 @@ type DataLayer interface { SetRPCEndpoint(string) GetNetworkName() string GetStatus(c config.RollappConfig) string + GetKeyName() string + GetExportKeyCmd() *exec.Cmd } type DAManager struct { diff --git a/data_layer/damock/damock.go b/data_layer/damock/damock.go index 74676217..8dc012b0 100644 --- a/data_layer/damock/damock.go +++ b/data_layer/damock/damock.go @@ -16,6 +16,10 @@ func (d *DAMock) GetStatus(c config.RollappConfig) string { return "" } +func (d *DAMock) GetExportKeyCmd() *exec.Cmd { + return nil +} + func NewDAMock() *DAMock { return &DAMock{} } @@ -55,6 +59,10 @@ func (d *DAMock) GetLightNodeEndpoint() string { func (d *DAMock) SetRPCEndpoint(string) { } +func (c *DAMock) GetKeyName() string { + return "" +} + func (d *DAMock) GetNetworkName() string { return "mock" }