Download verifier/adapters/ipfs_client.py from TTOPM/belel-protocol: direct link, hf CLI and curl.
- Browser
- Download file 1.61 kB
-
https://huggingface.co/TTOPM/belel-protocol/resolve/main/verifier/adapters/ipfs_client.py
- Command line
-
hf download hf://TTOPM/belel-protocol/verifier/adapters/ipfs_client.py
-
curl -L -o ipfs_client.py https://huggingface.co/TTOPM/belel-protocol/resolve/main/verifier/adapters/ipfs_client.py
1.61 kB
| import os, json | |
| # ENV (optional): | |
| # - IPFS_API_MULTIADDR (default: /ip4/127.0.0.1/tcp/5001/http) | |
| # - PINATA_JWT (if present, uses Pinata pinning as fallback) | |
| def pin_bytes_to_ipfs(data: bytes) -> dict: | |
| """ | |
| Try local IPFS daemon first; fallback to Pinata if configured. | |
| Returns: {"ok":True,"cid":"..."} or {"ok":False,"error":"..."} | |
| """ | |
| # 1) Local daemon via ipfshttpclient | |
| try: | |
| import ipfshttpclient | |
| addr = os.environ.get("IPFS_API_MULTIADDR", "/ip4/127.0.0.1/tcp/5001/http") | |
| with ipfshttpclient.connect(addr) as client: | |
| res = client.add_bytes(data) | |
| return {"ok": True, "cid": res} | |
| except Exception as e: | |
| err_local = str(e) | |
| # 2) Pinata fallback | |
| token = os.environ.get("PINATA_JWT", "").strip() | |
| if token: | |
| import requests | |
| try: | |
| r = requests.post( | |
| "https://api.pinata.cloud/pinning/pinFileToIPFS", | |
| headers={"Authorization": f"Bearer {token}"}, | |
| files={"file": ("bundle.json", data, "application/json")}, | |
| timeout=30 | |
| ) | |
| if r.status_code in (200, 202): | |
| cid = r.json().get("IpfsHash") or r.json().get("ipfsHash") | |
| if cid: | |
| return {"ok": True, "cid": cid} | |
| return {"ok": False, "error": f"Pinata error: {r.status_code} {r.text}"} | |
| except Exception as e: | |
| return {"ok": False, "error": f"Pinata exception: {e}"} | |
| return {"ok": False, "error": f"IPFS local failed: {err_local}; no Pinata fallback configured"} | |