From 8a9d1326fb3e4bb6b231c494d001410129832924 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Thu, 26 Apr 2018 20:15:30 -0500 Subject: [PATCH] signed blocks related crashes fixed hopefully --- onionr/communicator.py | 11 ++++++++--- onionr/onionrcrypto.py | 5 ++++- onionr/onionrutils.py | 11 +++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/onionr/communicator.py b/onionr/communicator.py index 42ce8b56..fe2eeaaf 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -262,7 +262,8 @@ class OnionrCommunicate: # deal with block metadata blockContent = self._core.getData(i) try: - blockMetadata = json.loads(self._core.getData(i)).split('}')[0] + '}' + #blockMetadata = json.loads(self._core.getData(i)).split('}')[0] + '}' + blockMetadata = json.loads(self._core.getData(i).split(b'}')[0] + b'}') try: blockMetadata['sig'] blockMetadata['id'] @@ -270,15 +271,18 @@ class OnionrCommunicate: pass else: creator = self._utils.getPeerByHashId(blockMetadata['id']) - if self._crypto.edVerify(blockContent, creator): + if self._crypto.edVerify(blockContent.split(b'}')[1], creator, blockMetadata['sig'], encodedData=True): self._core.updateBlockInfo(i, 'sig', 'true') else: self._core.updateBlockInfo(i, 'sig', 'false') try: - blockMetadata['type'] + logger.info('Block type is ' + blockMetadata['type']) + self._core.updateBlockInfo(i, 'dataType', blockMetadata['type']) except KeyError: + logger.warn('Block has no type') pass except json.decoder.JSONDecodeError: + logger.warn('Could not decode block metadata') pass return @@ -312,6 +316,7 @@ class OnionrCommunicate: self._core.setData(data) logger.info('Successfully obtained data for ' + hash, timestamp=True) retVal = True + break ''' if data.startswith(b'-txt-'): self._core.setBlockType(hash, 'txt') diff --git a/onionr/onionrcrypto.py b/onionr/onionrcrypto.py index 8d0aed8a..b631d20b 100644 --- a/onionr/onionrcrypto.py +++ b/onionr/onionrcrypto.py @@ -47,7 +47,10 @@ class OnionrCrypto: key = nacl.signing.VerifyKey(key=key, encoder=nacl.encoding.Base32Encoder) retData = False sig = base64.b64decode(sig) - data = data.encode() + try: + data = data.encode() + except AttributeError: + pass if encodedData: try: retData = key.verify(data, sig) # .encode() is not the same as nacl.encoding diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 59d65029..4d2d0ebd 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -337,11 +337,12 @@ class OnionrUtils: try: sig = json.loads(data[0].strip() + '}')['sig'] signer = self._core._utils.getPeerByHashId(metadata['id']) - print('signer',signer) - print('signature', metadata['sig']) + logger.debug('signer ' + signer) + logger.debug('signature ' + metadata['sig']) except KeyError: pass else: + # TODO: Possible refactor to use verification on proccessblocks sigResult = self._core._crypto.edVerify(message, signer, sig, encodedData=True) #sigResult = False if sigResult != False: @@ -355,7 +356,10 @@ class OnionrUtils: logger.error('Unable to decrypt ' + i, error=e) else: logger.info('Recieved message: ' + message.decode()) - logger.info(sigResult) + if sigResult.startswith('Invalid'): + logger.warn(sigResult) + else: + logger.info(sigResult) except FileNotFoundError: pass except Exception as error: @@ -373,7 +377,6 @@ class OnionrUtils: c = conn.cursor() command = (hash,) retData = '' - print('finding', hash) for row in c.execute('SELECT ID FROM peers where hashID=?', command): if row[0] != '': retData = row[0]