def _collect_package_contents(contract_dir: str, check_all_files: bool) -> List[str]: """ Return list of relative paths of all files which should be part of the package for the contract in contract_dir. Raises an exception if contract.zkay, manifest.json or any of the files required by contract.zkay is missing. :param contract_dir: path to directory containing manifest and zkay file :param check_all_files: if true, checks whether all expected files are present :raise FileNotFoundError: if any of the expected files is not present :return: list of relative paths (relative to contract_dir) """ zkay_filename = os.path.join(contract_dir, 'contract.zkay') if not os.path.exists(zkay_filename): raise FileNotFoundError('contract.zkay not found in package') manifest_filename = os.path.join(contract_dir, 'manifest.json') if not os.path.exists(manifest_filename): raise FileNotFoundError('manifest.json not found in package') manifest = Manifest.load(contract_dir) files = ['contract.zkay', 'manifest.json'] with open(zkay_filename) as f: verifier_names = get_verification_contract_names(f.read()) with Manifest.with_manifest_config(manifest): gen_cls = generator_classes[cfg.snark_backend] files += [os.path.join(cfg.get_circuit_output_dir_name(v), k) for k in gen_cls.get_vk_and_pk_filenames() for v in verifier_names] if check_all_files: for f in files: path = os.path.join(contract_dir, f) if not os.path.exists(path) or not os.path.isfile(path): raise FileNotFoundError(f) return files
def extract_zkay_package(zkp_filename: str, output_dir: str): """ Unpack and compile a zkay contract. :param zkp_filename: path to the packaged contract :param output_dir: directory where to unpack and compile the contract :raise Exception: if import fails """ os.makedirs(output_dir) try: with zipfile.ZipFile(zkp_filename) as zkp: with print_step('Checking zip file integrity'): if zkp.testzip() is not None: raise ValueError('Corrupt archive') with print_step('Checking for correct file structure'): zkp.extract('contract.zkay', output_dir) zkp.extract('manifest.json', output_dir) expected_files = sorted(_collect_package_contents(output_dir, False)) contained_files = sorted([d.filename for d in zkp.infolist() if not d.is_dir()]) if expected_files != contained_files: raise ValueError(f'Package is invalid, does not match expected contents') with print_step('Extracting archive'): zkp.extractall(output_dir) # Compile extracted contract zkay_filename = os.path.join(output_dir, 'contract.zkay') manifest = Manifest.load(output_dir) with Manifest.with_manifest_config(manifest): compile_zkay_file(zkay_filename, output_dir, import_keys=True) except Exception as e: # If there was an exception, the archive is not safe -> remove extracted contents print(f'Package {zkp_filename} is either corrupt or incompatible with this zkay version.') shutil.rmtree(output_dir) raise e
def use_config_from_manifest(project_dir: str): """Override zkay configuration with values from the manifest file in project_dir.""" manifest = Manifest.load(project_dir) Manifest.import_manifest_config(manifest) Runtime.reset()
def use_configuration_from_manifest(contract_dir: str) -> Any: from zkay.transaction.runtime import Runtime manifest = Manifest.load(contract_dir) Manifest.import_manifest_config(manifest) Runtime.reset()