Skip to content

Commit 7a5a9f6

Browse files
authored
✨ Add --version Flag (#25)
### 🕓 Changelog This PR introduces a `--version` flag to the CLI that displays the latest commit hash (=version) of the script. It also improves error handling in the `validate_network` function for better clarity. Additionally, minor code comments have been updated for improved understanding. --------- Signed-off-by: Pascal Marco Caversaccio <[email protected]>
1 parent 66643a9 commit 7a5a9f6

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,17 @@ This Bash [script](./safe_hashes.sh) calculates the Safe transaction hashes by r
7373
**Options:**
7474

7575
- `--help`: Display this help message.
76+
- `--version`: Display the latest local commit hash (=version) of the script.
7677
- `--list-networks`: List all supported networks and their chain IDs.
7778
- `--network <network>`: Specify the network (e.g., `ethereum`, `polygon`).
7879
- `--address <address>`: Specify the Safe multisig address.
7980
- `--nonce <nonce>`: Specify the transaction nonce (required for transaction hashes).
8081
- `--message <file>`: Specify the message file (required for off-chain message hashes).
8182
- `--interactive`: Use the interactive mode (optional for transaction hashes).
8283

84+
> [!NOTE]
85+
> Please note that `--help`, `--version`, and `--list-networks` can be used independently or alongside other options without causing the script to fail. They are special options that can be called without affecting the rest of the command processing.
86+
8387
Before you invoke the [script](./safe_hashes.sh), make it executable:
8488

8589
```console

safe_hashes.sh

+41-3
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ Usage: $0 [--help] [--list-networks]
204204
205205
Options:
206206
--help Display this help message
207+
--version Display the latest local commit hash (=version) of the script
207208
--list-networks List all supported networks and their chain IDs
208209
--network <network> Specify the network (required)
209210
--address <address> Specify the Safe multisig address (required)
@@ -223,6 +224,26 @@ EOF
223224
exit "${1:-1}"
224225
}
225226

227+
# Utility function to retrieve the latest local commit hash from the Git repository.
228+
# We don't include `git` in the `check_required_tools` function to avoid making
229+
# it a strict dependency for the script to run.
230+
get_latest_git_commit_hash() {
231+
local commit_hash=""
232+
if command -v git &>/dev/null; then
233+
commit_hash=$(git rev-parse HEAD 2>/dev/null)
234+
if [[ -n "$commit_hash" ]]; then
235+
echo -e "Latest local commit hash (=version) of the script: ${GREEN}$commit_hash${RESET}."
236+
exit 0
237+
else
238+
echo -e "${BOLD}${RED}No commit hash information available. There may be an issue with your Git installation or repository configuration.${RESET}"
239+
exit 1
240+
fi
241+
else
242+
echo -e "${BOLD}${RED}Git is not installed or not found. Unable to retrieve the commit hash information!${RESET}"
243+
exit 1
244+
fi
245+
}
246+
226247
# Utility function to list all supported networks.
227248
list_networks() {
228249
echo "Supported Networks:"
@@ -492,9 +513,18 @@ calculate_hashes() {
492513
# Utility function to validate the network name.
493514
validate_network() {
494515
local network="$1"
516+
517+
if [[ -z "$network" ]]; then
518+
echo -e "${BOLD}${RED}Network name is empty!${RESET}" >&2
519+
echo
520+
calculate_safe_hashes --list-networks >&2
521+
exit 1
522+
fi
523+
495524
if [[ -z "${API_URLS[$network]:-}" || -z "${CHAIN_IDS[$network]:-}" ]]; then
496525
echo -e "${BOLD}${RED}Invalid network name: \"${network}\"${RESET}\n" >&2
497-
calculate_safe_tx_hashes --list-networks >&2
526+
echo
527+
calculate_safe_hashes --list-networks >&2
498528
exit 1
499529
fi
500530
}
@@ -645,7 +675,7 @@ calculate_offchain_message_hashes() {
645675

646676
# Safe Transaction/Message Hashes Calculator
647677
# This function orchestrates the entire process of calculating the Safe transaction/message hashes:
648-
# 1. Parses command-line arguments (`help`, `network`, `address`, `nonce`, `message`, `interactive`, `list-networks`).
678+
# 1. Parses command-line arguments (`help`, `version`, `list-networks`, `network`, `address`, `nonce`, `message`, `interactive`).
649679
# 2. Validates that all required parameters are provided.
650680
# 3. Retrieves the API URL and chain ID for the specified network.
651681
# 4. Constructs the API endpoint URL.
@@ -669,15 +699,20 @@ calculate_safe_hashes() {
669699
local network="" address="" nonce="" message_file="" interactive=""
670700

671701
# Parse the command line arguments.
702+
# Please note that `--help`, `--version`, and `--list-networks` can be used
703+
# independently or alongside other options without causing the script to fail.
704+
# They are special options that can be called without affecting the rest of
705+
# the command processing.
672706
while [[ $# -gt 0 ]]; do
673707
case "$1" in
674708
--help) usage 0 ;;
709+
--version) get_latest_git_commit_hash ;;
710+
--list-networks) list_networks ;;
675711
--network) network="$2"; shift 2 ;;
676712
--address) address="$2"; shift 2 ;;
677713
--nonce) nonce="$2"; shift 2 ;;
678714
--message) message_file="$2"; shift 2 ;;
679715
--interactive) interactive="1"; shift ;;
680-
--list-networks) list_networks ;;
681716
*) echo "Unknown option: $1" >&2; usage ;;
682717
esac
683718
done
@@ -733,6 +768,9 @@ EOF
733768
fi
734769

735770
# Validate if the nonce parameter has the correct format.
771+
# Please note that the nonce validation is intentionally placed
772+
# after the domain and message hash calculations for off-chain
773+
# messages, where a nonce is not required.
736774
validate_value "$nonce" "nonce"
737775

738776
# Fetch the transaction data from the API.

0 commit comments

Comments
 (0)