import sys
from bitcoinrpc.authproxy import AuthServiceProxy
RPC_ADDRESS="127.0.0.1:8332"
RPC_USER="user"
RPC_PASSWORD="password"
def connect(address, user, password):
return AuthServiceProxy("http://%s:%s@%s"%(user, password, address))
def extract_block_addresses(rpc, block_hash):
block = rpc.getblock(block_hash)
addresses = []
for tx in block[u'tx']:
raw_tx = rpc.getrawtransaction(tx, True)
if not raw_tx.has_key('vout'):
sys.stderr.write("Transaction %s has no 'vout': %s\n"%(tx, raw_tx))
break
for vout in raw_tx[u'vout']:
if not vout.has_key("scriptPubKey"):
sys.stderr.write("Vout %s of Transaction %s has no 'scriptPubKey'\n"%(vout, tx))
break
if vout["scriptPubKey"]["type"] == "nulldata":
# arbitrary data
break
elif vout['scriptPubKey'].has_key('addresses'):
addresses.extend(vout['scriptPubKey']['addresses'])
else:
sys.stderr.write("Can't handle %s transaction output type in transaction %s\n"%(vout["scriptPubKey"]["type"], raw_tx))
return addresses
if __name__ == "__main__":
if len(sys.argv) > 1:
start_block = int(sys.argv[1])
else:
start_block = 1
if len(sys.argv) > 2:
end_block = int(sys.argv[2])
else:
end_block = 0
rpc = connect(RPC_ADDRESS, RPC_USER, RPC_PASSWORD)
if end_block == 0:
end_block = rpc.getblockcount()
b = start_block
for b in xrange(start_block, end_block+1):
try:
block_hash = rpc.getblockhash(b)
for addr in extract_block_addresses(rpc, block_hash):
print addr
except:
rpc = connect(RPC_ADDRESS, RPC_USER, RPC_PASSWORD)
block_hash = rpc.getblockhash(b)
for addr in extract_block_addresses(rpc, block_hash):
print addr