Skip to content

Instantly share code, notes, and snippets.

@mxgxw
Last active September 21, 2021 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mxgxw/4f5f2f8d3b0b12be44fbad4c0015f78f to your computer and use it in GitHub Desktop.
Save mxgxw/4f5f2f8d3b0b12be44fbad4c0015f78f to your computer and use it in GitHub Desktop.
Bitcoin Blockchain Transaction Explorer
import datetime
import json
import time
import os
from bitcoinlib.services.bitcoind import BitcoindClient
from bitcoinlib.transactions import transaction_deserialize
from creds import *
import sys
from transactions import *
sys.setrecursionlimit(5000)
# Configuration parameters are stored within creds.py.
# See creds_example.py for more info.
result = open("traverse_outputB_d3.csv","w+")
result.write("Source,Target,Label,Address,Weight,Origin,Date,Height,Fee\n")
# Preload Addresses
prev_result = open("Analisis2.csv", "r")
line = prev_result.readline()
ht = {}
first = True
n = 0
while line:
n += 1
data = line.split(",")
if first:
ht[data[0]] = True
ht[data[1]] = True
first = False
else:
ht[data[0]] = True
line = prev_result.readline()
print("Nodes found: {}".format(len(ht)))
base_url = "http://{}:{}@{}:{}".format(
rpc_user,
rpc_pass,
server_addr,
server_port
)
bdc = BitcoindClient(base_url=base_url)
def gettransaction(txid,tries=1):
global bdc
try_counter = 0
response = None
while try_counter < tries:
try:
response = bdc.gettransaction(txid)
except:
print("Error fetching transactions. Trying again...")
time.sleep(3)
bdc = BitcoindClient(base_url=base_url)
try_counter += 1
if response is None:
raise Exception("Error: unable to fetch transaction")
return response
def getrawtransaction(txid,tries=1):
global bdc
try_counter = 0
response = None
while try_counter < tries:
try:
response = bdc.getrawtransaction(txid)
except:
print("Error fetching transactions. Trying again...")
time.sleep(3)
bdc = BitcoindClient(base_url=base_url)
try_counter += 1
if response == None:
raise Exception("Error: unable to fetch transaction")
return response
not_before = datetime.datetime(2018, 1, 1)
def extracttransactions(txid,max_depth=3,depth=0,prev_tx=None):
global bdc, ht
if depth == max_depth:
return
is_origin = False
tx = None
if prev_tx is None:
is_origin = True
tx = gettransaction(txid,15)
else:
tx = prev_tx
print("--Current depth-- {}".format(depth))
for tx_i in tx.inputs:
tx_i_dict = tx_i.as_dict()
#prev_raw_tx = getrawtransaction(tx_i_dict['prev_txid'],15)
#prev_tx = transaction_deserialize(prev_raw_tx)
prev_tx = gettransaction(tx_i_dict['prev_txid'],15)
value = prev_tx.outputs[tx_i_dict['output_n']].value
address = 'coinbase-{}'.format(tx_i_dict['prev_txid'][-5:]) if prev_tx.coinbase else tx_i_dict['address']
address = 'p2pk-{}'.format(tx_i_dict['prev_txid'][-5:]) if address == '' else address
fee = '' if prev_tx.fee is None else prev_tx.fee
output = "{},{},{},edge,{},{},\"{}\",{},{}\n".format(
tx_i_dict['prev_txid'],
txid,
address,
value,
is_origin,
prev_tx.date.isoformat(),
prev_tx.block_height,
fee
)
result.write(output)
result.flush()
print(output)
#if value < 100000000:
# print("Previous branch less than 1btc. Skipping")
# continue
if prev_tx.date < not_before:
print("Previous branch out of scope for analysis")
continue
if tx_i_dict['prev_txid'] in ht:
print("Previous branch already fully explored. Skipping to next one")
continue
if tx_i_dict['prev_txid'] in not_follow:
print("Skipping next branches. [Tx found in not-follow]")
continue
if not prev_tx.coinbase and tx_i_dict['prev_txid'] not in not_follow:
extracttransactions(tx_i_dict['prev_txid'],max_depth,depth+1,prev_tx)
ht[txid] = True
# We use a dictionary as a hashtable
# to prevent traversing previously
# explored branches
for txid in transactions:
extracttransactions(txid,max_depth=5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment