Skip to content

Instantly share code, notes, and snippets.

@imaginator
Last active August 21, 2024 05:24
Show Gist options
  • Save imaginator/8e3f80389d55ab76baee72136cc505ab to your computer and use it in GitHub Desktop.
Save imaginator/8e3f80389d55ab76baee72136cc505ab to your computer and use it in GitHub Desktop.
#!/bin/bash
# BIP39 wordlist URL
WORDLIST_URL="https://raw.githubusercontent.com/bitcoin/bips/master/bip-0039/english.txt"
# Function to download the BIP39 word list if it doesn't exist
download_wordlist() {
if [ ! -f bip39_wordlist.txt ]; then
echo "Downloading BIP39 word list..."
curl -s $WORDLIST_URL -o bip39_wordlist.txt
if [ $? -ne 0 ]; then
echo "Failed to download BIP39 word list."
exit 1
fi
fi
}
# Convert binary to decimal
binary_to_decimal() {
echo "$((2#$1))"
}
# Get the word from the word list using the index
get_bip39_word() {
local index=$1
local word=$(sed -n "$((index))p" bip39_wordlist.txt)
echo "$word"
}
# Check if input is binary
is_binary() {
if [[ $1 =~ ^[01]+$ ]]; then
return 0
else
return 1
fi
}
# Main script execution
main() {
# Check if input is provided
if [ -z "$1" ]; then
echo "Usage: $0 <binary_input>"
exit 1
fi
# Validate input
if ! is_binary "$1"; then
echo "Error: Input should only contain 1s and 0s."
exit 1
fi
# Download the BIP39 word list if necessary
download_wordlist
# Convert binary input to decimal
decimal_value=$(binary_to_decimal "$1")
# Validate if the decimal value is within the wordlist range
if [ $decimal_value -lt 0 ] || [ $decimal_value -gt 2047 ]; then
echo "Error: Decimal value out of range (0-2047)."
exit 1
fi
# Get the corresponding word from the BIP39 word list
bip39_word=$(get_bip39_word "$decimal_value")
echo "BIP39 Word: $bip39_word"
}
# Run the main function with the provided argument
main "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment