Metadata-Version: 2.4
Name: zg-storage
Version: 0.1.0
Summary: Python SDK for 0G Storage
Author: 0G Foundation
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: httpx>=0.27
Requires-Dist: pydantic>=2.7
Requires-Dist: web3>=6.0
Requires-Dist: cryptography>=41.0
Provides-Extra: test
Requires-Dist: pytest>=7.4; extra == "test"
Requires-Dist: black>=24.0; extra == "test"
Requires-Dist: pylint>=3.0; extra == "test"
Requires-Dist: isort>=5.13; extra == "test"
Requires-Dist: autoflake>=2.3; extra == "test"
Requires-Dist: rtoml>=0.9; extra == "test"
Requires-Dist: eth_utils>=2.0; extra == "test"
Requires-Dist: requests>=2.28; extra == "test"
Requires-Dist: coincurve>=18.0; extra == "test"
Requires-Dist: rlp>=3.0; extra == "test"
Requires-Dist: py_ecc>=6.0; extra == "test"
Requires-Dist: jsonrpcclient>=4.0; extra == "test"

# 0G Storage Python SDK

This repository provides a Python SDK for interacting with 0G Storage.

## Installation

- Requires Python 3.9+
- Install (editable): `pip install -e .`
- Dev deps: `pip install -e .[test]`

## Quick Start (Upload: Indexer or Node)

```python
import os

from zg_storage import EvmClient
from zg_storage.indexer import IndexerClient
from zg_storage.transfer import NodeUploader, NodeUploaderConfig, UploadOption

USE_INDEXER = True

private_key = os.environ["PRIVATE_KEY"]
evm = EvmClient(
    rpc_url="https://evmrpc-testnet.0g.ai",
    private_key=private_key,
)

if USE_INDEXER:
    indexer = IndexerClient(
        "https://indexer-storage-testnet-turbo.0g.ai",
        evm_client=evm,
        flow_address="0x...",
    )
    tx_hash, root_hex = indexer.upload(file_path="your_file.dat", tags=b"", option=UploadOption())
else:
    cfg = NodeUploaderConfig(
        nodes=["http://node-1:5678", "http://node-2:5678"],
        evm_client=evm,
        flow_address="0x...",
    )
    uploader = NodeUploader.from_config(cfg)
    tx_hash, root_hex = uploader.upload(file_path="your_file.dat", tags=b"", option=UploadOption())

print("tx:", tx_hash)
print("root:", root_hex)
```

## Download (Indexer or Node List)

```python
from zg_storage.indexer import IndexerClient
from zg_storage.transfer import DownloadOption, NodeDownloader, NodeDownloaderConfig

USE_INDEXER = True
root = "0x..."
target = "downloaded.bin"

opt = DownloadOption()

if USE_INDEXER:
    indexer = IndexerClient("https://indexer-storage-testnet-turbo.0g.ai")
    indexer.download(root=root, filename=target, option=opt)
else:
    cfg = NodeDownloaderConfig(
        nodes=["http://node-1:5678", "http://node-2:5678"],
    )
    downloader = NodeDownloader.from_config(cfg)
    downloader.download(root=root, target=target, option=opt)
```

For all detailed parameters, advanced options, and latest endpoint/config guidance, see `https://doc.0g.ai`.

## KV Write (Memory Upload)

```python
import os

from zg_storage import EvmClient
from zg_storage.indexer import IndexerClient
from zg_storage.kv import StreamDataBuilder
from zg_storage.kv.types import create_tags
from zg_storage.core.data import BytesDataSource
from zg_storage.transfer import UploadOption

private_key = os.environ["PRIVATE_KEY"]
evm = EvmClient(rpc_url="https://evmrpc-testnet.0g.ai", private_key=private_key)

indexer = IndexerClient(
    "https://indexer-storage-testnet-turbo.0g.ai",
    evm_client=evm,
    flow_address="0x...",
)

builder = StreamDataBuilder()
builder.set(
    stream_id="0x" + "00" * 32,  # dummy stream id
    key=b"demo_key",             # dummy key
    data=b"demo_value",          # dummy value
)
stream_data = builder.build(sorted_items=True)
payload = stream_data.encode()
tags = create_tags(builder.stream_ids(), sorted_ids=True)

opt = UploadOption(tags=tags)
tx_hash, root_hex = indexer.upload(
    file_path=BytesDataSource(payload),
    tags=tags,
    option=opt,
)
```

## KV Read

```python
from zg_storage.kv import KvClient

kv = KvClient("http://your-kv-node:6789")
data = kv.get_values(
    "0x" + "00" * 32,  # dummy stream id
    ["demo_key"],      # dummy keys
)
print(data)
```

## Development

- Requires Python 3.9+
- Install dev deps: `pip install -e .[test]`
- Run tests: `pytest`
