trc-8004-sdk 0.1.0b1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,411 @@
1
+ Metadata-Version: 2.4
2
+ Name: trc-8004-sdk
3
+ Version: 0.1.0b1
4
+ Summary: TRC-8004 Agent SDK for TRON - Agent identity, reputation, and validation
5
+ Author-email: sun-protocol <dev@sun.io>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/sun-protocol/trc-8004-sdk
8
+ Project-URL: Documentation, https://github.com/sun-protocol/trc-8004-sdk#readme
9
+ Project-URL: Repository, https://github.com/sun-protocol/trc-8004-sdk
10
+ Project-URL: Issues, https://github.com/sun-protocol/trc-8004-sdk/issues
11
+ Keywords: tron,blockchain,agent,erc-8004,trc-8004,sdk
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
+ Requires-Python: >=3.11
20
+ Description-Content-Type: text/markdown
21
+ Requires-Dist: tronpy>=0.6.2
22
+ Requires-Dist: httpx>=0.27.0
23
+ Provides-Extra: dev
24
+ Requires-Dist: pytest>=8.2.0; extra == "dev"
25
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
26
+ Requires-Dist: ruff>=0.1.0; extra == "dev"
27
+ Requires-Dist: build>=1.0.0; extra == "dev"
28
+ Requires-Dist: twine>=5.0.0; extra == "dev"
29
+
30
+ # TRC-8004 Agent SDK
31
+
32
+ 去中心化 Agent 协作的 Python SDK,实现 ERC-8004 (Trustless Agent Protocol) 规范。
33
+
34
+ ## 特性
35
+
36
+ - 🔗 **多链支持**:抽象的 Adapter 架构,当前支持 TRON,可扩展 EVM 链
37
+ - 🔄 **自动重试**:可配置的指数退避重试策略
38
+ - 🛡️ **类型安全**:完整的类型注解和 Pydantic 校验
39
+ - 📝 **详细日志**:结构化日志便于调试
40
+ - ⚡ **异步支持**:同时提供同步和异步 API
41
+
42
+ ## 安装
43
+
44
+ ```bash
45
+ # 使用 uv
46
+ uv add trc-8004-sdk
47
+
48
+ # 使用 pip
49
+ pip install trc-8004-sdk
50
+ ```
51
+
52
+ ## CLI 工具
53
+
54
+ 安装后可使用 `trc8004` 命令行工具:
55
+
56
+ ```bash
57
+ # 创建新 Agent 项目
58
+ trc8004 init MyAgent
59
+ trc8004 init MyAgent --port 8200 --tags "swap,defi"
60
+
61
+ # 测试 Agent 连通性
62
+ trc8004 test --url http://localhost:8100
63
+
64
+ # 注册 Agent 到链上
65
+ trc8004 register --token-uri https://example.com/agent.json --name MyAgent
66
+ ```
67
+
68
+ ### 创建 Agent 项目示例
69
+
70
+ ```bash
71
+ $ trc8004 init MySwapAgent --port 8200 --tags "swap,defi"
72
+
73
+ ✅ Agent 项目创建成功!
74
+
75
+ 📁 myswapagent/
76
+ ├── app.py # Agent 主程序
77
+ ├── pyproject.toml # 项目配置
78
+ ├── .env.example # 环境变量模板
79
+ ├── README.md # 文档
80
+ └── tests/ # 测试
81
+
82
+ 🚀 下一步:
83
+ cd myswapagent
84
+ uv sync
85
+ python app.py
86
+ ```
87
+
88
+ ## 快速开始
89
+
90
+ ```python
91
+ from sdk import AgentSDK
92
+
93
+ # 初始化 SDK
94
+ sdk = AgentSDK(
95
+ private_key="your_hex_private_key",
96
+ rpc_url="https://nile.trongrid.io",
97
+ network="tron:nile",
98
+ identity_registry="TIdentityRegistryAddress",
99
+ validation_registry="TValidationRegistryAddress",
100
+ reputation_registry="TReputationRegistryAddress",
101
+ )
102
+
103
+ # 注册 Agent
104
+ tx_id = sdk.register_agent(
105
+ token_uri="https://example.com/agent.json",
106
+ metadata=[{"key": "name", "value": "MyAgent"}],
107
+ )
108
+ print(f"Agent registered: {tx_id}")
109
+
110
+ # 构建订单承诺
111
+ commitment = sdk.build_commitment({
112
+ "asset": "TRX/USDT",
113
+ "amount": 100.0,
114
+ "slippage": 0.01,
115
+ })
116
+ ```
117
+
118
+ ## 核心功能
119
+
120
+ ### 1. 身份注册 (IdentityRegistry)
121
+
122
+ ```python
123
+ # 方式 1: 使用 token_uri 注册
124
+ tx_id = sdk.register_agent(
125
+ token_uri="https://example.com/agent.json",
126
+ metadata=[
127
+ {"key": "name", "value": "MyAgent"},
128
+ {"key": "version", "value": "1.0.0"},
129
+ ],
130
+ )
131
+
132
+ # 方式 2: 从 agent-card.json 自动提取 metadata
133
+ import json
134
+ with open(".well-known/agent-card.json") as f:
135
+ card = json.load(f)
136
+
137
+ metadata = AgentSDK.extract_metadata_from_card(card)
138
+ # metadata 包含: name, description, version, url, skills, tags, endpoints
139
+ tx_id = sdk.register_agent(metadata=metadata)
140
+
141
+ # 更新元数据
142
+ tx_id = sdk.update_metadata(
143
+ agent_id=1,
144
+ key="description",
145
+ value="Updated description",
146
+ )
147
+ ```
148
+
149
+ ### 2. 验证请求 (ValidationRegistry)
150
+
151
+ ```python
152
+ # 发起验证请求
153
+ tx_id = sdk.validation_request(
154
+ validator_addr="TValidatorAddress",
155
+ agent_id=1,
156
+ request_uri="ipfs://QmXxx...",
157
+ request_hash="0x" + "aa" * 32,
158
+ )
159
+
160
+ # 提交验证响应(验证者调用)
161
+ tx_id = sdk.validation_response(
162
+ request_hash="0x" + "aa" * 32,
163
+ response=95, # 0-100 评分
164
+ response_uri="ipfs://QmYyy...",
165
+ )
166
+ ```
167
+
168
+ ### 3. 信誉反馈 (ReputationRegistry)
169
+
170
+ ```python
171
+ # 提交信誉反馈
172
+ tx_id = sdk.submit_reputation(
173
+ agent_id=1,
174
+ score=95,
175
+ tag1="0x" + "11" * 32, # 可选标签
176
+ feedback_auth="0x...", # Agent 提供的授权签名
177
+ )
178
+ ```
179
+
180
+ ### 4. 签名构建
181
+
182
+ ```python
183
+ # 构建 A2A 请求签名
184
+ signature = sdk.build_a2a_signature(
185
+ action_commitment="0x...",
186
+ timestamp=int(time.time()),
187
+ caller_address="TCallerAddress",
188
+ )
189
+
190
+ # 构建反馈授权
191
+ feedback_auth = sdk.build_feedback_auth(
192
+ agent_id=1,
193
+ client_addr="TClientAddress",
194
+ index_limit=10,
195
+ expiry=int(time.time()) + 3600,
196
+ chain_id=None, # 自动解析
197
+ identity_registry="TIdentityRegistry",
198
+ )
199
+ ```
200
+
201
+ ### 5. 请求构建辅助
202
+
203
+ ```python
204
+ # 市价单报价请求
205
+ quote_req = sdk.build_market_order_quote_request(
206
+ asset="TRX/USDT",
207
+ amount=100.0,
208
+ slippage=0.01,
209
+ )
210
+
211
+ # X402 执行请求
212
+ execute_req = sdk.build_x402_execute_request(
213
+ action_commitment="0x...",
214
+ order_params={"asset": "TRX/USDT", "amount": 100.0},
215
+ payment_tx_hash="0x...",
216
+ timestamp=int(time.time()),
217
+ caller_address="TCallerAddress",
218
+ )
219
+ ```
220
+
221
+ ## 重试配置
222
+
223
+ SDK 提供可配置的重试策略:
224
+
225
+ ```python
226
+ from sdk import AgentSDK, RetryConfig, AGGRESSIVE_RETRY_CONFIG
227
+
228
+ # 使用预定义配置
229
+ sdk = AgentSDK(
230
+ private_key="...",
231
+ retry_config=AGGRESSIVE_RETRY_CONFIG, # 5 次重试
232
+ )
233
+
234
+ # 自定义配置
235
+ custom_config = RetryConfig(
236
+ max_attempts=3,
237
+ base_delay=1.0,
238
+ max_delay=30.0,
239
+ exponential_base=2.0,
240
+ jitter=True,
241
+ )
242
+ sdk = AgentSDK(private_key="...", retry_config=custom_config)
243
+ ```
244
+
245
+ 预定义配置:
246
+ - `DEFAULT_RETRY_CONFIG`: 3 次重试,1s 基础延迟
247
+ - `AGGRESSIVE_RETRY_CONFIG`: 5 次重试,0.5s 基础延迟
248
+ - `CONSERVATIVE_RETRY_CONFIG`: 2 次重试,2s 基础延迟
249
+ - `NO_RETRY_CONFIG`: 不重试
250
+
251
+ ## 异常处理
252
+
253
+ SDK 提供细粒度的异常类型:
254
+
255
+ ```python
256
+ from sdk import (
257
+ SDKError,
258
+ ContractCallError,
259
+ TransactionFailedError,
260
+ RetryExhaustedError,
261
+ InsufficientEnergyError,
262
+ )
263
+
264
+ try:
265
+ tx_id = sdk.register_agent(token_uri="...")
266
+ except InsufficientEnergyError:
267
+ print("账户能量不足,请充值")
268
+ except RetryExhaustedError as e:
269
+ print(f"重试耗尽: {e.last_error}")
270
+ except ContractCallError as e:
271
+ print(f"合约调用失败: {e.code} - {e.details}")
272
+ except SDKError as e:
273
+ print(f"SDK 错误: {e}")
274
+ ```
275
+
276
+ 异常层级:
277
+ ```
278
+ SDKError
279
+ ├── ConfigurationError
280
+ │ ├── MissingContractAddressError
281
+ │ ├── InvalidPrivateKeyError
282
+ │ └── ChainIdResolutionError
283
+ ├── NetworkError
284
+ │ ├── RPCError
285
+ │ ├── TimeoutError
286
+ │ └── RetryExhaustedError
287
+ ├── ContractError
288
+ │ ├── ContractCallError
289
+ │ ├── ContractFunctionNotFoundError
290
+ │ ├── TransactionFailedError
291
+ │ └── InsufficientEnergyError
292
+ ├── SignatureError
293
+ │ ├── InvalidSignatureError
294
+ │ └── SignerNotAvailableError
295
+ ├── DataError
296
+ │ ├── InvalidAddressError
297
+ │ ├── InvalidHashError
298
+ │ ├── SerializationError
299
+ │ └── DataLoadError
300
+ └── ValidationError
301
+ ├── RequestHashMismatchError
302
+ ├── FeedbackAuthExpiredError
303
+ └── FeedbackAuthInvalidError
304
+ ```
305
+
306
+ ## HTTP 客户端
307
+
308
+ ### AgentClient
309
+
310
+ 智能 HTTP 客户端,自动解析 Agent 元数据中的端点:
311
+
312
+ ```python
313
+ from sdk import AgentClient
314
+
315
+ client = AgentClient(
316
+ metadata=agent_metadata, # 从 Central Service 获取
317
+ base_url="https://agent.example.com",
318
+ )
319
+
320
+ # 自动解析端点并发送请求
321
+ response = client.post("quote", {"asset": "TRX/USDT", "amount": 100})
322
+ ```
323
+
324
+ ### AgentProtocolClient
325
+
326
+ Agent Protocol 标准客户端:
327
+
328
+ ```python
329
+ from sdk import AgentProtocolClient
330
+
331
+ client = AgentProtocolClient(base_url="https://agent.example.com")
332
+
333
+ # 创建任务并执行
334
+ result = client.run({
335
+ "skill": "market_order",
336
+ "params": {"asset": "TRX/USDT", "amount": 100},
337
+ })
338
+ ```
339
+
340
+ ## 链工具
341
+
342
+ ```python
343
+ from sdk import load_request_data, fetch_event_logs
344
+
345
+ # 加载请求数据(支持 file://, ipfs://, http://)
346
+ data = load_request_data("ipfs://QmXxx...")
347
+
348
+ # 获取链上事件
349
+ events = fetch_event_logs(
350
+ client=tron_client,
351
+ contract_address="TValidationRegistry",
352
+ event_name="ValidationRequest",
353
+ from_block=1000000,
354
+ to_block=1001000,
355
+ )
356
+ ```
357
+
358
+ ## 扩展多链支持
359
+
360
+ SDK 使用 Adapter 模式,可轻松扩展其他链:
361
+
362
+ ```python
363
+ from sdk import ContractAdapter, Signer
364
+
365
+ class EVMContractAdapter(ContractAdapter):
366
+ def __init__(self, rpc_url: str, ...):
367
+ from web3 import Web3
368
+ self.w3 = Web3(Web3.HTTPProvider(rpc_url))
369
+
370
+ def send(self, contract: str, method: str, params: list, signer: Signer) -> str:
371
+ # EVM 交易逻辑
372
+ ...
373
+
374
+ class EVMSigner(Signer):
375
+ def __init__(self, private_key: str):
376
+ from eth_account import Account
377
+ self.account = Account.from_key(private_key)
378
+
379
+ def sign_message(self, payload: bytes) -> str:
380
+ # EIP-191 签名
381
+ ...
382
+ ```
383
+
384
+ ## 环境变量
385
+
386
+ | 变量 | 说明 | 默认值 |
387
+ |------|------|--------|
388
+ | `TRON_RPC_URL` | TRON RPC 节点 | `https://nile.trongrid.io` |
389
+ | `TRON_NETWORK` | 网络标识 | `tron:nile` |
390
+ | `IDENTITY_REGISTRY` | IdentityRegistry 地址 | - |
391
+ | `VALIDATION_REGISTRY` | ValidationRegistry 地址 | - |
392
+ | `REPUTATION_REGISTRY` | ReputationRegistry 地址 | - |
393
+ | `TRON_FEE_LIMIT` | 交易费用上限 (sun) | `10000000` |
394
+ | `IPFS_GATEWAY_URL` | IPFS 网关 | `https://ipfs.io/ipfs` |
395
+
396
+ ## 开发
397
+
398
+ ```bash
399
+ # 安装依赖
400
+ uv sync
401
+
402
+ # 运行测试
403
+ uv run pytest
404
+
405
+ # 类型检查
406
+ uv run mypy src/sdk
407
+ ```
408
+
409
+ ## License
410
+
411
+ MIT
@@ -0,0 +1,16 @@
1
+ sdk/__init__.py,sha256=cI1-ZogyyEvvfiyDzUfnFAKrtvtlLzb9ioAk6ZyWcvI,3060
2
+ sdk/agent_protocol_client.py,sha256=fcA6yTPf2rE4jRgEssCQ0h3Lcsahwg6TAWtGYjxynSI,5391
3
+ sdk/agent_sdk.py,sha256=778DfiEV2oY3Jfvq14_bK7IWL_fiDs1dkL_pDHjhU4Y,50921
4
+ sdk/chain_utils.py,sha256=FF9illvYtYX2OkZVcbYPhVnRgWvVBt_N4rQNqUtLC5U,7933
5
+ sdk/cli.py,sha256=2ZbTBQ072-fz1DCatQFSCMCMq8vKeHXoryxoE4yZc2w,15680
6
+ sdk/client.py,sha256=0sAPrU2q3MYg1KPmBjGj_hJSBrJNVtqBRWHl1Bj0Pfg,6259
7
+ sdk/contract_adapter.py,sha256=gVu0WJRWLeTrQsfpmuNDmMOwX7GU-rJ46TGa0-JT6VE,17610
8
+ sdk/exceptions.py,sha256=oscTUo_ChlBV3kWoIMkJv_yGxA9mLHMvpowdKjiaGo0,16626
9
+ sdk/retry.py,sha256=LWRQUe2b7pDjBP7Dbv7DDkK_Q13T9y0o3NqE21rJpB8,14558
10
+ sdk/signer.py,sha256=dvk0vFgZEdv2UKuJ1YditW2g_BJZri2fP5gaPLH1NKA,7874
11
+ sdk/utils.py,sha256=0GPSG4YMpXNiTv63GW6RCQhYet226iNrjtIAo9KIrvo,4244
12
+ trc_8004_sdk-0.1.0b1.dist-info/METADATA,sha256=6iEiYyaeyDuSZOvEOhT2hgvBytc9pNkHIQK1UxpUfo8,10065
13
+ trc_8004_sdk-0.1.0b1.dist-info/WHEEL,sha256=qELbo2s1Yzl39ZmrAibXA2jjPLUYfnVhUNTlyF1rq0Y,92
14
+ trc_8004_sdk-0.1.0b1.dist-info/entry_points.txt,sha256=n1azNW1KdJu6J5Z29LUXyC2PZGoH7tgaLgQzY_iE03w,41
15
+ trc_8004_sdk-0.1.0b1.dist-info/top_level.txt,sha256=JuXS55gRP3EzGKmJgbLfVfUolO3cM_u69k3qvutnBuw,4
16
+ trc_8004_sdk-0.1.0b1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.10.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ trc8004 = sdk.cli:main
@@ -0,0 +1 @@
1
+ sdk