diff --git a/cmd/config/init/init.go b/cmd/config/init/init.go index 7b45305e..b74f65d5 100644 --- a/cmd/config/init/init.go +++ b/cmd/config/init/init.go @@ -2,12 +2,13 @@ package initconfig import ( "fmt" + "os" + "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" "github.com/dymensionxyz/roller/config" datalayer "github.com/dymensionxyz/roller/data_layer" "github.com/spf13/cobra" - "os" ) func InitCmd() *cobra.Command { @@ -99,11 +100,13 @@ func runInit(cmd *cobra.Command, args []string) error { spin.Suffix = " Initializing RollApp configuration files..." spin.Restart() /* ---------------------------- Initialize relayer --------------------------- */ + rollappPrefix, err := utils.GetAddressPrefix(initConfig.RollappBinary) + utils.PrettifyErrorIfExists(err) err = 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..7942ba0e 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,12 @@ 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 { + // TODO: Add support for custom EVM rollapp binaries (#196) + var coinType = "118" + if keyConfig.ChainBinary == consts.Executables.RollappEVM { + coinType = "60" + } return exec.Command( consts.Executables.Relayer, "keys", @@ -135,6 +133,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 17c5bb9f..6130ea18 100644 --- a/cmd/utils/fetch_accounts_data.go +++ b/cmd/utils/fetch_accounts_data.go @@ -61,7 +61,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..bd01d5be 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") + 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") +}