From 00b377ce3e9da62535908f8da3a80f6337e1156e Mon Sep 17 00:00:00 2001
From: Marcin Sobczyk <msobczyk@syncad.com>
Date: Mon, 25 Nov 2024 15:38:47 +0100
Subject: [PATCH] Use clive unlock in docker entrypoint

---
 docker/entrypoint.sh          |   1 +
 scripts/activate_beekeeper.sh | 115 +++++-----------------------------
 2 files changed, 16 insertions(+), 100 deletions(-)

diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh
index be778f44aa..633a2d8a5c 100755
--- a/docker/entrypoint.sh
+++ b/docker/entrypoint.sh
@@ -4,6 +4,7 @@
 set -euo pipefail
 
 SCRIPTPATH="$(cd -- "$(dirname "$0")" >/dev/null 2>&1 && pwd -P)"
+SELECTED_PROFILE=""
 TESTNET_NODE_LOG_FILE="testnet_node.log"
 INTERACTIVE_CLI_MODE=0
 
diff --git a/scripts/activate_beekeeper.sh b/scripts/activate_beekeeper.sh
index dbc3b2cf65..e9357aff52 100755
--- a/scripts/activate_beekeeper.sh
+++ b/scripts/activate_beekeeper.sh
@@ -3,32 +3,6 @@
 BEEKEEPER_ALREADY_CLOSED=0
 trap "clean_up" SIGTERM SIGQUIT SIGHUP EXIT
 
-# Retry passed function
-retry() {
-    local func=$1
-    local retries=0
-    local max_retries=3
-
-    if ! declare -f "$func" > /dev/null; then
-        echo "Error: '$func' is not a function."
-        exit 1
-    fi
-
-    while (( retries < max_retries )); do
-        "$func"
-        # shellcheck disable=SC2181
-        if [[ $? -eq 0 ]]; then
-            return 0
-        fi
-
-        ((retries++))
-
-        echo "Attempt $retries of $max_retries failed. Retrying..."
-    done
-    echo "Maximum attempts reached. Exiting."
-    exit 1
-}
-
 # Start Beekeeper with prepared session token
 start_beekeeper_with_prepared_session_token() {
   output=$(clive beekeeper spawn)
@@ -37,17 +11,17 @@ start_beekeeper_with_prepared_session_token() {
     echo "Error: Fail to spawn Beekeeper. Aborting..."
     exit 1
   fi
-  BEEKEEPER_HTTP_ENDPOINT=$(echo "$output" | grep -oE 'http://[0-9.]+:[0-9]+')
+  CLIVE_BEEKEEPER__REMOTE_ADDRESS=$(echo "$output" | grep -oE 'http://[0-9.]+:[0-9]+')
 
   CLIVE_BEEKEEPER__SESSION_TOKEN=$(curl -s --data '{
     "jsonrpc": "2.0",
     "method": "beekeeper_api.create_session",
     "params": {
       "salt": "clive-cli-session",
-      "notifications_endpoint": "'"${BEEKEEPER_HTTP_ENDPOINT}"'"
+      "notifications_endpoint": "'"${CLIVE_BEEKEEPER__REMOTE_ADDRESS}"'"
     },
     "id": 1
-  }' "${BEEKEEPER_HTTP_ENDPOINT}" | jq .result.token | tr -d '"')
+  }' "${CLIVE_BEEKEEPER__REMOTE_ADDRESS}" | jq .result.token | tr -d '"')
 
 
   if [[ "${CLIVE_BEEKEEPER__SESSION_TOKEN}" == "null" ]]; then
@@ -55,32 +29,19 @@ start_beekeeper_with_prepared_session_token() {
     exit 1
   fi
 
-  export CLIVE_BEEKEEPER__SESSION_TOKEN=${CLIVE_BEEKEEPER__SESSION_TOKEN}
-  export BEEKEEPER_HTTP_ENDPOINT=${BEEKEEPER_HTTP_ENDPOINT}
-
+  export CLIVE_BEEKEEPER__REMOTE_ADDRESS
+  export CLIVE_BEEKEEPER__SESSION_TOKEN
 }
 
 # Unlock wallet for selected profile
 unlock_wallet() {
-  read -rsp "Enter password for profile ${SELECTED_PROFILE}: " password
-  echo
-  password="${password//$'\n'/}"
-  response=$(curl -s --data '{
-    "jsonrpc": "2.0",
-    "method": "beekeeper_api.unlock",
-    "params": {
-      "token": "'"${CLIVE_BEEKEEPER__SESSION_TOKEN}"'",
-      "wallet_name": "'"${SELECTED_PROFILE}"'",
-      "password": "'"${password}"'"
-    },
-    "id": 1
-  }' "${BEEKEEPER_HTTP_ENDPOINT}")
-
-  error=$(echo "${response}" | jq .error)
-  if [[ "${error}" != "null" ]]; then
-    return 1
+  local profile_name_arg=""
+  if [[ -n "$SELECTED_PROFILE" ]]; then
+    profile_name_arg="--profile-name=${SELECTED_PROFILE}"
   fi
-  return 0
+  # shellcheck disable=SC2086
+  clive unlock $profile_name_arg --include-create-new-profile
+  return $?
 }
 
 # Print info about how to create profile
@@ -91,55 +52,6 @@ how_to_create_profile() {
   echo ""
 }
 
-# Select one of the existing profiles
-select_profile(){
-  clive_profiles=$(clive show profiles)
-  clive_profiles_formated=$(echo "$clive_profiles" | grep -oP "\[\K[^\]]+")
-  IFS=',' read -ra profile_array <<< "$clive_profiles_formated"
-
-  if [[ -n "${SELECTED_PROFILE:-}" ]]; then
-    for profile in "${profile_array[@]}"; do
-      profile=$(echo "${profile}" | tr -d "' ")
-      if [[ "${profile}" == "${SELECTED_PROFILE}" ]]; then
-        retry unlock_wallet
-        return
-      fi
-    done
-    echo "Error: Given profile ${SELECTED_PROFILE} does not exists."
-    echo "${clive_profiles}"
-    exit 1
-  fi
-
-  if [ ${#profile_array[@]} -eq 0 ]; then
-    echo "There are no profiles."
-    how_to_create_profile
-  else
-    echo "Select profile:"
-    for i in "${!profile_array[@]}"; do
-      profile=$(echo "${profile_array[i]}" | tr -d "' ")
-      echo "$((i + 1)). $profile"
-    done
-
-    echo "$(( ${#profile_array[@]} + 1 )). create new profile"
-
-    read -rp "Enter the number: " choice
-
-    if [[ $choice -ge 1 && $choice -le ${#profile_array[@]} ]]; then
-      selected_profile=$(echo "${profile_array[$((choice - 1))]}" | tr -d "' ")
-      echo "You selected: $selected_profile"
-      SELECTED_PROFILE=${selected_profile}
-      echo "Selected profile is ${SELECTED_PROFILE}"
-      retry unlock_wallet
-    elif [[ $choice -eq $(( ${#profile_array[@]} + 1 )) ]]; then
-      echo "You selected: create new profile"
-      how_to_create_profile
-    else
-      echo "Error: Invalid selection!"
-      return 1
-    fi
-  fi
-}
-
 # Execute a script passed as an argument
 execute_passed_script() {
   if [[ -n "${FILE_TO_EXECUTE:-}" ]]; then
@@ -167,7 +79,10 @@ close_beekeeper() {
 # Execute before entering interactive mode
 setup() {
   start_beekeeper_with_prepared_session_token
-  retry select_profile
+  if ! unlock_wallet; then
+    echo "Error: Failed to unlock wallet. Aborting..."
+    exit 1
+  fi
   execute_passed_script
   # shellcheck disable=SC1090
   source ~/.bashrc
-- 
GitLab