def pow(self, reporting=False): startTime = math.floor(time.time()) self.hashing = True self.reporting = reporting iFound = False # if current thread is the one that found the answer nonce = int(binascii.hexlify(nacl.utils.random(64)), 16) while self.hashing: #token = nacl.hash.blake2b(rand + self.data).decode() self.metadata['pow'] = nonce payload = json.dumps(self.metadata).encode() + b'\n' + self.data token = hashers.sha3_hash(payload) try: # on some versions, token is bytes token = token.decode() except AttributeError: pass if self.puzzle == token[0:self.difficulty]: self.hashing = False iFound = True self.result = payload break nonce += 1 if iFound: endTime = math.floor(time.time()) if self.reporting: logger.debug('Found token after %s seconds: %s' % (endTime - startTime, token), timestamp=True)
def client_api_insert_block(): encrypt: bool = False insert_data: JSONSerializable = request.get_json(force=True) message = insert_data['message'] message_hash = bytesconverter.bytes_to_str(hashers.sha3_hash(message)) kv: 'DeadSimpleKV' = g.too_many.get_by_string('DeadSimpleKV') # Detect if message (block body) is not specified if type(message) is None: return 'failure due to unspecified message', 400 # Detect if block with same message is already being inserted if message_hash in kv.get('generating_blocks'): return 'failure due to duplicate insert', 400 else: kv.get('generating_blocks').append(message_hash) encrypt_type = '' sign = True meta = {} to = '' try: if insert_data['encrypt']: to = insert_data['to'].strip() if "-" in to: to = mnemonickeys.get_base32(to) encrypt_type = 'asym' except KeyError: pass try: if not insert_data['sign']: sign = False except KeyError: pass try: bType = insert_data['type'] except KeyError: bType = 'bin' try: meta = json.loads(insert_data['meta']) except KeyError: pass try: # The setting in the UI is for if forward secrecy is enabled, not disabled disable_forward_secrecy = not insert_data['forward'] except KeyError: disable_forward_secrecy = False threading.Thread(target=onionrblocks.insert, args=(message, ), kwargs={ 'header': bType, 'encryptType': encrypt_type, 'sign': sign, 'asymPeer': to, 'meta': meta, 'disableForward': disable_forward_secrecy }).start() return Response('success')
def do_pow(self, pipe): """find partial hash colision generating nonce for a block""" nonce = 0 data = self.data metadata = self.metadata metadata['n'] = secrets.randbits(16) puzzle = self.puzzle difficulty = self.difficulty try: while True: #logger.info('still running', terminal=True) # Break if shutdown received try: if pipe.poll() and pipe.recv() == 'shutdown': break except KeyboardInterrupt: break # Load nonce into block metadata metadata['c'] = nonce # Serialize metadata, combine with block data payload = json.dumps(metadata).encode() + b'\n' + data # Check sha3_256 hash of block, compare to puzzle # Send payload if puzzle finished token = sha3_hash(payload) # ensure token is string token = bytesconverter.bytes_to_str(token) if puzzle == token[0:difficulty]: pipe.send(payload) break nonce += 1 except KeyboardInterrupt: pass
def client_api_insert_block(): encrypt = False bData = request.get_json(force=True) message = bData['message'] message_hash = bytesconverter.bytes_to_str(hashers.sha3_hash(message)) # Detect if message (block body) is not specified if type(message) is None: return 'failure due to unspecified message', 400 # Detect if block with same message is already being inserted if message_hash in g.too_many.get_by_string( "OnionrCommunicatorDaemon").generating_blocks: return 'failure due to duplicate insert', 400 else: g.too_many.get_by_string( "OnionrCommunicatorDaemon").generating_blocks.append(message_hash) subject = 'temp' encryptType = '' sign = True meta = {} to = '' try: if bData['encrypt']: to = bData['to'].strip() if "-" in to: to = mnemonickeys.get_base32(to) encrypt = True encryptType = 'asym' except KeyError: pass try: if not bData['sign']: sign = False except KeyError: pass try: bType = bData['type'] except KeyError: bType = 'bin' try: meta = json.loads(bData['meta']) except KeyError: pass threading.Thread(target=onionrblocks.insert, args=(message, ), kwargs={ 'header': bType, 'encryptType': encryptType, 'sign': sign, 'asymPeer': to, 'meta': meta }).start() return Response('success')
def store(data, blockHash=''): if not stringvalidators.validate_hash(blockHash): raise ValueError ourHash = hashers.sha3_hash(data) if blockHash != '': if not ourHash == blockHash: raise ValueError( 'Hash specified does not meet internal hash check') else: blockHash = ourHash if DB_ENTRY_SIZE_LIMIT >= sys.getsizeof(data): _dbInsert(blockHash, data) else: with open('%s/%s.dat' % (filepaths.block_data_location, blockHash), 'wb') as blockFile: blockFile.write(data)
def store(data, block_hash=''): if not stringvalidators.validate_hash(block_hash): raise ValueError ourHash = hashers.sha3_hash(data) if block_hash != '': if not ourHash == block_hash: raise ValueError( 'Hash specified does not meet internal hash check') else: block_hash = ourHash if DB_ENTRY_SIZE_LIMIT >= sys.getsizeof(data): _dbInsert(block_hash, data) else: with open(f'{block_data_location}/{block_hash}{BLOCK_EXPORT_FILE_EXT}', 'wb') as blck_file: blck_file.write(data)