2019-06-26 20:13:36 +00:00
|
|
|
'''
|
|
|
|
Onionr - Private P2P Communication
|
|
|
|
|
|
|
|
Module to fetch block metadata from raw block data and process it
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
'''
|
2019-06-25 23:07:35 +00:00
|
|
|
import json, sqlite3
|
2019-06-23 07:00:27 +00:00
|
|
|
import logger, onionrevents
|
|
|
|
from onionrusers import onionrusers
|
|
|
|
from etc import onionrvalues
|
2019-06-25 08:21:36 +00:00
|
|
|
import onionrblockapi
|
2019-06-25 23:07:35 +00:00
|
|
|
from . import epoch, stringvalidators, bytesconverter
|
2019-06-25 08:21:36 +00:00
|
|
|
def get_block_metadata_from_data(blockData):
|
2019-06-23 07:00:27 +00:00
|
|
|
'''
|
|
|
|
accepts block contents as string, returns a tuple of
|
|
|
|
metadata, meta (meta being internal metadata, which will be
|
|
|
|
returned as an encrypted base64 string if it is encrypted, dict if not).
|
|
|
|
'''
|
|
|
|
meta = {}
|
|
|
|
metadata = {}
|
|
|
|
data = blockData
|
|
|
|
try:
|
|
|
|
blockData = blockData.encode()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
metadata = json.loads(blockData[:blockData.find(b'\n')].decode())
|
|
|
|
except json.decoder.JSONDecodeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
data = blockData[blockData.find(b'\n'):].decode()
|
|
|
|
|
|
|
|
if not metadata['encryptType'] in ('asym', 'sym'):
|
|
|
|
try:
|
|
|
|
meta = json.loads(metadata['meta'])
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
meta = metadata['meta']
|
|
|
|
return (metadata, meta, data)
|
|
|
|
|
2019-06-25 23:07:35 +00:00
|
|
|
def process_block_metadata(core_inst, blockHash):
|
2019-06-23 07:00:27 +00:00
|
|
|
'''
|
|
|
|
Read metadata from a block and cache it to the block database
|
|
|
|
'''
|
2019-06-25 08:21:36 +00:00
|
|
|
curTime = epoch.get_rounded_epoch(roundS=60)
|
2019-06-25 23:07:35 +00:00
|
|
|
myBlock = onionrblockapi.Block(blockHash, core_inst)
|
2019-06-23 07:00:27 +00:00
|
|
|
if myBlock.isEncrypted:
|
|
|
|
myBlock.decrypt()
|
|
|
|
if (myBlock.isEncrypted and myBlock.decrypted) or (not myBlock.isEncrypted):
|
|
|
|
blockType = myBlock.getMetadata('type') # we would use myBlock.getType() here, but it is bugged with encrypted blocks
|
2019-06-26 04:48:24 +00:00
|
|
|
|
2019-06-25 23:07:35 +00:00
|
|
|
signer = bytesconverter.bytes_to_str(myBlock.signer)
|
2019-06-23 07:00:27 +00:00
|
|
|
valid = myBlock.verifySig()
|
|
|
|
if myBlock.getMetadata('newFSKey') is not None:
|
2019-06-25 23:07:35 +00:00
|
|
|
onionrusers.OnionrUser(core_inst, signer).addForwardKey(myBlock.getMetadata('newFSKey'))
|
2019-06-23 07:00:27 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
if len(blockType) <= 10:
|
2019-06-25 23:07:35 +00:00
|
|
|
core_inst.updateBlockInfo(blockHash, 'dataType', blockType)
|
2019-06-23 07:00:27 +00:00
|
|
|
except TypeError:
|
|
|
|
logger.warn("Missing block information")
|
|
|
|
pass
|
|
|
|
# Set block expire time if specified
|
|
|
|
try:
|
|
|
|
expireTime = myBlock.getHeader('expire')
|
|
|
|
assert len(str(int(expireTime))) < 20 # test that expire time is an integer of sane length (for epoch)
|
|
|
|
except (AssertionError, ValueError, TypeError) as e:
|
|
|
|
expireTime = onionrvalues.OnionrValues().default_expire + curTime
|
|
|
|
finally:
|
2019-06-25 23:07:35 +00:00
|
|
|
core_inst.updateBlockInfo(blockHash, 'expire', expireTime)
|
2019-06-23 07:00:27 +00:00
|
|
|
if not blockType is None:
|
2019-06-25 23:07:35 +00:00
|
|
|
core_inst.updateBlockInfo(blockHash, 'dataType', blockType)
|
|
|
|
onionrevents.event('processblocks', data = {'block': myBlock, 'type': blockType, 'signer': signer, 'validSig': valid}, onionr = core_inst.onionrInst)
|
2019-06-23 07:00:27 +00:00
|
|
|
else:
|
2019-06-25 23:07:35 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def has_block(core_inst, hash):
|
|
|
|
'''
|
|
|
|
Check for new block in the list
|
|
|
|
'''
|
|
|
|
conn = sqlite3.connect(core_inst.blockDB)
|
|
|
|
c = conn.cursor()
|
|
|
|
if not stringvalidators.validate_hash(hash):
|
|
|
|
raise Exception("Invalid hash")
|
|
|
|
for result in c.execute("SELECT COUNT() FROM hashes WHERE hash = ?", (hash,)):
|
|
|
|
if result[0] >= 1:
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
return False
|
|
|
|
return False
|