SHA256
Новый формат блоков CORE/CONTENT test5595
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
"""Minimal pair encoding for test POST writers; no new publication transport."""
|
||||
import base64
|
||||
import hashlib
|
||||
import struct
|
||||
import time
|
||||
from protocol_tags import NAMESPACE
|
||||
|
||||
MAX_NUMBER = 2147482623
|
||||
|
||||
|
||||
def sign_post(sign_item, key, prev_hash, number, line_code, prev_line_number, prev_line_hash, text, timestamp=None):
|
||||
body = text.encode('utf-8')
|
||||
if not text.strip() or len(body) > 65535:
|
||||
raise ValueError('invalid post text')
|
||||
if not (0 <= line_code <= prev_line_number < number <= MAX_NUMBER) or len(prev_hash) != 32 or len(prev_line_hash) != 32:
|
||||
raise ValueError('invalid CORE predecessor')
|
||||
content = struct.pack('>BHHHH', 1, 1, 10, 1, len(body)) + body
|
||||
content_item = sign_item(key, [], content)
|
||||
core = struct.pack('>B32siq32s32sii32s', 1, prev_hash, number,
|
||||
int(time.time()) if timestamp is None else timestamp,
|
||||
hashlib.sha256(content).digest(), hashlib.sha256(content_item[2:66]).digest(),
|
||||
line_code, prev_line_number, prev_line_hash)
|
||||
core_item = sign_item(key, [('App', NAMESPACE), ('n', str(number)), ('l', str(line_code))], core)
|
||||
return struct.pack('>I', len(core_item)) + core_item + content_item, hashlib.sha256(core).digest()
|
||||
|
||||
|
||||
def _payload(item):
|
||||
if len(item) < 117 or item[:2] != b'\x02\x00' or item[98:100] != b'\0\0':
|
||||
raise ValueError('expected canonical ANS-104')
|
||||
size = struct.unpack_from('<Q', item, 108)[0]
|
||||
if size > 65536 or 116 + size >= len(item):
|
||||
raise ValueError('invalid ANS-104 tags size')
|
||||
return item[116 + size:]
|
||||
|
||||
|
||||
def decode_stored_pair(raw_b64, expected_hash):
|
||||
raw = base64.urlsafe_b64decode(raw_b64 + '=' * (-len(raw_b64) % 4))
|
||||
if len(raw) < 236:
|
||||
raise ValueError('expected CORE/CONTENT container')
|
||||
n = struct.unpack_from('>I', raw)[0]
|
||||
if n < 117 or n + 4 + 117 > len(raw):
|
||||
raise ValueError('invalid pair length')
|
||||
core = _payload(raw[4:4+n]); content_item = raw[4+n:]; content = _payload(content_item)
|
||||
if (len(core) not in (109, 149) or core[0] != 1 or len(content) < 7 or content[0] != 1
|
||||
or hashlib.sha256(core).hexdigest() != expected_hash.lower()
|
||||
or hashlib.sha256(content).digest() != core[45:77]
|
||||
or hashlib.sha256(content_item[2:66]).digest() != core[77:109]):
|
||||
raise ValueError('stored pair hash mismatch')
|
||||
return core, content
|
||||
|
||||
|
||||
async def find_channel_tail(fetch_block, cfg, head_number, root_hash):
|
||||
"""Use global block numbers, including edits/meta; don't mistake lineStep for a block number."""
|
||||
root = int(cfg['channel_root_block_number'])
|
||||
if not 0 <= root <= head_number:
|
||||
raise ValueError('channel root not in history')
|
||||
for number in range(head_number, root, -1):
|
||||
block_hash, raw = await fetch_block(cfg, number)
|
||||
core, content = decode_stored_pair(raw, block_hash)
|
||||
if struct.unpack_from('>i', core, 33)[0] != number:
|
||||
raise ValueError('server returned wrong block number')
|
||||
typ, subtype, version = struct.unpack_from('>HHH', content, 1)
|
||||
if (len(core) == 149 and typ == 1 and version == 1 and subtype in (10,11,50,90,92,100,110,120,130)
|
||||
and struct.unpack_from('>i', core, 109)[0] == root):
|
||||
if subtype == 92:
|
||||
raise ValueError('channel has been deleted')
|
||||
return number, bytes.fromhex(block_hash)
|
||||
return root, bytes.fromhex(root_hash)
|
||||
|
||||
|
||||
def require_namespace(state):
|
||||
if state.get('namespace') != NAMESPACE:
|
||||
raise ValueError('old publisher state; use a new state_file for ' + NAMESPACE)
|
||||
Reference in New Issue
Block a user