Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add option to export Celestia private key #242

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/config/init/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions cmd/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
51 changes: 25 additions & 26 deletions cmd/keys/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package export

import (
"fmt"
"os/exec"
datalayer "github.com/dymensionxyz/roller/data_layer"
"path/filepath"
"strings"

Expand All @@ -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 <key-id>",
Short: fmt.Sprintf("Exports the private key of the given key id. The supported keys are %s",
strings.Join(supportedKeys, ", ")),
Use: "export <key-id>",
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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why any keyID does the DA thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand

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, ", ")))
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion cmd/keys/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
12 changes: 12 additions & 0 deletions cmd/utils/bash_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions cmd/utils/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
13 changes: 11 additions & 2 deletions data_layer/celestia/celestia.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
})
Expand Down
2 changes: 2 additions & 0 deletions data_layer/da_layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type DataLayer interface {
SetRPCEndpoint(string)
GetNetworkName() string
GetStatus(c config.RollappConfig) string
GetKeyName() string
GetExportKeyCmd() *exec.Cmd
}

type DAManager struct {
Expand Down
8 changes: 8 additions & 0 deletions data_layer/damock/damock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand Down Expand Up @@ -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"
}