Metadata-Version: 2.4
Name: 0g-storage-sdk
Version: 0.2.0
Summary: Official Python SDK for 0G Storage - A decentralized storage network with merkle tree verification
Home-page: https://github.com/0glabs/0g-py-sdk
Author: 0G Labs
Author-email: support@0g.ai
Project-URL: Bug Tracker, https://github.com/0glabs/0g-py-sdk/issues
Project-URL: Documentation, https://docs.0g.ai
Project-URL: Source Code, https://github.com/0glabs/0g-py-sdk/tree/main/0g_py_storage
Keywords: 0g storage blockchain web3 merkle cryptography decentralized
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Operating System :: OS Independent
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Requires-Dist: pycryptodome>=3.23.0
Requires-Dist: web3>=7.14.0
Requires-Dist: eth-account>=0.13.7
Requires-Dist: requests>=2.32.5
Provides-Extra: dev
Requires-Dist: pytest>=8.4.2; extra == "dev"
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: keywords
Dynamic: project-url
Dynamic: provides-extra
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# 0G Storage Python SDK

**Official Python SDK for 0G Storage** - A decentralized storage network with merkle tree verification.

[![Production Ready](https://img.shields.io/badge/status-production--ready-brightgreen.svg)]()
[![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)]()
[![Tests Passing](https://img.shields.io/badge/tests-66%2F66%20passing-success.svg)]()
[![TypeScript Parity](https://img.shields.io/badge/TypeScript%20SDK-100%25%20parity-blue.svg)]()

Complete line-by-line port of the official TypeScript SDK: [`@0glabs/0g-ts-sdk`](https://github.com/0glabs/0g-ts-sdk)

## 🎯 Production Status

✅ **Verified on 0G Testnet**
- Successfully uploaded files (TX: `9f01808921020c29b25e21204bfeb7079ce7cf3dad232e0a6c65451eef82a5f2`)
- Successfully downloaded and verified files
- All 66 unit tests passing
- Merkle roots verified to match TypeScript SDK 100%

✅ **Verified on 0G Mainnet**
- Successfully uploaded files with dynamic storage fee calculation
- Storage fee calculated from market contract (matches TypeScript SDK)
- Example TX: `0xeda94ed4698361d5fe61c17d21963e7d2333c15acb190e1b05128272b88882b6`
- Full feature parity with TypeScript SDK

✅ **Ready for PyPI Deployment**
- Standard Python packaging
- All dependencies available on PyPI
- Cross-platform compatible (Linux, macOS, Windows)
- Production-grade - used on mainnet

## ✨ Features

- 🔐 **Cryptographically Verified Merkle Trees** - Identical output to TypeScript SDK
- 📤 **File Upload** - Submit to blockchain and distribute across storage nodes
- 📥 **File Download** - Retrieve with automatic shard routing
- 🔗 **Smart Contract Integration** - Flow contract for on-chain submissions
- 🌐 **Sharded Storage** - Optimal node selection using segment tree algorithm
- 🔄 **Automatic Retry Logic** - Handles "too many data writing" errors
- ✅ **Production Tested** - Real transactions on 0G Storage testnet

## 📦 Installation

### From PyPI (Coming Soon)

```bash
pip install 0g-storage-sdk
```

### From Source

```bash
# Clone repository
git clone <repository-url>
cd 0g_py_storage

# Install dependencies
pip install -r requirements.txt
```

### Requirements

```
pycryptodome>=3.23.0  # Keccak256 hashing
web3>=7.14.0          # Blockchain RPC
eth-account>=0.13.7   # Account management
requests>=2.32.5      # HTTP client
```

## 🚀 Quick Start

### 1. Generate Merkle Tree

```python
from core.file import ZgFile

# From file path
file = ZgFile.from_file_path("./data.txt")
tree, err = file.merkle_tree()

if err is None:
    print(f"Root Hash: {tree.root_hash()}")
    print(f"File Size: {file.size()} bytes")
    print(f"Chunks: {file.num_chunks()}")
    print(f"Segments: {file.num_segments()}")

file.close()

# From bytes
data = b"Hello, 0G Storage!"
file = ZgFile.from_bytes(data)
tree, err = file.merkle_tree()
print(f"Root Hash: {tree.root_hash()}")
```

### 2. Upload File to 0G Storage

```python
from core.indexer import Indexer
from core.file import ZgFile
from eth_account import Account

# Configuration
INDEXER_RPC = "https://indexer-storage-testnet-turbo.0g.ai"
BLOCKCHAIN_RPC = "https://evmrpc-testnet.0g.ai"
PRIVATE_KEY = "0x..."  # Your private key

# Setup
indexer = Indexer(INDEXER_RPC)
account = Account.from_key(PRIVATE_KEY)
file = ZgFile.from_file_path("./data.txt")

# Upload options
upload_opts = {
    'tags': b'\x00',
    'finalityRequired': True,
    'taskSize': 10,
    'expectedReplica': 1,
    'skipTx': False,
    'account': account,
}

# Upload
result, err = indexer.upload(
    file,
    BLOCKCHAIN_RPC,
    account,
    upload_opts
)

if err is None:
    print(f"✅ Upload successful!")
    print(f"   Transaction Hash: {result['txHash']}")
    print(f"   Root Hash: {result['rootHash']}")
else:
    print(f"❌ Upload failed: {err}")

file.close()
```

**Example Output:**
```
✅ Upload successful!
   Transaction Hash: 0x9f01808921020c29b25e21204bfeb7079ce7cf3dad232e0a6c65451eef82a5f2
   Root Hash: 0x11fdd3fd0a6e9594bf4ffe86a5cf095d85ac00f23b4f2e559802d624f6a86b58
```

> **Note:** You may see a warning "⚠️ Some direct uploads failed, but file may still propagate via network" - this is normal and the upload succeeds through network propagation.

### 3. Download File from 0G Storage

```python
from core.indexer import Indexer

# Configuration
INDEXER_RPC = "https://indexer-storage-testnet-turbo.0g.ai"
root_hash = "0x11fdd3fd0a6e9594bf4ffe86a5cf095d85ac00f23b4f2e559802d624f6a86b58"

# Download
indexer = Indexer(INDEXER_RPC)
err = indexer.download(root_hash, "./output.txt", proof=False)

if err is None:
    print("✅ Download successful!")
else:
    print(f"❌ Download failed: {err}")
```

**Note:** Files need 3-5 minutes to propagate across storage shards before download.

## 🏗️ Architecture

```
0g_py_storage/
├── core/
│   ├── merkle.py          # Merkle tree (Keccak256, proof generation)
│   ├── file.py            # File operations & iteration
│   ├── uploader.py        # Upload orchestration with retry logic
│   ├── downloader.py      # Download with shard routing
│   ├── indexer.py         # Indexer RPC client
│   ├── storage_node.py    # Storage node RPC (14 methods)
│   └── node_selector.py   # Segment tree shard selection
├── contracts/
│   ├── abis.py           # Flow contract ABI
│   └── flow.py           # Flow contract wrapper
├── utils/
│   ├── crypto.py         # Keccak256 hashing
│   ├── http.py           # JSON-RPC HTTP client
│   ├── transfer.py       # Transfer utilities
│   └── ...               # Other utilities
├── config.py             # Default constants
└── requirements.txt      # Dependencies
```

## 🧪 Testing

```bash
# Run all tests (66 tests)
pytest tests/ -v

# Run specific test suite
pytest tests/test_merkle.py -v    # 26 tests
pytest tests/test_file.py -v      # 18 tests
pytest tests/test_node_selector.py -v  # 22 tests

# With coverage
pytest tests/ --cov=core --cov=utils
```

**Test Results:**
```
✅ 66/66 tests passing
✅ Merkle roots verified against TypeScript SDK
✅ Live network upload successful
✅ Live network download successful
```

## 🔍 Verification Against TypeScript SDK

The Python SDK produces **100% identical** merkle roots to the TypeScript SDK:

```bash
# Python verification
python3 verify_against_ts.py

# TypeScript verification
node verify_against_ts.cjs
```

**Verification Results:**
```
Testing with 5 different file sizes...
✓ File 1 (256 bytes):   Root hashes MATCH
✓ File 2 (1024 bytes):  Root hashes MATCH
✓ File 3 (4096 bytes):  Root hashes MATCH
✓ File 4 (16384 bytes): Root hashes MATCH
✓ File 5 (65536 bytes): Root hashes MATCH

✅ All merkle roots match perfectly!
```

## ⚙️ Configuration

Default constants (matching TypeScript SDK):

```python
DEFAULT_CHUNK_SIZE = 256          # 256 bytes per chunk
DEFAULT_SEGMENT_SIZE = 262144     # 256 KB per segment (1024 chunks)
DEFAULT_SEGMENT_MAX_CHUNKS = 1024 # Chunks per segment
```

## 📚 API Reference

### ZgFile

```python
# Create file instance
file = ZgFile.from_file_path(path: str) -> ZgFile
file = ZgFile.from_bytes(data: bytes) -> ZgFile

# Generate merkle tree
tree, err = file.merkle_tree() -> Tuple[MerkleTree, Optional[Exception]]

# File information
size = file.size() -> int
chunks = file.num_chunks() -> int
segments = file.num_segments() -> int

# Create blockchain submission
submission, err = file.create_submission(tags: bytes) -> Tuple[dict, Optional[Exception]]

# Cleanup
file.close()
```

### Indexer

```python
# Initialize
indexer = Indexer(url: str)

# Node discovery
nodes = indexer.get_sharded_nodes() -> dict
locations = indexer.get_file_locations(root_hash: str) -> list
clients, err = indexer.select_nodes(expected_replica: int) -> Tuple[list, Optional[Exception]]

# Upload file
result, err = indexer.upload(
    file: ZgFile,
    blockchain_rpc: str,
    signer: Account,
    upload_opts: dict,
    retry_opts: Optional[dict] = None
) -> Tuple[Optional[dict], Optional[Exception]]

# Download file
err = indexer.download(
    root_hash: str,
    file_path: str,
    proof: bool = False
) -> Optional[Exception]
```

### StorageNode

```python
# Initialize
node = StorageNode(url: str)

# Node operations
status = node.get_status() -> dict
config = node.get_shard_config() -> dict
info = node.get_file_info(root: str, need_available: bool = False) -> dict

# Upload operations
result = node.upload_segment(segment: dict) -> any
result = node.upload_segments(segments: list) -> any
result = node.upload_segments_by_tx_seq(segs: list, tx_seq: int) -> any

# Download operations
data = node.download_segment(root: str, start: int, end: int) -> str
data = node.download_segment_with_proof(root: str, index: int) -> dict
```

### MerkleTree

```python
# Add data
tree.add_leaf(data: bytes)

# Get root hash
root = tree.root_hash() -> str

# Generate proof
proof = tree.proof_at(index: int) -> Proof

# Validate proof
is_valid = proof.validate(root: str, data: bytes, index: int, proof_check: Proof) -> Tuple[bool, Optional[Exception]]
```

## 🌐 Network Configuration

### Testnet

```python
BLOCKCHAIN_RPC = "https://evmrpc-testnet.0g.ai"
INDEXER_RPC = "https://indexer-storage-testnet-turbo.0g.ai"
FLOW_CONTRACT = "0x22e03a6a89b950f1c82ec5e74f8eca321a105296"
CHAIN_ID = 16602
```

### Mainnet

```python
BLOCKCHAIN_RPC = "https://evmrpc.0g.ai"
INDEXER_RPC = "https://indexer-storage-turbo.0g.ai"
FLOW_CONTRACT = "0x62D4144dB0F0a6fBBaeb6296c785C71B3D57C526"
CHAIN_ID = 16661
```

**Status: ✅ Production Ready**
- Fully tested and working on mainnet
- Dynamic storage fee calculation from market contract
- All 66 unit tests passing

## 🔬 Development

### Implementation Phases

- ✅ **Phase 1:** Foundation (config, utils, exceptions)
- ✅ **Phase 2:** Models (transaction, node, file)
- ✅ **Phase 3:** Core Cryptography (merkle tree)
- ✅ **Phase 4:** Smart Contracts (flow contract)
- ✅ **Phase 5:** File Operations (file iteration)
- ✅ **Phase 6:** Network Layer (indexer, storage nodes)
- ✅ **Phase 7:** Upload (uploader with retry logic)
- ✅ **Phase 8:** Download (downloader with shard routing)

### Code Quality

- ✅ Line-by-line port from TypeScript SDK
- ✅ Maintains exact same behavior
- ✅ 66 comprehensive tests (100% passing)
- ✅ Type hints throughout
- ✅ Detailed documentation with TS SDK line references

### Example Code Structure

```python
def upload_task(self, file, tree, upload_task, retry_opts):
    """
    Upload a single task (batch of segments).

    TS SDK lines 315-381.  # ← References exact TypeScript lines

    Args:
        file: File object
        tree: Merkle tree
        upload_task: Task definition
        retry_opts: Retry options
    """
    # Implementation matches TS SDK exactly...
```

## 🤝 Contributing

This SDK is a direct port of the official TypeScript SDK. Contributions should:

1. **Match TypeScript SDK behavior** - Verify outputs match
2. **Include tests** - Add corresponding test cases
3. **Update documentation** - Keep README current
4. **Reference TS SDK** - Include line number references

## 📄 License

Same license as the official TypeScript SDK.

## 🔗 Links

- **TypeScript SDK:** https://github.com/0glabs/0g-ts-sdk
- **0G Storage Docs:** https://docs.0g.ai
- **0G Website:** https://0g.ai
- **Testnet Explorer:** https://chainscan-galileo.0g.ai/

## 🆘 Support

For Python SDK issues:
- Open an issue with Python version, error message, and minimal reproduction code
- Include comparison with TypeScript SDK behavior if applicable

For 0G Storage general questions:
- Check official documentation: https://docs.0g.ai
- Join 0G community channels

## 📊 Status

| Component | Status | Tests | TypeScript Parity |
|-----------|--------|-------|-------------------|
| Merkle Tree | ✅ Production | 26/26 | 100% |
| File Operations | ✅ Production | 18/18 | 100% |
| Node Selection | ✅ Production | 22/22 | 100% |
| Upload | ✅ Production | Verified | 100% |
| Download | ✅ Production | Verified | 100% |
| Contract Integration | ✅ Production | Verified | 100% |

**Last Verified:** Transaction `9f01808921020c29b25e21204bfeb7079ce7cf3dad232e0a6c65451eef82a5f2` on 0G Testnet

---

**Built with ❤️ for the 0G Storage ecosystem**
