I was thinking about what you said you wanted the library for. I realized that I didn't have a good way to find the sender of a given input: the sender is not always identified in a TxIn, so you have to use the BlockDataManager to go find the TxOut. Anyways, I squashed a bug and added some methods to make this a lot easier, definitely worth updating if you already checked it out. (and if you prefer this over genjix's tool... I'm sure his sql database would be easy to access in python, too).
Here's some sample python code to get you started with my code if you plan to use it (this is scattered throughout testswig.py, but I extracted the important parts).
from sys import path
path.append('..') # I'm running this from the cppForSwig directory
from pybtcengine import *
from datetime import datetime
from BlockUtils import *
# Create the BlockDataManager, scan the chain, organize the block headers
bdm = BlockDataManager_FullRAM.GetInstance()
bdm.readBlkFile_FromScratch('../blk0001.dat') # point this to your blkfile!!!
# At this point all blockheaders and txs are loaded into memory.
# Can access instantaneously by hash or by height
# Get block 100,014 because it's got 6 diverse Tx
someBlk = bdm.getHeaderByHeight(100014)
# A helper method to convert header times to a pretty format
def unixTimeToFormatStr(unixTime, formatStr='%Y-%b-%d %I:%M%p'):
dtobj = datetime.fromtimestamp(unixTime)
dtstr = dtobj.strftime(formatStr)
return dtstr[:-2] + dtstr[-2:].lower()
# Another helper to convert raw, 20-byte address values to Base58
b = PyBtcAddress().createFromPublicKeyHash160(hash160)
# Going to print senders, receivers
print 'TxList for block #', someBlk.getBlockHeight()
topTxPtrList = someBlk.getTxRefPtrList()
print 'NumTx:', len(topTxPtrList)
for txptr in topTxPtrList:
# We print big-endian because we like to to be able to put it in BlockExplorer to verify
print '\nTx:', binary_to_hex(txptr.getThisHash().toString(), BIGENDIAN)[:16],
# Each Tx has a pointer to the header of the block it's included in
blkHead = txptr.getHeaderPtr()
print 'Blk:', blkHead.getBlockHeight(),
print 'Timestamp:', unixTimeToFormatStr(blkHead.getTimestamp())
# Print the TxIns
for i in range(nIn):
txin = txptr.getTxInRef(i)
print '\tSender:', '<COINBASE/GENERATION>'.center(34),
print 'Value: 50 [probably]';
print '\tSender:', hash160ToAddr(bdm.getSenderAddr20(txin).toString()),
print 'Value:', coin2str(bdm.getSentValue(txin))
# Print the TxOuts
for i in range(nOut):
txout = txptr.getTxOutRef(i)
print '\tRecip: ', hash160ToAddr(txout.getRecipientAddr().toString()),
print 'Value:', coin2str(txout.getValue())
You can probably construct your BTC webs pretty easily by combining calls from above. If you have problems, make sure you're passing binary forms, not hex, and whether you converted to or from a BinaryData object. All python methods require binary
python strings. All C++/SWIG methods require BinaryData objects (create with BinaryData(pyBinaryStr) and back via bindata.toString()).
P.S. -- I also cleaned up some code and put most methods in the .cpp files so you can scan the .h files easier to find the methods you're looking for.