<p>If you have txindex enabled and are using a Linux, debian-based machine, install jq: <code>sudo apt install jq</code></p>
<p>and save this script somewhere as &quot;any-name.sh&quot;:</p>
<pre><code>#!/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;
</code></pre>
<p>Make the script executable: <code>chmod +x any-name.sh</code></p>
<p>then you can run the script with <code>./any-name.sh</code> . 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.</p>
<p>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 <code>bitcoin-cli getrawtransaction &lt;transactionhash&gt; true</code> 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.</p>

CC BY-SA 4.0
2020-07-29T20:35:35.157Z
2020-07-29T20:35:35.157Z
Septem151
<p>I have written a program in Python3 that allows you to search for any address on bitcoin core whether it belongs to your wallet or not.</p>
<p>Here is the github link:</p>
<p><a href="https://github.com/ORP967/Bitcoin_Core_RPC_par_address" rel="nofollow noreferrer">https://github.com/ORP967/Bitcoin_Core_RPC_par_address</a></p>
<p>Let me know what you think or if you have any improvements you might have.</p>

CC BY-SA 4.0
2022-01-23T22:54:24.553Z
2022-01-23T22:54:24.553Z
ORP967
<p>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 <code>getblock &lt;hash&gt; 2</code> but it returns only addresses in vout and I need the ones are in vin too.</p>

CC BY-SA 4.0
2020-07-27T16:53:54.977Z
Are you using the Bitcoin Core wallet as your watching only wallet?
Ava Chow
<bitcoin-core><block>
How to search for an address in last block?
Nima
# How to search for an address in last block?

(Asked by: username_0 on 2020-07-27)

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.

**Tags:** bitcoin-core, block

> **Comments:**
> **username_1**: Are you using the Bitcoin Core wallet as your watching only wallet?  

---

## 2 answers

### Answer by username_2 on 2020-07-29. Score: 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.


---

### Answer by username_3 on 2022-01-23. Score: 0

I have written a program in Python3 that allows you to search for any address on bitcoin core whether it belongs to your wallet or not.

Here is the github link:

<https://github.com/username_3/Bitcoin_Core_RPC_par_address>

Let me know what you think or if you have any improvements you might have.


---

