How to search for an address in last block? 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 &lt;hash&gt; 2 but it returns only addresses in vout and I need the ones are in vin too.
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 &quot;any-name.sh&quot;:
#!/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 &quot;${VIN_TXNS_TEMPFILE}&quot; ]; then
  rm ${VIN_TXNS_TEMPFILE}
fi

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

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

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

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

# Check if the address file exists. If so, we do not need to run the script again.
ADDRESSES_FILE=&quot;Block_${BLOCK_HEIGHT}_Addresses.txt&quot;
if [ -f &quot;${ADDRESSES_FILE}&quot; ]; 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 &quot;.tx[].vin[].txid&quot; ${BLOCK_TEMPFILE} &gt; ${VIN_TXNS_TEMPFILE}
tail -n +2 &quot;${VIN_TXNS_TEMPFILE}&quot; &gt; &quot;${VIN_TXNS_TEMPFILE}.temp&quot; &amp;&amp; mv &quot;${VIN_TXNS_TEMPFILE}.temp&quot; &quot;${VIN_TXNS_TEMPFILE}&quot;
sed -i 's/&quot;//g' ${VIN_TXNS_TEMPFILE}

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

# Delete the vin n tempfile
rm ${VIN_N_TEMPFILE}

# Get the block txout addresses
jq &quot;.tx[].vout[].scriptPubKey.addresses | .[]?&quot; ${BLOCK_TEMPFILE} &gt; ${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 &quot;$line&quot; true) | jq &quot;.vout[${val}].scriptPubKey.addresses | .[]?&quot; &gt;&gt; ${ADDRESSES_FILE}
  let &quot;i=i+1&quot;
done &lt; ${VIN_TXNS_TEMPFILE}

# Delete transaction IDs tempfile
rm ${VIN_TXNS_TEMPFILE}

sed -i 's/&quot;//g' ${ADDRESSES_FILE}
awk '!a[$0]++' ${ADDRESSES_FILE} &gt; &quot;${ADDRESSES_FILE}.temp&quot; &amp;&amp; mv &quot;${ADDRESSES_FILE}.temp&quot; &quot;${ADDRESSES_FILE}&quot;

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 &lt;transactionhash&gt; 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 &quot;null&quot; 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.