0

I want to check in bitcoin-core and see if there is any of my watching addresses in last block. So my question is how can I know a specific address is included in last block or not? I can search with getblock <hash> 2 but it returns only addresses in vout and I need the ones are in vin too.

1
  • Are you using the Bitcoin Core wallet as your watching only wallet? – Andrew Chow Jul 27 '20 at 16:53
1

If you have txindex enabled and are using a Linux, debian-based machine, install jq: sudo apt install jq

and save this script somewhere as "any-name.sh":

#!/bin/bash

BLOCK_HASH=$(bitcoin-cli getbestblockhash)
BLOCK_TEMPFILE=bestblock.json
VIN_TXNS_TEMPFILE=vin_txns.temp
VIN_N_TEMPFILE=vin_n.temp

# Check if all temp files and addresses file exists, and delete them if they do before recreating them.
if [ -f "${VIN_TXNS_TEMPFILE}" ]; then
  rm ${VIN_TXNS_TEMPFILE}
fi

if [ -f "${VIN_N_TEMPFILE}" ]; then
  rm ${VIN_N_TEMPFILE}
fi

if [ -f "${BLOCK_TEMPFILE}" ]; then
  rm ${BLOCK_TEMPFILE}
fi

# Get the block information in JSON format
bitcoin-cli getblock ${BLOCK_HASH} 2 > ${BLOCK_TEMPFILE}

# Get the block height for use in address file name
BLOCK_HEIGHT=$(jq ".height" ${BLOCK_TEMPFILE})

# Check if the address file exists. If so, we do not need to run the script again.
ADDRESSES_FILE="Block_${BLOCK_HEIGHT}_Addresses.txt"
if [ -f "${ADDRESSES_FILE}" ]; then
  rm ${BLOCK_TEMPFILE}
  exit 0
fi
touch ${ADDRESSES_FILE}
touch ${VIN_TXNS_TEMPFILE}
touch ${VIN_N_TEMPFILE}
touch ${BLOCK_TEMPFILE}

# Get the block transaction ID's, excluding the first transaction (coinbase)
jq ".tx[].vin[].txid" ${BLOCK_TEMPFILE} > ${VIN_TXNS_TEMPFILE}
tail -n +2 "${VIN_TXNS_TEMPFILE}" > "${VIN_TXNS_TEMPFILE}.temp" && mv "${VIN_TXNS_TEMPFILE}.temp" "${VIN_TXNS_TEMPFILE}"
sed -i 's/"//g' ${VIN_TXNS_TEMPFILE}

# Get the block transaction n's, excluding the first transaction (coinbase)
jq ".tx[].vin[].vout" ${BLOCK_TEMPFILE} > ${VIN_N_TEMPFILE}
tail -n +2 "${VIN_N_TEMPFILE}" > "${VIN_N_TEMPFILE}.temp" && mv "${VIN_N_TEMPFILE}.temp" "${VIN_N_TEMPFILE}"
readarray -t n_vals < ${VIN_N_TEMPFILE}

# Delete the vin n tempfile
rm ${VIN_N_TEMPFILE}

# Get the block txout addresses
jq ".tx[].vout[].scriptPubKey.addresses | .[]?" ${BLOCK_TEMPFILE} > ${ADDRESSES_FILE}

# Delete the block tempfile
rm ${BLOCK_TEMPFILE}

# Get each transaction's vout addresses
i=0
while read line; do
  val=${n_vals[${i}]}
  echo $(bitcoin-cli getrawtransaction "$line" true) | jq ".vout[${val}].scriptPubKey.addresses | .[]?" >> ${ADDRESSES_FILE}
  let "i=i+1"
done < ${VIN_TXNS_TEMPFILE}

# Delete transaction IDs tempfile
rm ${VIN_TXNS_TEMPFILE}

sed -i 's/"//g' ${ADDRESSES_FILE}
awk '!a[$0]++' ${ADDRESSES_FILE} > "${ADDRESSES_FILE}.temp" && mv "${ADDRESSES_FILE}.temp" "${ADDRESSES_FILE}"

Make the script executable: chmod +x any-name.sh

then you can run the script with ./any-name.sh . Keep in mind that this script takes several minutes to complete. I run a node on a raspberry pi 3b, and this script takes ~4 minutes to complete.

Script explanation: The vins from the most recent block contain a transaction ID as well as the vout index from that transaction that's being spent. We can use the command bitcoin-cli getrawtransaction <transactionhash> true to print JSON information from the transaction as long as we have txindex enabled. This information includes, most importantly for us, the address that is being spent (and "null" if the address being spent is not a standard address type). So for each vin in the block, we find the relevant transaction, and get the address at the specified vout index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.