Beispiel #1
0
    def executeScript(self, binaryScript, stack=[]):
        self.stack = stack
        self.stackAlt = []
        scriptData = BinaryUnpacker(binaryScript)
        self.lastOpCodeSepPos = None

        while scriptData.getRemainingSize() > 0:
            opcode = scriptData.get(UINT8)
            exitCode = self.executeOpCode(opcode, scriptData, self.stack,
                                          self.stackAlt)
            if not exitCode == SCRIPT_NO_ERROR:
                if exitCode == OP_NOT_IMPLEMENTED:
                    LOGERROR(
                        '***ERROR: OpCodes OP_IF, OP_NOTIF, OP_ELSE, OP_ENDIF,'
                    )
                    LOGERROR(
                        '          have not been implemented, yet.  This script'
                    )
                    LOGERROR('          could not be evaluated.')
                if exitCode == OP_DISABLED:
                    LOGERROR(
                        '***ERROR: This script included an op code that has been'
                    )
                    LOGERROR(
                        '          disabled for security reasons.  Script eval'
                    )
                    LOGERROR('          failed.')
                return exitCode

        return SCRIPT_NO_ERROR
   def unserialize(self, toUnpack):
      if isinstance(toUnpack, BinaryUnpacker):
         addrData = toUnpack
      else:
         addrData = BinaryUnpacker( toUnpack )

      self.addrList = []
      naddr = addrData.get(VAR_INT)
      for i in range(naddr):
         self.addrList.append( PyNetAddress().unserialize(addrData) )
      return self
Beispiel #3
0
    def unserialize(self, toUnpack):
        if isinstance(toUnpack, BinaryUnpacker):
            addrData = toUnpack
        else:
            addrData = BinaryUnpacker(toUnpack)

        self.addrList = []
        naddr = addrData.get(VAR_INT)
        for i in range(naddr):
            self.addrList.append(PyNetAddress().unserialize(addrData))
        return self
Beispiel #4
0
    def unserialize(self, toUnpack):
        if isinstance(toUnpack, BinaryUnpacker):
            invData = toUnpack
        else:
            invData = BinaryUnpacker(toUnpack)

        numInv = invData.get(VAR_INT)
        for i in range(numInv):
            invType = invData.get(UINT32)
            invHash = invData.get(BINARY_CHUNK, 32)
            self.invList.append([invType, invHash])
        return self
Beispiel #5
0
    def unserialize(self, toUnpack):
        if isinstance(toUnpack, BinaryUnpacker):
            blkData = toUnpack
        else:
            blkData = BinaryUnpacker(toUnpack)

        self.txList = []
        self.header.unserialize(blkData)
        numTx = blkData.get(VAR_INT)
        for i in range(numTx):
            self.txList.append(PyTx().unserialize(blkData))
        return self
   def unserialize(self, toUnpack):
      if isinstance(toUnpack, BinaryUnpacker):
         invData = toUnpack
      else:
         invData = BinaryUnpacker( toUnpack )

      numInv = invData.get(VAR_INT)
      for i in range(numInv):
         invType = invData.get(UINT32)
         invHash = invData.get(BINARY_CHUNK, 32)
         self.invList.append( [invType, invHash] )
      return self
   def unserialize(self, toUnpack):
      if isinstance(toUnpack, BinaryUnpacker):
         gbData = toUnpack
      else:
         gbData = BinaryUnpacker( toUnpack )

      self.version = gbData.get(UINT32)
      nhash = gbData.get(VAR_INT)
      for i in range(nhash):
         self.hashList.append(gbData.get(BINARY_CHUNK, 32))
      self.hashStop = gbData.get(BINARY_CHUNK, 32)
      return self
   def unserialize(self, toUnpack):
      if isinstance(toUnpack, BinaryUnpacker):
         blkData = toUnpack
      else:
         blkData = BinaryUnpacker( toUnpack )

      self.txList = []
      self.header.unserialize(blkData)
      numTx = blkData.get(VAR_INT)
      for i in range(numTx):
         self.txList.append(PyTx().unserialize(blkData))
      return self
Beispiel #9
0
    def unserialize(self, toUnpack):
        if isinstance(toUnpack, BinaryUnpacker):
            blkData = toUnpack
        else:
            blkData = BinaryUnpacker(toUnpack)

        self.txList = []
        self.numTx = blkData.get(VAR_INT)
        for i in xrange(self.numTx):
            self.txList.append(PyTx().unserialize(blkData))
        self.merkleTree = []
        self.merkleRoot = ''
        return self
Beispiel #10
0
   def unserialize(self, toUnpack):
      if isinstance(toUnpack, BinaryUnpacker):
         headerData = toUnpack
      else:
         headerData = BinaryUnpacker( toUnpack )

      self.headerList = []
      self.header.unserialize(headerData)
      numHeader = headerData.get(VAR_INT)
      for i in range(numHeader):
         self.headerList.append(PyBlockHeader().unserialize(headerData))
      headerData.get(VAR_INT) # Not sure if this is even used, ever
      return self
Beispiel #11
0
   def unserialize(self, toUnpack):
      if isinstance(toUnpack, BinaryUnpacker):
         blkData = toUnpack
      else:
         blkData = BinaryUnpacker( toUnpack )

      self.txList = []
      self.numTx  = blkData.get(VAR_INT)
      for i in xrange(self.numTx):
         self.txList.append( PyTx().unserialize(blkData) )
      self.merkleTree = []
      self.merkleRoot = ''
      return self
Beispiel #12
0
    def unserialize(self, toUnpack):
        if isinstance(toUnpack, BinaryUnpacker):
            headerData = toUnpack
        else:
            headerData = BinaryUnpacker(toUnpack)

        self.headerList = []
        self.header.unserialize(headerData)
        numHeader = headerData.get(VAR_INT)
        for i in range(numHeader):
            self.headerList.append(PyBlockHeader().unserialize(headerData))
        headerData.get(VAR_INT)  # Not sure if this is even used, ever
        return self
Beispiel #13
0
   def unserialize(self, toUnpack, hasTimeField=True):
      if isinstance(toUnpack, BinaryUnpacker):
         addrData = toUnpack
      else:
         addrData = BinaryUnpacker( toUnpack )

      if hasTimeField:
         self.time     = addrData.get(UINT32)

      self.services = addrData.get(UINT64)
      self.addrQuad = addrData.get(BINARY_CHUNK,16)[-4:]
      self.port     = addrData.get(UINT16, endianness=NETWORKENDIAN)

      self.services = int_to_bitset(self.services)
      self.addrQuad = binary_to_quad(self.addrQuad)
      return self
Beispiel #14
0
    def unserialize(self, toUnpack):
        if isinstance(toUnpack, BinaryUnpacker):
            blkData = toUnpack
        else:
            blkData = BinaryUnpacker(toUnpack)

        return self
Beispiel #15
0
 def unserialize(self, toUnpack):
     bu = BinaryUnpacker(toUnpack)
     self.messageType = bu.get(VAR_STR)
     self.rejectCode = bu.get(INT8)
     self.message = bu.get(VAR_STR)
     self.data = bu.get(BINARY_CHUNK, bu.getRemainingSize())
     self.serializedData = toUnpack
     return self
Beispiel #16
0
    def unserialize(self, toUnpack):
        if isinstance(toUnpack, BinaryUnpacker):
            blkData = toUnpack
        else:
            blkData = BinaryUnpacker(toUnpack)

        self.txList = []
        self.blockHeader = PyBlockHeader().unserialize(blkData)
        self.blockData = PyBlockData().unserialize(blkData)
        return self
Beispiel #17
0
 def testSerializeUnserialize(self):
     tx1 = PyTx().unserialize(tx1raw)
     tx2 = PyTx().unserialize(BinaryUnpacker(tx2raw))
     tx1again = tx1.serialize()
     tx2again = tx2.serialize()
     self.assertEqual(tx1again, tx1raw)
     self.assertEqual(tx2again, tx2raw)
     blk = PyBlock().unserialize(hex_to_binary(hexBlock))
     blockReHex = binary_to_hex(blk.serialize())
     self.assertEqual(hexBlock, blockReHex)
     binRoot = blk.blockData.getMerkleRoot()
     self.assertEqual(blk.blockHeader.merkleRoot, blk.blockData.merkleRoot)
   def executeScript(self, binaryScript, stack=[]):
      self.stack = stack
      self.stackAlt  = []
      scriptData = BinaryUnpacker(binaryScript)
      self.lastOpCodeSepPos = None

      while scriptData.getRemainingSize() > 0:
         opcode = scriptData.get(UINT8)
         exitCode = self.executeOpCode(opcode, scriptData, self.stack, self.stackAlt)
         if not exitCode == SCRIPT_NO_ERROR:
            if exitCode==OP_NOT_IMPLEMENTED:
               LOGERROR('***ERROR: OpCodes OP_IF, OP_NOTIF, OP_ELSE, OP_ENDIF,')
               LOGERROR('          have not been implemented, yet.  This script')
               LOGERROR('          could not be evaluated.')
            if exitCode==OP_DISABLED:
               LOGERROR('***ERROR: This script included an op code that has been')
               LOGERROR('          disabled for security reasons.  Script eval')
               LOGERROR('          failed.')
            return exitCode

      return SCRIPT_NO_ERROR
Beispiel #19
0
    def unserialize(self, toUnpack):
        if isinstance(toUnpack, BinaryUnpacker):
            blkData = toUnpack
        else:
            blkData = BinaryUnpacker(toUnpack)

        self.version = blkData.get(UINT32)
        self.prevBlkHash = blkData.get(BINARY_CHUNK, 32)
        self.merkleRoot = blkData.get(BINARY_CHUNK, 32)
        self.timestamp = blkData.get(UINT32)
        self.diffBits = blkData.get(BINARY_CHUNK, 4)
        self.nonce = blkData.get(UINT32)
        self.theHash = hash256(self.serialize())
        return self
Beispiel #20
0
 def unserialize(self, toUnpack):
    bu = BinaryUnpacker(toUnpack)
    self.messageType = bu.get(VAR_STR)
    self.rejectCode = bu.get(INT8)
    self.message = bu.get(VAR_STR)
    self.data = bu.get(BINARY_CHUNK, bu.getRemainingSize())
    self.serializedData = toUnpack
    return self
Beispiel #21
0
    def unserialize(self, toUnpack):
        if isinstance(toUnpack, BinaryUnpacker):
            verData = toUnpack
        else:
            verData = BinaryUnpacker(toUnpack)

        self.version = verData.get(INT32)
        self.services = int_to_bitset(verData.get(UINT64), widthBytes=8)
        self.time = verData.get(INT64)
        self.addrRecv = PyNetAddress().unserialize(verData, hasTimeField=False)
        self.addrFrom = PyNetAddress().unserialize(verData, hasTimeField=False)
        self.nonce = verData.get(UINT64)
        self.subver = verData.get(VAR_STR)
        self.height0 = verData.get(INT32)
        return self
Beispiel #22
0
   def unserialize(self, toUnpack):
      if isinstance(toUnpack, BinaryUnpacker):
         verData = toUnpack
      else:
         verData = BinaryUnpacker( toUnpack )

      self.version  = verData.get(INT32)
      self.services = int_to_bitset(verData.get(UINT64), widthBytes=8)
      self.time     = verData.get(INT64)
      self.addrRecv = PyNetAddress().unserialize(verData, hasTimeField=False)
      self.addrFrom = PyNetAddress().unserialize(verData, hasTimeField=False)
      self.nonce    = verData.get(UINT64)
      self.subver   = verData.get(VAR_STR)
      self.height0  = verData.get(INT32)
      return self
Beispiel #23
0
    def unserialize(self, toUnpack):
        if isinstance(toUnpack, BinaryUnpacker):
            gbData = toUnpack
        else:
            gbData = BinaryUnpacker(toUnpack)

        self.version = gbData.get(UINT32)
        nhash = gbData.get(VAR_INT)
        for i in range(nhash):
            self.hashList.append(gbData.get(BINARY_CHUNK, 32))
        self.hashStop = gbData.get(BINARY_CHUNK, 32)
        return self
Beispiel #24
0
   def unserialize(self, toUnpack):
      if isinstance(toUnpack, BinaryUnpacker):
         blkData = toUnpack
      else:
         blkData = BinaryUnpacker( toUnpack )

      self.version     = blkData.get(UINT32)
      self.prevBlkHash = blkData.get(BINARY_CHUNK, 32)
      self.merkleRoot  = blkData.get(BINARY_CHUNK, 32)
      self.timestamp   = blkData.get(UINT32)
      self.diffBits    = blkData.get(BINARY_CHUNK, 4)
      self.nonce       = blkData.get(UINT32)
      self.theHash     = hash256(self.serialize())
      return self
Beispiel #25
0
    def unserialize(self, toUnpack):
        if isinstance(toUnpack, BinaryUnpacker):
            msgData = toUnpack
        else:
            msgData = BinaryUnpacker(toUnpack)

        self.magic = msgData.get(BINARY_CHUNK, 4)
        self.cmd = msgData.get(BINARY_CHUNK, 12).strip('\x00')
        length = msgData.get(UINT32)
        chksum = msgData.get(BINARY_CHUNK, 4)
        payload = msgData.get(BINARY_CHUNK, length)
        payload = verifyChecksum(payload, chksum)

        self.payload = PayloadMap[self.cmd]().unserialize(payload)

        if self.magic != MAGIC_BYTES:
            raise NetworkIDError, 'Message has wrong network bytes!'
        return self
Beispiel #26
0
    def unserialize(self, toUnpack, hasTimeField=True):
        if isinstance(toUnpack, BinaryUnpacker):
            addrData = toUnpack
        else:
            addrData = BinaryUnpacker(toUnpack)

        if hasTimeField:
            self.time = addrData.get(UINT32)

        self.services = addrData.get(UINT64)
        self.addrQuad = addrData.get(BINARY_CHUNK, 16)[-4:]
        self.port = addrData.get(UINT16, endianness=NETWORKENDIAN)

        self.services = int_to_bitset(self.services)
        self.addrQuad = binary_to_quad(self.addrQuad)
        return self
   def unserialize(self, toUnpack):
      if isinstance(toUnpack, BinaryUnpacker):
         msgData = toUnpack
      else:
         msgData = BinaryUnpacker( toUnpack )


      self.magic = msgData.get(BINARY_CHUNK, 4)
      self.cmd   = msgData.get(BINARY_CHUNK, 12).strip('\x00')
      length     = msgData.get(UINT32)
      chksum     = msgData.get(BINARY_CHUNK, 4)
      payload    = msgData.get(BINARY_CHUNK, length)
      payload    = verifyChecksum(payload, chksum)

      self.payload = PayloadMap[self.cmd]().unserialize(payload)

      if self.magic != MAGIC_BYTES:
         raise NetworkIDError, 'Message has wrong network bytes!'
      return self
Beispiel #28
0
   def unserialize(self, toUnpack):
      alertData = BinaryUnpacker( toUnpack )
      self.nonSigLength = alertData.get(INT8)
      self.version = alertData.get(UINT32)
      self.relayUntil = alertData.get(UINT64)
      self.expiration = alertData.get(UINT64)
      self.uniqueID = alertData.get(UINT32)
      self.cancelVal = alertData.get(UINT32)
      numCancel = alertData.get(INT8)
      for i in range(numCancel):
         self.cancelSet.append(alertData.get(UINT32))
      self.minVersion = alertData.get(UINT32)
      self.maxVersion = alertData.get(UINT32)
      numSubVer = alertData.get(INT8)
      for i in range(numSubVer):
         self.subVerSet.append(alertData.get(VAR_STR))
      self.priority = alertData.get(UINT32)
      self.comment = alertData.get(VAR_STR)
      self.statusBar = alertData.get(VAR_STR)
      self.reserved = alertData.get(VAR_STR)
      self.signature = alertData.get(VAR_STR)

      return self
Beispiel #29
0
    def unserialize(self, toUnpack):
        alertData = BinaryUnpacker(toUnpack)
        self.nonSigLength = alertData.get(INT8)
        self.version = alertData.get(UINT32)
        self.relayUntil = alertData.get(UINT64)
        self.expiration = alertData.get(UINT64)
        self.uniqueID = alertData.get(UINT32)
        self.cancelVal = alertData.get(UINT32)
        numCancel = alertData.get(INT8)
        for i in range(numCancel):
            self.cancelSet.append(alertData.get(UINT32))
        self.minVersion = alertData.get(UINT32)
        self.maxVersion = alertData.get(UINT32)
        numSubVer = alertData.get(INT8)
        for i in range(numSubVer):
            self.subVerSet.append(alertData.get(VAR_STR))
        self.priority = alertData.get(UINT32)
        self.comment = alertData.get(VAR_STR)
        self.statusBar = alertData.get(VAR_STR)
        self.reserved = alertData.get(VAR_STR)
        self.signature = alertData.get(VAR_STR)

        return self
Beispiel #30
0
 def testCopyPyOutPoint(self):
     outpoint = PyOutPoint().unserialize(BinaryUnpacker(ALL_ZERO_OUTPOINT))
     outpointCopy = outpoint.copy()
     self.assertEqual(outpoint.txHash, outpointCopy.txHash)
     self.assertEqual(outpoint.txOutIndex, outpointCopy.txOutIndex)
Beispiel #31
0
   def dataReceived(self, data):
      """
      Called by the reactor when data is received over the connection. 
      This method will do nothing if we don't receive a full message.
      """

      
      #print '\n\nData Received:',
      #pprintHex(binary_to_hex(data), withAddr=False)

      # Put the current buffer into an unpacker, process until empty
      self.recvData += data
      buf = BinaryUnpacker(self.recvData)

      messages = []
      while True:
         try:
            # recvData is only modified if the unserialize succeeds
            # Had a serious issue with references, so I had to convert 
            # messages to strings to guarantee that copies were being 
            # made!  (yes, hacky...)
            thisMsg = PyMessage().unserialize(buf)
            messages.append( thisMsg.serialize() )
            self.recvData = buf.getRemainingString()
         except NetworkIDError:
            LOGERROR('Message for a different network!' )
            if BLOCKCHAINS.has_key(self.recvData[:4]):
               LOGERROR( '(for network: %s)', BLOCKCHAINS[self.recvData[:4]])
            # Before raising the error, we should've finished reading the msg
            # So pop it off the front of the buffer
            self.recvData = buf.getRemainingString()
            return
         except UnknownNetworkPayload:
            return
         except UnpackerError:
            # Expect this error when buffer isn't full enough for a whole msg
            break

      # We might've gotten here without anything to process -- if so, bail
      if len(messages)==0:
         return


      # Finally, we have some message to process, let's do it
      for msgStr in messages:
         msg = PyMessage().unserialize(msgStr)
         cmd = msg.cmd

         # Log the message if netlog option
         if CLI_OPTIONS.netlog:
            LOGDEBUG( 'DataReceived: %s', msg.payload.command)
            if msg.payload.command == 'tx':
               LOGDEBUG('\t' + binary_to_hex(msg.payload.tx.thisHash))
            elif msg.payload.command == 'block':
               LOGDEBUG('\t' + msg.payload.header.getHashHex())
            elif msg.payload.command == 'inv':
               for inv in msg.payload.invList:
                  LOGDEBUG(('\tBLOCK: ' if inv[0]==2 else '\tTX   : ') + \
                                                      binary_to_hex(inv[1]))


         # We process version and verackk only if we haven't yet
         if cmd=='version' and not self.sentVerack:
            self.peerInfo = {}
            self.peerInfo['version'] = msg.payload.version
            self.peerInfo['subver']  = msg.payload.subver
            self.peerInfo['time']    = msg.payload.time
            self.peerInfo['height']  = msg.payload.height0
            LOGINFO('Received version message from peer:')
            LOGINFO('   Version:     %s', str(self.peerInfo['version']))
            LOGINFO('   SubVersion:  %s', str(self.peerInfo['subver']))
            LOGINFO('   TimeStamp:   %s', str(self.peerInfo['time']))
            LOGINFO('   StartHeight: %s', str(self.peerInfo['height']))
            self.sentVerack = True
            self.sendMessage( PayloadVerack() )
         elif cmd=='verack':
            self.gotVerack = True
            self.factory.handshakeFinished(self)
            #self.startHeaderDL()

         ####################################################################
         # Don't process any other messages unless the handshake is finished
         if self.gotVerack and self.sentVerack:
            self.processMessage(msg)
Beispiel #32
0
 def testPPrintPyOutPoint(self):
     # No return value - Should just print 0s
     outpoint = PyOutPoint().unserialize(BinaryUnpacker(ALL_ZERO_OUTPOINT))
     print "PyOutPoint PPrint Test. Expect all 0s: "
     outpoint.pprint()
Beispiel #33
0
    def dataReceived(self, data):
        """
      Called by the reactor when data is received over the connection. 
      This method will do nothing if we don't receive a full message.
      """

        #print '\n\nData Received:',
        #pprintHex(binary_to_hex(data), withAddr=False)

        # Put the current buffer into an unpacker, process until empty
        self.recvData += data
        buf = BinaryUnpacker(self.recvData)

        messages = []
        while True:
            try:
                # recvData is only modified if the unserialize succeeds
                # Had a serious issue with references, so I had to convert
                # messages to strings to guarantee that copies were being
                # made!  (yes, hacky...)
                thisMsg = PyMessage().unserialize(buf)
                messages.append(thisMsg.serialize())
                self.recvData = buf.getRemainingString()
            except NetworkIDError:
                LOGERROR('Message for a different network!')
                if BLOCKCHAINS.has_key(self.recvData[:4]):
                    LOGERROR('(for network: %s)',
                             BLOCKCHAINS[self.recvData[:4]])
                # Before raising the error, we should've finished reading the msg
                # So pop it off the front of the buffer
                self.recvData = buf.getRemainingString()
                return
            except UnpackerError:
                # Expect this error when buffer isn't full enough for a whole msg
                break

        # We might've gotten here without anything to process -- if so, bail
        if len(messages) == 0:
            return

        # Finally, we have some message to process, let's do it
        for msgStr in messages:
            msg = PyMessage().unserialize(msgStr)
            cmd = msg.cmd

            # Log the message if netlog option
            if CLI_OPTIONS.netlog:
                LOGDEBUG('DataReceived: %s', msg.payload.command)
                if msg.payload.command == 'tx':
                    LOGDEBUG('\t' + binary_to_hex(msg.payload.tx.thisHash))
                elif msg.payload.command == 'block':
                    LOGDEBUG('\t' + msg.payload.header.getHashHex())
                elif msg.payload.command == 'inv':
                    for inv in msg.payload.invList:
                        LOGDEBUG(('\tBLOCK: ' if inv[0]==2 else '\tTX   : ') + \
                                                            binary_to_hex(inv[1]))

            # We process version and verackk only if we haven't yet
            if cmd == 'version' and not self.sentVerack:
                self.peerInfo = {}
                self.peerInfo['version'] = msg.payload.version
                self.peerInfo['subver'] = msg.payload.subver
                self.peerInfo['time'] = msg.payload.time
                self.peerInfo['height'] = msg.payload.height0
                LOGINFO('Received version message from peer:')
                LOGINFO('   Version:     %s', str(self.peerInfo['version']))
                LOGINFO('   SubVersion:  %s', str(self.peerInfo['subver']))
                LOGINFO('   TimeStamp:   %s', str(self.peerInfo['time']))
                LOGINFO('   StartHeight: %s', str(self.peerInfo['height']))
                self.sentVerack = True
                self.sendMessage(PayloadVerack())
            elif cmd == 'verack':
                self.gotVerack = True
                self.factory.handshakeFinished(self)
                #self.startHeaderDL()

            ####################################################################
            # Don't process any other messages unless the handshake is finished
            if self.gotVerack and self.sentVerack:
                self.processMessage(msg)
Beispiel #34
0
 def testUnpackUnserializePyOutPoint(self):
     outpoint = PyOutPoint().unserialize(BinaryUnpacker(ALL_ZERO_OUTPOINT))
     self.assertEqual(outpoint.txHash, hex_to_binary('00' * 32))
     self.assertEqual(outpoint.txOutIndex, 0)