Skip to content

Commit

Permalink
fix: support os keyring for relayer whitelisting (#1129)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Nov 24, 2024
1 parent c3fd585 commit 74846cc
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 92 deletions.
5 changes: 3 additions & 2 deletions cmd/relayer/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -365,6 +365,7 @@ func Cmd() *cobra.Command {
err := sequencerutils.UpdateWhitelistedRelayers(
home,
relKeys[consts.KeysIds.RollappRelayer].Address,
kb,
*hd,
)
if err != nil {
Expand All @@ -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
Expand Down
10 changes: 2 additions & 8 deletions cmd/rollapp/sequencer/bond/decrease/decrease.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
16 changes: 16 additions & 0 deletions utils/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
117 changes: 56 additions & 61 deletions utils/keys/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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
}
24 changes: 16 additions & 8 deletions utils/relayer/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand Down
54 changes: 41 additions & 13 deletions utils/sequencer/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 74846cc

Please sign in to comment.