From bdfbacc16c9b8bb952a4c08ec0c6ffc9eccc5b43 Mon Sep 17 00:00:00 2001 From: Itay Levy Date: Tue, 4 Jul 2023 14:15:13 +0300 Subject: [PATCH 1/3] Added support for a custom sdk binary --- cmd/config/init/init.go | 4 +- cmd/config/init/keys.go | 77 +++++++++++++++----------------- cmd/consts/consts.go | 16 +------ cmd/keys/list/list.go | 7 ++- cmd/utils/fetch_accounts_data.go | 2 +- cmd/utils/keys.go | 45 +++++++++---------- 6 files changed, 67 insertions(+), 84 deletions(-) diff --git a/cmd/config/init/init.go b/cmd/config/init/init.go index 97d5438b..cdc192de 100644 --- a/cmd/config/init/init.go +++ b/cmd/config/init/init.go @@ -73,11 +73,13 @@ func InitCmd() *cobra.Command { spin.Suffix = " Initializing RollApp configuration files..." spin.Restart() /* ---------------------------- Initilize relayer --------------------------- */ + rollappPrefix, err := utils.GetAddressPrefix(initConfig.RollappBinary) + utils.PrettifyErrorIfExists(err) utils.PrettifyErrorIfExists(initializeRelayerConfig(ChainConfig{ ID: initConfig.RollappID, RPC: consts.DefaultRollappRPC, Denom: initConfig.Denom, - AddressPrefix: consts.AddressPrefixes.Rollapp, + AddressPrefix: rollappPrefix, }, ChainConfig{ ID: initConfig.HubData.ID, RPC: initConfig.HubData.RPC_URL, diff --git a/cmd/config/init/keys.go b/cmd/config/init/keys.go index ddee8136..45c7fcec 100644 --- a/cmd/config/init/keys.go +++ b/cmd/config/init/keys.go @@ -1,14 +1,12 @@ package initconfig import ( - "os/exec" - "path" - "path/filepath" - "strconv" - "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" "github.com/dymensionxyz/roller/config" + "os/exec" + "path" + "path/filepath" ) func generateKeys(rollappConfig config.RollappConfig) ([]utils.AddressData, error) { @@ -24,16 +22,12 @@ func generateKeys(rollappConfig config.RollappConfig) ([]utils.AddressData, erro } func generateSequencersKeys(initConfig config.RollappConfig) ([]utils.AddressData, error) { - keys := getSequencerKeysConfig() + keys := getSequencerKeysConfig(initConfig) addresses := make([]utils.AddressData, 0) for _, key := range keys { var address string var err error - if key.Prefix == consts.AddressPrefixes.Rollapp { - address, err = createAddressBinary(key, consts.Executables.RollappEVM, initConfig.Home) - } else { - address, err = createAddressBinary(key, consts.Executables.Dymension, initConfig.Home) - } + address, err = createAddressBinary(key, initConfig.Home) if err != nil { return nil, err } @@ -45,47 +39,46 @@ func generateSequencersKeys(initConfig config.RollappConfig) ([]utils.AddressDat return addresses, nil } -func getSequencerKeysConfig() []utils.CreateKeyConfig { - return []utils.CreateKeyConfig{ +func getSequencerKeysConfig(rollappConfig config.RollappConfig) []utils.KeyConfig { + return []utils.KeyConfig{ { - Dir: consts.ConfigDirName.HubKeys, - ID: consts.KeysIds.HubSequencer, - CoinType: consts.CoinTypes.EVM, - Algo: consts.AlgoTypes.Secp256k1, - Prefix: consts.AddressPrefixes.Hub, + Dir: consts.ConfigDirName.HubKeys, + ID: consts.KeysIds.HubSequencer, + ChainBinary: consts.Executables.Dymension, }, { - Dir: consts.ConfigDirName.Rollapp, - ID: consts.KeysIds.RollappSequencer, - CoinType: consts.CoinTypes.EVM, - Algo: consts.AlgoTypes.Ethsecp256k1, - Prefix: consts.AddressPrefixes.Rollapp, + Dir: consts.ConfigDirName.Rollapp, + ID: consts.KeysIds.RollappSequencer, + ChainBinary: rollappConfig.RollappBinary, }, } } -func getRelayerKeysConfig(rollappConfig config.RollappConfig) map[string]utils.CreateKeyConfig { - return map[string]utils.CreateKeyConfig{ +func getRelayerKeysConfig(rollappConfig config.RollappConfig) map[string]utils.KeyConfig { + return map[string]utils.KeyConfig{ consts.KeysIds.RollappRelayer: { - Dir: path.Join(rollappConfig.Home, consts.ConfigDirName.Relayer), - ID: consts.KeysIds.RollappRelayer, - CoinType: consts.CoinTypes.EVM, - Algo: consts.AlgoTypes.Ethsecp256k1, - Prefix: consts.AddressPrefixes.Rollapp, + Dir: path.Join(rollappConfig.Home, consts.ConfigDirName.Relayer), + ID: consts.KeysIds.RollappRelayer, + ChainBinary: rollappConfig.RollappBinary, }, consts.KeysIds.HubRelayer: { - Dir: path.Join(rollappConfig.Home, consts.ConfigDirName.Relayer), - ID: consts.KeysIds.HubRelayer, - CoinType: consts.CoinTypes.Cosmos, - Algo: consts.AlgoTypes.Secp256k1, - Prefix: consts.AddressPrefixes.Hub, + Dir: path.Join(rollappConfig.Home, consts.ConfigDirName.Relayer), + ID: consts.KeysIds.HubRelayer, + ChainBinary: consts.Executables.Dymension, }, } } -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), "--algo", keyConfig.Algo, "--output", "json") +func createAddressBinary(keyConfig utils.KeyConfig, home string) (string, error) { + args := []string{ + "keys", "add", keyConfig.ID, "--keyring-backend", "test", + "--keyring-dir", filepath.Join(home, keyConfig.Dir), + "--output", "json", + } + if keyConfig.ChainBinary == consts.Executables.Dymension { + args = append(args, "--algo", consts.AlgoTypes.Secp256k1) + } + createKeyCommand := exec.Command(keyConfig.ChainBinary, args...) out, err := utils.ExecBashCommand(createKeyCommand) if err != nil { return "", err @@ -125,7 +118,11 @@ func generateRelayerKeys(rollappConfig config.RollappConfig) ([]utils.AddressDat return relayerAddresses, err } -func getAddRlyKeyCmd(keyConfig utils.CreateKeyConfig, chainID string) *exec.Cmd { +func getAddRlyKeyCmd(keyConfig utils.KeyConfig, chainID string) *exec.Cmd { + var coinType = "118" + if keyConfig.ChainBinary == consts.Executables.RollappEVM { + coinType = "60" + } return exec.Command( consts.Executables.Relayer, "keys", @@ -135,6 +132,6 @@ func getAddRlyKeyCmd(keyConfig utils.CreateKeyConfig, chainID string) *exec.Cmd "--home", keyConfig.Dir, "--coin-type", - strconv.Itoa(int(keyConfig.CoinType)), + coinType, ) } diff --git a/cmd/consts/consts.go b/cmd/consts/consts.go index 73952cc9..edbcc2fa 100644 --- a/cmd/consts/consts.go +++ b/cmd/consts/consts.go @@ -40,13 +40,9 @@ var KeysIds = struct { } var AddressPrefixes = struct { - Hub string - Rollapp string - DA string + Hub string }{ - Rollapp: "ethm", - Hub: "dym", - DA: "celestia", + Hub: "dym", } var ConfigDirName = struct { @@ -61,14 +57,6 @@ var ConfigDirName = struct { HubKeys: "hub-keys", } -var CoinTypes = struct { - Cosmos uint32 - EVM uint32 -}{ - Cosmos: 118, - EVM: 60, -} - var AlgoTypes = struct { Secp256k1 string Ethsecp256k1 string diff --git a/cmd/keys/list/list.go b/cmd/keys/list/list.go index bd78feca..e1f89721 100644 --- a/cmd/keys/list/list.go +++ b/cmd/keys/list/list.go @@ -18,7 +18,6 @@ func Cmd() *cobra.Command { home := cmd.Flag(utils.FlagNames.Home).Value.String() rollappConfig, err := config.LoadConfigFromTOML(home) utils.PrettifyErrorIfExists(err) - addresses := make([]utils.AddressData, 0) damanager := datalayer.NewDAManager(rollappConfig.DA, rollappConfig.Home) @@ -30,7 +29,7 @@ func Cmd() *cobra.Command { Name: consts.KeysIds.DALightNode, }) } - hubSeqAddr, err := utils.GetAddressBinary(utils.GetKeyConfig{ + hubSeqAddr, err := utils.GetAddressBinary(utils.KeyConfig{ Dir: filepath.Join(rollappConfig.Home, consts.ConfigDirName.HubKeys), ID: consts.KeysIds.HubSequencer, }, consts.Executables.Dymension) @@ -39,10 +38,10 @@ func Cmd() *cobra.Command { Addr: hubSeqAddr, Name: consts.KeysIds.HubSequencer, }) - rollappSeqAddr, err := utils.GetAddressBinary(utils.GetKeyConfig{ + rollappSeqAddr, err := utils.GetAddressBinary(utils.KeyConfig{ Dir: filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp), ID: consts.KeysIds.RollappSequencer, - }, consts.Executables.RollappEVM) + }, rollappConfig.RollappBinary) utils.PrettifyErrorIfExists(err) addresses = append(addresses, utils.AddressData{ Addr: rollappSeqAddr, diff --git a/cmd/utils/fetch_accounts_data.go b/cmd/utils/fetch_accounts_data.go index 55e09845..45e39ee3 100644 --- a/cmd/utils/fetch_accounts_data.go +++ b/cmd/utils/fetch_accounts_data.go @@ -46,7 +46,7 @@ func GetHubRlyAccData(cfg config.RollappConfig) (*AccountData, error) { } func GetSequencerData(cfg config.RollappConfig) (*AccountData, error) { - sequencerAddress, err := GetAddressBinary(GetKeyConfig{ + sequencerAddress, err := GetAddressBinary(KeyConfig{ ID: consts.KeysIds.HubSequencer, Dir: filepath.Join(cfg.Home, consts.ConfigDirName.HubKeys), }, consts.Executables.Dymension) diff --git a/cmd/utils/keys.go b/cmd/utils/keys.go index 32dd0060..63b63746 100644 --- a/cmd/utils/keys.go +++ b/cmd/utils/keys.go @@ -28,20 +28,13 @@ func ParseAddressFromOutput(output bytes.Buffer) (string, error) { return key.Address, nil } -type GetKeyConfig struct { - Dir string - ID string +type KeyConfig struct { + Dir string + ID string + ChainBinary string } -type CreateKeyConfig struct { - Dir string - ID string - CoinType uint32 - Algo string - Prefix string -} - -func GetAddressBinary(keyConfig GetKeyConfig, binaryPath string) (string, error) { +func GetAddressBinary(keyConfig KeyConfig, binaryPath string) (string, error) { showKeyCommand := exec.Command( binaryPath, "keys", "show", keyConfig.ID, "--keyring-backend", "test", "--keyring-dir", keyConfig.Dir, "--output", "json", @@ -53,18 +46,6 @@ func GetAddressBinary(keyConfig GetKeyConfig, binaryPath string) (string, error) return ParseAddressFromOutput(output) } -func MergeMaps(map1, map2 map[string]string) map[string]string { - result := make(map[string]string) - for key, value := range map1 { - result[key] = value - } - for key, value := range map2 { - result[key] = value - } - - 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), @@ -106,3 +87,19 @@ func GetSequencerPubKey(rollappConfig config.RollappConfig) (string, error) { } return strings.ReplaceAll(strings.ReplaceAll(string(out), "\n", ""), "\\", ""), nil } + +func GetAddressPrefix(binaryPath string) (string, error) { + cmd := exec.Command(binaryPath, "debug", "addr", "ffffffffffffff", "--log_format", "json") + out, err := ExecBashCommand(cmd) + if err != nil { + return "", err + } + lines := strings.Split(out.String(), "\n") + for _, line := range lines { + if strings.HasPrefix(line, "Bech32 Acc:") { + prefix := strings.Split(strings.TrimSpace(strings.Split(line, ":")[1]), "1")[0] + return strings.TrimSpace(prefix), nil + } + } + return "", fmt.Errorf("could not find address prefix in binary debug command output") +} From 42012584071c237dba1770891cb4a469f604abc0 Mon Sep 17 00:00:00 2001 From: Itay Levy Date: Tue, 4 Jul 2023 18:38:52 +0300 Subject: [PATCH 2/3] Removed the json flag --- cmd/utils/keys.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/utils/keys.go b/cmd/utils/keys.go index 63b63746..bd01d5be 100644 --- a/cmd/utils/keys.go +++ b/cmd/utils/keys.go @@ -89,7 +89,7 @@ func GetSequencerPubKey(rollappConfig config.RollappConfig) (string, error) { } func GetAddressPrefix(binaryPath string) (string, error) { - cmd := exec.Command(binaryPath, "debug", "addr", "ffffffffffffff", "--log_format", "json") + cmd := exec.Command(binaryPath, "debug", "addr", "ffffffffffffff") out, err := ExecBashCommand(cmd) if err != nil { return "", err From ca38bc481a6980f89af9a06b3f436e8399ce20ae Mon Sep 17 00:00:00 2001 From: Itay Levy Date: Wed, 5 Jul 2023 14:49:40 +0300 Subject: [PATCH 3/3] Added todo to handle evm custom rollapps --- cmd/config/init/keys.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/config/init/keys.go b/cmd/config/init/keys.go index 45c7fcec..7942ba0e 100644 --- a/cmd/config/init/keys.go +++ b/cmd/config/init/keys.go @@ -119,6 +119,7 @@ func generateRelayerKeys(rollappConfig config.RollappConfig) ([]utils.AddressDat } func getAddRlyKeyCmd(keyConfig utils.KeyConfig, chainID string) *exec.Cmd { + // TODO: Add support for custom EVM rollapp binaries (#196) var coinType = "118" if keyConfig.ChainBinary == consts.Executables.RollappEVM { coinType = "60"