Once an address database is built of all addresses, you also need to run a batch file ever 15 minutes to nab the current addresses from the current pool, this is where you can check against all new used addresses with value, and look back in your bloom filters to see if you already have the private-key for that address.
...
.sh shell script will follow for running this python, note it can also be ran by bitcoin, as they have a way to run batch scripts everytime there is a new block, but that is too late for hackers.
Here we use a bloom-filters for 'seen' addresses so we don't waste time on addresses that are already in our database, we're looking for new addresses, and we're looking for old addresses that we have private-key that now have value.
...
cat get-mempool3.py
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
from pybloom import BloomFilter
global sblf #make bloom filter globally available
from collections import Counter
# dup = [k for k,v in Counter(sl).items() if v>1]
import base58
if __name__ == '__main__':
rpc_user = "hacker"
rpc_password = "system"
#rpc_connection = AuthServiceProxy("
http://%s:%[email protected]:8332" % (rpc_user, rpc_password), timeout=3000)
rpc_connection = AuthServiceProxy("
http://%s:%[email protected]:8332" % (rpc_user, rpc_password), timeout=3000)
r = rpc_connection
best_block_hash = rpc_connection.getbestblockhash()
# dump current block count height
blkcnt = rpc_connection.getblockcount()
print("getblockcount = %s" % blkcnt)
# import pdb; pdb.set_trace()
mplist=r.getrawmempool() # get list of all tx's in mempool
mpe= r.getmempoolentry(mplist[0])
BLMSIZ = 5120000
# rblf = BloomFilter(capacity=5120000, error_rate=0.001)
import os.path
if (os.path.exists('memorypool.blm')):
with open("mempool.blm", "r") as myfile:
sblf = BloomFilter.fromfile(myfile)
else:
sblf = BloomFilter(capacity=BLMSIZ, error_rate=0.001)
with open("mempool.blm", "w") as myfile:
sblf.add(mpe) # mark memory pool entry in bloom filter
sblf.tofile(myfile)
# for all transactions in pool get the in/out address list
alist=[]
ie=iu=0
i = 1
for mpe in mplist:
# may not be correct what if there is not yet a tx for this entry??
seen = sblf.add(mpe) # mark as seen, if seen before skip work
if seen and i > 1:
print 'BLF SKIP', i, mpe
continue
if i%100==0:
print "@ ", i,len(mplist)
i = i +1
# sometimes the entry has already expired
try:
mptx= r.getmempoolentry(mpe)
except:
#print "MPTX Fail mpe", mpe
ie = ie + 1
pass
if 'wtxid' in mptx :
txid = mptx['wtxid']
else :
continue
# print 'MP txid,mptx', txid,mptx
# print mptx.keys()
# sometimes the txid's in the memory pool are dead or bad, but study them all to learn
try :
tx = r.getrawtransaction(txid,True)
except:
iu = iu + 1
#print "WTXID Fail txid/mptx", iu, txid, mptx
#pass
continue
for o in tx['vout']:
#for s in o['scriptPubKey']:
if 'scriptPubKey' in o:
s = o['scriptPubKey']
if 'addresses' in s:
for a in s['addresses']:
alist.append(a)
if 'depends' in s:
d = s['depends']
if len(d)>0:
print "DEPENDS", d
with open('pending-mptx.txt', 'w' ) as fp:
alist = set(alist) # get rid of duplicates
#print len(a),a
for a in alist:
if len(a) == 34 or len(a) == 33: # a may already be a hash160 value
h = base58.b58decode(a).encode('hex')[2:42]
fp.write( "%s:%s\n" % ( a, h ) )
else :
print "ABNORMAL ADDR",len(a),a
h = a
#fp.write( "%s:%s\n" % ( a, h ) )
alist=[] # clear list for next round
print "ADDR Found", len(alist)
print "Fail ie,iu,len(mplist)", ie, iu, len(mplist)
# save current bloom filter to file
with open("mempool.blm", "w") as myfile:
sblf.tofile(myfile) # close bloom-filter
''' for i in tx['vin']:
for s in i['scriptSig']:
if 'addresses' in i:
for a in i['addresses']:
alist.append(a)