From 8d88e1d59ced0e573e364283b1b75f4e46048aba Mon Sep 17 00:00:00 2001 From: d4v1d03 Date: Thu, 13 Feb 2025 06:04:17 +0530 Subject: [PATCH 1/2] dag_to_car working Signed-off-by: d4v1d03 --- packages/w3up-python-client/.gitignore | 3 ++ packages/w3up-python-client/README.md | 0 packages/w3up-python-client/config.py | 0 packages/w3up-python-client/dag_to_car.py | 40 ++++++++++++++++++++ packages/w3up-python-client/myfile.txt | 1 + packages/w3up-python-client/requirements.txt | 2 + packages/w3up-python-client/setup.py | 27 +++++++++++++ 7 files changed, 73 insertions(+) create mode 100644 packages/w3up-python-client/.gitignore create mode 100644 packages/w3up-python-client/README.md create mode 100644 packages/w3up-python-client/config.py create mode 100644 packages/w3up-python-client/dag_to_car.py create mode 100644 packages/w3up-python-client/myfile.txt create mode 100644 packages/w3up-python-client/requirements.txt create mode 100644 packages/w3up-python-client/setup.py diff --git a/packages/w3up-python-client/.gitignore b/packages/w3up-python-client/.gitignore new file mode 100644 index 000000000..be4b7a503 --- /dev/null +++ b/packages/w3up-python-client/.gitignore @@ -0,0 +1,3 @@ +.env +*.car +/dag_to_car.egg-info \ No newline at end of file diff --git a/packages/w3up-python-client/README.md b/packages/w3up-python-client/README.md new file mode 100644 index 000000000..e69de29bb diff --git a/packages/w3up-python-client/config.py b/packages/w3up-python-client/config.py new file mode 100644 index 000000000..e69de29bb diff --git a/packages/w3up-python-client/dag_to_car.py b/packages/w3up-python-client/dag_to_car.py new file mode 100644 index 000000000..fc41de138 --- /dev/null +++ b/packages/w3up-python-client/dag_to_car.py @@ -0,0 +1,40 @@ +import subprocess +import sys +import os + +def run_command(command): + try: + result = subprocess.run(command, shell=True, check=True, text=True, capture_output=True) + return result.stdout.strip() + except subprocess.CalledProcessError as e: + print(f"❌ Error: {e.stderr}") + sys.exit(1) + + +def create_dag_and_car(file_path): + if not os.path.exists(file_path): + print(f"❌ Error: File '{file_path}' not found.") + sys.exit(1) + + print(f"📂 Adding '{file_path}' to IPFS DAG...") + + # Add file to IPFS DAG and get CID + cid = run_command(f"ipfs add --cid-version=1 --raw-leaves --quieter {file_path}") + print(f"✅ File added to DAG. CID: {cid}") + + car_file = f"{file_path}.car" + print(f"📦 Exporting DAG to CAR file: {car_file}...") + run_command(f"ipfs dag export {cid} > {car_file}") + print(f"✅ CAR file created: {car_file}") + + return cid, car_file + + +# Main script execution +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: python dag_to_car.py ") + sys.exit(1) + + file_path = sys.argv[1] + cid, car_file = create_dag_and_car(file_path) \ No newline at end of file diff --git a/packages/w3up-python-client/myfile.txt b/packages/w3up-python-client/myfile.txt new file mode 100644 index 000000000..911f61af0 --- /dev/null +++ b/packages/w3up-python-client/myfile.txt @@ -0,0 +1 @@ +Test conversion to car file \ No newline at end of file diff --git a/packages/w3up-python-client/requirements.txt b/packages/w3up-python-client/requirements.txt new file mode 100644 index 000000000..d44fe4434 --- /dev/null +++ b/packages/w3up-python-client/requirements.txt @@ -0,0 +1,2 @@ +requests +python-dotenv \ No newline at end of file diff --git a/packages/w3up-python-client/setup.py b/packages/w3up-python-client/setup.py new file mode 100644 index 000000000..28e9917d0 --- /dev/null +++ b/packages/w3up-python-client/setup.py @@ -0,0 +1,27 @@ +from setuptools import setup, find_packages + +setup( + name="dag_to_car", + version="0.1.0", + packages=find_packages(), + install_requires=[ + "requests", + "python-dotenv", + ], + entry_points={ + "console_scripts": [ + "dag-to-car=dag_to_car:main", + ], + }, + author="Amit Pandey", + author_email="a_pandey1@ce.iitr.ac.in", + description="A tool to automate IPFS DAG creation and CAR file generation.", + long_description=open("README.md").read(), + long_description_content_type="text/markdown", + classifiers=[ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + ], + python_requires=">=3.6", +) From b243aee99498d029c24e1f89a35e38b3c54704ac Mon Sep 17 00:00:00 2001 From: Amit Pandey <95427130+d4v1d03@users.noreply.github.com> Date: Sun, 16 Feb 2025 10:09:54 +0530 Subject: [PATCH 2/2] "w3cli used" --- packages/w3up-python-client/config.py | 2 + packages/w3up-python-client/dag_to_car.py | 77 +++++++++++++++++--- packages/w3up-python-client/requirements.txt | 2 +- 3 files changed, 71 insertions(+), 10 deletions(-) diff --git a/packages/w3up-python-client/config.py b/packages/w3up-python-client/config.py index e69de29bb..50ff41a2f 100644 --- a/packages/w3up-python-client/config.py +++ b/packages/w3up-python-client/config.py @@ -0,0 +1,2 @@ +STORACHA_API_URL = "https://w3s.link/api/upload" +SPACE_NAME = "MyStorachaSpace" diff --git a/packages/w3up-python-client/dag_to_car.py b/packages/w3up-python-client/dag_to_car.py index fc41de138..88c001c03 100644 --- a/packages/w3up-python-client/dag_to_car.py +++ b/packages/w3up-python-client/dag_to_car.py @@ -1,6 +1,9 @@ import subprocess import sys import os +import json +import requests +from config import SPACE_NAME def run_command(command): try: @@ -10,31 +13,87 @@ def run_command(command): print(f"❌ Error: {e.stderr}") sys.exit(1) - def create_dag_and_car(file_path): if not os.path.exists(file_path): print(f"❌ Error: File '{file_path}' not found.") sys.exit(1) - + print(f"📂 Adding '{file_path}' to IPFS DAG...") - # Add file to IPFS DAG and get CID cid = run_command(f"ipfs add --cid-version=1 --raw-leaves --quieter {file_path}") print(f"✅ File added to DAG. CID: {cid}") - + car_file = f"{file_path}.car" print(f"📦 Exporting DAG to CAR file: {car_file}...") run_command(f"ipfs dag export {cid} > {car_file}") print(f"✅ CAR file created: {car_file}") - + return cid, car_file +try: + space_data = json.loads(space_output) + space_did = space_data.get("did", "Unknown DID") + print(f"✅ Space created successfully. DID: {space_did}") +except json.JSONDecodeError: + print(f"✅ Space created. CLI Output: {space_output}") + space_did = space_output.strip() + + +def create_delegation(): + print("🔑 Creating delegation...") + delegation = run_command("w3 delegation create") + print("✅ Delegation created.") + return delegation + +def get_http_auth(): + print("🔍 Retrieving HTTP authentication details...") + auth_json = run_command("w3 bridge generate-tokens") + auth_data = json.loads(auth_json) + return auth_data["X-Auth-Secret"], auth_data["Authorization"] + +def upload_car_file(car_file, x_auth_secret, authorization): + print(f"📤 Uploading CAR file: {car_file} to Storacha HTTP bridge...") + + # Define the URL endpoint for upload + upload_url = "https://w3s.link/api/upload" + headers = { + "X-Auth-Secret": x_auth_secret, + "Authorization": authorization, + "Content-Type": "application/car" + } + + with open(car_file, 'rb') as file: + response = requests.post(upload_url, headers=headers, files={"file": file}) + + if response.status_code == 200: + receipt = response.json() + print("✅ Upload successful! Client Receipt:") + print(json.dumps(receipt, indent=4)) + return receipt + else: + print(f"❌ Upload failed: {response.text}") + sys.exit(1) -# Main script execution +# Main execution if __name__ == "__main__": if len(sys.argv) < 2: - print("Usage: python dag_to_car.py ") + print("Usage: python storacha_upload.py ") sys.exit(1) - + file_path = sys.argv[1] - cid, car_file = create_dag_and_car(file_path) \ No newline at end of file + space_name = "MyStorachaSpace" + + cid, car_file = create_dag_and_car(file_path) + + space_did = create_w3_space(space_name) + create_delegation() + + x_auth_secret, authorization = get_http_auth() + + client_receipt = upload_car_file(car_file, x_auth_secret, authorization) + + #Client receipt + receipt_file = "client_receipt.json" + with open(receipt_file, "w") as f: + json.dump(client_receipt, f, indent=4) + print(f"📜 Client receipt saved to {receipt_file}") diff --git a/packages/w3up-python-client/requirements.txt b/packages/w3up-python-client/requirements.txt index d44fe4434..df7458c25 100644 --- a/packages/w3up-python-client/requirements.txt +++ b/packages/w3up-python-client/requirements.txt @@ -1,2 +1,2 @@ requests -python-dotenv \ No newline at end of file +python-dotenv