-
Notifications
You must be signed in to change notification settings - Fork 25
feat: Add dag_to_car functionality #1622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
d4v1d03
wants to merge
16
commits into
storacha:main
Choose a base branch
from
d4v1d03:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8d88e1d
dag_to_car working
d4v1d03 51ad358
Merge branch 'storacha:main' into main
d4v1d03 b243aee
"w3cli used"
d4v1d03 42f972c
added the use of http bridge
d4v1d03 9d978d9
test
d4v1d03 859b057
level-1 done
d4v1d03 3bb1338
clean code and readme added
d4v1d03 799582c
added all the available instructions in the http bridge
d4v1d03 aa2b148
Merge branch 'main' into main
d4v1d03 2093ead
fixes after review
d4v1d03 d026401
"fixes done after review"
d4v1d03 6112b9d
all fixes done
d4v1d03 a2cbd59
fixes
d4v1d03 eb84770
Merge branch 'main' into main
d4v1d03 1c87135
changes according to the feedback given
d4v1d03 14ed8b5
added to package.json
d4v1d03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| .env | ||
| *.car | ||
| /storacha_uploader.egg-info | ||
| /myenv | ||
| node_modules | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # Storacha Uploader | ||
|
|
||
| A tool to create CAR files and upload content to Storacha decentralized storage. | ||
|
|
||
| ## Install | ||
|
|
||
| ```bash | ||
| # Requires Python 3.6+, Node.js and npm | ||
| pip install . | ||
| ``` | ||
|
|
||
| ## Configure | ||
|
|
||
| Create a `.env` file with your credentials: | ||
| ``` | ||
| X_AUTH_SECRET_HEADER=your_auth_secret | ||
| AUTHORIZATION_HEADER=your_authorization | ||
| SPACE_DID=your_space_did | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```bash | ||
| storacha-uploader myfile.txt | ||
| ``` | ||
|
|
||
| This converts the file to CAR format, uploads it to Storacha, and provides an access link. | ||
|
|
||
|
|
||
| ## Instructions to be used over the http bridge | ||
|
|
||
| Below instructions can be used over the http bridge by adding them to the list.json file. | ||
|
|
||
| ```bash | ||
| { | ||
| "tasks": [ | ||
| /* | ||
| [ | ||
| "access/delegate", | ||
| "did:key:z6Mkabc123", | ||
| { "delegate": "<delegate_address>" } | ||
| ], | ||
| [ | ||
| "space/info", | ||
| "did:key:z6Mkabc123" | ||
| ], | ||
| [ | ||
| "space/allocate", | ||
| "did:key:z6Mkabc123", | ||
| { "allocation": "<allocation_details>" } | ||
| ], | ||
| [ | ||
| "store/add", | ||
| "did:key:z6Mkabc123", | ||
| { "root": { "/": "<cid>" }, "shards": [] } | ||
| ], | ||
| [ | ||
| "store/get", | ||
| "did:key:z6Mkabc123", | ||
| { "root": { "/": "<cid>" } } | ||
| ], | ||
| [ | ||
| "store/remove", | ||
| "did:key:z6Mkabc123", | ||
| { "root": { "/": "<cid>" } } | ||
| ], | ||
| [ | ||
| "store/list", | ||
| "did:key:z6Mkabc123" | ||
| ], | ||
| */ | ||
| [ | ||
| "upload/add", | ||
| "did:key:z6Mkabc123", | ||
| { "root": { "/": "<cid>" }, "shards": [] } | ||
| ] | ||
| /* | ||
| , | ||
| [ | ||
| "upload/list", | ||
| "did:key:z6Mkabc123", | ||
| {} | ||
| ], | ||
| [ | ||
| "upload/remove", | ||
| "did:key:z6Mkabc123", | ||
| { "root": { "/": "<cid>" } } | ||
| ], | ||
| [ | ||
| "usage/report", | ||
| "did:key:z6Mkabc123" | ||
| ] | ||
| */ | ||
| ] | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "tasks": [ | ||
| [ | ||
| "upload/list", | ||
| "did:key:z6MksbhMHBFohHmhhDNkLVSRr9FgSJRU4rHrP74ggr2EiRt7", | ||
| {} | ||
| ] | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test conversion to car file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| requests | ||
| python-dotenv | ||
| setuptools |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import subprocess | ||
| import sys | ||
| import platform | ||
| from setuptools import setup, find_packages | ||
|
|
||
| def check_w3cli(): | ||
| print("🔍 Checking for w3cli...") | ||
| try: | ||
| subprocess.run(["w3", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
| print("✅ w3cli is already installed.") | ||
| return True | ||
| except FileNotFoundError: | ||
| print("❌ w3cli not found.") | ||
| print("📦 Installing w3cli globally...") | ||
| try: | ||
| subprocess.run("npm install -g @web3-storage/w3cli", shell=True, check=True) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it need to be global - can you just add it to the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| print("✅ w3cli installed successfully.") | ||
| return True | ||
| except subprocess.CalledProcessError as e: | ||
| print(f"❌ Failed to install w3cli: {e}") | ||
| print("Please install it manually with: npm install -g @web3-storage/w3cli") | ||
| return False | ||
|
|
||
| w3cli_installed = check_w3cli() | ||
|
|
||
| with open('requirements.txt') as f: | ||
| requirements = f.read().splitlines() | ||
|
|
||
| setup( | ||
| name="storacha_uploader", | ||
| version="0.1.0", | ||
| packages=find_packages(), | ||
| install_requires=requirements, | ||
| entry_points={ | ||
| "console_scripts": [ | ||
| "storacha-uploader=storacha_uploader:main", | ||
| ], | ||
| }, | ||
| author="Amit Pandey", | ||
| author_email="a_pandey1@ce.iitr.ac.in", | ||
| description="A tool to automate IPFS DAG creation, CAR file generation, and Web3 Storage upload.", | ||
| long_description=open("README.md").read() if sys.version_info[0] >= 3 else "", | ||
| long_description_content_type="text/markdown", | ||
| classifiers=[ | ||
| "Programming Language :: Python :: 3", | ||
| "License :: OSI Approved :: MIT License", | ||
| "Operating System :: OS Independent", | ||
| ], | ||
| python_requires=">=3.6", | ||
| ) | ||
|
|
||
| print("\n✅ Setup completed.") | ||
| print("📝 Note: Make sure Node.js and npm are installed on your system.") | ||
| print("🚀 You can now use the 'storacha-uploader' command to upload files to Storacha network.") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import os | ||
| import sys | ||
| import json | ||
| import subprocess | ||
| import requests | ||
| import tempfile | ||
| import platform | ||
| from dotenv import load_dotenv | ||
|
|
||
| def run_command(command): | ||
| try: | ||
| result = subprocess.check_output(command, shell=True, text=True).strip() | ||
| return result | ||
| except subprocess.CalledProcessError as e: | ||
| print(f"❌ Error: {e}") | ||
| return None | ||
|
|
||
| def send_request_to_the_bridge(json_payload, auth_secret, authorization, endpoint): | ||
| headers = { | ||
| "X_AUTH_SECRET": auth_secret, | ||
| "Authorization": authorization, | ||
| "Content-Type": "application/json" | ||
| } | ||
|
|
||
| response = requests.post(endpoint, headers=headers, data=json_payload) | ||
|
|
||
| if response.status_code == 200: | ||
| response_data = response.json() | ||
| upload_url = response_data.get('upload_url') | ||
| if not upload_url: | ||
| raise ValueError("Upload URL not found in the response.") | ||
| return upload_url | ||
| else: | ||
| raise requests.HTTPError(f"Upload registration failed: {response.status_code} {response.text}") | ||
|
|
||
| def upload_car_file(upload_url, car_file_path): | ||
| with open(car_file_path, 'rb') as f: | ||
| response = requests.put(upload_url, data=f) | ||
| if response.status_code not in [200, 201]: | ||
| raise requests.HTTPError(f"CAR file upload failed: {response.status_code} {response.text}") | ||
|
|
||
| def upload_add(cid): | ||
| payload = { | ||
| "tasks": [ | ||
| ["upload/add", "did:key:z6Mkabc123", {"root": {"/": cid}, "shards": []}] | ||
| ] | ||
| } | ||
| return send_request(payload) | ||
|
|
||
|
|
||
| def upload_list(): | ||
| payload = { | ||
| "tasks": [ | ||
| ["upload/list", "did:key:z6Mkabc123", {}] | ||
| ] | ||
| } | ||
| return send_request(payload) | ||
|
|
||
|
|
||
| def upload_remove(cid): | ||
| payload = { | ||
| "tasks": [ | ||
| ["upload/remove", "did:key:z6Mkabc123", {"root": {"/": cid}}] | ||
| ] | ||
| } | ||
| return send_request(payload) | ||
|
|
||
|
|
||
| def send_request(payload,endpoint,headers): | ||
| response = requests.post(endpoint, headers=headers, json=payload) | ||
| if response.status_code == 200: | ||
| return response.json() | ||
| else: | ||
| raise requests.HTTPError(f"Request failed: {response.status_code} {response.text}") | ||
|
|
||
| def main(): | ||
| load_dotenv() | ||
|
|
||
| auth_secret = os.getenv("X_AUTH_SECRET_HEADER") | ||
| authorization = os.getenv("AUTHORIZATION_HEADER") | ||
| endpoint = os.getenv("HTTPS_ENDPOINT", "https://up.storacha.network/bridge") | ||
|
|
||
| if len(sys.argv) < 2: | ||
| print("❌ Error: Please provide an operation (upload_add, upload_list, upload_remove) and optional CID.") | ||
| print("Usage: python storacha_uploader.py <operation> <CID>") | ||
| sys.exit(1) | ||
|
|
||
| operation = sys.argv[1] | ||
| cid = sys.argv[2] if len(sys.argv) > 2 else None | ||
|
|
||
| operations = { | ||
| "upload_add": upload_add, | ||
| "upload_list": upload_list, | ||
| "upload_remove": upload_remove, | ||
| } | ||
|
|
||
| if operation in operations: | ||
| result = operations[operation](cid if "list" not in operation else None) | ||
| print(json.dumps(result, indent=2)) | ||
| else: | ||
| print(f"Unknown operation: {operation}") | ||
|
|
||
|
|
||
| file_path = sys.argv[1] | ||
|
|
||
| space_did = os.getenv("SPACE_DID") | ||
| if not space_did: | ||
| print("❌ Error: SPACE_DID not found in .env file.") | ||
| print("Please add SPACE_DID=your_space_did to your .env file.") | ||
| sys.exit(1) | ||
|
|
||
| print(f"🔑 Using Space DID: {space_did}") | ||
|
|
||
| car_file = f"{file_path}.car" | ||
| ipfs_car_cmd = ["ipfs-car", "pack", file_path, "--output", car_file] | ||
|
|
||
| try: | ||
| result = subprocess.run(ipfs_car_cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
| cid = result.stdout.decode().strip().split('\n')[-1] | ||
| print(f"✅ CAR file created: {car_file} with CID: {cid}") | ||
| except subprocess.CalledProcessError as e: | ||
| print(f"❌ Error creating CAR file: {e.stderr.decode().strip()}") | ||
| sys.exit(1) | ||
|
|
||
| upload_url = send_request_to_the_bridge(auth_secret, authorization, endpoint) | ||
| upload_car_file(upload_url, car_file) | ||
| upload_add(cid) | ||
| print(f"✅ File uploaded successfully to Storacha network.") | ||
| print(f"✅ Access your file at: https://{cid}.ipfs.w3s.link") | ||
| print(f"✅ Response details: {json.dumps(result, indent=2)}") | ||
|
|
||
| except Exception as e: | ||
| print(f"❌ Error: {e}") | ||
| sys.exit(1) | ||
| if __name__ == "__main__": | ||
| main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a bit confusing, why not expose these as separate methods on the client?