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 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 binary_payload(self) -> bytes: """ Get binary payload used for injection/hash calculation. """ if not self.signature: raise ValueError('Not signed') return bytes.fromhex(self.forge()) + forge_base58(self.signature)
def forge_operation_group(operation_group): """ Forge operation group (locally). :param operation_group: {"branch": "B...", "contents": [], ...} """ res = forge_base58(operation_group['branch']) res += b''.join(map(forge_operation, operation_group['contents'])) return res
def forge_activate_account(content: dict): res = forge_nat(operation_tags[content['kind']]) res += forge_base58(content['pkh']) res += bytes.fromhex(content['secret']) return res
def forge_operation_group(operation_group): res = forge_base58(operation_group['branch']) res += b''.join(map(forge_operation, operation_group['contents'])) return res