Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Thanks for your interest in contributing to RustChain! We pay bounties in RTC to

## What Gets Rejected

- AI-generated bulk PRs with no testing evidence
- Bulk PRs with no testing evidence
- PRs that include all code from prior PRs (we track this)
- "Fixes" that break existing functionality
- Submissions that don't match the bounty requirements
Expand Down
54 changes: 54 additions & 0 deletions docs/sdk-design/2026-02-15-rustchain-python-sdk-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# RustChain Python SDK 设计文档 (2026-02-15)

## 1. 概述
本项目旨在为 RustChain 区块链开发一个功能完善、易于使用的 Python SDK。该 SDK 将作为开发者与 RustChain 节点交互的主要工具,支持查询链状态、管理账户以及执行加密签名交易。

## 2. 设计原则
- **开发者友好**:提供直观的 API、完整的类型提示和详尽的错误信息。
- **现代化**:基于 Python 3.8+,充分利用类型注解和异步特性。
- **高性能**:采用独立双客户端模式,确保同步和异步场景下的最优表现。
- **类型安全**:使用 Pydantic 进行数据验证和模型化。

## 3. 核心架构

### 3.1 独立双客户端模式
SDK 提供两个主要的客户端类,共享底层的逻辑处理逻辑,但使用不同的网络传输层。
- `RustChainClient`:基于 `httpx` 的同步客户端。
- `AsyncRustChainClient`:基于 `httpx` 的异步客户端。

### 3.2 身份与签名机制 (Identity & Crypto)
将身份认证逻辑与网络通信解耦:
- `Identity` 类:负责持有 Ed25519 私钥/种子,执行签名操作。
- SDK 内部自动处理 Nonce 管理,确保交易的顺序性和防重放保护。

### 3.3 目录结构
```text
rustchain/
├── __init__.py # 导出常用类
├── client.py # 同步客户端
├── async_client.py # 异步客户端
├── identity.py # 身份与签名逻辑
├── models.py # Pydantic 数据模型
├── exceptions.py # 异常定义
└── utils.py # 通用工具
```

## 4. 关键依赖
- `httpx`:统一的同步/异步 HTTP 请求。
- `pydantic`:数据模型定义与校验。
- `pynacl`:Ed25519 加密签名。

## 5. API 范围
- **Health**: `/health` (节点状态)
- **Chain**: `/api/stats`, `/epoch` (链统计)
- **Miners**: `/api/miners` (矿工列表)
- **Wallet**:
- `/wallet/balance` (余额查询)
- `/wallet/transfer/signed` (签名转账)
- **Attestation**: `/attest/submit` (硬件认证)

## 6. 错误处理
定义统一的异常体系,方便用户捕获网络错误、业务逻辑错误(如余额不足)或认证错误(签名无效)。

---
---
204 changes: 204 additions & 0 deletions docs/sdk-design/2026-02-15-rustchain-python-sdk-plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
# RustChain Python SDK 实施计划

**Goal:** 构建一个支持同步和异步双接口的 RustChain Python SDK,具备加密签名交易和硬件认证功能。

**Architecture:** 采用独立双客户端模式(`RustChainClient` 和 `AsyncRustChainClient`)。身份认证(Ed25519 签名)与网络请求解耦,使用 Pydantic 进行数据模型化。

**Tech Stack:** Python 3.8+, `httpx`, `pydantic`, `PyNaCl`.

---

### Task 1: 项目初始化与依赖配置

**Files:**
- Create: `pyproject.toml`
- Create: `src/rustchain/__init__.py`

**Step 1: 创建 pyproject.toml 并配置依赖**

```toml
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "rustchain-sdk"
version = "0.1.0"
description = "Python SDK for RustChain Blockchain"
requires-python = ">=3.8"
dependencies = [
"httpx>=0.24.0",
"pydantic>=2.0.0",
"pynacl>=1.5.0",
]

[project.optional-dependencies]
test = ["pytest", "pytest-asyncio", "respx"]

[tool.hatch.build.targets.wheel]
packages = ["src/rustchain"]
```

**Step 2: 安装依赖**

运行: `pip install -e ".[test]"`
预期: 成功安装 `httpx`, `pydantic`, `pynacl` 等。

**Step 3: 提交**

```bash
git add pyproject.toml src/rustchain/__init__.py
git commit -m "chore: initialize project and dependencies"
```

---

### Task 2: 定义数据模型 (Models)

**Files:**
- Create: `src/rustchain/models.py`
- Create: `tests/test_models.py`

**Step 1: 编写数据模型测试**

```python
from rustchain.models import Miner
def test_miner_model():
data = {"miner": "abc", "hardware_type": "x86", "antiquity_multiplier": 0.8, "last_attest": 123}
miner = Miner(**data)
assert miner.miner == "abc"
```

**Step 2: 运行测试并验证失败**

运行: `pytest tests/test_models.py`
预期: FAIL (ModuleNotFoundError)

**Step 3: 实现 Pydantic 模型**

```python
from pydantic import BaseModel
from typing import List, Optional

class Miner(BaseModel):
miner: str
hardware_type: str
antiquity_multiplier: float
last_attest: int

class Stats(BaseModel):
epoch: int
total_miners: int
total_balance: float
```

**Step 4: 运行测试并验证通过**

运行: `pytest tests/test_models.py`
预期: PASS

**Step 5: 提交**

```bash
git add src/rustchain/models.py tests/test_models.py
git commit -m "feat: add data models using pydantic"
```

---

### Task 3: 实现身份认证与签名逻辑 (Identity)

**Files:**
- Create: `src/rustchain/identity.py`
- Create: `tests/test_identity.py`

**Step 1: 编写签名逻辑测试**

```python
from rustchain.identity import Identity
def test_identity_signing():
id = Identity.from_seed("test" * 8)
msg = b"hello"
sig = id.sign(msg)
assert len(sig) == 64
```

**Step 2: 运行测试并验证失败**

运行: `pytest tests/test_identity.py`
预期: FAIL

**Step 3: 使用 PyNaCl 实现签名逻辑**

```python
import nacl.signing
import nacl.encoding

class Identity:
def __init__(self, signing_key: nacl.signing.SigningKey):
self._key = signing_key
self.address = self._key.verify_key.encode(nacl.encoding.HexEncoder).decode()

@classmethod
def from_seed(cls, seed_hex: str):
return cls(nacl.signing.SigningKey(seed_hex, encoder=nacl.encoding.HexEncoder))

def sign(self, message: bytes) -> str:
return self._key.sign(message).signature.hex()
```

**Step 4: 运行测试并验证通过**

运行: `pytest tests/test_identity.py`
预期: PASS

**Step 5: 提交**

```bash
git commit -m "feat: implement ed25519 identity and signing"
```

---

### Task 4: 实现同步客户端 (RustChainClient)

**Files:**
- Create: `src/rustchain/client.py`
- Create: `tests/test_client_sync.py`

**Step 1: 编写同步请求测试 (使用 respx 模拟)**

**Step 2: 实现基于 httpx.Client 的 RustChainClient**

**Step 3: 运行测试并验证通过**

**Step 4: 提交**

---

### Task 5: 实现异步客户端 (AsyncRustChainClient)

**Files:**
- Create: `src/rustchain/async_client.py`
- Create: `tests/test_client_async.py`

**Step 1: 编写异步请求测试**

**Step 2: 实现基于 httpx.AsyncClient 的客户端**

**Step 3: 运行测试并验证通过**

**Step 4: 提交**

---

### Task 6: 集成转账与 Nonce 管理

**Files:**
- Modify: `src/rustchain/client.py`
- Modify: `src/rustchain/async_client.py`

**Step 1: 在客户端中添加自动 Nonce 获取逻辑**
**Step 2: 实现 signed_transfer 方法**
**Step 3: 编写集成测试**
**Step 4: 提交**
2 changes: 1 addition & 1 deletion node/rustchain_v2_integrated_v2.2.1_rip200.py
Original file line number Diff line number Diff line change
Expand Up @@ -3831,7 +3831,7 @@ def api_wallet_balances_all():


# ============================================================================
# P2P SYNC INTEGRATION (AI-Generated, Security Score: 90/100)
# P2P SYNC INTEGRATION (Security Score: 90/100)
# ============================================================================

try:
Expand Down
20 changes: 20 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "rustchain-sdk"
version = "0.1.0"
description = "Python SDK for RustChain Blockchain"
requires-python = ">=3.8"
dependencies = [
"httpx>=0.24.0",
"pydantic>=2.0.0",
"pynacl>=1.5.0",
]

[project.optional-dependencies]
test = ["pytest", "pytest-asyncio", "respx"]

[tool.hatch.build.targets.wheel]
packages = ["rips/python/rustchain"]
10 changes: 10 additions & 0 deletions rips/python/rustchain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@
RustChainNode,
)

from .identity import Identity
from .models import Miner, Stats
from .client import RustChainClient
from .async_client import AsyncRustChainClient

__all__ = [
"Identity",
"Miner",
"Stats",
"RustChainClient",
"AsyncRustChainClient",
# Core Types
"HardwareTier",
"HardwareInfo",
Expand Down
Loading