diff --git a/checktransaction.sh b/checktransaction.sh index 26e45f0..a5bac4e 100755 --- a/checktransaction.sh +++ b/checktransaction.sh @@ -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 @@ -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 @@ -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 diff --git a/inc.common.sh b/inc.common.sh index 5c8bcee..b34987f 100644 --- a/inc.common.sh +++ b/inc.common.sh @@ -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() diff --git a/tests/hex.bats b/tests/hex.bats index 5240587..e2a9250 100755 --- a/tests/hex.bats +++ b/tests/hex.bats @@ -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" {