File size: 3,181 Bytes
9ee5bbb
 
05b9f96
9ee5bbb
 
05b9f96
 
9ee5bbb
05b9f96
9ee5bbb
 
 
 
05b9f96
 
 
 
 
 
9ee5bbb
05b9f96
 
 
 
 
 
 
 
 
 
9ee5bbb
 
05b9f96
 
 
 
9ee5bbb
05b9f96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ee5bbb
 
05b9f96
 
 
 
 
 
 
9ee5bbb
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# src/protocol/decentralized_comm/ipfs_client.py 🌐🗃️

import os
import json
import logging
import ipfshttpclient
from typing import Optional

logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

class IPFSClient:
    """
    Interface to IPFS for storing and retrieving JSON data.

    Args:
        ipfs_address: Multiaddr for the IPFS API. Falls back to IPFS_API_ADDR env
                      or '/dns/localhost/tcp/5001/http'.
        strict: If True, raise RuntimeError on failures. If False, return None on failures.
        pin_on_add: If True, pin CIDs after adding (best-effort).
    """
    def __init__(
        self,
        ipfs_address: Optional[str] = None,
        *,
        strict: bool = True,
        pin_on_add: bool = False,
    ):
        addr = ipfs_address or os.getenv("IPFS_API_ADDR", "/dns/localhost/tcp/5001/http")
        self.strict = strict
        self.pin_on_add = pin_on_add

        try:
            self.client = ipfshttpclient.connect(addr)
            # Lightweight check that we can actually talk to the node
            _ = self.client.id()
            logging.info(f"Connected to IPFS node at {addr}")
        except Exception as e:
            logging.error(f"Failed to connect to IPFS at {addr}: {e}")
            if self.strict:
                raise RuntimeError(f"IPFS connect failed: {e}") from e
            self.client = None

    def _fail(self, msg: str, exc: Exception | None = None):
        logging.error(msg)
        if self.strict:
            raise RuntimeError(msg) from exc
        return None

    def add_json(self, data: dict) -> Optional[str]:
        """
        Add a JSON object to IPFS. Returns the CID string on success.
        """
        if not self.client:
            return self._fail("IPFS add_json failed: no active client", None)

        try:
            encoded = json.dumps(data, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
            cid = self.client.add_bytes(encoded)
            if self.pin_on_add:
                try:
                    self.client.pin.add(cid)
                except Exception as e:
                    logging.warning(f"Pinning CID {cid} failed (continuing): {e}")
            logging.info(f"Data added to IPFS: CID {cid}")
            return cid
        except Exception as e:
            return self._fail(f"IPFS add_json failed: {e}", e)

    def cat_json(self, cid: str) -> Optional[dict]:
        """
        Retrieve and decode a JSON object from IPFS by CID.
        """
        if not self.client:
            return self._fail("IPFS cat_json failed: no active client", None)

        try:
            raw = self.client.cat(cid)
            decoded = json.loads(raw.decode("utf-8"))
            logging.info(f"Data retrieved from IPFS: CID {cid}")
            return decoded
        except Exception as e:
            return self._fail(f"IPFS cat_json failed for CID {cid}: {e}", e)

            logging.info(f"Data retrieved from IPFS: CID {cid}")
            return decoded
        except Exception as e:
            logging.error(f"IPFS cat_json failed for CID {cid}: {e}")
            return None