Skip to content

Instantly share code, notes, and snippets.

@pcaversaccio
Created April 30, 2025 17:56
Show Gist options
  • Save pcaversaccio/8228892223f4dbdc70a4409197d94858 to your computer and use it in GitHub Desktop.
Save pcaversaccio/8228892223f4dbdc70a4409197d94858 to your computer and use it in GitHub Desktop.
Patch `safe_hashes.sh` script API URLs.
#!/usr/bin/env bash
##########################################
# Patch `safe_hashes.sh` Script API URLs #
##########################################
# @license GNU Affero General Public License v3.0 only
# @author pcaversaccio
# This Bash script modifies the default `safe_hashes.sh` script by
# replacing specific URLs within its `API_URLS` associative array
# with custom URLs defined in the `CUSTOM_API_URLS` array below.
# Usage:
# 1. Configure the `CUSTOM_API_URLS` array below with the networks
# and custom URLs you want to use. If you do not need them all,
# delete the ones you do not need.
# 2. Ensure the `ORIGINAL_SCRIPT` variable points to the correct path
# of the `safe_hashes.sh` script.
# 3. Run the script: `./patch_script.sh`.
# 4. A new file named `safe_hashes_custom_urls.sh` will be created with
# the specified URLs replaced.
# Define your new API URLs here.
declare -A -r CUSTOM_API_URLS=(
["arbitrum"]="https://your-custom-url-arbitrum.com"
["aurora"]="https://your-custom-url-aurora.com"
["avalanche"]="https://your-custom-url-avalanche.com"
["base"]="https://your-custom-url-base.com"
["base-sepolia"]="https://your-custom-url-base-sepolia.com"
["berachain"]="https://your-custom-url-berachain.com"
["blast"]="https://your-custom-url-blast.com"
["bsc"]="https://your-custom-url-bsc.com"
["celo"]="https://your-custom-url-celo.com"
["ethereum"]="https://your-custom-url-mainnet.com"
["gnosis"]="https://your-custom-url-gnosis-chain.com"
["gnosis-chiado"]="https://your-custom-url-chiado.com"
["ink"]="https://your-custom-url-ink.com"
["linea"]="https://your-custom-url-linea.com"
["mantle"]="https://your-custom-url-mantle.com"
["optimism"]="https://your-custom-url-optimism.com"
["polygon"]="https://your-custom-url-polygon.com"
["polygon-zkevm"]="https://your-custom-url-zkevm.com"
["scroll"]="https://your-custom-url-scroll.com"
["sepolia"]="https://your-custom-url-sepolia.com"
["sonic"]="https://your-custom-url-sonic.com"
["unichain"]="https://your-custom-url-unichain.com"
["worldchain"]="https://your-custom-url-worldchain.com"
["xlayer"]="https://your-custom-url-xlayer.com"
["zksync"]="https://your-custom-url-zksync.com"
)
readonly ORIGINAL_SCRIPT="safe_hashes.sh"
PATCHED_SCRIPT="safe_hashes_custom_urls.sh"
TEMP_FILE=$(mktemp)
# Check if the original `safe_hashes.sh` script exists.
if [[ ! -f "$ORIGINAL_SCRIPT" ]]; then
echo "Error: Original script '$ORIGINAL_SCRIPT' not found."
rm "$TEMP_FILE"
exit 1
fi
# Make a copy to modify.
cp "$ORIGINAL_SCRIPT" "$PATCHED_SCRIPT"
# Loop over the custom URLs and apply the patches sequentially.
for network in "${!CUSTOM_API_URLS[@]}"; do
# Escape potential special characters in the network key for the `sed` address pattern.
sed_network_pattern=$(printf '%s\n' "$network" | sed 's:[][\\/.^$*]:\\&:g')
# Escape potential special characters in the URL for the `sed` replacement string.
sed_url_replacement=$(printf '%s\n' "${CUSTOM_API_URLS[$network]}" | sed 's:[&\\|]:\\&:g')
# The address pattern now requires:
# 1. The key pattern: `\["${sed_network_pattern}\"\]=`,
# 2. Followed by any characters: `.*`,
# 3. Followed specifically by `https://` (with `/` escaped): `https:\/\/`.
# This ensures we only match lines from the `API_URLS` array, as `CHAIN_IDS` values don't contain `https://`.
sed "/\[\"${sed_network_pattern}\"\]=.*https:\/\// s|=.*$|=\"${sed_url_replacement}\"|" "$PATCHED_SCRIPT" >"$TEMP_FILE" && mv "$TEMP_FILE" "$PATCHED_SCRIPT"
done
# Ensure the temporary file is removed even if `sed` fails mid-loop.
rm "$TEMP_FILE" 2>/dev/null || true
# Make the patched script executable.
chmod +x "$PATCHED_SCRIPT"
echo "Patched script created at '$PATCHED_SCRIPT'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment