qusal/salt/sys-bitcoin/files/server/bin/bitcoin-whitepaper
Ben Grande bdd4c789c1
fix: avoid echo usage
Echo can interpret operand as an option and checking every variable to
be echoed is troublesome while with printf, if the format specifier is
present before the operand, printing as string can be enforced.
2024-08-06 18:15:24 +02:00

102 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
## SPDX-FileCopyrightText: 2024 Benjamin Grande M. S. <ben.grande.b@gmail.com>
##
## SPDX-License-Identifier: AGPL-3.0-or-later
set -eu
#date_mined="20130604"
#block_number="230009"
block_hash="00000000000000ecbbff6bafb7efa2f7df05b227d5c73dca8f2635af32a2e949"
txid="54e48e5f5c656b26c3bca14a8c95aa583d07ebe84dde3b7dd4a78f4e4186e713"
dir="${HOME}"
file="bitcoin.pdf"
has(){
cmd="$(command -v "${1}" 2>/dev/null)" || return 1
test -x "${cmd}" || return 1
}
check_installed(){
missing_programs=0
for prog in "${@}"; do
# shellcheck disable=SC2310
if ! has "${prog}"; then
printf '%s\n' "Missing program: ${prog}" >&2
missing_programs=1
fi
done
if test "${missing_programs}" = "1"; then
exit 1
fi
}
validate_dir(){
if ! test -d "${dir}"; then
printf '%s\n' "Directory '${dir}' does not exist" >&2
exit 1
fi
if ! test -w "${dir}"; then
printf '%s\n' "Directory '${dir}' is not writable" >&2
exit 1
fi
}
getblock(){
check_installed bitcoin-cli xxd
# shellcheck disable=SC2312
bitcoin-cli getblock "${block_hash}" 0 \
| tail -c+92167 \
| for ((o=0;o<946;++o)); do \
read -rN420 x; \
printf '%s' "${x::130}${x:132:130}${x:264:130}"; \
done \
| xxd -r -p \
| tail -c+9 \
| head -c184292 \
| tee -- "${output_file}" >/dev/null
}
getrawtransaction(){
check_installed bitcoin-cli xxd
# shellcheck disable=SC2312
bitcoin-cli getrawtransaction "${txid}" 0 "${block_hash}" \
| sed -e 's/0100000000000000/\n/g' \
| tail -n +2 \
| cut -c7-136,139-268,271-400 \
| tr -d '\n' \
| cut -c17-368600 \
| xxd -p -r \
| tee -- "${output_file}" >/dev/null
}
gettxout(){
check_installed bitcoin-cli jq xxd seq
# shellcheck disable=SC2312
seq 0 947 \
| (while read -r n; do bitcoin-cli gettxout "${txid}" "${n}" \
| jq -r '.scriptPubKey.asm' \
| awk '{ print $2 $3 $4 }'; done) \
| tr -d '\n' \
| cut -c 17-368600 \
| xxd -r -p \
| tee "${output_file}" >/dev/null
}
usage(){
printf '%s\n' "Usage: ${0##*/} getblock|getrawtransaction|gettxout [DIR]"
printf '%s\n' "Note: gettxout works with pruned node"
printf '%s\n' "Note: DIR defaults to \${HOME}"
exit 1
}
case "${1:-}" in
"getblock"|"getrawtransaction"|"gettxout")
test -z "${2-}" || dir="${2}"
validate_dir "${dir}"
output_file="${dir}/${file}"
"${1}"
;;
*) usage;;
esac