work on new header system

This commit is contained in:
Kevin Froman 2018-04-26 02:40:39 -05:00
parent acd5494979
commit ad56082271
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
4 changed files with 73 additions and 17 deletions

View File

@ -19,7 +19,7 @@ and code to operate as a daemon, getting commands from the command queue databas
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import sqlite3, requests, hmac, hashlib, time, sys, os, math, logger, urllib.parse, base64, binascii, random import sqlite3, requests, hmac, hashlib, time, sys, os, math, logger, urllib.parse, base64, binascii, random, json
import core, onionrutils, onionrcrypto, netcontroller, onionrproofs, btc, config, onionrplugins as plugins import core, onionrutils, onionrcrypto, netcontroller, onionrproofs, btc, config, onionrplugins as plugins
class OnionrCommunicate: class OnionrCommunicate:
@ -254,8 +254,32 @@ class OnionrCommunicate:
self.newHashes[i] += 1 self.newHashes[i] += 1
logger.warn('UNSAVED BLOCK: ' + i) logger.warn('UNSAVED BLOCK: ' + i)
data = self.downloadBlock(i) data = self.downloadBlock(i)
# if block was successfull gotten (hash already verified)
if data: if data:
del self.newHashes[i] del self.newHashes[i] # remove from probation list
# deal with block metadata
blockContent = self._core.getData(i)
try:
blockMetadata = json.loads(self._core.getData(i)).split('}')[0] + '}'
try:
blockMetadata['sig']
blockMetadata['id']
except KeyError:
pass
else:
creator = self._utils.getPeerByHashId(blockMetadata['id'])
if self._crypto.edVerify(blockContent, creator):
self._core.updateBlockInfo(i, 'sig', 'true')
else:
self._core.updateBlockInfo(i, 'sig', 'false')
try:
blockMetadata['type']
except KeyError:
pass
except json.decoder.JSONDecodeError:
pass
return return
def downloadBlock(self, hash, peerTries=3): def downloadBlock(self, hash, peerTries=3):
@ -288,10 +312,12 @@ class OnionrCommunicate:
self._core.setData(data) self._core.setData(data)
logger.info('Successfully obtained data for ' + hash, timestamp=True) logger.info('Successfully obtained data for ' + hash, timestamp=True)
retVal = True retVal = True
'''
if data.startswith(b'-txt-'): if data.startswith(b'-txt-'):
self._core.setBlockType(hash, 'txt') self._core.setBlockType(hash, 'txt')
if len(data) < 120: if len(data) < 120:
logger.debug('Block text:\n' + data.decode()) logger.debug('Block text:\n' + data.decode())
'''
else: else:
logger.warn("Failed to validate " + hash + " " + " hash calculated was " + digest) logger.warn("Failed to validate " + hash + " " + " hash calculated was " + digest)
peerTryCount += 1 peerTryCount += 1

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import sqlite3, os, sys, time, math, base64, tarfile, getpass, simplecrypt, hashlib, nacl, logger import sqlite3, os, sys, time, math, base64, tarfile, getpass, simplecrypt, hashlib, nacl, logger, json
#from Crypto.Cipher import AES #from Crypto.Cipher import AES
#from Crypto import Random #from Crypto import Random
import netcontroller import netcontroller
@ -87,8 +87,9 @@ class Core:
if not self._utils.validatePubKey(peerID): if not self._utils.validatePubKey(peerID):
return False return False
conn = sqlite3.connect(self.peerDB) conn = sqlite3.connect(self.peerDB)
hashID = self._crypto.pubKeyHashID(peerID)
c = conn.cursor() c = conn.cursor()
t = (peerID, name, 'unknown') t = (peerID, name, 'unknown', hashID)
for i in c.execute("SELECT * FROM PEERS where id = '" + peerID + "';"): for i in c.execute("SELECT * FROM PEERS where id = '" + peerID + "';"):
try: try:
@ -99,7 +100,7 @@ class Core:
pass pass
except IndexError: except IndexError:
pass pass
c.execute('INSERT INTO peers (id, name, dateSeen) VALUES(?, ?, ?);', t) c.execute('INSERT INTO peers (id, name, dateSeen, hashID) VALUES(?, ?, ?, ?);', t)
conn.commit() conn.commit()
conn.close() conn.close()
@ -211,7 +212,8 @@ class Core:
dateSeen not null, dateSeen not null,
bytesStored int, bytesStored int,
trust int, trust int,
pubkeyExchanged int); pubkeyExchanged int,
hashID);
''') ''')
conn.commit() conn.commit()
conn.close() conn.close()
@ -228,7 +230,7 @@ class Core:
dataFound - if the data has been found for the block dataFound - if the data has been found for the block
dataSaved - if the data has been saved for the block dataSaved - if the data has been saved for the block
sig - optional signature by the author (not optional if author is specified) sig - optional signature by the author (not optional if author is specified)
author - multi-round partial scrypt hash of authors public key author - multi-round partial sha3-256 hash of authors public key
''' '''
if os.path.exists(self.blockDB): if os.path.exists(self.blockDB):
raise Exception("Block database already exists") raise Exception("Block database already exists")
@ -466,11 +468,12 @@ class Core:
bytesStored int, 5 bytesStored int, 5
trust int 6 trust int 6
pubkeyExchanged int 7 pubkeyExchanged int 7
hashID text 8
''' '''
conn = sqlite3.connect(self.peerDB) conn = sqlite3.connect(self.peerDB)
c = conn.cursor() c = conn.cursor()
command = (peer,) command = (peer,)
infoNumbers = {'id': 0, 'name': 1, 'adders': 2, 'forwardKey': 3, 'dateSeen': 4, 'bytesStored': 5, 'trust': 6, 'pubkeyExchanged': 7} infoNumbers = {'id': 0, 'name': 1, 'adders': 2, 'forwardKey': 3, 'dateSeen': 4, 'bytesStored': 5, 'trust': 6, 'pubkeyExchanged': 7, 'hashID': 8}
info = infoNumbers[info] info = infoNumbers[info]
iterCount = 0 iterCount = 0
retVal = '' retVal = ''
@ -586,20 +589,41 @@ class Core:
c.execute("UPDATE hashes SET dataType='" + blockType + "' WHERE hash = '" + hash + "';") c.execute("UPDATE hashes SET dataType='" + blockType + "' WHERE hash = '" + hash + "';")
conn.commit() conn.commit()
conn.close() conn.close()
return return
def updateBlockInfo(self, hash, key, data):
'''
sets info associated with a block
'''
if key not in ('dateReceived', 'decrypted', 'dataType', 'dataFound', 'dataSaved', 'sig', 'author'):
return False
conn = sqlite3.connect(self.blockDB)
c = conn.cursor()
args = (data, hash)
c.execute("UPDATE hashes SET " + key + " = ? where hash = ?;", args)
conn.commit()
conn.close()
return True
def insertBlock(self, data, header='txt', sign=False): def insertBlock(self, data, header='txt', sign=False):
''' '''
Inserts a block into the network Inserts a block into the network
''' '''
retData = ''
metadata = '-' + header + '-'
metadata = metadata.encode()
try: try:
data.decode() data.decode()
except AttributeError: except AttributeError:
data = data.encode() data = data.encode()
retData = ''
metadata = {'type': header}
if sign:
signature = self._crypto.edSign(data, self._crypto.privKey, encodedResult=True)
ourID = self._crypto.pubKeyHashID()
metadata['id'] = ourID
metadata['sig'] = signature
metadata = json.dumps(metadata)
metadata = metadata.encode()
if len(data) == 0: if len(data) == 0:
logger.error('Will not insert empty block') logger.error('Will not insert empty block')
else: else:

View File

@ -371,13 +371,14 @@ class Onionr:
while True: while True:
messageToAdd = '-txt-' + logger.readline('Broadcast message to network: ') messageToAdd = logger.readline('Broadcast message to network: ')
if len(messageToAdd) - 5 >= 1: if len(messageToAdd) - 5 >= 1:
break break
addedHash = self.onionrCore.setData(messageToAdd) #addedHash = self.onionrCore.setData(messageToAdd)
self.onionrCore.addToBlockDB(addedHash, selfInsert=True) addedHash = self.onionrCore.insertBlock(messageToAdd, header='')
self.onionrCore.setBlockType(addedHash, 'txt') #self.onionrCore.addToBlockDB(addedHash, selfInsert=True)
#self.onionrCore.setBlockType(addedHash, 'txt')
logger.info("inserted your message as block: " + addedHash) logger.info("inserted your message as block: " + addedHash)
return return

View File

@ -338,3 +338,8 @@ class OnionrUtils:
except Exception as error: except Exception as error:
logger.error('Failed to open block ' + str(i) + '.', error=error) logger.error('Failed to open block ' + str(i) + '.', error=error)
return return
def getPeerByHashId(self, hash):
'''
Return the pubkey of the user if known from the hash
'''