Last active
April 24, 2024 15:05
-
-
Save jaonoctus/ac16fa3847aaa99f71a868c1c2f5a0b3 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 | |
# dependencies: base58 | |
# usage | |
# ./xpub-converter.sh <ZPUB> xpub | |
main () { | |
local input="$1" | |
local prefix="$2" | |
declare -A prefixes=( | |
["xpub"]="0488b21e" | |
["ypub"]="049d7cb2" | |
["Ypub"]="0295b43f" | |
["zpub"]="04b24746" | |
["Zpub"]="02aa7ed3" | |
["tpub"]="043587cf" | |
["upub"]="044a5262" | |
["Upub"]="024289ef" | |
["vpub"]="045f1cf6" | |
["Vpub"]="02575483" | |
) | |
local newPrefix="${prefixes[$prefix]}" | |
### BASE58-CHECK DECODE | |
# decode the base58 input to hex | |
local decoded=$(echo $input | base58 -d | xxd -p -c 100) | |
# remove the last 4 bytes (checksum) | |
local payload=${decoded::-8} | |
# get the last 4 bytes (checksum) | |
local checksum=${decoded:(-8)} | |
local computedChecksum=$(echo -n $payload | xxd -r -p | sha256sum | xxd -r -p | sha256sum | xxd -r -p | head -c 4 | xxd -p -c 100) | |
# compare the checksums | |
if [ "$checksum" != "$computedChecksum" ]; then | |
echo "Checksums do not match" | |
exit 1 | |
fi | |
### CHANGE PREFIX | |
# remove the first 4 bytes from payload | |
local newPayload=${payload:8} | |
# add the new prefix | |
local newPayloadWithPrefix="$newPrefix$newPayload" | |
### BASE58-CHECK ENCODE | |
local newChecksum=$(echo -n $newPayloadWithPrefix | xxd -r -p | sha256sum | xxd -r -p | sha256sum | xxd -r -p | head -c 4 | xxd -p -c 100) | |
local newEncoded=$(echo -n "$newPayloadWithPrefix$newChecksum" | xxd -r -p | base58) | |
echo $newEncoded | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment