blkreader.py


SUBMITTED BY: Guest

DATE: Oct. 16, 2012, 12:24 a.m.

FORMAT: Python

SIZE: 4.9 kB

HITS: 3167

  1. # Python 2.5
  2. # I made this and I'm releasing it to the public domain.
  3. # also check http://www.bitbin.it/AJUiYzbX
  4. import time, hashlib
  5. def hash256(x): return hashlib.sha256(hashlib.sha256(x).digest()).digest()
  6. class fstring(): # file methods for strings
  7. def __init__(self, somestring):
  8. self.pos=0
  9. self.string=somestring
  10. def read(self, length=None):
  11. if length==None: length=len(self.string)
  12. out=self.string[self.pos:self.pos+length]; self.pos+=length
  13. return out
  14. def seek(self, point): self.pos=point
  15. def tell(self): return self.pos
  16. class txinput():
  17. def __init__(self, handle):
  18. if type(handle)==str: handle=fstring(handle)
  19. startingpos = handle.tell()
  20. self.prevouthash = handle.read(32)[::-1].encode('hex')
  21. self.prevoutn = sum([ord(handle.read(1))*(256**x) for x in range(4)])
  22. if self.prevouthash=='0'*64:
  23. self.coinbasesize = ord(handle.read(1))
  24. if self.coinbasesize>=253: self.coinbasesize = sum([ord(handle.read(1))*(256**x) for x in range(2*(self.coinbasesize-252))])
  25. self.coinbase = handle.read(self.coinbasesize)
  26. self.sequence = sum([ord(handle.read(1))*(256**x) for x in range(4)])
  27. else:
  28. self.scriptsize = ord(handle.read(1))
  29. if self.scriptsize>=253: self.scriptsize = sum([ord(handle.read(1))*(256**x) for x in range(2*(self.scriptsize-252))])
  30. self.script = handle.read(self.scriptsize)
  31. self.sequence = sum([ord(handle.read(1))*(256**x) for x in range(4)])
  32. class txoutput():
  33. def __init__(self, handle):
  34. if type(handle)==str: handle=fstring(handle)
  35. startingpos = handle.tell()
  36. self.value = sum([ord(handle.read(1))*(256**x) for x in range(8)])
  37. self.scriptsize = ord(handle.read(1))
  38. if self.scriptsize>=253: self.scriptsize = sum([ord(handle.read(1))*(256**x) for x in range(2*(self.scriptsize-252))])
  39. self.script = handle.read(self.scriptsize)
  40. #self.asm
  41. class transaction():
  42. def __init__(self, handle):
  43. if type(handle)==str: handle=fstring(handle)
  44. startingpos = handle.tell()
  45. self.version = sum([ord(handle.read(1))*(256**x) for x in range(4)])
  46. self.numinputs = ord(handle.read(1))
  47. if self.numinputs>=253: self.numinputs = sum([ord(handle.read(1))*(256**x) for x in range(2*(self.numinputs-252))])
  48. self.inputs = [txinput(handle) for inputnum in range(self.numinputs)]
  49. self.numoutputs = ord(handle.read(1))
  50. if self.numoutputs>=253: self.numoutputs = sum([ord(handle.read(1))*(256**x) for x in range(2*(self.numoutputs-252))])
  51. self.outputs = [txoutput(handle) for outputnum in range(self.numoutputs)]
  52. self.locktime = sum([ord(handle.read(1))*(256**x) for x in range(4)])
  53. self.size = handle.tell() - startingpos; handle.seek(startingpos)
  54. self.raw = handle.read(self.size)
  55. self.hash = hash256(self.raw)[::-1].encode('hex')
  56. class block():
  57. def __init__(self, handle):
  58. if type(handle)==str: handle=fstring(handle)
  59. self.unknown = handle.read(8) # I don't know what this is
  60. startingpos = handle.tell()
  61. self.version = sum([ord(handle.read(1))*(256**x) for x in range(4)])
  62. self.prevblock = handle.read(32)[::-1].encode('hex')
  63. self.merkleroot = handle.read(32)[::-1].encode('hex')
  64. self.timestamp = time.gmtime(sum([ord(handle.read(1))*(256**x) for x in range(4)]))
  65. self.bits = sum([ord(handle.read(1))*(256**x) for x in range(4)])
  66. self.nonce = sum([ord(handle.read(1))*(256**x) for x in range(4)])
  67. handle.seek(startingpos)
  68. self.header = handle.read(80)
  69. self.hash = hash256(self.header)[::-1].encode('hex')
  70. self.txcount = ord(handle.read(1))
  71. if self.txcount>=253: self.txcount = sum([ord(handle.read(1))*(256**x) for x in range(2*(self.txcount-252))])
  72. self.tx = [transaction(handle) for txn in range(self.txcount)]
  73. self.size = handle.tell() - startingpos; handle.seek(startingpos)
  74. self.raw = handle.read(self.size)
  75. #self.merkletree
  76. #self.isvalid() to verify merkle tree, hash, and difficulty
  77. class blkfile():
  78. def __init__(self, filename): self.file=file(filename,'rb+')
  79. def tell(self): return self.file.tell()
  80. def getblock(self): return block(self.file)
  81. def readblock(self): return block(self.file).raw
  82. # small demo
  83. # defrag file if getblock is too slow. or simply copy in the same dir, delete original, and rename copy.
  84. #blk0001=blkfile('some directory/blk0001.dat') # change this
  85. #genesisblock=blk0001.getblock()
  86. #for property in ['hash','prevblock','merkleroot','bits','nonce','version','txcount','timestamp','size']:
  87. # print property,':',vars(genesisblock)[property]

comments powered by Disqus