From ef85be396f973e48a68933abf4384f6baad0fbc4 Mon Sep 17 00:00:00 2001 From: Greg Szabo <16846635+greg-szabo@users.noreply.github.com> Date: Fri, 28 May 2021 10:18:50 -0400 Subject: [PATCH] Fix gm hermes keys command when hdpath is set (#976) * Fix gm hermes keys command when hdpath is set * Update CHANGELOG.md Co-authored-by: Romain Ruetschi * Update scripts/gm/CHANGELOG.md Co-authored-by: Romain Ruetschi * Updated stoml required version Co-authored-by: Romain Ruetschi --- CHANGELOG.md | 3 ++ scripts/gm/CHANGELOG.md | 18 ++++++++++++ scripts/gm/bin/lib-gm | 61 ++++++++++++++++++++++++++++++++++------- 3 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 scripts/gm/CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 4613d93683..a1b3247f32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ - [ibc-relayer] - Fix for a client worker bug; Hermes `start` returns error if no chain is reachable ([#972]) +- [gaia-manager] + - Import hermes keys properly even if wallet HD derivation path is set ([#975]) ### BREAKING CHANGES @@ -30,6 +32,7 @@ [#871]: https://github.com/informalsystems/ibc-rs/issues/871 [#911]: https://github.com/informalsystems/ibc-rs/issues/911 [#972]: https://github.com/informalsystems/ibc-rs/issues/972 +[#975]: https://github.com/informalsystems/ibc-rs/issues/975 [#983]: https://github.com/informalsystems/ibc-rs/issues/983 [#996]: https://github.com/informalsystems/ibc-rs/issues/996 diff --git a/scripts/gm/CHANGELOG.md b/scripts/gm/CHANGELOG.md new file mode 100644 index 0000000000..be3ff1976c --- /dev/null +++ b/scripts/gm/CHANGELOG.md @@ -0,0 +1,18 @@ +# Gaiad Manager Change Log + +## v0.0.2 + +### BUGFIXES +- Import hermes keys properly even if hdpath is set ([#975]) + +### FEATURES +- Introduced [CHANGELOG](https://github.com/informalsystems/ibc-rs/blob/master/scripts/gm/CHANGELOG.md) file. + +[#975]: https://github.com/informalsystems/ibc-rs/issues/975 + +## v0.0.1 + +### FEATURES +- Initial release ([#902]) + +[#902]: https://github.com/informalsystems/ibc-rs/issues/902 diff --git a/scripts/gm/bin/lib-gm b/scripts/gm/bin/lib-gm index 899334edc5..06d5905d61 100644 --- a/scripts/gm/bin/lib-gm +++ b/scripts/gm/bin/lib-gm @@ -5,7 +5,7 @@ if [ "${DEBUG:-}" == "2" ]; then fi version() { - echo "v0.0.1" + echo "v0.0.2" } config_defaults() { @@ -50,17 +50,29 @@ install() { } enforce_requirements() { - if [ -z "$(which stoml)" ]; then - exit_with_error "missing stoml, install it from https://github.com/freshautomations/stoml/releases" - fi if [ -z "$(which sconfig)" ]; then exit_with_error "missing sconfig, install it from https://github.com/freshautomations/sconfig/releases" fi - if [ -z "$(which sed)" ]; then - exit_with_error "missing sed, please install it (this requirement will be removed in the future)" + SED="$(which sed)" + if [ -z "$SED" ]; then + exit_with_error "missing sed, please install it" fi if [ -z "$(which tr)" ]; then - exit_with_error "missing tr, please install it (this requirement will be removed in the future)" + exit_with_error "missing tr, please install it" + fi + if [ -z "$(which dirname)" ]; then + exit_with_error "missing dirname, please install it" + fi + STOML="$(which stoml)" + if [ -z "$STOML" ]; then + exit_with_error "missing stoml, install it from https://github.com/freshautomations/stoml/releases" + fi + STOML_VERSION="$("$STOML" --version | "$SED" 's/^stoml version //')" + MAJOR="$(echo "${STOML_VERSION}" | "$SED" "s/^\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)$/\1/")" + MINOR="$(echo "${STOML_VERSION}" | "$SED" "s/^\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)$/\2/")" + PATCH="$(echo "${STOML_VERSION}" | "$SED" "s/^\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)$/\3/")" + if [ $((MAJOR)) -eq 0 ] && [ $((MINOR)) -lt 6 ] || { [ $((MAJOR)) -eq 0 ] && [ $((MINOR)) -eq 6 ] && [ $((PATCH)) -lt 2 ]; }; then + exit_with_error "stoml too old, install 0.6.2 or newer from https://github.com/freshautomations/stoml/releases" fi } @@ -696,6 +708,10 @@ list_keys() { } hermes_config() { + HERMES_DIR="$(dirname "$GLOBAL_HERMES_CONFIG")" + if [ ! -d "$HERMES_DIR" ]; then + mkdir -p "$HERMES_DIR" + fi cat < "$GLOBAL_HERMES_CONFIG" [global] strategy = '${GLOBAL_HERMES_STRATEGY}' @@ -730,14 +746,39 @@ EOF done } +# This is a helper function that extracts the coinType from an absolute hdpath (m/44'/118'/0'/0/0) for hermes. +get_cointype() { + WALLET_HDPATH="$(get_wallet_hdpath "$1")" + if [ -n "$WALLET_HDPATH" ]; then + COINTYPE=$(echo "$WALLET_HDPATH" | sed 's,^m/[0-9][0-9]*'\''/\([0-9][0-9]*\)'\''/[0-9][0-9]*'\''/[0-9][0-9]*/[0-9][0-9]*$,\1,') + if [ "$COINTYPE" != "$WALLET_HDPATH" ]; then + echo "$COINTYPE" + fi + fi +} + hermes_keys() { ID="$(get_chain_id "$1")" NETWORK_HOME_DIR="$(get_home_dir "$ID")" test -x "$GLOBAL_HERMES_BINARY" || exit_with_error "hermes binary \"${GLOBAL_HERMES_BINARY}\" not found, check your gm.toml config." - if [ -z "$GLOBAL_HERMES_CONFIG" ]; then - "$GLOBAL_HERMES_BINARY" keys add "$ID" -f "${NETWORK_HOME_DIR}/wallet_seed.json" + WALLET_HDPATH="$(get_wallet_hdpath "$1")" + COINTYPE="$(get_cointype "$1")" + if [ -n "$WALLET_HDPATH" ] && [ -z "${COINTYPE}" ]; then + warn "cointype could not be parsed. Reverting to default coin type." + fi + if [ -z "${COINTYPE}" ]; then + if [ -z "$GLOBAL_HERMES_CONFIG" ]; then + "$GLOBAL_HERMES_BINARY" keys add "$ID" -f "${NETWORK_HOME_DIR}/wallet_seed.json" + else + "$GLOBAL_HERMES_BINARY" -c "$GLOBAL_HERMES_CONFIG" keys add "$ID" -f "${NETWORK_HOME_DIR}/wallet_seed.json" + fi else - "$GLOBAL_HERMES_BINARY" -c "$GLOBAL_HERMES_CONFIG" keys add "$ID" -f "${NETWORK_HOME_DIR}/wallet_seed.json" + MNEMONIC="$(stoml "${NETWORK_HOME_DIR}/wallet_seed.json" "mnemonic")" + if [ -z "$GLOBAL_HERMES_CONFIG" ]; then + "$GLOBAL_HERMES_BINARY" keys restore "$ID" -t "$COINTYPE" -m "$MNEMONIC" + else + "$GLOBAL_HERMES_BINARY" -c "$GLOBAL_HERMES_CONFIG" keys restore "$ID" -t "$COINTYPE" -m "$MNEMONIC" + fi fi }