Esempio n. 1
0
def _pin_source(
    name: str,
    compiler_output: Dict[str, Any],
    ipfs_backend: BaseIPFSBackend,
    package_root_dir: Optional[Path],
    manifest: Manifest,
) -> Manifest:
    names_and_paths = get_names_and_paths(compiler_output)
    source_path = names_and_paths[name]
    if package_root_dir:
        if not (package_root_dir / source_path).is_file():
            raise ManifestBuildingError(
                f"Unable to find and pin contract source: {source_path} "
                f"under specified package_root_dir: {package_root_dir}.")
        (ipfs_data, ) = ipfs_backend.pin_assets(package_root_dir / source_path)
    else:
        cwd = Path.cwd()
        if not (cwd / source_path).is_file():
            raise ManifestBuildingError(
                f"Unable to find and pin contract source: {source_path} "
                f"current working directory: {cwd}.")
        (ipfs_data, ) = ipfs_backend.pin_assets(cwd / source_path)

    return assoc_in(manifest, ["sources", source_path],
                    f"ipfs://{ipfs_data['Hash']}")
Esempio n. 2
0
def _pin_source(
    name: str,
    compiler_output: Dict[str, Any],
    ipfs_backend: BaseIPFSBackend,
    package_root_dir: Optional[Path],
    manifest: Manifest,
) -> Manifest:
    names_and_paths = get_names_and_paths(compiler_output)
    try:
        source_path = names_and_paths[name]
    except KeyError:
        raise ManifestBuildingError(
            f"Unable to pin source: {name}. "
            f"Available sources include: {list(sorted(names_and_paths.keys()))}."
        )
    if package_root_dir:
        if not (package_root_dir / source_path).is_file():
            raise ManifestBuildingError(
                f"Unable to find and pin contract source: {source_path} "
                f"under specified package_root_dir: {package_root_dir}.")
        (ipfs_data, ) = ipfs_backend.pin_assets(package_root_dir / source_path)
    else:
        cwd = Path.cwd()
        if not (cwd / source_path).is_file():
            raise ManifestBuildingError(
                f"Unable to find and pin contract source: {source_path} "
                f"current working directory: {cwd}.")
        (ipfs_data, ) = ipfs_backend.pin_assets(cwd / source_path)

    source_data_object = {
        "urls": [f"ipfs://{ipfs_data['Hash']}"],
        "type": "solidity",
        "installPath": source_path,
    }
    return assoc_in(manifest, ["sources", source_path], source_data_object)
def pin_to_ipfs(
        manifest: Manifest, *, backend: BaseIPFSBackend, prettify: Optional[bool] = False
) -> List[Dict[str, str]]:
    """
    Returns the IPFS pin data after pinning the manifest to the provided IPFS Backend.

    `pin_to_ipfs()` Should *always* be the last argument in a builder, as it will return the pin
    data and not the manifest.
    """
    contents = format_manifest(manifest, prettify=prettify)

    with tempfile.NamedTemporaryFile() as temp:
        temp.write(to_bytes(text=contents))
        temp.seek(0)
        return backend.pin_assets(Path(temp.name))