From 74846cce3d381e10a5f7221ab0151059ccac2b66 Mon Sep 17 00:00:00 2001 From: artpav <19916123+artemijspavlovs@users.noreply.github.com> Date: Sun, 24 Nov 2024 09:10:58 +0200 Subject: [PATCH] fix: support os keyring for relayer whitelisting (#1129) --- cmd/relayer/setup/setup.go | 5 +- .../sequencer/bond/decrease/decrease.go | 10 +- utils/filesystem/filesystem.go | 16 +++ utils/keys/relayer.go | 117 +++++++++--------- utils/relayer/relayer.go | 24 ++-- utils/sequencer/sequencer.go | 54 ++++++-- 6 files changed, 134 insertions(+), 92 deletions(-) diff --git a/cmd/relayer/setup/setup.go b/cmd/relayer/setup/setup.go index 026158ef..f1b3d61f 100644 --- a/cmd/relayer/setup/setup.go +++ b/cmd/relayer/setup/setup.go @@ -46,7 +46,7 @@ func Cmd() *cobra.Command { relayerHome := relayerutils.GetHomeDir(home) relayerConfigPath := relayerutils.GetConfigFilePath(relayerHome) - raID, hd, err := relayerutils.GetRollappToRunFor(home) + raID, hd, kb, err := relayerutils.GetRollappToRunFor(home) if err != nil { pterm.Error.Println("failed to determine what RollApp to run for:", err) return @@ -365,6 +365,7 @@ func Cmd() *cobra.Command { err := sequencerutils.UpdateWhitelistedRelayers( home, relKeys[consts.KeysIds.RollappRelayer].Address, + kb, *hd, ) if err != nil { @@ -373,7 +374,7 @@ func Cmd() *cobra.Command { } } - raOpAddr, err := sequencerutils.GetSequencerOperatorAddress(home) + raOpAddr, err := sequencerutils.GetSequencerOperatorAddress(home, kb) if err != nil { pterm.Error.Println("failed to get RollApp's operator address:", err) return diff --git a/cmd/rollapp/sequencer/bond/decrease/decrease.go b/cmd/rollapp/sequencer/bond/decrease/decrease.go index 8a8c3ada..c11d43fe 100644 --- a/cmd/rollapp/sequencer/bond/decrease/decrease.go +++ b/cmd/rollapp/sequencer/bond/decrease/decrease.go @@ -71,15 +71,9 @@ func Cmd() *cobra.Command { var txHash string if rollerData.KeyringBackend == consts.SupportedKeyringBackends.OS { - pswFileName, err := filesystem.GetOsKeyringPswFileName(consts.Executables.Dymension) + psw, err := filesystem.ReadOsKeyringPswFile(home, consts.Executables.Dymension) if err != nil { - pterm.Error.Println("failed to get os keyring psw file name", err) - return - } - fp := filepath.Join(home, string(pswFileName)) - psw, err := filesystem.ReadFromFile(fp) - if err != nil { - pterm.Error.Println("failed to read keyring passphrase file", err) + pterm.Error.Println("failed to read os keyring password file", err) return } diff --git a/utils/filesystem/filesystem.go b/utils/filesystem/filesystem.go index b1f732bb..95535df5 100644 --- a/utils/filesystem/filesystem.go +++ b/utils/filesystem/filesystem.go @@ -253,3 +253,19 @@ func GetOsKeyringPswFileName(command string) (consts.OsKeyringPwdFileName, error } return pswFileName, nil } + +func ReadOsKeyringPswFile(home, command string) (string, error) { + pswFileName, err := GetOsKeyringPswFileName(command) + if err != nil { + pterm.Error.Println("failed to get os keyring psw file name", err) + return "", err + } + fp := filepath.Join(home, string(pswFileName)) + psw, err := ReadFromFile(fp) + if err != nil { + pterm.Error.Println("failed to read keyring passphrase file", err) + return "", err + } + + return psw, nil +} diff --git a/utils/keys/relayer.go b/utils/keys/relayer.go index 10b455d8..b40f2578 100644 --- a/utils/keys/relayer.go +++ b/utils/keys/relayer.go @@ -27,7 +27,6 @@ func GetRelayerAddressInfo(keyConfig KeyConfig, chainId string) (*KeyInfo, error "--output", "json", ) - fmt.Println(showKeyCommand.String()) output, err := bash.ExecCommandWithStdout(showKeyCommand) if err != nil { @@ -111,85 +110,29 @@ func GetRelayerKeysToFund(rollappConfig roller.RollappConfig) error { return nil } -func AddRlyKey(kc KeyConfig, chainID string) (*KeyInfo, error) { - addKeyCmd := getAddRlyKeyCmd( - kc, - chainID, - ) - - out, err := bash.ExecCommandWithStdout(addKeyCmd) - if err != nil { - return nil, err - } - - ki, err := ParseAddressFromOutput(out) - if err != nil { - return nil, err - } - ki.Name = kc.ID - - return ki, nil -} - func GenerateRelayerKeys(rollerData roller.RollappConfig) (map[string]KeyInfo, error) { pterm.Info.Println("creating relayer keys") createdRlyKeys := map[string]KeyInfo{} keys := GetRelayerKeysConfig(rollerData) for k, v := range keys { - pterm.Info.Printf("checking %s in %s\n", k, v.Dir) - switch v.ID { case consts.KeysIds.RollappRelayer: chainId := rollerData.RollappID - isPresent, err := IsRlyAddressWithNameInKeyring(v, chainId) + ki, err := createRelayerKeyIfNotPresent(k, chainId, v) if err != nil { - pterm.Error.Printf("failed to check address: %v\n", err) return nil, err } - if !isPresent { - pterm.Info.Printf("creating %s in %s\n", k, v.Dir) - key, err := AddRlyKey(v, rollerData.RollappID) - if err != nil { - pterm.Error.Printf("failed to add key: %v\n", err) - } - createdRlyKeys[consts.KeysIds.RollappRelayer] = *key - } else { - ki, err := GetRelayerAddressInfo( - v, - rollerData.RollappID, - ) - if err != nil { - return nil, err - } - createdRlyKeys[consts.KeysIds.RollappRelayer] = *ki - } + createdRlyKeys[consts.KeysIds.RollappRelayer] = *ki case consts.KeysIds.HubRelayer: chainId := rollerData.HubData.ID - isPresent, err := IsRlyAddressWithNameInKeyring(v, chainId) + ki, err := createRelayerKeyIfNotPresent(k, chainId, v) if err != nil { - pterm.Error.Printf("failed to check address: %v\n", err) return nil, err } - if !isPresent { - pterm.Info.Printf("creating %s in %s\n", k, v.Dir) - key, err := AddRlyKey(v, rollerData.HubData.ID) - if err != nil { - pterm.Error.Printf("failed to add key: %v\n", err) - } - createdRlyKeys[consts.KeysIds.HubRelayer] = *key - } else { - ki, err := GetRelayerAddressInfo( - v, - rollerData.HubData.ID, - ) - if err != nil { - return nil, err - } - createdRlyKeys[consts.KeysIds.HubRelayer] = *ki - } + createdRlyKeys[consts.KeysIds.HubRelayer] = *ki default: return nil, fmt.Errorf("invalid key name: %s", v.ID) } @@ -204,6 +147,38 @@ func GenerateRelayerKeys(rollerData roller.RollappConfig) (map[string]KeyInfo, e return createdRlyKeys, nil } +func createRelayerKeyIfNotPresent( + keyName, chainID string, + kc KeyConfig, +) (*KeyInfo, error) { + isPresent, err := IsRlyAddressWithNameInKeyring(kc, chainID) + var ki KeyInfo + if err != nil { + pterm.Error.Printf("failed to check address: %v\n", err) + return nil, err + } + + if !isPresent { + key, err := AddRlyKey(kc, chainID) + if err != nil { + pterm.Error.Printf("failed to add key: %v\n", err) + } + + ki = *key + } else { + key, err := GetRelayerAddressInfo( + kc, + chainID, + ) + if err != nil { + return nil, err + } + + ki = *key + } + return &ki, nil +} + func getAddRlyKeyCmd(keyConfig KeyConfig, chainID string) *exec.Cmd { coinType := "60" if keyConfig.Type == consts.WASM_ROLLAPP { @@ -221,3 +196,23 @@ func getAddRlyKeyCmd(keyConfig KeyConfig, chainID string) *exec.Cmd { coinType, ) } + +func AddRlyKey(kc KeyConfig, chainID string) (*KeyInfo, error) { + addKeyCmd := getAddRlyKeyCmd( + kc, + chainID, + ) + + out, err := bash.ExecCommandWithStdout(addKeyCmd) + if err != nil { + return nil, err + } + + ki, err := ParseAddressFromOutput(out) + if err != nil { + return nil, err + } + ki.Name = kc.ID + + return ki, nil +} diff --git a/utils/relayer/relayer.go b/utils/relayer/relayer.go index eba14678..677c1234 100644 --- a/utils/relayer/relayer.go +++ b/utils/relayer/relayer.go @@ -17,11 +17,17 @@ import ( sequencerutils "github.com/dymensionxyz/roller/utils/sequencer" ) -func GetRollappToRunFor(home string) (string, *consts.HubData, error) { +// GetRollappToRunFor function retrieves the RollApp ID and Hub Data from the roller +// configuration file if it is present and returns +// the RollApp ID, Hub Data, keyring backend to use and error, if any. +// when no roller configuration file is present, it prompts the user for the +// necessary information and returns the RollApp ID, Hub Data, keyring backend to use +// and error, if any. +func GetRollappToRunFor(home string) (string, *consts.HubData, string, error) { rollerConfigFilePath := roller.GetConfigPath(home) rollerConfigExists, err := filesystem.DoesFileExist(rollerConfigFilePath) if err != nil { - return "", nil, err + return "", nil, "", err } if rollerConfigExists { @@ -31,7 +37,7 @@ func GetRollappToRunFor(home string) (string, *consts.HubData, error) { rollerData, err := roller.LoadConfig(home) if err != nil { pterm.Error.Printf("failed to load rollapp config: %v\n", err) - return "", nil, err + return "", nil, "", err } msg := fmt.Sprintf( @@ -46,7 +52,7 @@ func GetRollappToRunFor(home string) (string, *consts.HubData, error) { raID := rollerData.RollappID hd := rollerData.HubData - return raID, &hd, nil + return raID, &hd, string(rollerData.KeyringBackend), nil } } @@ -76,7 +82,9 @@ func NewIbcConnenctionCanBeCreatedOnCurrentNode(home, raID string) (bool, error) return true, nil } -func promptForRaAndHd() (string, *consts.HubData, error) { +// promptForRaAndHd function prompts the user for the RollApp ID and Hub Data +// and returns the RollApp ID, Hub Data, keyring backend to use and error, if any +func promptForRaAndHd() (string, *consts.HubData, string, error) { var hd consts.HubData raID := config.PromptRaID() @@ -97,17 +105,17 @@ func promptForRaAndHd() (string, *consts.HubData, error) { DaNetwork: consts.CelestiaTestnet, } if err != nil { - return "", nil, err + return "", nil, "", err } err = dependencies.InstallCustomDymdVersion(chd.DymensionHash) if err != nil { pterm.Error.Println("failed to install custom dymd version: ", err) - return "", nil, err + return "", nil, "", err } } - return raID, &hd, nil + return raID, &hd, "test", nil } func VerifyRelayerBalances(hd consts.HubData) error { diff --git a/utils/sequencer/sequencer.go b/utils/sequencer/sequencer.go index d6f1355f..363d4f80 100644 --- a/utils/sequencer/sequencer.go +++ b/utils/sequencer/sequencer.go @@ -575,29 +575,45 @@ func CheckExistingSequencer(home string) (*CheckExistingSequencerResponse, error } func UpdateWhitelistedRelayers( - home, raRelayerAddress string, + home, raRelayerAddress, kb string, hd consts.HubData, ) error { - cmd := exec.Command( - consts.Executables.Dymension, + args := []string{ "tx", "sequencer", "update-whitelisted-relayers", raRelayerAddress, "--from", consts.KeysIds.HubSequencer, "--home", filepath.Join(home, consts.ConfigDirName.HubKeys), - "--keyring-backend", "test", + "--keyring-backend", kb, "--chain-id", hd.ID, "--node", hd.RpcUrl, "--fees", fmt.Sprintf("%d%s", consts.DefaultTxFee, consts.Denoms.Hub), - ) + } - txOutput, err := bash.ExecCommandWithInput(home, cmd, "signatures") + psw, err := filesystem.ReadOsKeyringPswFile(home, consts.Executables.Dymension) if err != nil { return err } - txHash, err := bash.ExtractTxHash(txOutput) + automaticPrompts := map[string]string{ + "Enter keyring passphrase": psw, + } + manualPromptResponses := map[string]string{ + "signatures": "this transaction is going to update the whitelisted relayers. do you want to continue?", + } + + txOutput, err := bash.ExecuteCommandWithPromptHandler( + consts.Executables.Dymension, + args, + automaticPrompts, + manualPromptResponses, + ) + if err != nil { + return err + } + + txHash, err := bash.ExtractTxHash(txOutput.String()) if err != nil { return err } @@ -610,23 +626,35 @@ func UpdateWhitelistedRelayers( return nil } -func GetSequencerOperatorAddress(home string) (string, error) { +func GetSequencerOperatorAddress(home string, kb string) (string, error) { rollappConfigDirPath := filepath.Join(home, consts.ConfigDirName.HubKeys) - getOperatorAddrCommand := exec.Command( - consts.Executables.RollappEVM, + args := []string{ "keys", "show", consts.KeysIds.HubSequencer, "-a", "--keyring-backend", - "test", + kb, "--home", rollappConfigDirPath, "--bech", "val", - ) + } + psw, err := filesystem.ReadOsKeyringPswFile(home, consts.Executables.Dymension) + if err != nil { + return "", err + } + + automaticPrompts := map[string]string{ + "Enter keyring passphrase": psw, + } - addr, err := bash.ExecCommandWithStdout(getOperatorAddrCommand) + addr, err := bash.ExecuteCommandWithPromptHandler( + consts.Executables.RollappEVM, + args, + automaticPrompts, + nil, + ) if err != nil { fmt.Println("val addr failed") return "", err