Exemplo n.º 1
0
 def test_plaintext_insert(self):
     setup_test()
     message = 'hello world'
     bl = onionrblocks.insert(message)
     self.assertTrue(bl.startswith('0'))
     self.assertIn(bytesconverter.str_to_bytes(message),
                   onionrstorage.getData(bl))
Exemplo n.º 2
0
def doExport(bHash):
    createdirs.create_dirs()
    data = onionrstorage.getData(bHash)
    with open('%s/%s.dat' % (filepaths.export_location, bHash),
              'wb') as exportFile:
        exportFile.write(data)
        logger.info('Block exported as file', terminal=True)
Exemplo n.º 3
0
    def exists(bHash):
        '''
            Checks if a block is saved to file or not

            Inputs:
            - hash (str/Block):
              - if (Block): check if this block is saved to file
              - if (str): check if a block by this hash is in file

            Outputs:
            - (bool): whether or not the block file exists
        '''

        # no input data? scrap it.
        if bHash is None:
            return False
        '''
        if type(hash) == Block:
            blockfile = hash.getBlockFile()
        else:
            blockfile = onionrcore.Core().dataDir + 'blocks/%s.dat' % hash
        '''
        if isinstance(bHash, Block):
            bHash = bHash.getHash()

        ret = isinstance(onionrstorage.getData(onionrcore.Core(), bHash),
                         type(None))

        return not ret
Exemplo n.º 4
0
    def getData(self, hash):
        '''
            Simply return the data associated to a hash
        '''

        data = onionrstorage.getData(self, hash)

        return data
Exemplo n.º 5
0
def _do_export(b_hash):
    createdirs.create_dirs()
    data = onionrstorage.getData(b_hash)
    with open(
            '%s/%s%s' %
        (filepaths.export_location, b_hash, BLOCK_EXPORT_FILE_EXT),
            'wb') as export:
        export.write(data)
        logger.info('Block exported as file', terminal=True)
Exemplo n.º 6
0
def doExport(o_inst, bHash):
    exportDir = o_inst.dataDir + 'block-export/'
    if not os.path.exists(exportDir):
        if os.path.exists(o_inst.dataDir):
            os.mkdir(exportDir)
        else:
            logger.error('Onionr Not initialized')
    data = onionrstorage.getData(o_inst.onionrCore, bHash)
    with open('%s/%s.dat' % (exportDir, bHash), 'wb') as exportFile:
        exportFile.write(data)
Exemplo n.º 7
0
def set_data(data) -> str:
    '''
        Set the data assciated with a hash
    '''
    storage_counter = storagecounter.StorageCounter()
    data = data
    dataSize = sys.getsizeof(data)
    nonce_hash = crypto.hashers.sha3_hash(
        bytesconverter.str_to_bytes(
            blockmetadata.fromdata.get_block_metadata_from_data(data)[2]))
    nonce_hash = bytesconverter.bytes_to_str(nonce_hash)

    if not type(data) is bytes:
        data = data.encode()

    dataHash = crypto.hashers.sha3_hash(data)

    if type(dataHash) is bytes:
        dataHash = dataHash.decode()
    blockFileName = filepaths.block_data_location + dataHash + '.dat'
    try:
        onionrstorage.getData(dataHash)
    except onionrexceptions.NoDataAvailable:
        if storage_counter.add_bytes(dataSize) != False:
            onionrstorage.store(data, blockHash=dataHash)
            conn = sqlite3.connect(dbfiles.block_meta_db, timeout=30)
            c = conn.cursor()
            c.execute("UPDATE hashes SET dataSaved=1 WHERE hash = ?;",
                      (dataHash, ))
            conn.commit()
            conn.close()
            with open(filepaths.data_nonce_file, 'a') as nonceFile:
                nonceFile.write(nonce_hash + '\n')
        else:
            raise onionrexceptions.DiskAllocationReached
    else:
        raise onionrexceptions.DataExists("Data is already set for " +
                                          dataHash)

    return dataHash
Exemplo n.º 8
0
def remove_block(block):
    """Remove a block from this node.

    (does not automatically blacklist).
    **You may want blacklist.addToDB(blockHash)
    """
    if stringvalidators.validate_hash(block):
        conn = sqlite3.connect(dbfiles.block_meta_db, timeout=30)
        c = conn.cursor()
        t = (block,)
        c.execute('Delete from hashes where hash=?;', t)
        conn.commit()
        conn.close()
        dataSize = sys.getsizeof(onionrstorage.getData(block))
        storagecounter.StorageCounter().remove_bytes(dataSize)
    else:
        raise onionrexceptions.InvalidHexHash
Exemplo n.º 9
0
    def removeBlock(self, block):
        '''
            remove a block from this node (does not automatically blacklist)

            **You may want blacklist.addToDB(blockHash)
        '''

        if self._utils.validateHash(block):
            conn = sqlite3.connect(self.blockDB, timeout=30)
            c = conn.cursor()
            t = (block, )
            c.execute('Delete from hashes where hash=?;', t)
            conn.commit()
            conn.close()
            dataSize = sys.getsizeof(onionrstorage.getData(self, block))
            self._utils.storageCounter.removeBytes(dataSize)
        else:
            raise onionrexceptions.InvalidHexHash
Exemplo n.º 10
0
def remove_block(block):
    """Remove a block from this node.

    (does not automatically blacklist).
    **You may want blacklist.addToDB(blockHash)
    """
    if stringvalidators.validate_hash(block):
        try:
            data_size = sys.getsizeof(onionrstorage.getData(block))
        except onionrexceptions.NoDataAvailable:
            data_size = 0
        conn = sqlite3.connect(dbfiles.block_meta_db,
                               timeout=DATABASE_LOCK_TIMEOUT)
        c = conn.cursor()
        t = (block, )
        c.execute('Delete from hashes where hash=?;', t)
        conn.commit()
        conn.close()
        if data_size:
            storage_counter.remove_bytes(data_size)
    else:
        raise onionrexceptions.InvalidHexHash
Exemplo n.º 11
0
    def exists(bHash):
        """
            Checks if a block is saved to file or not

            Inputs:
            - hash (str/Block):
              - if (Block): check if this block is saved to file
              - if (str): check if a block by this hash is in file

            Outputs:
            - (bool): whether or not the block file exists
        """

        # no input data? scrap it.
        if bHash is None:
            return False

        if isinstance(bHash, Block):
            bHash = bHash.getHash()

        ret = isinstance(onionrstorage.getData(bHash), type(None))

        return not ret
Exemplo n.º 12
0
    def update(self, data=None, file=None):
        '''
            Loads data from a block in to the current object.

            Inputs:
            - data (str):
              - if None: will load from file by hash
              - else: will load from `data` string
            - file (str):
              - if None: will load from file specified in this parameter
              - else: will load from wherever block is stored by hash

            Outputs:
            - (bool): indicates whether or not the operation was successful
        '''
        try:
            # import from string
            blockdata = data

            # import from file
            if blockdata is None:
                try:
                    blockdata = onionrstorage.getData(self.core,
                                                      self.getHash()).decode()
                except AttributeError:
                    raise onionrexceptions.NoDataAvailable(
                        'Block does not exist')
            else:
                self.blockFile = None
            # parse block
            self.raw = str(blockdata)
            self.bheader = json.loads(
                self.getRaw()[:self.getRaw().index('\n')])
            self.bcontent = self.getRaw()[self.getRaw().index('\n') + 1:]
            if ('encryptType' in self.bheader) and (self.bheader['encryptType']
                                                    in ('asym', 'sym')):
                self.bmetadata = self.getHeader('meta', None)
                self.isEncrypted = True
            else:
                self.bmetadata = json.loads(self.getHeader('meta', None))
            self.parent = self.getMetadata('parent', None)
            self.btype = self.getMetadata('type', None)
            self.signed = ('sig' in self.getHeader()
                           and self.getHeader('sig') != '')
            # TODO: detect if signer is hash of pubkey or not
            self.signer = self.getHeader('signer', None)
            self.signature = self.getHeader('sig', None)
            # signed data is jsonMeta + block content (no linebreak)
            self.signedData = (None if not self.isSigned() else
                               self.getHeader('meta') + self.getContent())
            self.date = self.getCore().getBlockDate(self.getHash())
            self.claimedTime = self.getHeader('time', None)

            if not self.getDate() is None:
                self.date = datetime.datetime.fromtimestamp(self.getDate())

            self.valid = True

            if len(self.getRaw()) <= config.get('allocations.blockCache',
                                                500000):
                self.cache()

            if self.autoDecrypt:
                self.decrypt()

            return True
        except Exception as e:
            logger.warn('Failed to parse block %s.' % self.getHash(),
                        error=e,
                        timestamp=False)

            # if block can't be parsed, it's a waste of precious space. Throw it away.
            if not self.delete():
                logger.warn('Failed to delete invalid block %s.' %
                            self.getHash(),
                            error=e)
            else:
                logger.debug('Deleted invalid block %s.' % self.getHash(),
                             timestamp=False)

        self.valid = False
        return False
Exemplo n.º 13
0
    def update(self, data=None, file=None):
        """
            Loads data from a block in to the current object.

            Inputs:
            - data (str):
              - if None: will load from file by hash
              - else: will load from `data` string
            - file (str):
              - if None: will load from file specified in this parameter
              - else: will load from wherever block is stored by hash

            Outputs:
            - (bool): indicates whether or not the operation was successful
        """
        try:
            # import from string
            blockdata = data

            # import from file
            if blockdata is None:
                try:
                    blockdata = onionrstorage.getData(
                        self.getHash())  #.decode()
                except AttributeError:
                    raise onionrexceptions.NoDataAvailable(
                        'Block does not exist')
            else:
                self.blockFile = None
            # parse block
            self.raw = blockdata
            self.bheader = json.loads(
                self.getRaw()[:self.getRaw().index(b'\n')])
            self.bcontent = self.getRaw()[self.getRaw().index(b'\n') + 1:]
            if ('encryptType' in self.bheader) and (self.bheader['encryptType']
                                                    in ('asym', 'sym')):
                self.bmetadata = self.getHeader('meta', None)
                self.isEncrypted = True
            else:
                self.bmetadata = json.loads(self.getHeader('meta', None))
            self.btype = self.getMetadata('type', None)
            self.signed = ('sig' in self.getHeader()
                           and self.getHeader('sig') != '')
            # TODO: detect if signer is hash of pubkey or not
            self.signer = self.getHeader('signer', None)
            self.signature = self.getHeader('sig', None)
            # signed data is jsonMeta + block content (no linebreak)
            self.signedData = (None if not self.isSigned() else
                               self.getHeader('meta').encode() +
                               self.getContent())
            self.date = blockmetadb.get_block_date(self.getHash())
            self.claimedTime = self.getHeader('time', None)

            if not self.getDate() is None:
                self.date = datetime.datetime.fromtimestamp(self.getDate())

            self.valid = True

            if self.autoDecrypt:
                self.decrypt()

            return True
        except Exception as e:
            logger.warn('Failed to parse block %s' % self.getHash(),
                        error=e,
                        timestamp=False)

        self.valid = False
        return False