From 0087e04f579b92a8a8a27770af8cad6f230f93ab Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Wed, 20 Jun 2018 15:56:28 -0500 Subject: [PATCH] work on new blocks and encryption --- onionr/core.py | 39 +++++++++++------------- onionr/onionrproofs.py | 67 ++++++++++++++++++++++++++---------------- 2 files changed, 59 insertions(+), 47 deletions(-) diff --git a/onionr/core.py b/onionr/core.py index 15485dbc..91d72e7e 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -696,6 +696,7 @@ class Core: if len(jsonMeta) > 1000: raise onionrexceptions.InvalidMetadata('meta in json encoded form must not exceed 1000 bytes') + # encrypt block metadata/sig/content if encryptType == 'sym': if len(symKey) < self.requirements.passwordLength: raise onionrexceptions.SecurityError('Weak encryption key') @@ -711,31 +712,25 @@ class Core: else: raise onionrexceptions.InvalidPubkey(asymPeer + ' is not a valid base32 encoded ed25519 key') + powProof = onionrproofs.POW(data) + + # wait for proof to complete + powToken = powProof.waitForResult() + + powToken = base64.b64encode(powToken[1]) + try: + powToken = powToken.decode() + except AttributeError: + pass + finally: + break + + # compile metadata metadata['meta'] = jsonMeta metadata['sig'] = signature metadata['signer'] = signer - - powProof = onionrproofs.POW(data) - powToken = '' - # wait for proof to complete - try: - while True: - powToken = powProof.getResult() - if powToken == False: - time.sleep(0.3) - continue - powToken = base64.b64encode(powToken[1]) - try: - powToken = powToken.decode() - except AttributeError: - pass - finally: - break - except KeyboardInterrupt: - logger.warn("Got keyboard interrupt while working on inserting block, stopping.") - powProof.shutdown() - return '' - + metadata['powRandomToken'] = powToken + metadata['time'] = str(self._utils.getEpoch()) return retData diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index c1b3fb41..7f65bff7 100644 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -22,31 +22,6 @@ import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, lo import core class POW: - def pow(self, reporting = False, myCore = None): - startTime = math.floor(time.time()) - self.hashing = True - self.reporting = reporting - iFound = False # if current thread is the one that found the answer - answer = '' - heartbeat = 200000 - hbCount = 0 - - while self.hashing: - rand = nacl.utils.random() - token = nacl.hash.blake2b(rand + self.data).decode() - #print(token) - if self.puzzle == token[0:self.difficulty]: - self.hashing = False - iFound = True - break - - if iFound: - endTime = math.floor(time.time()) - if self.reporting: - logger.debug('Found token after %s seconds: %s' % (endTime - startTime, token), timestamp=True) - logger.debug('Random value was: %s' % base64.b64encode(rand).decode()) - self.result = (token, rand) - def __init__(self, data, threadCount = 5): self.foundHash = False self.difficulty = 0 @@ -77,6 +52,31 @@ class POW: return + def pow(self, reporting = False, myCore = None): + startTime = math.floor(time.time()) + self.hashing = True + self.reporting = reporting + iFound = False # if current thread is the one that found the answer + answer = '' + heartbeat = 200000 + hbCount = 0 + + while self.hashing: + rand = nacl.utils.random() + token = nacl.hash.blake2b(rand + self.data).decode() + #print(token) + if self.puzzle == token[0:self.difficulty]: + self.hashing = False + iFound = True + break + + if iFound: + endTime = math.floor(time.time()) + if self.reporting: + logger.debug('Found token after %s seconds: %s' % (endTime - startTime, token), timestamp=True) + logger.debug('Random value was: %s' % base64.b64encode(rand).decode()) + self.result = (token, rand) + def shutdown(self): self.hashing = False self.puzzle = '' @@ -96,3 +96,20 @@ class POW: self.result = False return retVal + + def waitForResult(self): + ''' + Returns the result only when it has been found, False if not running and not found + ''' + result = False + try: + while True: + result = self.getResult() + if not self.hashing: + break + else: + time.sleep(2) + except KeyboardInterrupt: + self.shutdown() + logger.warn('Got keyboard interrupt while waiting for POW result, stopping') + return result \ No newline at end of file