work on new block metadata format and some small bug fixes

master
Kevin Froman 2018-04-24 23:04:12 -05:00
parent bbd881785d
commit 6b9d4f8fc6
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
4 changed files with 36 additions and 14 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, random, base64, binascii import sqlite3, requests, hmac, hashlib, time, sys, os, math, logger, urllib.parse, base64, binascii, secrets
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:
@ -149,7 +149,7 @@ class OnionrCommunicate:
peersCheck = len(peerList) peersCheck = len(peerList)
while peersCheck > peersChecked: while peersCheck > peersChecked:
i = random.randint(0, maxN) i = secrets.randbelow(maxN)
logger.info('Using ' + peerList[i] + ' to find new peers', timestamp=True) logger.info('Using ' + peerList[i] + ' to find new peers', timestamp=True)
try: try:
newAdders = self.performGet('pex', peerList[i], skipHighFailureAddress=True) newAdders = self.performGet('pex', peerList[i], skipHighFailureAddress=True)
@ -260,7 +260,7 @@ class OnionrCommunicate:
blocks = '' blocks = ''
for i in peerList: for i in peerList:
hasher = hashlib.sha3_256() hasher = hashlib.sha3_256()
data = self.performGet('getData', i, hash) data = self.performGet('getData', i, hash, skipHighFailureAddress=True)
if data == False or len(data) > 10000000 or data == '': if data == False or len(data) > 10000000 or data == '':
continue continue
try: try:

View File

@ -221,12 +221,14 @@ class Core:
''' '''
Create a database for blocks Create a database for blocks
hash - the hash of a block hash - the hash of a block
dateReceived - the date the block was recieved, not necessarily when it was created dateReceived - the date the block was recieved, not necessarily when it was created
decrypted - if we can successfully decrypt the block (does not describe its current state) decrypted - if we can successfully decrypt the block (does not describe its current state)
dataType - data type of the block dataType - data type of the block
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
signature - optional signature by the author (not optional if author is specified)
author - multi-round partial scrypt 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")
@ -238,7 +240,10 @@ class Core:
decrypted int, decrypted int,
dataType text, dataType text,
dataFound int, dataFound int,
dataSaved int); dataSaved int,
signature text,
author text,
);
''') ''')
conn.commit() conn.commit()
conn.close() conn.close()
@ -584,15 +589,19 @@ class Core:
return return
def insertBlock(self, data, header='txt'): def insertBlock(self, data, header='txt', sign=False):
''' '''
Inserts a block into the network Inserts a block into the network
''' '''
retData = '' retData = ''
metadata = header
if sign:
metadata += '-' + self._crypto.pubKeyHashID() + '-'
metadata += self._crypto.edSign(data, encodeResult=True) + '-'
if len(data) == 0: if len(data) == 0:
logger.error('Will not insert empty block') logger.error('Will not insert empty block')
else: else:
addedHash = self.setData('-' + header + '-' + data) addedHash = self.setData(metadata + data)
self.addToBlockDB(addedHash, selfInsert=True) self.addToBlockDB(addedHash, selfInsert=True)
self.setBlockType(addedHash, header) self.setBlockType(addedHash, header)
retData = addedHash retData = addedHash

View File

@ -576,7 +576,7 @@ class Onionr:
except FileNotFoundError: except FileNotFoundError:
logger.warn('That file does not exist. Improper path?') logger.warn('That file does not exist. Improper path?')
else: else:
print(new) logger.debug(new)
self.onionrCore.insertBlock(new, header='bin') self.onionrCore.insertBlock(new, header='bin')

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 nacl.signing, nacl.encoding, nacl.public, nacl.secret, os, binascii, base64 import nacl.signing, nacl.encoding, nacl.public, nacl.secret, os, binascii, base64, hashlib
class OnionrCrypto: class OnionrCrypto:
def __init__(self, coreInstance): def __init__(self, coreInstance):
@ -26,6 +26,8 @@ class OnionrCrypto:
self.pubKey = None self.pubKey = None
self.privKey = None self.privKey = None
self.HASH_ID_ROUNDS = 2000
# Load our own pub/priv Ed25519 keys, gen & save them if they don't exist # Load our own pub/priv Ed25519 keys, gen & save them if they don't exist
if os.path.exists(self._keyFile): if os.path.exists(self._keyFile):
with open('data/keys.txt', 'r') as keys: with open('data/keys.txt', 'r') as keys:
@ -171,3 +173,14 @@ class OnionrCrypto:
private_key = nacl.signing.SigningKey.generate() private_key = nacl.signing.SigningKey.generate()
public_key = private_key.verify_key.encode(encoder=nacl.encoding.Base32Encoder()) public_key = private_key.verify_key.encode(encoder=nacl.encoding.Base32Encoder())
return (public_key.decode(), private_key.encode(encoder=nacl.encoding.Base32Encoder()).decode()) return (public_key.decode(), private_key.encode(encoder=nacl.encoding.Base32Encoder()).decode())
def pubKeyHashID(self, pubkey=self.pubKey):
'''Accept a ed25519 public key, return a truncated result of X many sha3_256 hash rounds'''
prev = ''
pubkey = pubkey.encode()
for i in range(self.HASH_ID_ROUNDS):
hasher = hashlib.sha3_256()
hasher.update(pubkey + prev.encode())
prev = hasher.hexdigest()
result = prev
return result