I want to search for data in OP_Returns.
I want to look for text in OP_Return data and see in which transaction is a text like "Chancellor" or "fillippone" or any other text in the OP_Return data.
I know some websites were allowing that, like preturnio.com or blockchair.com or others, but for various reasons (offline, API only), I haven't been able to get use of them.
I have a Windows-based Bitcoin node as well (my Raspberry node is currently offline).
Is there any step-by-step guide for this?
Thanks!
This script is to search for specific texts within blockchain blocks, it uses the blockchain API but you can adapt it to your own node for speed, it stores the tx where matches are found.
update: Case Insensitivity: To make the search case-insensitive, you can convert both the text and the value to lowercase before comparing them.
import requests
import binascii
import json
import logging
logging.basicConfig(level=logging.INFO)
def is_hex(s):
if isinstance(s, str):
try:
int(s, 16)
return True
except ValueError:
return False
return False
def decode_hex(value):
try:
return binascii.unhexlify(value).decode('latin-1', errors='ignore')
except (binascii.Error, ValueError):
return value
def search_text_in_block(texts, start_block=0):
latest_block_url = "https://blockchain.info/latestblock"
try:
latest_block_response = requests.get(latest_block_url)
latest_block_response.raise_for_status()
latest_block = latest_block_response.json()
latest_block_height = latest_block['height']
except requests.RequestException as e:
logging.error(f"Error fetching latest block: {e}")
return
for block_height in range(start_block, latest_block_height + 1):
logging.info(f"Processing block: {block_height}")
block_url = f"https://blockchain.info/block-height/{block_height}?format=json"
try:
block_response = requests.get(block_url)
block_response.raise_for_status()
block_data = block_response.json()
except requests.RequestException as e:
logging.error(f"Error fetching block {block_height}: {e}")
continue
for block in block_data['blocks']:
for tx in block['tx']:
match_found = False
for key, value in tx.items():
if isinstance(value, str):
for text in texts:
if text.lower() in value.lower():
logging.info(f"Found '{text}' in transaction {tx['hash']} in field '{key}' (raw): {value}")
match_found = True
if is_hex(value) and key != 'hash':
decoded_data = decode_hex(value)
if text.lower() in decoded_data.lower():
logging.info(f"Found '{text}' in transaction {tx['hash']} in field '{key}' (decoded): {decoded_data}")
match_found = True
elif isinstance(value, list):
for item in value:
if isinstance(item, dict):
for sub_key, sub_value in item.items():
if isinstance(sub_value, str):
for text in texts:
if text.lower() in sub_value.lower():
logging.info(f"Found '{text}' in transaction {tx['hash']} in field '{sub_key}' (raw): {sub_value}")
match_found = True
if is_hex(sub_value) and sub_key != 'hash':
decoded_data = decode_hex(sub_value)
if text.lower() in decoded_data.lower():
logging.info(f"Found '{text}' in transaction {tx['hash']} in field '{sub_key}' (decoded): {decoded_data}")
match_found = True
if match_found:
decoded_tx = {k: (decode_hex(v) if is_hex(v) and k != 'hash' else v) for k, v in tx.items()}
with open(f"{tx['hash']}_decoded.json", "w") as f:
json.dump(decoded_tx, f, indent=4)
logging.info(f"Transaction {tx['hash']} saved to {tx['hash']}_decoded.json")
search_text_in_block(["Chancellor", "bitcoin", "btc", "free"], start_block=0)