Skip to content

Instantly share code, notes, and snippets.

@delvison
Created March 24, 2023 23:29
Show Gist options
  • Save delvison/7ec602ee4f7df054aa4d5171d395d449 to your computer and use it in GitHub Desktop.
Save delvison/7ec602ee4f7df054aa4d5171d395d449 to your computer and use it in GitHub Desktop.
#!/bin/bash
readonly bip39_list="bip-39-eng-wordlist.txt"
check_wordlist() {
if [ ! -f "${bip39_list}" ]; then
curl -s \
https://raw.githubusercontent.com/bitcoin/bips/master/bip-0039/english.txt \
> "${bip39_list}"
fi
}
# ./seedqr_standard decode "000000010002000300040005000600070008000900100011"
decode() {
check_wordlist
echo "$@" > .encoded
while IFS= read -r -n4 char
do
if [ -n "${char}" ]; then
num=$(echo $char | sed 's/^0*//')
index=$((num+1))
sed -n "${index}p" "${bip39_list}"
fi
done < ".encoded"
}
# ./seedqr_standard encode "abandon ability able about above absent absorb abstract absurd abuse access accident"
encode() {
check_wordlist
local words="$1"
for word in $words; do
index=$(grep -no -E "^$word\$" $bip39_list | cut -d':' -f1)
[ -z "${index}" ] && echo "$word not on list" && exit 1
index=$((index-1))
printf "%04g" $index
done
}
# Check if the function exists (bash specific)
if declare -f "$1" > /dev/null
then
# call arguments verbatim
"$@"
else
# Show a helpful error
echo "'$1' is not a known function name" >&2
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment