2020-04-03 09:02:36 +00:00
|
|
|
"""Onionr - P2P Anonymous Storage Network.
|
2018-05-15 06:43:29 +00:00
|
|
|
|
2021-01-17 04:58:28 +00:00
|
|
|
oldblocks class for abstraction of blocks
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
|
|
|
import datetime
|
|
|
|
import onionrstorage
|
|
|
|
|
|
|
|
import unpaddedbase32
|
|
|
|
import ujson as json
|
|
|
|
import nacl.exceptions
|
|
|
|
|
|
|
|
import logger
|
|
|
|
import onionrexceptions
|
|
|
|
from onionrusers import onionrusers
|
|
|
|
from onionrutils import stringvalidators, epoch
|
|
|
|
from coredb import blockmetadb
|
|
|
|
from onionrutils import bytesconverter
|
2021-01-17 04:58:28 +00:00
|
|
|
import oldblocks
|
2020-04-03 09:02:36 +00:00
|
|
|
from onionrcrypto import encryption, cryptoutils as cryptoutils, signing
|
|
|
|
"""
|
2018-05-15 06:43:29 +00:00
|
|
|
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/>.
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2018-05-15 06:43:29 +00:00
|
|
|
class Block:
|
2018-06-14 04:17:58 +00:00
|
|
|
blockCacheOrder = list() # NEVER write your own code that writes to this!
|
|
|
|
blockCache = dict() # should never be accessed directly, look at Block.getCache()
|
|
|
|
|
2019-07-19 19:49:56 +00:00
|
|
|
def __init__(self, hash = None, type = None, content = None, expire=None, decrypt=False, bypassReplayCheck=False):
|
2018-06-20 04:36:07 +00:00
|
|
|
# take from arguments
|
2018-06-22 00:34:42 +00:00
|
|
|
# sometimes people input a bytes object instead of str in `hash`
|
2018-08-04 03:47:56 +00:00
|
|
|
if (not hash is None) and isinstance(hash, bytes):
|
2018-06-22 00:34:42 +00:00
|
|
|
hash = hash.decode()
|
2018-07-11 19:45:38 +00:00
|
|
|
|
2018-06-20 04:36:07 +00:00
|
|
|
self.hash = hash
|
|
|
|
self.btype = type
|
|
|
|
self.bcontent = content
|
2018-09-30 16:53:39 +00:00
|
|
|
self.expire = expire
|
2019-02-26 04:19:37 +00:00
|
|
|
self.bypassReplayCheck = bypassReplayCheck
|
2018-05-16 02:12:23 +00:00
|
|
|
|
2018-05-16 01:47:58 +00:00
|
|
|
# initialize variables
|
|
|
|
self.valid = True
|
|
|
|
self.raw = None
|
|
|
|
self.signed = False
|
|
|
|
self.signature = None
|
|
|
|
self.signedData = None
|
2018-05-16 03:08:42 +00:00
|
|
|
self.blockFile = None
|
2018-05-16 01:47:58 +00:00
|
|
|
self.bheader = {}
|
|
|
|
self.bmetadata = {}
|
2018-07-13 21:02:41 +00:00
|
|
|
self.isEncrypted = False
|
2018-07-18 07:33:23 +00:00
|
|
|
self.decrypted = False
|
|
|
|
self.signer = None
|
|
|
|
self.validSig = False
|
2019-02-01 06:38:12 +00:00
|
|
|
self.autoDecrypt = decrypt
|
2020-10-10 08:41:55 +00:00
|
|
|
self.claimedTime = None
|
2018-05-16 01:47:58 +00:00
|
|
|
|
2019-01-28 06:06:20 +00:00
|
|
|
self.update()
|
2018-05-15 06:43:29 +00:00
|
|
|
|
2019-02-17 20:21:03 +00:00
|
|
|
def decrypt(self, encodedData = True):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-07-30 00:37:12 +00:00
|
|
|
Decrypt a block, loading decrypted data into their vars
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-09-30 03:13:30 +00:00
|
|
|
|
2018-07-18 07:33:23 +00:00
|
|
|
if self.decrypted:
|
|
|
|
return True
|
|
|
|
retData = False
|
2018-07-17 07:18:17 +00:00
|
|
|
# decrypt data
|
2018-07-18 07:33:23 +00:00
|
|
|
if self.getHeader('encryptType') == 'asym':
|
|
|
|
try:
|
2020-10-27 15:58:06 +00:00
|
|
|
self.bcontent = encryption.pub_key_decrypt(self.bcontent, encodedData=False)
|
2020-10-15 02:46:20 +00:00
|
|
|
|
2019-07-20 00:01:16 +00:00
|
|
|
bmeta = encryption.pub_key_decrypt(self.bmetadata, encodedData=encodedData)
|
2020-10-15 02:46:20 +00:00
|
|
|
|
2018-07-20 03:02:09 +00:00
|
|
|
try:
|
|
|
|
bmeta = bmeta.decode()
|
|
|
|
except AttributeError:
|
|
|
|
# yet another bytes fix
|
|
|
|
pass
|
|
|
|
self.bmetadata = json.loads(bmeta)
|
2019-07-20 00:01:16 +00:00
|
|
|
self.signature = encryption.pub_key_decrypt(self.signature, encodedData=encodedData)
|
2020-10-27 15:58:06 +00:00
|
|
|
|
2019-07-20 00:01:16 +00:00
|
|
|
self.signer = encryption.pub_key_decrypt(self.signer, encodedData=encodedData)
|
2020-10-27 15:58:06 +00:00
|
|
|
|
2019-02-01 06:38:12 +00:00
|
|
|
self.bheader['signer'] = self.signer.decode()
|
2020-10-27 15:58:06 +00:00
|
|
|
self.signedData = json.dumps(self.bmetadata).encode() + self.bcontent
|
2019-02-26 04:19:37 +00:00
|
|
|
|
2019-08-15 08:36:05 +00:00
|
|
|
if not self.signer is None:
|
|
|
|
if not self.verifySig():
|
|
|
|
raise onionrexceptions.SignatureError("Block has invalid signature")
|
|
|
|
|
2019-02-26 04:19:37 +00:00
|
|
|
# Check for replay attacks
|
|
|
|
try:
|
2019-07-17 16:25:29 +00:00
|
|
|
if epoch.get_epoch() - blockmetadb.get_block_date(self.hash) > 60:
|
2020-01-07 11:44:53 +00:00
|
|
|
if not cryptoutils.replay_validator(self.bmetadata['rply']): raise onionrexceptions.ReplayAttack
|
2019-09-09 00:21:36 +00:00
|
|
|
except (AssertionError, KeyError, TypeError, onionrexceptions.ReplayAttack) as e:
|
2019-02-26 04:19:37 +00:00
|
|
|
if not self.bypassReplayCheck:
|
|
|
|
# Zero out variables to prevent reading of replays
|
|
|
|
self.bmetadata = {}
|
|
|
|
self.signer = ''
|
|
|
|
self.bheader['signer'] = ''
|
|
|
|
self.signedData = ''
|
|
|
|
self.signature = ''
|
|
|
|
raise onionrexceptions.ReplayAttack('Signature is too old. possible replay attack')
|
2018-10-08 02:25:59 +00:00
|
|
|
try:
|
2019-09-09 00:21:36 +00:00
|
|
|
if not self.bmetadata['forwardEnc']: raise KeyError
|
2018-10-08 02:25:59 +00:00
|
|
|
except (AssertionError, KeyError) as e:
|
|
|
|
pass
|
|
|
|
else:
|
2018-10-08 05:11:46 +00:00
|
|
|
try:
|
2019-07-19 19:49:56 +00:00
|
|
|
self.bcontent = onionrusers.OnionrUser(self.signer).forwardDecrypt(self.bcontent)
|
2018-10-09 23:36:52 +00:00
|
|
|
except (onionrexceptions.DecryptionError, nacl.exceptions.CryptoError) as e:
|
2020-07-27 00:02:39 +00:00
|
|
|
logger.error(str(e))
|
2018-10-08 05:11:46 +00:00
|
|
|
pass
|
2020-10-27 15:58:06 +00:00
|
|
|
except (nacl.exceptions.CryptoError,) as e:
|
|
|
|
logger.debug(f'Could not decrypt block. encodedData: {encodedData}. Either invalid key or corrupted data ' + str(e))
|
2019-02-26 04:19:37 +00:00
|
|
|
except onionrexceptions.ReplayAttack:
|
|
|
|
logger.warn('%s is possibly a replay attack' % (self.hash,))
|
2018-07-18 07:33:23 +00:00
|
|
|
else:
|
|
|
|
retData = True
|
|
|
|
self.decrypted = True
|
|
|
|
return retData
|
2018-07-30 00:37:12 +00:00
|
|
|
|
2018-07-18 07:33:23 +00:00
|
|
|
def verifySig(self):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-07-30 00:37:12 +00:00
|
|
|
Verify if a block's signature is signed by its claimed signer
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2019-10-08 02:32:33 +00:00
|
|
|
if self.signer is None:
|
|
|
|
return False
|
|
|
|
if signing.ed_verify(data=self.signedData, key=self.signer, sig=self.signature, encodedData=True):
|
2018-07-18 07:33:23 +00:00
|
|
|
self.validSig = True
|
|
|
|
else:
|
|
|
|
self.validSig = False
|
|
|
|
return self.validSig
|
2018-07-17 07:18:17 +00:00
|
|
|
|
2018-05-15 06:43:29 +00:00
|
|
|
def update(self, data = None, file = None):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-15 06:43:29 +00:00
|
|
|
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
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 01:47:58 +00:00
|
|
|
try:
|
|
|
|
# import from string
|
|
|
|
blockdata = data
|
|
|
|
|
|
|
|
# import from file
|
|
|
|
if blockdata is None:
|
2019-02-22 21:04:03 +00:00
|
|
|
try:
|
2019-09-10 20:25:50 +00:00
|
|
|
blockdata = onionrstorage.getData(self.getHash())#.decode()
|
2019-02-22 21:04:03 +00:00
|
|
|
except AttributeError:
|
|
|
|
raise onionrexceptions.NoDataAvailable('Block does not exist')
|
2018-05-16 01:47:58 +00:00
|
|
|
else:
|
|
|
|
self.blockFile = None
|
|
|
|
# parse block
|
2019-09-10 20:25:50 +00:00
|
|
|
self.raw = blockdata
|
|
|
|
self.bheader = json.loads(self.getRaw()[:self.getRaw().index(b'\n')])
|
|
|
|
self.bcontent = self.getRaw()[self.getRaw().index(b'\n') + 1:]
|
2018-09-27 00:40:02 +00:00
|
|
|
if ('encryptType' in self.bheader) and (self.bheader['encryptType'] in ('asym', 'sym')):
|
2018-07-13 21:02:41 +00:00
|
|
|
self.bmetadata = self.getHeader('meta', None)
|
|
|
|
self.isEncrypted = True
|
|
|
|
else:
|
|
|
|
self.bmetadata = json.loads(self.getHeader('meta', None))
|
2018-06-20 04:36:07 +00:00
|
|
|
self.btype = self.getMetadata('type', None)
|
2018-05-16 01:47:58 +00:00
|
|
|
self.signed = ('sig' in self.getHeader() and self.getHeader('sig') != '')
|
2018-07-18 07:33:23 +00:00
|
|
|
# TODO: detect if signer is hash of pubkey or not
|
|
|
|
self.signer = self.getHeader('signer', None)
|
2018-06-20 04:36:07 +00:00
|
|
|
self.signature = self.getHeader('sig', None)
|
2018-07-18 07:33:23 +00:00
|
|
|
# signed data is jsonMeta + block content (no linebreak)
|
2019-10-08 02:32:33 +00:00
|
|
|
self.signedData = (None if not self.isSigned() else self.getHeader('meta').encode() + self.getContent())
|
2019-07-17 16:25:29 +00:00
|
|
|
self.date = blockmetadb.get_block_date(self.getHash())
|
2018-09-04 18:56:05 +00:00
|
|
|
self.claimedTime = self.getHeader('time', None)
|
2018-05-16 01:47:58 +00:00
|
|
|
|
|
|
|
if not self.getDate() is None:
|
|
|
|
self.date = datetime.datetime.fromtimestamp(self.getDate())
|
|
|
|
|
|
|
|
self.valid = True
|
2020-01-07 11:44:53 +00:00
|
|
|
|
2019-02-01 06:38:12 +00:00
|
|
|
if self.autoDecrypt:
|
|
|
|
self.decrypt()
|
2018-06-14 04:17:58 +00:00
|
|
|
|
2018-05-16 01:47:58 +00:00
|
|
|
return True
|
|
|
|
except Exception as e:
|
2019-09-04 06:20:11 +00:00
|
|
|
logger.warn('Failed to parse block %s' % self.getHash(), error = e, timestamp = False)
|
2018-09-25 04:16:51 +00:00
|
|
|
|
2018-05-16 01:47:58 +00:00
|
|
|
|
|
|
|
self.valid = False
|
2018-05-15 06:43:29 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
# getters
|
|
|
|
|
2018-10-02 05:02:05 +00:00
|
|
|
def getExpire(self):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-10-02 05:02:05 +00:00
|
|
|
Returns the expire time for a block
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
- (int): the expire time for a block, or None
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-10-02 05:02:05 +00:00
|
|
|
return self.expire
|
|
|
|
|
2018-05-15 06:43:29 +00:00
|
|
|
def getHash(self):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
Returns the hash of the block if saved to file
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
- (str): the hash of the block, or None
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
|
2018-05-15 06:43:29 +00:00
|
|
|
return self.hash
|
|
|
|
|
|
|
|
def getType(self):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
Returns the type of the block
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
- (str): the type of the block
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-15 06:43:29 +00:00
|
|
|
return self.btype
|
|
|
|
|
2018-05-16 01:47:58 +00:00
|
|
|
def getRaw(self):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
Returns the raw contents of the block, if saved to file
|
|
|
|
|
|
|
|
Outputs:
|
2019-09-10 20:25:50 +00:00
|
|
|
- (bytes): the raw contents of the block, or None
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
|
2019-09-10 20:25:50 +00:00
|
|
|
return self.raw
|
2018-05-16 01:47:58 +00:00
|
|
|
|
2018-06-20 04:36:07 +00:00
|
|
|
def getHeader(self, key = None, default = None):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
Returns the header information
|
|
|
|
|
|
|
|
Inputs:
|
|
|
|
- key (str): only returns the value of the key in the header
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
- (dict/str): either the whole header as a dict, or one value
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
|
2018-05-16 01:47:58 +00:00
|
|
|
if not key is None:
|
2018-06-20 04:36:07 +00:00
|
|
|
if key in self.getHeader():
|
|
|
|
return self.getHeader()[key]
|
|
|
|
return default
|
2018-06-01 07:02:56 +00:00
|
|
|
return self.bheader
|
2018-05-16 01:47:58 +00:00
|
|
|
|
2018-06-20 04:36:07 +00:00
|
|
|
def getMetadata(self, key = None, default = None):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
Returns the metadata information
|
|
|
|
|
|
|
|
Inputs:
|
|
|
|
- key (str): only returns the value of the key in the metadata
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
- (dict/str): either the whole metadata as a dict, or one value
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
|
2018-05-16 01:47:58 +00:00
|
|
|
if not key is None:
|
2018-06-20 04:36:07 +00:00
|
|
|
if key in self.getMetadata():
|
|
|
|
return self.getMetadata()[key]
|
|
|
|
return default
|
2018-06-01 07:02:56 +00:00
|
|
|
return self.bmetadata
|
2018-05-15 06:43:29 +00:00
|
|
|
|
|
|
|
def getContent(self):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
Returns the contents of the block
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
- (str): the contents of the block
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
|
2019-10-08 02:32:33 +00:00
|
|
|
return self.bcontent
|
2018-06-05 02:26:04 +00:00
|
|
|
|
2018-05-15 06:43:29 +00:00
|
|
|
def getDate(self):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
Returns the date that the block was received, if loaded from file
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
- (datetime): the date that the block was received
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
|
2018-05-15 06:43:29 +00:00
|
|
|
return self.date
|
|
|
|
|
2018-05-16 01:47:58 +00:00
|
|
|
def getBlockFile(self):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
Returns the location of the block file if it is saved
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
- (str): the location of the block file, or None
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
|
2018-05-16 01:47:58 +00:00
|
|
|
return self.blockFile
|
|
|
|
|
2018-05-15 06:43:29 +00:00
|
|
|
def isValid(self):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
Checks if the block is valid
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
- (bool): whether or not the block is valid
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
|
2018-05-15 06:43:29 +00:00
|
|
|
return self.valid
|
|
|
|
|
|
|
|
def isSigned(self):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
Checks if the block was signed
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
- (bool): whether or not the block is signed
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
|
2018-05-15 06:43:29 +00:00
|
|
|
return self.signed
|
|
|
|
|
2018-05-16 01:47:58 +00:00
|
|
|
def getSignature(self):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
Returns the base64-encoded signature
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
- (str): the signature, or None
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
|
2018-05-16 01:47:58 +00:00
|
|
|
return self.signature
|
|
|
|
|
|
|
|
def getSignedData(self):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
Returns the data that was signed
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
- (str): the data that was signed, or None
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
|
2018-05-16 01:47:58 +00:00
|
|
|
return self.signedData
|
|
|
|
|
|
|
|
def isSigner(self, signer, encodedData = True):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
Checks if the block was signed by the signer inputted
|
|
|
|
|
|
|
|
Inputs:
|
|
|
|
- signer (str): the public key of the signer to check against
|
|
|
|
- encodedData (bool): whether or not the `signer` argument is base64 encoded
|
|
|
|
|
|
|
|
Outputs:
|
|
|
|
- (bool): whether or not the signer of the block is the signer inputted
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2019-10-08 02:32:33 +00:00
|
|
|
signer = unpaddedbase32.repad(bytesconverter.str_to_bytes(signer))
|
2018-05-16 01:47:58 +00:00
|
|
|
try:
|
2019-06-25 08:21:36 +00:00
|
|
|
if (not self.isSigned()) or (not stringvalidators.validate_pub_key(signer)):
|
2018-05-16 01:47:58 +00:00
|
|
|
return False
|
|
|
|
|
2019-07-20 00:01:16 +00:00
|
|
|
return bool(signing.ed_verify(self.getSignedData(), signer, self.getSignature(), encodedData = encodedData))
|
2018-05-16 01:47:58 +00:00
|
|
|
except:
|
|
|
|
return False
|
2018-05-15 06:43:29 +00:00
|
|
|
|
|
|
|
# setters
|
|
|
|
|
|
|
|
def setType(self, btype):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
Sets the type of the block
|
|
|
|
|
|
|
|
Inputs:
|
|
|
|
- btype (str): the type of block to be set to
|
|
|
|
|
|
|
|
Outputs:
|
2018-06-01 07:02:56 +00:00
|
|
|
- (Block): the Block instance
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
|
2018-05-15 06:43:29 +00:00
|
|
|
self.btype = btype
|
|
|
|
return self
|
2018-06-05 02:26:04 +00:00
|
|
|
|
2018-06-01 07:02:56 +00:00
|
|
|
def setMetadata(self, key, val):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-06-01 07:02:56 +00:00
|
|
|
Sets a custom metadata value
|
2018-06-05 02:26:04 +00:00
|
|
|
|
2018-06-01 07:02:56 +00:00
|
|
|
Metadata should not store block-specific data structures.
|
2018-06-05 02:26:04 +00:00
|
|
|
|
2018-06-01 07:02:56 +00:00
|
|
|
Inputs:
|
|
|
|
- key (str): the key
|
|
|
|
- val: the value (type is irrelevant)
|
2018-06-05 02:26:04 +00:00
|
|
|
|
2018-06-01 07:02:56 +00:00
|
|
|
Outputs:
|
|
|
|
- (Block): the Block instance
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-06-05 02:26:04 +00:00
|
|
|
|
2019-08-15 08:36:05 +00:00
|
|
|
self.bmetadata[key] = val
|
2018-06-01 07:02:56 +00:00
|
|
|
return self
|
2018-05-15 06:43:29 +00:00
|
|
|
|
|
|
|
def setContent(self, bcontent):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
Sets the contents of the block
|
|
|
|
|
|
|
|
Inputs:
|
|
|
|
- bcontent (str): the contents to be set to
|
|
|
|
|
|
|
|
Outputs:
|
2018-06-01 07:02:56 +00:00
|
|
|
- (Block): the Block instance
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
|
2018-05-16 01:47:58 +00:00
|
|
|
self.bcontent = str(bcontent)
|
2018-05-15 06:43:29 +00:00
|
|
|
return self
|
2018-06-05 02:26:04 +00:00
|
|
|
|
2018-06-01 07:02:56 +00:00
|
|
|
# static functions
|
2019-01-08 07:25:56 +00:00
|
|
|
def exists(bHash):
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
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
|
2020-04-03 09:02:36 +00:00
|
|
|
"""
|
2018-05-16 02:12:23 +00:00
|
|
|
|
2018-06-14 04:17:58 +00:00
|
|
|
# no input data? scrap it.
|
2019-01-08 07:25:56 +00:00
|
|
|
if bHash is None:
|
2018-05-16 01:47:58 +00:00
|
|
|
return False
|
2019-07-19 19:49:56 +00:00
|
|
|
|
2019-01-08 07:25:56 +00:00
|
|
|
if isinstance(bHash, Block):
|
|
|
|
bHash = bHash.getHash()
|
2020-01-07 11:44:53 +00:00
|
|
|
|
2019-07-19 19:49:56 +00:00
|
|
|
ret = isinstance(onionrstorage.getData(bHash), type(None))
|
2018-05-16 01:47:58 +00:00
|
|
|
|
2019-01-07 05:50:20 +00:00
|
|
|
return not ret
|