traia-iatp 0.1.2__py3-none-any.whl → 0.1.67__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.
- traia_iatp/__init__.py +105 -8
- traia_iatp/cli/main.py +85 -1
- traia_iatp/client/__init__.py +28 -3
- traia_iatp/client/crewai_a2a_tools.py +32 -12
- traia_iatp/client/d402_a2a_client.py +348 -0
- traia_iatp/contracts/__init__.py +11 -0
- traia_iatp/contracts/data/abis/contract-abis-localhost.json +4091 -0
- traia_iatp/contracts/data/abis/contract-abis-sepolia.json +4890 -0
- traia_iatp/contracts/data/addresses/contract-addresses.json +17 -0
- traia_iatp/contracts/data/addresses/contract-proxies.json +12 -0
- traia_iatp/contracts/iatp_contracts_config.py +263 -0
- traia_iatp/contracts/wallet_creator.py +369 -0
- traia_iatp/core/models.py +17 -3
- traia_iatp/d402/MIDDLEWARE_ARCHITECTURE.md +205 -0
- traia_iatp/d402/PRICE_BUILDER_USAGE.md +249 -0
- traia_iatp/d402/README.md +489 -0
- traia_iatp/d402/__init__.py +54 -0
- traia_iatp/d402/asgi_wrapper.py +469 -0
- traia_iatp/d402/chains.py +102 -0
- traia_iatp/d402/client.py +150 -0
- traia_iatp/d402/clients/__init__.py +7 -0
- traia_iatp/d402/clients/base.py +218 -0
- traia_iatp/d402/clients/httpx.py +266 -0
- traia_iatp/d402/common.py +114 -0
- traia_iatp/d402/encoding.py +28 -0
- traia_iatp/d402/examples/client_example.py +197 -0
- traia_iatp/d402/examples/server_example.py +171 -0
- traia_iatp/d402/facilitator.py +481 -0
- traia_iatp/d402/mcp_middleware.py +296 -0
- traia_iatp/d402/models.py +116 -0
- traia_iatp/d402/networks.py +98 -0
- traia_iatp/d402/path.py +43 -0
- traia_iatp/d402/payment_introspection.py +126 -0
- traia_iatp/d402/payment_signing.py +183 -0
- traia_iatp/d402/price_builder.py +164 -0
- traia_iatp/d402/servers/__init__.py +61 -0
- traia_iatp/d402/servers/base.py +139 -0
- traia_iatp/d402/servers/example_general_server.py +140 -0
- traia_iatp/d402/servers/fastapi.py +253 -0
- traia_iatp/d402/servers/mcp.py +304 -0
- traia_iatp/d402/servers/starlette.py +878 -0
- traia_iatp/d402/starlette_middleware.py +529 -0
- traia_iatp/d402/types.py +300 -0
- traia_iatp/mcp/D402_MCP_ADAPTER_FLOW.md +357 -0
- traia_iatp/mcp/__init__.py +3 -0
- traia_iatp/mcp/d402_mcp_tool_adapter.py +526 -0
- traia_iatp/mcp/mcp_agent_template.py +78 -13
- traia_iatp/mcp/templates/Dockerfile.j2 +27 -4
- traia_iatp/mcp/templates/README.md.j2 +104 -8
- traia_iatp/mcp/templates/cursor-rules.md.j2 +194 -0
- traia_iatp/mcp/templates/deployment_params.json.j2 +1 -2
- traia_iatp/mcp/templates/docker-compose.yml.j2 +13 -3
- traia_iatp/mcp/templates/env.example.j2 +60 -0
- traia_iatp/mcp/templates/mcp_health_check.py.j2 +2 -2
- traia_iatp/mcp/templates/pyproject.toml.j2 +11 -5
- traia_iatp/mcp/templates/pyrightconfig.json.j2 +22 -0
- traia_iatp/mcp/templates/run_local_docker.sh.j2 +320 -10
- traia_iatp/mcp/templates/server.py.j2 +174 -197
- traia_iatp/mcp/traia_mcp_adapter.py +182 -20
- traia_iatp/registry/__init__.py +47 -12
- traia_iatp/registry/atlas_search_indexes.json +108 -54
- traia_iatp/registry/iatp_search_api.py +169 -39
- traia_iatp/registry/mongodb_registry.py +241 -69
- traia_iatp/registry/readmes/EMBEDDINGS_SETUP.md +1 -1
- traia_iatp/registry/readmes/IATP_SEARCH_API_GUIDE.md +8 -8
- traia_iatp/registry/readmes/MONGODB_X509_AUTH.md +1 -1
- traia_iatp/registry/readmes/README.md +3 -3
- traia_iatp/registry/readmes/REFACTORING_SUMMARY.md +6 -6
- traia_iatp/scripts/__init__.py +2 -0
- traia_iatp/scripts/create_wallet.py +244 -0
- traia_iatp/server/a2a_server.py +22 -7
- traia_iatp/server/iatp_server_template_generator.py +23 -0
- traia_iatp/server/templates/.dockerignore.j2 +48 -0
- traia_iatp/server/templates/Dockerfile.j2 +23 -1
- traia_iatp/server/templates/README.md +2 -2
- traia_iatp/server/templates/README.md.j2 +5 -5
- traia_iatp/server/templates/__main__.py.j2 +374 -66
- traia_iatp/server/templates/agent.py.j2 +12 -11
- traia_iatp/server/templates/agent_config.json.j2 +3 -3
- traia_iatp/server/templates/agent_executor.py.j2 +45 -27
- traia_iatp/server/templates/env.example.j2 +32 -4
- traia_iatp/server/templates/gitignore.j2 +7 -0
- traia_iatp/server/templates/pyproject.toml.j2 +13 -12
- traia_iatp/server/templates/run_local_docker.sh.j2 +143 -11
- traia_iatp/server/templates/server.py.j2 +197 -10
- traia_iatp/special_agencies/registry_search_agency.py +1 -1
- traia_iatp/utils/iatp_utils.py +6 -6
- traia_iatp-0.1.67.dist-info/METADATA +320 -0
- traia_iatp-0.1.67.dist-info/RECORD +117 -0
- traia_iatp-0.1.2.dist-info/METADATA +0 -414
- traia_iatp-0.1.2.dist-info/RECORD +0 -72
- {traia_iatp-0.1.2.dist-info → traia_iatp-0.1.67.dist-info}/WHEEL +0 -0
- {traia_iatp-0.1.2.dist-info → traia_iatp-0.1.67.dist-info}/entry_points.txt +0 -0
- {traia_iatp-0.1.2.dist-info → traia_iatp-0.1.67.dist-info}/licenses/LICENSE +0 -0
- {traia_iatp-0.1.2.dist-info → traia_iatp-0.1.67.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
# X402 Payment Integration for IATP
|
|
2
|
+
|
|
3
|
+
This module integrates the x402 (HTTP 402 Payment Required) protocol into the Inter-Agent Transfer Protocol (IATP), enabling utility agents to monetize their services and client agents to pay for API access using on-chain stablecoin payments.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The x402 integration connects three key components:
|
|
8
|
+
|
|
9
|
+
1. **Utility Agents (Servers)**: Accept x402 payments for API access
|
|
10
|
+
2. **Client Agents**: Pay for services using x402 protocol
|
|
11
|
+
3. **IATP Settlement Layer**: On-chain smart contract for payment settlement
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
┌──────────────────┐ X-PAYMENT header ┌──────────────────┐
|
|
15
|
+
│ Client Agent │ ──────────────────────────▶│ Utility Agent │
|
|
16
|
+
│ (Payer) │ │ (Payee) │
|
|
17
|
+
└──────────────────┘ └──────────────────┘
|
|
18
|
+
│ │
|
|
19
|
+
│ 1. EIP-3009 │ 2. Provider
|
|
20
|
+
│ Authorization │ Attestation
|
|
21
|
+
│ │
|
|
22
|
+
▼ ▼
|
|
23
|
+
┌──────────────────────────────────────────────────────────────────┐
|
|
24
|
+
│ IATP Settlement Facilitator (Relayer) │
|
|
25
|
+
│ - Verifies consumer signature │
|
|
26
|
+
│ - Verifies provider attestation │
|
|
27
|
+
│ - Submits to on-chain settlement │
|
|
28
|
+
└──────────────────────────────────────────────────────────────────┘
|
|
29
|
+
│
|
|
30
|
+
▼
|
|
31
|
+
┌──────────────────────────────────────────────────────────────────┐
|
|
32
|
+
│ IATPSettlementLayer.sol (Smart Contract) │
|
|
33
|
+
│ - Validates signatures │
|
|
34
|
+
│ - Processes payment │
|
|
35
|
+
│ - Credits provider's epoch balance │
|
|
36
|
+
│ - Handles disputes │
|
|
37
|
+
└──────────────────────────────────────────────────────────────────┘
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Key Concepts
|
|
41
|
+
|
|
42
|
+
### X402 Protocol
|
|
43
|
+
|
|
44
|
+
X402 is a modern HTTP payment protocol that:
|
|
45
|
+
- Uses HTTP 402 status code (Payment Required)
|
|
46
|
+
- Supports instant stablecoin payments (USDC/TRAIA)
|
|
47
|
+
- Works with EIP-3009 payment authorizations
|
|
48
|
+
- Enables programmatic payments by AI agents
|
|
49
|
+
|
|
50
|
+
### IATP Settlement Layer
|
|
51
|
+
|
|
52
|
+
The on-chain component that:
|
|
53
|
+
- Manages utility agent contracts (TraiaUtilityAgent.sol)
|
|
54
|
+
- Processes settlements in epochs
|
|
55
|
+
- Handles dispute resolution
|
|
56
|
+
- Maintains reputation scores
|
|
57
|
+
|
|
58
|
+
### Facilitator vs Relayer
|
|
59
|
+
|
|
60
|
+
- **Facilitator** (x402 term): Service that verifies and settles payments
|
|
61
|
+
- **Relayer** (IATP term): Service that submits transactions to blockchain
|
|
62
|
+
- In IATP: The facilitator IS the relayer - it handles both verification and on-chain submission
|
|
63
|
+
|
|
64
|
+
## Components
|
|
65
|
+
|
|
66
|
+
### Server-Side (Utility Agents)
|
|
67
|
+
|
|
68
|
+
#### 1. X402 Middleware
|
|
69
|
+
|
|
70
|
+
Protects API endpoints with payment requirements:
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from traia_iatp.x402 import X402Config, X402ServicePrice, require_iatp_payment
|
|
74
|
+
from fastapi import FastAPI, Request
|
|
75
|
+
|
|
76
|
+
app = FastAPI()
|
|
77
|
+
|
|
78
|
+
# Configure x402 payments
|
|
79
|
+
x402_config = X402Config(
|
|
80
|
+
enabled=True,
|
|
81
|
+
pay_to_address="0x1234...", # Utility agent contract address
|
|
82
|
+
default_price=X402ServicePrice(
|
|
83
|
+
usd_amount="0.01", # $0.01 per request
|
|
84
|
+
network="base-mainnet",
|
|
85
|
+
asset_address="0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", # USDC on Base
|
|
86
|
+
max_timeout_seconds=300
|
|
87
|
+
),
|
|
88
|
+
facilitator_url="https://api.traia.io/x402/facilitator",
|
|
89
|
+
service_description="AI-powered financial sentiment analysis"
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
# Add middleware
|
|
93
|
+
@app.middleware("http")
|
|
94
|
+
async def payment_middleware(request: Request, call_next):
|
|
95
|
+
middleware = require_iatp_payment(x402_config)
|
|
96
|
+
return await middleware(request, call_next)
|
|
97
|
+
|
|
98
|
+
@app.post("/analyze")
|
|
99
|
+
async def analyze(request: Request):
|
|
100
|
+
# This endpoint now requires payment
|
|
101
|
+
data = await request.json()
|
|
102
|
+
return {"sentiment": "positive", "confidence": 0.95}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### 2. Custom Facilitator
|
|
106
|
+
|
|
107
|
+
The IATP Settlement Facilitator that connects to the smart contract:
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from traia_iatp.x402 import create_iatp_facilitator
|
|
111
|
+
|
|
112
|
+
# Create custom facilitator
|
|
113
|
+
facilitator = create_iatp_facilitator(
|
|
114
|
+
relayer_url="https://api.traia.io/relayer",
|
|
115
|
+
relayer_api_key=os.getenv("TRAIA_RELAYER_API_KEY"),
|
|
116
|
+
provider_operator_key=os.getenv("OPERATOR_PRIVATE_KEY"),
|
|
117
|
+
web3_provider="https://mainnet.base.org"
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
# Use in middleware
|
|
121
|
+
x402_config = X402Config(
|
|
122
|
+
# ... other config ...
|
|
123
|
+
facilitator_url="custom" # Signals to use custom facilitator
|
|
124
|
+
)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Client-Side (Agent-to-Agent Payments)
|
|
128
|
+
|
|
129
|
+
#### 1. X402-Enabled A2A Client
|
|
130
|
+
|
|
131
|
+
Automatically handles payments when calling utility agents:
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
from traia_iatp.client import create_x402_a2a_client
|
|
135
|
+
|
|
136
|
+
# Create client with payment support
|
|
137
|
+
client = create_x402_a2a_client(
|
|
138
|
+
agent_endpoint="https://sentiment-agent.traia.io",
|
|
139
|
+
payment_private_key=os.getenv("CLIENT_PRIVATE_KEY"),
|
|
140
|
+
max_payment_usd=5.0 # Max $5 per request
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
# Send message (automatically pays if required)
|
|
144
|
+
response = await client.send_message_with_payment(
|
|
145
|
+
"Analyze sentiment: 'Tech stocks rally on strong earnings'"
|
|
146
|
+
)
|
|
147
|
+
print(response)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
#### 2. Integration with CrewAI
|
|
151
|
+
|
|
152
|
+
Use paid utility agents as CrewAI tools:
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
from crewai import Agent, Task, Crew
|
|
156
|
+
from traia_iatp.client import X402CrewAITool
|
|
157
|
+
|
|
158
|
+
# Create tool with payment support
|
|
159
|
+
sentiment_tool = X402CrewAITool(
|
|
160
|
+
agent_endpoint="https://sentiment-agent.traia.io",
|
|
161
|
+
payment_private_key=os.getenv("CLIENT_PRIVATE_KEY"),
|
|
162
|
+
max_payment_usd=1.0
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
# Use in CrewAI agent
|
|
166
|
+
analyst = Agent(
|
|
167
|
+
role="Financial Analyst",
|
|
168
|
+
goal="Analyze market sentiment",
|
|
169
|
+
tools=[sentiment_tool],
|
|
170
|
+
backstory="Expert in market analysis"
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
task = Task(
|
|
174
|
+
description="Analyze sentiment of recent tech news",
|
|
175
|
+
expected_output="Sentiment summary with confidence scores",
|
|
176
|
+
agent=analyst
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
crew = Crew(agents=[analyst], tasks=[task])
|
|
180
|
+
result = crew.kickoff()
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Configuration
|
|
184
|
+
|
|
185
|
+
### Environment Variables
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
# For Utility Agents (Server)
|
|
189
|
+
UTILITY_AGENT_CONTRACT_ADDRESS=0x... # On-chain contract address
|
|
190
|
+
OPERATOR_PRIVATE_KEY=0x... # For signing provider attestations
|
|
191
|
+
TRAIA_RELAYER_API_KEY=your-api-key # For relayer authentication
|
|
192
|
+
X402_ENABLED=true # Enable x402 payments
|
|
193
|
+
X402_DEFAULT_PRICE_USD=0.01 # Default price per request
|
|
194
|
+
|
|
195
|
+
# For Client Agents
|
|
196
|
+
CLIENT_PRIVATE_KEY=0x... # For signing payment authorizations
|
|
197
|
+
CLIENT_CONTRACT_ADDRESS=0x... # Optional: client agent contract
|
|
198
|
+
MAX_PAYMENT_USD=10.0 # Maximum payment per request
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Pricing Configuration
|
|
202
|
+
|
|
203
|
+
Per-skill pricing can be configured:
|
|
204
|
+
|
|
205
|
+
```python
|
|
206
|
+
x402_config = X402Config(
|
|
207
|
+
enabled=True,
|
|
208
|
+
pay_to_address="0x1234...",
|
|
209
|
+
default_price=X402ServicePrice(usd_amount="0.01", ...),
|
|
210
|
+
skill_prices={
|
|
211
|
+
"sentiment_analysis": X402ServicePrice(usd_amount="0.01", ...),
|
|
212
|
+
"entity_extraction": X402ServicePrice(usd_amount="0.02", ...),
|
|
213
|
+
"summarization": X402ServicePrice(usd_amount="0.05", ...)
|
|
214
|
+
}
|
|
215
|
+
)
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Payment Flow
|
|
219
|
+
|
|
220
|
+
### 1. Initial Request (No Payment)
|
|
221
|
+
|
|
222
|
+
```
|
|
223
|
+
Client → Utility Agent: GET /analyze
|
|
224
|
+
Utility Agent → Client: 402 Payment Required
|
|
225
|
+
{
|
|
226
|
+
"x402Version": 1,
|
|
227
|
+
"accepts": [{
|
|
228
|
+
"scheme": "exact",
|
|
229
|
+
"network": "base-mainnet",
|
|
230
|
+
"maxAmountRequired": "10000", // 0.01 USDC (6 decimals)
|
|
231
|
+
"payTo": "0x...", // Utility agent contract
|
|
232
|
+
"resource": "https://agent.example.com/analyze",
|
|
233
|
+
"asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913" // USDC
|
|
234
|
+
}]
|
|
235
|
+
}
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### 2. Payment Authorization (Client)
|
|
239
|
+
|
|
240
|
+
Client creates EIP-3009 authorization:
|
|
241
|
+
|
|
242
|
+
```javascript
|
|
243
|
+
{
|
|
244
|
+
"from": "0xClient...", // Client wallet
|
|
245
|
+
"to": "0xUtilityAgent...", // Utility agent contract
|
|
246
|
+
"value": "10000", // Amount in atomic units
|
|
247
|
+
"validAfter": "1640000000", // Unix timestamp
|
|
248
|
+
"validBefore": "1640001000", // Unix timestamp
|
|
249
|
+
"nonce": "0xrandom..." // Random nonce
|
|
250
|
+
}
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Signs with EIP-712 and sends as X-PAYMENT header.
|
|
254
|
+
|
|
255
|
+
### 3. Request with Payment
|
|
256
|
+
|
|
257
|
+
```
|
|
258
|
+
Client → Utility Agent: GET /analyze
|
|
259
|
+
X-PAYMENT: base64(signed_authorization)
|
|
260
|
+
|
|
261
|
+
Utility Agent:
|
|
262
|
+
1. Decodes X-PAYMENT header
|
|
263
|
+
2. Calls facilitator.verify(payment, requirements)
|
|
264
|
+
3. Processes request if valid
|
|
265
|
+
4. Calls facilitator.settle(payment, requirements)
|
|
266
|
+
5. Returns response with X-PAYMENT-RESPONSE header
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
### 4. Settlement (Facilitator → Relayer → Blockchain)
|
|
270
|
+
|
|
271
|
+
```
|
|
272
|
+
Facilitator:
|
|
273
|
+
1. Verifies consumer signature (EIP-712)
|
|
274
|
+
2. Creates provider attestation (signed by operator key)
|
|
275
|
+
3. Submits to relayer
|
|
276
|
+
|
|
277
|
+
Relayer:
|
|
278
|
+
1. Calls IATPSettlementLayer.settleRequest()
|
|
279
|
+
2. Verifies both signatures on-chain
|
|
280
|
+
3. Processes EIP-3009 transfer
|
|
281
|
+
4. Credits provider's epoch balance
|
|
282
|
+
|
|
283
|
+
Smart Contract:
|
|
284
|
+
1. Validates utility agent is registered
|
|
285
|
+
2. Validates signatures
|
|
286
|
+
3. Transfers payment
|
|
287
|
+
4. Records in current epoch
|
|
288
|
+
5. Updates reputation scores
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
## Smart Contract Integration
|
|
292
|
+
|
|
293
|
+
### TraiaUtilityAgent.sol
|
|
294
|
+
|
|
295
|
+
Each deployed utility agent has an on-chain contract that:
|
|
296
|
+
- Stores agent metadata (name, description, operator address)
|
|
297
|
+
- Manages earnings and withdrawals
|
|
298
|
+
- Interacts with settlement layer
|
|
299
|
+
|
|
300
|
+
```solidity
|
|
301
|
+
contract TraiaUtilityAgent {
|
|
302
|
+
address public creator;
|
|
303
|
+
address public operatorAddress;
|
|
304
|
+
string public name;
|
|
305
|
+
string public description;
|
|
306
|
+
|
|
307
|
+
// Withdraw earnings from settlement layer
|
|
308
|
+
function releaseAndWithdrawAvailableEpochs(bool withdrawToCreator) external;
|
|
309
|
+
|
|
310
|
+
// View balances in settlement layer
|
|
311
|
+
function getSettlementLayerUnlockedBalance() external view returns (uint256);
|
|
312
|
+
function getSettlementLayerLockedBalance() external view returns (uint256);
|
|
313
|
+
}
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
### IATPSettlementLayer.sol
|
|
317
|
+
|
|
318
|
+
The main settlement contract that:
|
|
319
|
+
- Processes service requests with signatures
|
|
320
|
+
- Manages epoch-based settlement
|
|
321
|
+
- Handles disputes
|
|
322
|
+
- Maintains provider/consumer reputation
|
|
323
|
+
|
|
324
|
+
```solidity
|
|
325
|
+
contract IATPSettlementLayer {
|
|
326
|
+
// Settle a single request (called by relayer)
|
|
327
|
+
function settleRequest(
|
|
328
|
+
bytes calldata signedRequest,
|
|
329
|
+
ServiceRequest calldata req,
|
|
330
|
+
bytes calldata providerSignature,
|
|
331
|
+
uint256 attestationTimestamp
|
|
332
|
+
) external onlyRelayer;
|
|
333
|
+
|
|
334
|
+
// Provider withdraws earnings after release delay
|
|
335
|
+
function releaseAndWithdrawAvailableEpochs(address provider) external;
|
|
336
|
+
|
|
337
|
+
// Dispute resolution
|
|
338
|
+
function disputeRequest(bytes32 requestId, string calldata reason) external;
|
|
339
|
+
function resolveDispute(bytes32 requestId, bool consumerWon) external;
|
|
340
|
+
}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
## Security Considerations
|
|
344
|
+
|
|
345
|
+
### Payment Verification
|
|
346
|
+
|
|
347
|
+
1. **Signature Validation**: All payments must have valid EIP-712 signatures
|
|
348
|
+
2. **Expiration Checks**: Authorizations have time bounds (validAfter/validBefore)
|
|
349
|
+
3. **Nonce Protection**: Prevents replay attacks
|
|
350
|
+
4. **Amount Verification**: Ensures payment matches requirements
|
|
351
|
+
|
|
352
|
+
### Provider Protection
|
|
353
|
+
|
|
354
|
+
1. **Epoch-Based Release**: Funds locked for dispute window (default: 2 epochs)
|
|
355
|
+
2. **Dispute System**: Consumers can dispute within window
|
|
356
|
+
3. **Reputation Tracking**: Maintains provider/consumer scores
|
|
357
|
+
4. **Operator Separation**: Operator key != creator key for security
|
|
358
|
+
|
|
359
|
+
### Client Protection
|
|
360
|
+
|
|
361
|
+
1. **Max Value Limits**: Clients can set maximum payment per request
|
|
362
|
+
2. **Pre-Verification**: Payment verified before service execution
|
|
363
|
+
3. **Dispute Rights**: Can dispute if service not delivered
|
|
364
|
+
4. **Auto-Refill Control**: Optional auto-refill with caps
|
|
365
|
+
|
|
366
|
+
## Testing
|
|
367
|
+
|
|
368
|
+
### Local Testing
|
|
369
|
+
|
|
370
|
+
```bash
|
|
371
|
+
# Start local test environment
|
|
372
|
+
docker-compose up -d
|
|
373
|
+
|
|
374
|
+
# Deploy test contracts
|
|
375
|
+
cd traia-contracts
|
|
376
|
+
npx hardhat deploy --network localhost
|
|
377
|
+
|
|
378
|
+
# Run IATP tests
|
|
379
|
+
cd IATP
|
|
380
|
+
pytest tests/test_x402_integration.py -v
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
### Integration Testing
|
|
384
|
+
|
|
385
|
+
```python
|
|
386
|
+
# Test utility agent with payments
|
|
387
|
+
async def test_paid_utility_agent():
|
|
388
|
+
from traia_iatp.x402 import X402Config, X402ServicePrice
|
|
389
|
+
from traia_iatp.client import create_x402_a2a_client
|
|
390
|
+
|
|
391
|
+
# Configure test agent
|
|
392
|
+
config = X402Config(
|
|
393
|
+
enabled=True,
|
|
394
|
+
pay_to_address="0xtest...",
|
|
395
|
+
default_price=X402ServicePrice(
|
|
396
|
+
usd_amount="0.01",
|
|
397
|
+
network="base-sepolia", # Testnet
|
|
398
|
+
asset_address="0x...", # Test USDC
|
|
399
|
+
),
|
|
400
|
+
facilitator_url="http://localhost:8080/facilitator"
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
# Create test client
|
|
404
|
+
client = create_x402_a2a_client(
|
|
405
|
+
agent_endpoint="http://localhost:8000",
|
|
406
|
+
payment_private_key="0xtest...",
|
|
407
|
+
max_payment_usd=1.0
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
# Test payment flow
|
|
411
|
+
response = await client.send_message_with_payment("test request")
|
|
412
|
+
assert response is not None
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
## Deployment
|
|
416
|
+
|
|
417
|
+
### Deploying a Utility Agent with X402
|
|
418
|
+
|
|
419
|
+
```python
|
|
420
|
+
from traia_iatp.deployment import deploy_utility_agent_with_x402
|
|
421
|
+
|
|
422
|
+
result = await deploy_utility_agent_with_x402(
|
|
423
|
+
mcp_server_name="sentiment-mcp",
|
|
424
|
+
agent_name="FinBERT Sentiment Agent",
|
|
425
|
+
# Contract will be deployed on-chain
|
|
426
|
+
contract_network="base-mainnet",
|
|
427
|
+
# X402 configuration
|
|
428
|
+
x402_enabled=True,
|
|
429
|
+
default_price_usd="0.01",
|
|
430
|
+
facilitator_url="https://api.traia.io/x402/facilitator",
|
|
431
|
+
# Deployment options
|
|
432
|
+
deploy_to_cloudrun=True,
|
|
433
|
+
push_to_mongodb=True
|
|
434
|
+
)
|
|
435
|
+
|
|
436
|
+
print(f"Deployed agent: {result['cloud_service_url']}")
|
|
437
|
+
print(f"Contract address: {result['contract_address']}")
|
|
438
|
+
print(f"X402 enabled: {result['x402_enabled']}")
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
## Troubleshooting
|
|
442
|
+
|
|
443
|
+
### Common Issues
|
|
444
|
+
|
|
445
|
+
1. **402 Response but Client Has No Payment Client**
|
|
446
|
+
```python
|
|
447
|
+
# Solution: Configure payment client
|
|
448
|
+
client = create_x402_a2a_client(
|
|
449
|
+
agent_endpoint="...",
|
|
450
|
+
payment_private_key="0x..." # ← Add this
|
|
451
|
+
)
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
2. **Payment Amount Exceeds Maximum**
|
|
455
|
+
```python
|
|
456
|
+
# Solution: Increase max_payment_usd or negotiate with provider
|
|
457
|
+
client = create_x402_a2a_client(
|
|
458
|
+
agent_endpoint="...",
|
|
459
|
+
payment_private_key="0x...",
|
|
460
|
+
max_payment_usd=10.0 # ← Increase limit
|
|
461
|
+
)
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
3. **Signature Verification Failed**
|
|
465
|
+
- Check operator private key is correct
|
|
466
|
+
- Ensure EIP-712 domain matches contract configuration
|
|
467
|
+
- Verify timestamp is within valid bounds
|
|
468
|
+
|
|
469
|
+
4. **Settlement Fails**
|
|
470
|
+
- Check relayer is accessible
|
|
471
|
+
- Verify relayer API key is valid
|
|
472
|
+
- Ensure provider is registered in UtilityAgentFactory
|
|
473
|
+
- Check consumer has deposited funds in settlement layer
|
|
474
|
+
|
|
475
|
+
## Resources
|
|
476
|
+
|
|
477
|
+
- [X402 Protocol Specification](https://docs.cdp.coinbase.com/x402/welcome)
|
|
478
|
+
- [EIP-3009: Transfer With Authorization](https://eips.ethereum.org/EIPS/eip-3009)
|
|
479
|
+
- [EIP-712: Typed Structured Data Hashing](https://eips.ethereum.org/EIPS/eip-712)
|
|
480
|
+
- [IATP Repository](https://github.com/Traia-IO/IATP)
|
|
481
|
+
- [Smart Contracts Repository](https://github.com/Traia-IO/traia-contracts)
|
|
482
|
+
|
|
483
|
+
## Support
|
|
484
|
+
|
|
485
|
+
For issues or questions:
|
|
486
|
+
- GitHub Issues: [https://github.com/Traia-IO/IATP/issues](https://github.com/Traia-IO/IATP/issues)
|
|
487
|
+
- Discord: [https://discord.gg/traia](https://discord.gg/traia)
|
|
488
|
+
- Email: support@traia.io
|
|
489
|
+
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""D402 payment integration for IATP protocol.
|
|
2
|
+
|
|
3
|
+
This module provides d402 (HTTP 402 Payment Required) payment capabilities
|
|
4
|
+
for the Inter-Agent Transfer Protocol (IATP). It enables utility agents to
|
|
5
|
+
accept payments and client agents to send payments for API access.
|
|
6
|
+
|
|
7
|
+
Components:
|
|
8
|
+
- middleware: FastAPI middleware for accepting d402 payments
|
|
9
|
+
- client: d402 client integration for sending payments
|
|
10
|
+
- facilitator: Custom facilitator that interfaces with IATPSettlementLayer
|
|
11
|
+
- models: Payment configuration models
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from .models import (
|
|
15
|
+
D402Config,
|
|
16
|
+
D402PaymentInfo,
|
|
17
|
+
D402ServicePrice,
|
|
18
|
+
PaymentScheme,
|
|
19
|
+
)
|
|
20
|
+
from .servers import require_payment, D402PaymentMiddleware
|
|
21
|
+
from .asgi_wrapper import D402ASGIWrapper
|
|
22
|
+
from .client import D402IATPClient
|
|
23
|
+
from .facilitator import IATPSettlementFacilitator
|
|
24
|
+
from .mcp_middleware import (
|
|
25
|
+
EndpointPaymentInfo,
|
|
26
|
+
get_active_api_key,
|
|
27
|
+
require_payment_for_tool,
|
|
28
|
+
settle_payment
|
|
29
|
+
)
|
|
30
|
+
from .clients.httpx import d402_payment_hooks, d402HttpxClient
|
|
31
|
+
from .clients.base import decode_x_payment_response
|
|
32
|
+
from .price_builder import D402PriceBuilder
|
|
33
|
+
|
|
34
|
+
__all__ = [
|
|
35
|
+
"D402Config",
|
|
36
|
+
"D402PaymentInfo",
|
|
37
|
+
"D402ServicePrice",
|
|
38
|
+
"PaymentScheme",
|
|
39
|
+
"require_payment",
|
|
40
|
+
"D402PaymentMiddleware",
|
|
41
|
+
"D402ASGIWrapper",
|
|
42
|
+
"D402IATPClient",
|
|
43
|
+
"IATPSettlementFacilitator",
|
|
44
|
+
"EndpointPaymentInfo",
|
|
45
|
+
"get_active_api_key",
|
|
46
|
+
"require_payment_for_tool",
|
|
47
|
+
"settle_payment",
|
|
48
|
+
"d402_payment_hooks",
|
|
49
|
+
"d402HttpxClient",
|
|
50
|
+
"decode_x_payment_response",
|
|
51
|
+
# Price builder helper
|
|
52
|
+
"D402PriceBuilder",
|
|
53
|
+
]
|
|
54
|
+
|