Skip to content

Commit

Permalink
Allow raw tx hex as a parameter for checktransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
kristapsk committed May 9, 2023
1 parent 3f4f8ae commit 01eac07
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
13 changes: 11 additions & 2 deletions checktransaction.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
. "$(dirname "$(readlink -m "$0")")/inc.common.sh"

if [ "$1" == "" ]; then
echo "Usage: $(basename "$0") [options] txid|address [blockhash]"
echo "Usage: $(basename "$0") [options] txid|address|rawtx [blockhash]"
echo "Where:"
echo " txid - transaction id (either plain hex or blockchain explorer URL containing it)"
echo " address - Bitcoin address (shows transactions received to address)"
echo " rawtx - raw transaction hex"
echo " blockhash - optional blockhash for a block where to look for a non-wallet / non-mempool tx"
exit
fi
Expand All @@ -35,6 +36,9 @@ elif is_http_url "$1"; then
txids+=("$txid")
elif is_hex_id "$1" "64"; then
txids+=("$1")
elif is_hex_string "$1"; then
txids+=("raw")
rawtx="$1"
else
echo "'$1' is neither valid transacion id nor address nor HTTP URL."
exit 2
Expand All @@ -48,6 +52,11 @@ if (( ${#txids[@]} == 0 )); then
fi

for i in $(seq 0 $(( ${#txids[@]} - 1 )) ); do
show_decoded_tx_for_human "$(show_tx_by_id "${txids[$i]}" "$blockhash")"
if [[ "${txids[$i]}" == "raw" ]]; then
decoded_tx="$(call_bitcoin_cli -stdin decoderawtransaction <<< "$rawtx")"
else
decoded_tx="$(show_tx_by_id "${txids[$i]}" "$blockhash")"
fi
show_decoded_tx_for_human "$decoded_tx"
echo
done
8 changes: 7 additions & 1 deletion inc.common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -655,11 +655,17 @@ function is_http_url()
grep -qsE "^https?://" <<< "$input"
}

function is_hex_string()
{
input="$1"
grep -qsE "^[A-Za-z0-9]+$" <<< "$input"
}

function is_hex_id()
{
input="$1"
hexlen="$2"
grep -qsE "[A-Za-z0-9]{$hexlen}" <<< "$input"
grep -qsE "^[A-Za-z0-9]{$hexlen}$" <<< "$input"
}

function get_hex_id_from_string()
Expand Down
13 changes: 12 additions & 1 deletion tests/hex.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@

. ../inc.common.sh

@test "Hex id validating tests" {
@test "Hex id matching tests" {
$(is_hex_id "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" "64")
$(is_hex_id "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b" "64")
! $(is_hex_id "not a hex id" "12")
! $(is_hex_id "not a hex id" "64")
! $(is_hex_id "" "64")
# wrong length
! $(is_hex_id "123456789" "2")
}

@test "Hex string matching tests" {
$(is_hex_string "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")
$(is_hex_string "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b")
$(is_hex_string "123456789")
! $(is_hex_string "not a hex string")
! $(is_hex_string "not a hex string")
! $(is_hex_string "")
}

@test "Hex substring matching tests" {
Expand Down

0 comments on commit 01eac07

Please sign in to comment.