2020-04-03 09:02:36 +00:00
|
|
|
import ujson as json
|
|
|
|
|
2021-01-17 04:58:28 +00:00
|
|
|
from oldblocks import onionrblockapi
|
2019-07-02 06:32:26 +00:00
|
|
|
from onionrutils import bytesconverter, stringvalidators
|
2019-09-09 00:21:36 +00:00
|
|
|
import onionrexceptions
|
2019-07-02 06:32:26 +00:00
|
|
|
class GetBlockData:
|
2019-07-20 00:01:16 +00:00
|
|
|
def __init__(self, client_api_inst=None):
|
|
|
|
return
|
2020-03-19 06:40:52 +00:00
|
|
|
|
2019-07-02 06:32:26 +00:00
|
|
|
def get_block_data(self, bHash, decrypt=False, raw=False, headerOnly=False):
|
2020-10-06 02:53:56 +00:00
|
|
|
if not stringvalidators.validate_hash(bHash):
|
|
|
|
raise onionrexceptions.InvalidHexHash(
|
|
|
|
"block hash not valid hash format")
|
2019-07-18 23:07:18 +00:00
|
|
|
bl = onionrblockapi.Block(bHash)
|
2019-07-02 06:32:26 +00:00
|
|
|
if decrypt:
|
|
|
|
bl.decrypt()
|
|
|
|
if bl.isEncrypted and not bl.decrypted:
|
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
if not raw:
|
|
|
|
if not headerOnly:
|
|
|
|
retData = {'meta':bl.bheader, 'metadata': bl.bmetadata, 'content': bl.bcontent}
|
|
|
|
for x in list(retData.keys()):
|
|
|
|
try:
|
|
|
|
retData[x] = retData[x].decode()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
validSig = False
|
|
|
|
signer = bytesconverter.bytes_to_str(bl.signer)
|
|
|
|
if bl.isSigned() and stringvalidators.validate_pub_key(signer) and bl.isSigner(signer):
|
2020-03-19 06:40:52 +00:00
|
|
|
validSig = True
|
2019-07-02 06:32:26 +00:00
|
|
|
bl.bheader['validSig'] = validSig
|
|
|
|
bl.bheader['meta'] = ''
|
|
|
|
retData = {'meta': bl.bheader, 'metadata': bl.bmetadata}
|
|
|
|
return json.dumps(retData)
|
|
|
|
else:
|
|
|
|
return bl.raw
|