def __init__(self): self.app = Flask(__name__) self.cryptor = NooCrypto() @self.app.route('/start/generatePair', methods=['GET','POST']) def generatePair(): if not request: abort(400) self.cryptor.generateKeys() return json.dumps({"PRIVATE": self.cryptor.getPrivateKey(), "PUBLIC": self.cryptor.getPublicKey()}, separators=(',', ':')) @self.app.route('/start/setPrivate', methods=['GET','POST']) def setPrivate(): if not request: abort(400) private = request.args.get('private') password = request.args.get('password') crf = cryptoFernet(password) token = crf.crypt(private) pFile = open("PRIVATE", "w") pFile.write(token.decode()) return json.dumps({"PRIVATE": self.cryptor.getPrivateKey(), "PUBLIC": self.cryptor.getPublicKey()}, separators=(',', ':'))
def __init__(self, privateKey): self.app = Flask(__name__) self.cryptor = NooCrypto() self.cryptor.setPrivateKey(privateKey) self.dataFieldsMas = <%FIELDS%> self.stemIP = "<%STEMIP%>" self.stemPK = "<%STEMPK%>" self.redis_host = os.getenv("REDIS_PORT_6379_TCP_ADDR") or 'localhost' self.redis_port = os.getenv("REDIS_PORT_6379_TCP_PORT") or '6379' self.redis = redis.StrictRedis(self.redis_host, self.redis_port, db=2) @self.app.route('/shard/getStatData', methods=['GET','POST']) def saveMapElement(): if not request: abort(400) jsond = {} for field in self.dataFieldsMas: if self.redis.get(field) is not None: jsond.update({field: self.redis.get(field)}) jsonstr = json.dumps(jsond) sign = self.cryptor.signMessage(jsonstr) return json.dumps({"DATA": jsond,"SIGNATURE": sign}, separators=(',', ':'))
def createPrecommitBlock(cryptor:NooCrypto, ver:str, transactions:set, bheight): jtransactions = [] for tran in transactions: jtransactions.append(json.loads(tran)) block = json.dumps({'TT': 'BL', 'SENDER': cryptor.getPublicKey(), 'VERSION': ver, 'TCOUNT': len(jtransactions), "BHEIGHT": bheight, 'TRANSACTIONS':jtransactions}, separators=(',', ':')) sign = cryptor.signMessage(block) jblock = json.loads(block) jblock.update({'SIGNATURE':sign}) return json.dumps(jblock, separators=(',', ':'))
class SignChecker(QObject): validTran = pyqtSignal(str, str) def __init__(self, parent=None): super().__init__(parent) self.__cryptor = NooCrypto() @pyqtSlot(str, str) def checkTran(self, address, packet): try: jsonPacket = json.loads(packet) except json.JSONDecodeError: print("WRONG JSON PACKET, IN CHECKTRAN WITH 2 ARG") return False signature = jsonPacket.pop("SIGNATURE", None) sender = jsonPacket.get("SENDER", None) if signature and sender: print("ПРОВЕРЯЮ БЛОК:", json.dumps(jsonPacket, separators=(',', ':'))) print("ПОДПИСЬ:", signature) print("КЛЮЧ:", sender) if self.__cryptor.verifyMessage( signature, sender, json.dumps(jsonPacket, separators=(',', ':'))): self.validTran.emit(address, packet) return True else: print("WRONG SIGNATURE", self.__cryptor.getLastError()) return False def checkTran(self, packet): try: jsonPacket = json.loads(packet) except json.JSONDecodeError: print("WRONG JSON PACKET, IN CHECKTRAN WITH 1 ARG") return False signature = jsonPacket.pop("SIGNATURE", None) sender = jsonPacket.get("SENDER", None) if signature and sender: if self.__cryptor.verifyMessage( signature, sender, json.dumps(jsonPacket, separators=(',', ':'))): return True else: print("WRONG SIGNATURE", self.__cryptor.getLastError()) return False
def __init__(self): super().__init__() self.slavesNodes = [] self.slave_max_count = 20 self.blocks_in_genesis = 100000 self.redis_host = os.getenv("REDIS_PORT_6379_TCP_ADDR") or 'localhost' self.redis_port = os.getenv("REDIS_PORT_6379_TCP_PORT") or '6379' self.redis = redis.StrictRedis(self.redis_host, self.redis_port, db=0) self.signChecker = SignChecker() self.lastCheckBlockBalance = 0 self.BALANCE = {} self.currentTokenType = "NZT" self.blockReward = 10 self.timeToBlock = 5*1000 #5 seconds self.lastChoiseNode = ("ip","puk") #------------------------------------------------ #self.cryptor.generateKeys() self.cryptor = NooCrypto() pKey = "000bd3006e1d880a9b2975a99c524b1e774a8eaddf8e981fff7a9f0cef2da7c0" self.cryptor.setPrivateKey(pKey) self.redis.flushall()
def createPurifierBlock(cryptor:NooCrypto, addressList:list, keyList:list, bheight:int): temp = json.dumps({'TT': 'PURIFIER', 'SENDER': cryptor.getPublicKey(), 'CLEAN_LIST': addressList, 'CLEAN_KEY_LIST': keyList, "BHEIGHT": bheight}, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE':sign}) return json.dumps(jtemp, separators=(',', ':'))
def applicantTransacton(cryptor:NooCrypto, ip:str): # Транзакция заявка на участие в следующем туре валидации temp = json.dumps({'TT': 'AT', 'SENDER': cryptor.getPublicKey(), 'IPADR': ip, "TST": str(int(time.time()))}, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE':sign}) return json.dumps(jtemp, separators=(',', ':'))
def chooserTransaction(cryptor:NooCrypto, blockchain_height): # Транзакция выбора ноды для работы temp = json.dumps({'TT': 'CT', 'SENDER': cryptor.getPublicKey(), "NBH": blockchain_height, "TST": str(int(time.time()))}, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE':sign}) return json.dumps(jtemp, separators=(',', ':'))
def giveAllSizeBlock(cryptor:NooCrypto, NBH:int): temp = json.dumps({'TT': 'ASB', 'SENDER': cryptor.getPublicKey(), "NBH": NBH}, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE':sign}) return json.dumps(jtemp, separators=(',', ':'))
def createCommitBlockForResendHash(cryptor:NooCrypto, block:str, bheight:int): block = json.dumps({'TT': 'CBRH', 'SENDER': cryptor.getPublicKey(), "HASH":NooTree.hash(block), "STEM_SIGNATURE": cryptor.signMessage(block), "BHEIGHT": bheight}, separators=(',', ':')) sign = cryptor.signMessage(block) jblock = json.loads(block) jblock.update({'SIGNATURE':sign}) return json.dumps(jblock, separators=(',', ':'))
def createSignaturePrecommitedBlock(cryptor:NooCrypto, pblock:str, hash:str): temp = json.dumps({'TT': 'SG', 'SENDER': cryptor.getPublicKey(), 'HASH': hash,'SIGNPB': cryptor.signMessage(pblock)}, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE':sign}) return json.dumps(jtemp, separators=(',', ':'))
def createCommitBlock(cryptor:NooCrypto, preCommitedBlock:str, signs:list): block = json.dumps({'TT': 'CB', 'SENDER': cryptor.getPublicKey(), "BLOCK" :json.loads(preCommitedBlock), "SIGNATURES":signs}, separators=(',', ':')) sign = cryptor.signMessage(block) jblock = json.loads(block) jblock.update({'SIGNATURE':sign}) return json.dumps(jblock, separators=(',', ':'))
def createCommitBlockForResend(cryptor:NooCrypto, block:str): block = json.dumps({'TT': 'CBR', 'SENDER': cryptor.getPublicKey(), "BLOCK" :json.loads(block)}, separators=(',', ':')) sign = cryptor.signMessage(block) jblock = json.loads(block) jblock.update({'SIGNATURE':sign}) return json.dumps(jblock, separators=(',', ':'))
def __init__(self, parent=None): super().__init__(parent) self.__cryptor = NooCrypto()
def createСomplaint(cryptor:NooCrypto, evidence_block:str, evidence_transaction:str): temp = json.dumps({'TT': 'COMPLAINT', 'SENDER': cryptor.getPublicKey(), 'EVIDENCE_BLOCK': evidence_block, 'EVIDENCE_TRANSACTION': evidence_transaction}, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE':sign}) return json.dumps(jtemp, separators=(',', ':'))
def standartTransacton(cryptor:NooCrypto, reciever:str, token_count:int, token_type:str): # Стандартная транзакция перевода средств temp = json.dumps({'TT': 'ST', 'SENDER': cryptor.getPublicKey(), 'RECEIVER': reciever, 'TTOKEN':token_type, 'CTOKEN': str(token_count), "TST": str(int(time.time()))}, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE':sign}) return json.dumps(jtemp, separators=(',', ':'))
def universalPacket(cryptor:NooCrypto, type:str, value): temp = json.dumps({'TT': type, 'DATA': value, 'SENDER': cryptor.getPublicKey()}, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE':sign}) return json.dumps(jtemp, separators=(',', ':'))
def badAnswer(cryptor:NooCrypto): temp = json.dumps({'TT': 'BAN', 'SENDER': cryptor.getPublicKey()}, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE':sign}) return json.dumps(jtemp, separators=(',', ':'))
def myBenchmarkResult(cryptor:NooCrypto, result:str, start:str): temp = json.dumps({'TT': 'MBR', 'SENDER': cryptor.getPublicKey(), 'RESULT': result, 'START': start}, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE':sign}) return json.dumps(jtemp, separators=(',', ':'))
def benchmarkTesting(cryptor:NooCrypto, uuid:str): temp = json.dumps({'TT': 'BT', 'SENDER': cryptor.getPublicKey(), 'START': uuid}, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE':sign}) return json.dumps(jtemp, separators=(',', ':'))
def __init__(self): self.app = Flask(__name__) self.cryptor = NooCrypto() @self.app.route('/img/<path:path>') def send_img(path): return send_from_directory('static/img', path) @self.app.route('/js/<path:path>') def send_js(path): return send_from_directory('static/js', path) @self.app.route('/css/<path:path>') def send_css(path): return send_from_directory('static/css', path) @self.app.route('/static/<path:path>') def send_html(path): return send_from_directory('static', path) @self.app.route('/start/generatePair', methods=['GET', 'POST']) def generatePair(): if not request: abort(400) self.cryptor.generateKeys() return json.dumps( { "PRIVATE": self.cryptor.getPrivateKey(), "PUBLIC": self.cryptor.getPublicKey() }, separators=(',', ':')) @self.app.route('/start/setPrivate', methods=['GET', 'POST']) def setPrivate(): if not request: abort(400) private = request.args.get('private') password = request.args.get('password') crf = cryptoFernet(password) token = crf.crypt(private) pFile = open("PRIVATE", "w") pFile.write(token.decode()) return json.dumps({}, separators=(',', ':')) @self.app.route('/start/startSlave', methods=['GET', 'POST']) def startSlave(): if not request: abort(400) password = request.args.get('password') os.system("slaveNode.py " + password) os.system("<%START BINARY COMMAND%>") print("RETURN!") return json.dumps({"START": "OK"}, separators=(',', ':')) @self.app.route('/start/getPrivateStatus', methods=['GET', 'POST']) def getPrivateStatus(): if not request: abort(400) if os.path.exists("PRIVATE"): return json.dumps({"PRIVATE": True}, separators=(',', ':')) else: return json.dumps({"PRIVATE": False}, separators=(',', ':'))
class Stem(QObject): floodPacket = pyqtSignal(str, str, int) sendPacket = pyqtSignal(str, str, int) setPurifyList = pyqtSignal(list) signalToMakePurifi = pyqtSignal() def __init__(self): super().__init__() self.slavesNodes = [] self.slave_max_count = 20 self.blocks_in_genesis = 100000 self.redis_host = os.getenv("REDIS_PORT_6379_TCP_ADDR") or 'localhost' self.redis_port = os.getenv("REDIS_PORT_6379_TCP_PORT") or '6379' self.redis = redis.StrictRedis(self.redis_host, self.redis_port, db=0) self.signChecker = SignChecker() self.lastCheckBlockBalance = 0 self.BALANCE = {} self.currentTokenType = "NZT" self.blockReward = 10 self.timeToBlock = 5*1000 #5 seconds self.lastChoiseNode = ("ip","puk") #------------------------------------------------ #self.cryptor.generateKeys() self.cryptor = NooCrypto() pKey = "000bd3006e1d880a9b2975a99c524b1e774a8eaddf8e981fff7a9f0cef2da7c0" self.cryptor.setPrivateKey(pKey) self.redis.flushall() #------------------------------------------------ # self.tokenomics.NZT_mas = 23000000 # self.tokenomics.vote_price = 1000 # self.tokenomics.vote_threshold = 15 #15 percent # self.tokenomics.noosphere_pkey = "MIGEAgEAMBAGByqGSM49AgEGBSuBBAAKBG0wawIBAQQg2T2hOBOge31fUR2WupCLA9SM/K4hBM/pLAWpj/YMOTKhRANCAATL3O9VXOpQSFWLbXHpIHinepF6iO6EPOo1ae5ykS0ymFCzB/1Y676DbEkpcQnYbn7iAYsXQiaz/FRUPCrndO9p" # self.tokenomics.min_fee = 0.01 # self.tokenomics.max_fee = 0.1 # self.tokenomics.slave_reward = 0.14 # self.tokenomics.stem_reward = 0.007 #------------------------------------------------ def createTest(self): self.benchmark.generateStemResult() def chooserPacket(self): print("CHOOSER") blockchain_height = self.redis.zcard("NOOCHAIN") print("CHOOSER PAKET BH:", blockchain_height) chooseNode = self.slavesNodes[randint(0, (len(self.slavesNodes) - 1))] packet = JsonPackets.chooserTransaction(self.cryptor, blockchain_height) self.sendPacket.emit(chooseNode, packet, 1) return @pyqtSlot(str, str) def handler(self, address: str, packet: str): if not self.signChecker.checkTran(packet): return jpacket = json.loads(packet) if jpacket["TT"] == "ASB": blockchain_height = self.redis.zcard("NOOCHAIN") if blockchain_height < jpacket["NBH"]: packet = JsonPackets.badAnswer(self.cryptor) else: data = self.redis.zrange("NOOCHAIN", jpacket["NBH"], jpacket["NBH"] + 1) if len(data) == 0: packet = JsonPackets.badAnswer(self.cryptor) else: packet = JsonPackets.createCommitBlockForResend(self.cryptor, data[0]) self.sendPacket.emit(address, packet, 1) return if jpacket["TT"] == "CB": blockchain_height = self.redis.zcard("NOOCHAIN") # Проверить что подписей достаточно и что они корректны !!! # RESEND TO ALL NODES NEW BLOCK # temp = jpacket['BLOCK'] newBlock = json.dumps(temp, separators=(',', ':')) newBlock = JsonPackets.createCommitBlockForResendHash(self.cryptor, newBlock, blockchain_height) self.floodPacket.emit(newBlock, str(), 1) # РАССЫЛКА ПРИНЯТОГО БЛОКА ВСЕМ ЧЛЕНАМ СЕТИ # RESEND TO ALL NODES NEW BLOCK # self.redis.zadd("NOOCHAIN", blockchain_height, json.dumps(jpacket['BLOCK'], separators=(',', ':'))) blockchain_height += 1 if blockchain_height % self.blocks_in_genesis == 0: self.signalToMakePurifi.emit() # Отправка сигнала на создание нового блока генезиса self.checkBalance() return if jpacket["TT"] == "MBR": if self.benchmark.testNodeResult(jpacket["START"], jpacket["RESULT"], jpacket["SENDER"]): print ("YOU WIN!") else: print ("YOU FAIL!") return if jpacket["TT"] == "BAN": return if jpacket["TT"] == "WBA": #WANT BE APPLICANT benchUUID = self.benchmark.getStemResult(jpacket["SENDER"]) print("RECIVE BEAPP") if benchUUID is not None: packet = JsonPackets.benchmarkTesting(self.cryptor, benchUUID) print("SEND BT") self.sendPacket.emit(address, packet, 1) else: print("NO ANY TEST") return # ========================= FOR SUPER STEM =============================== if jpacket["TT"] == "GYID": # GIVE YOUR IDENTITY packet = JsonPackets.universalPacket(self.cryptor, "TMID", self.currentTokenType) self.sendPacket.emit(address, packet, 1) return if jpacket["TT"] == "GYBL": # GIVE YOUR BLOCK blockchain_height = self.redis.zcard("NOOCHAIN") if blockchain_height < jpacket["NBH"]: packet = JsonPackets.badAnswer(self.cryptor) else: data = self.redis.zrange("NOOCHAIN", jpacket["NBH"], -1) packet = JsonPackets.universalPacket(self.cryptor, "MBL", data) self.sendPacket.emit(address, packet, 1) return # END OF FUNC return def balanceMainWorkRedis(self, bchain:list): for block in bchain: jblock = json.loads(block) if jblock['TT'] == "BL": trans = jblock['TRANSACTIONS'] if self.redis.zscore("BALANCE", jblock['SENDER']) is None: self.redis.zadd("BALANCE", 0, jblock['SENDER']) blockSenderBalance = self.redis.zscore("BALANCE", jblock['SENDER']) blockSenderBalance += self.blockReward print("ADDING BALANCE TO VALIDATOR:", blockSenderBalance, jblock['SENDER']) self.redis.zadd("BALANCE", blockSenderBalance, jblock['SENDER']) for tran in trans: if tran["TT"] == "ST" and tran["TTOKEN"] == self.currentTokenType and float(tran["CTOKEN"]) > 0: if self.redis.zscore("BALANCE", tran['SENDER']) is None: self.redis.zadd("BALANCE", 0, tran['SENDER']) if self.redis.zscore("BALANCE", tran['RECEIVER']) is None: self.redis.zadd("BALANCE", 0, tran['RECEIVER']) senderBalance = self.redis.zscore("BALANCE", tran['SENDER']) receiverBalance = self.redis.zscore("BALANCE", tran["RECEIVER"]) senderBalance = senderBalance - float(tran["CTOKEN"]) receiverBalance = receiverBalance + float(tran["CTOKEN"]) self.redis.zadd("BALANCE", senderBalance, tran["SENDER"]) self.redis.zadd("BALANCE", receiverBalance, tran["RECEIVER"]) def checkBalance(self, updateLen = -1): bheight = self.redis.zcard("NOOCHAIN") if updateLen == -1: bchain = self.redis.zrange("NOOCHAIN", self.lastCheckBlockBalance, bheight) self.lastCheckBlockBalance = bheight self.balanceMainWorkRedis(bchain) else: ### STEP MODE step = 0 while step < bheight: nextStep = step + updateLen if nextStep > bheight: nextStep = bheight bchain = self.redis.zrange("NOOCHAIN", step, nextStep) step = nextStep self.balanceMainWorkRedis(bchain) return def purifierMaker(self): blockchain_height = self.redis.zcard("NOOCHAIN") purifiBlock = str() if blockchain_height == 0: # Первый запуск netSettings = NetSettings() print(netSettings.keysList) print(netSettings.purifierList) purifiBlock = JsonPackets.createFirstPurifierBlock(self.cryptor, netSettings.purifierList, netSettings.keysList, blockchain_height) jpurify = json.loads(purifiBlock) print(purifiBlock) purify_list = jpurify["CLEAN_LIST"] print(purify_list) if len(purify_list) != 0: self.slavesNodes = purify_list self.setPurifyList.emit(purify_list) self.redis.zadd("NOOCHAIN", blockchain_height, purifiBlock) self.floodPacket.emit(purifiBlock, str(), 1) # РАССЫЛКА СТАРТОВОГО ПАКЕТА ВСЕМ НОВЫМ УЧАСТНИКАМ СЕТИ else: if blockchain_height % self.blocks_in_genesis == 0: last_genesis_block_number = ((blockchain_height // self.blocks_in_genesis) * self.blocks_in_genesis) - self.blocks_in_genesis genesis_zone = self.redis.zrange("NOOCHAIN", last_genesis_block_number + 1, -1) applicant_transactions = [] for block in genesis_zone: jblock = json.loads(block) for tran in jblock["TRANSACTIONS"]: if isinstance(tran, dict): jtran = tran else: jtran = json.loads(tran) if jtran["TT"] != "AT": continue else: if applicant_transactions.count(jtran["IPADR"]) == 0: applicant_transactions.append(jtran["IPADR"]) if len(applicant_transactions) == 0: self.setPurifyList.emit(self.slavesNodes) purifiBlock = JsonPackets.createPurifierBlock(self.cryptor, self.slavesNodes, blockchain_height) self.redis.zadd("NOOCHAIN", blockchain_height, purifiBlock) else: if len(applicant_transactions) < self.slave_max_count: self.slavesNodes = list(set(applicant_transactions)) else: self.slavesNodes = list(set(sample(applicant_transactions, self.slave_max_count))) self.setPurifyList.emit(self.slavesNodes) purifiBlock = JsonPackets.createPurifierBlock(self.cryptor, self.slavesNodes, blockchain_height) self.redis.zadd("NOOCHAIN", blockchain_height, purifiBlock) else: #Если нода запущена после остановки работы last_genesis_block_number = (blockchain_height // self.blocks_in_genesis) * self.blocks_in_genesis print("LAST NUMBER:", last_genesis_block_number) genesis_zone = self.redis.zrange("NOOCHAIN", last_genesis_block_number, last_genesis_block_number) print("GENESIS: " + str(genesis_zone[0])) last_genesis_block = json.loads(genesis_zone[0].decode()) self.slavesNodes = list(set(last_genesis_block["CLEAN_LIST"])) self.setPurifyList.emit(self.slavesNodes) purifiBlock = genesis_zone[0].decode() # ПЕРЕД ВЫБОРОМ И РАССЫЛКИ, МОЖНО (НУЖНО) ПРОВЕСТИ ПРОВЕРКУ ДОСТУПНОСТИ УЗЛОВ # print ("RESEND ", purifiBlock) self.floodPacket.emit(purifiBlock, str(), 1) # РАССЫЛКА НОВОГО ПАКЕТА ВСЕМ НОВЫМ УЧАСТНИКАМ СЕТИ