def encode_literal(value, prim, binary=False): core_type = 'string' if prim in ['int', 'nat', 'big_map']: core_type = 'int' elif prim == 'timestamp': if isinstance(value, int): core_type = 'int' elif isinstance(value, datetime): value = value.strftime('%Y-%m-%dT%H:%M:%SZ') elif prim == 'mutez': core_type = 'int' if isinstance(value, Decimal): value = int(value * 10 ** 6) elif prim == 'bool': core_type = 'prim' value = 'True' if value else 'False' elif prim == 'bytes': core_type = 'bytes' if isinstance(value, bytes): value = value.hex() elif prim == 'key' and binary: core_type = 'bytes' value = forge_public_key(value).hex() elif prim in ['address', 'contract', 'key_hash'] and binary: core_type = 'bytes' value = forge_address(value, tz_only=prim == 'key_hash').hex() elif prim == 'chain_id': # and binary ? core_type = 'bytes' value = forge_base58(value).hex() return {core_type: str(value)}
def try_pack(val_node, type_node): type_prim, type_args = parse_prim_expr(type_node) is_string = isinstance(val_node, dict) and val_node.get('string') if type_prim in ['set', 'list']: val_node = [try_pack(i, type_args[0]) for i in val_node] elif type_prim in ['map', 'big_map']: val_node = [{'prim': 'Elt', 'args': [try_pack(elt['args'][i], type_args[i]) for i in [0, 1]]} for elt in val_node] elif type_prim == 'pair': val_node['args'] = [try_pack(val_node['args'][i], type_args[i]) for i in [0, 1]] elif type_prim == 'option': if val_node['prim'] == 'Some': val_node['args'] = [try_pack(val_node['args'][0], type_args[0])] elif type_prim == 'or': type_idx = 0 if val_node['prim'] == 'Left' else 1 val_node['args'] = [try_pack(val_node['args'][0], type_args[type_idx])] elif type_prim == 'lambda': pass # TODO: PUSH, SELF, CONTRACT elif type_prim == 'chain_id' and is_string: return {'bytes': forge_base58(val_node['string']).hex()} elif type_prim == 'signature' and is_string: return {'bytes': forge_base58(val_node['string']).hex()} elif type_prim == 'key_hash' and is_string: return {'bytes': forge_address(val_node['string'], tz_only=True).hex()} elif type_prim == 'key' and is_string: return {'bytes': forge_public_key(val_node['string']).hex()} elif type_prim == 'address' and is_string: return {'bytes': forge_address(val_node['string']).hex()} elif type_prim == 'contract' and is_string: return {'bytes': forge_contract(val_node['string']).hex()} elif type_prim == 'timestamp' and is_string: return {'int': forge_timestamp(val_node['string'])} return val_node
def forge_reveal(content): res = forge_nat(operation_tags[content['kind']]) res += forge_address(content['source'], tz_only=True) res += forge_nat(int(content['fee'])) res += forge_nat(int(content['counter'])) res += forge_nat(int(content['gas_limit'])) res += forge_nat(int(content['storage_limit'])) res += forge_public_key(content['public_key']) return res