Created
March 24, 2023 23:29
-
-
Save delvison/7ec602ee4f7df054aa4d5171d395d449 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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