53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
from typing import Union
|
|
from time import time
|
|
from binascii import hexlify
|
|
|
|
import kasten
|
|
|
|
|
|
from .types import AnonymousVDFBlockResult
|
|
|
|
|
|
def __str_to_bytes(data: str):
|
|
try:
|
|
return data.encode('utf-8')
|
|
except AttributeError:
|
|
return data
|
|
|
|
|
|
def _packer(block_data: bytes, **kasten_header) -> kasten.main.KastenPacked:
|
|
def __get_optional(key, default):
|
|
try:
|
|
return kasten_header[key]
|
|
except KeyError:
|
|
return default
|
|
# Todo: validate length sanities
|
|
return kasten.generator.pack.pack(
|
|
block_data,
|
|
kasten_header['type'],
|
|
kasten_header['encrypt mode'],
|
|
__get_optional('signer', b''),
|
|
__get_optional('signature', b''),
|
|
__get_optional('metadata', None),
|
|
int(time()//60 * 60)
|
|
)
|
|
|
|
|
|
# Block creation functions handle calling the proof and compiling blocks
|
|
# They do not handle sigs or encryption (at least never directly)
|
|
|
|
def create_block_anonymous_vdf(
|
|
rounds: int,
|
|
block_data: Union[str, bytes],
|
|
**block_header) -> AnonymousVDFBlockResult:
|
|
block_data = __str_to_bytes(block_data)
|
|
# Block with anonymous VDF as proof
|
|
|
|
packed: kasten.types.KastenPacked = _packer(block_data, **block_header)
|
|
kasten_result: kasten.Kasten = \
|
|
kasten.generator.KastenMimcGenerator.generate(packed, rounds)
|
|
print('orig packed', packed)
|
|
return AnonymousVDFBlockResult(
|
|
kasten_result.id,
|
|
kasten_result.get_packed())
|