예제 #1
0
파일: r2ac.py 프로젝트: eduardo-arruda/r2ac
    def addBlock(self, devPubKey):
        aesKey = ''
        t1 = time.time()
        blk = chainFunctions.findBlock(devPubKey)
        if (blk != False and blk.index > 0):
            aesKey = findAESKey(devPubKey)
            if aesKey == False:
                logger.debug("Using existent block data")
                aesKey = generateAESKey(blk.publicKey)
        else:
            #logger.debug("Create New Block Header")
            logger.debug("***** New Block: Chain size:" +
                         str(chainFunctions.getBlockchainSize()))
            bl = chainFunctions.createNewBlock(devPubKey, gwPvt)
            sendBlockToPeers(
                bl)  # --->> this function should be run in a different thread.
            # try:
            #     #thread.start_new_thread(sendBlockToPeers,(bl))
            #     t1 = sendBlks(1, bl)
            #     t1.start()
            # except:
            #     print "thread not working..."
            aesKey = generateAESKey(devPubKey)

        encKey = criptoFunctions.encryptRSA2(devPubKey, aesKey)
        t2 = time.time()
        logger.debug("=====1=====>time to generate key: " +
                     '{0:.12f}'.format((t2 - t1) * 1000))

        return encKey
예제 #2
0
파일: r2ac.py 프로젝트: eduardo-arruda/r2ac
    def addTransaction(self, devPublicKey, encryptedObj):
        global gwPvt
        global gwPub
        t1 = time.time()
        blk = chainFunctions.findBlock(devPublicKey)

        if (blk != False and blk.index > 0):
            devAESKey = findAESKey(devPublicKey)
            if (devAESKey != False):
                # plainObject contains [Signature + Time + Data]

                plainObject = criptoFunctions.decryptAES(
                    encryptedObj, devAESKey)
                signature = plainObject[:-20]  # remove the last 20 chars
                devTime = plainObject[-20:
                                      -4]  # remove the 16 char of timestamp
                deviceData = plainObject[
                    -4:]  # retrieve the las 4 chars which are the data

                d = devTime + deviceData
                isSigned = criptoFunctions.signVerify(d, signature,
                                                      devPublicKey)

                if isSigned:
                    deviceInfo = DeviceInfo.DeviceInfo(signature, devTime,
                                                       deviceData)
                    nextInt = blk.transactions[len(blk.transactions) -
                                               1].index + 1
                    signData = criptoFunctions.signInfo(gwPvt, str(deviceInfo))
                    gwTime = "{:.0f}".format(((time.time() * 1000) * 1000))
                    # code responsible to create the hash between Info nodes.
                    prevInfoHash = criptoFunctions.calculateTransactionHash(
                        chainFunctions.getLatestBlockTransaction(blk))

                    transaction = Transaction.Transaction(
                        nextInt, prevInfoHash, gwTime, deviceInfo, signData)

                    # send to consensus
                    #if not consensus(newBlockLedger, gwPub, devPublicKey):
                    #    return "Not Approved"

                    chainFunctions.addBlockTransaction(blk, transaction)
                    logger.debug(
                        "block added locally... now sending to peers..")
                    t2 = time.time()
                    logger.debug(
                        "=====2=====>time to add transaction in a block: " +
                        '{0:.12f}'.format((t2 - t1) * 1000))
                    sendTransactionToPeers(
                        devPublicKey, transaction
                    )  # --->> this function should be run in a different thread.
                    #print("all done")
                    return "ok!"
                else:
                    return "Invalid Signature"
            return "key not found"
예제 #3
0
파일: r2ac.py 프로젝트: eduardo-arruda/r2ac
 def updateBlockLedger(self, pubKey, block):
     b = pickle.loads(block)
     t1 = time.time()
     logger.debug("Received Transaction #:" + (str(b.index)))
     blk = chainFunctions.findBlock(pubKey)
     if blk != False:
         if not (chainFunctions.blockContainsBlockTransaction(blk, b)):
             if validatorClient:
                 isTransactionValid(b, pubKey)
             chainFunctions.addBlockTransaction(blk, b)
     t2 = time.time()
     logger.debug("=====3=====>time to update transaction received: " +
                  '{0:.12f}'.format((t2 - t1) * 1000))
     return "done"
예제 #4
0
파일: r2ac.py 프로젝트: eduardo-arruda/r2ac
def isValidBlock(self, data, gatewayPublicKey, devicePublicKey, peer):
    newBlock = pickle.loads(data)
    blockIoT = chainFunctions.findBlock(devicePublicKey)
    consensus = True
    if blockIoT == False:
        print("Block not found in IoT ledger")
        consensus = False

    lastBlock = blockIoT.blockLedger[len(blockIoT.blockLedger) - 1]
    if newBlock.index != lastBlock.index + 1:
        print("New blovk Index not valid")
        consensus = False

    if lastBlock.calculateHashForBlockLedger(
            lastBlock) != newBlock.previousHash:
        print("New block previous hash not valid")
        consensus = False

    now = "{:.0f}".format(((time.time() * 1000) * 1000))

    # check time
    if not (newBlock.timestamp > newBlock.signature.timestamp
            and newBlock.timestamp < now):
        print("New block time not valid")
        consensus = False

    # check device time
    if not (newBlock.signature.timestamp > lastBlock.signature.timestamp
            and newBlock.signature.timestamp < now):
        print("New block device time not valid")
        consensus = False

    # check device signature with device public key
    if not (criptoFunctions.signVerify(newBlock.signature.data,
                                       newBlock.signature.deviceSignature,
                                       gatewayPublicKey)):
        print("New block device signature not valid")
        consensus = False
    peer = getPeer(peer)
    obj = peer.object
    obj.receiveBlockConsensus(data, gatewayPublicKey, devicePublicKey,
                              consensus)