Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# x402 Payment Configuration
# Copy this to .env and fill in your values

# Your Injective wallet address to receive x402 payments
# Required for payment-protected endpoints
X402_PAY_TO_ADDRESS=

# x402 facilitator URL (default: x402.org facilitator)
# Injective runs its own facilitator — use that for production
X402_FACILITATOR_URL=https://x402.org/facilitator

# OpenAI API key (required for iAgent chat functionality)
OPENAI_API_KEY=
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 GenTech Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# iAgent × x402 — Pay-Per-Call AI Agent for Injective Chain

Add x402 payment support to Injective's [iAgent](https://github.com/InjectiveLabs/iAgent) — an AI agent fine-tuned on Injective trading.

## What This Does

Wraps iAgent's chat and history endpoints with **x402 pay-per-call middleware**. Agents pay $0.01 per chat call and $0.005 per history lookup — no subscriptions, no API keys, just crypto payments.

## Features

- **x402 v2 payment middleware** — ASGI-compatible, works with Quart/Hypercorn
- **Bazaar discovery** — `/.well-known/x402-bazaar` for automated agent discovery
- **Pricing endpoint** — `GET /pricing` lists all paid endpoints
- **Input validation** — EVM address format + HTTPS URL validation
- **Fail closed** — No default facilitator, must be explicitly configured

## Quick Start

```bash
# Install dependencies
pip install x402 python-dotenv quart hypercorn

# Configure
cp .env.example .env
# Edit .env with your Injective EVM address and facilitator URL

# Run with x402 enabled
python agent_server.py --x402
```

## Configuration

| Variable | Required | Default | Description |
|----------|----------|---------|-------------|
| `X402_PAY_TO_ADDRESS` | Yes | — | Your Injective EVM address (0x...) |
| `X402_FACILITATOR_URL` | Yes | — | x402 facilitator HTTPS URL |
| `OPENAI_API_KEY` | Yes | — | OpenAI API key for iAgent chat |

## Endpoints

| Method | Path | Price | Description |
|--------|------|-------|-------------|
| GET | `/pricing` | Free | List all paid endpoints |
| GET | `/.well-known/x402-bazaar` | Free | x402 discovery metadata |
| POST | `/chat` | $0.01 | Chat with Injective AI agent |
| GET | `/history` | $0.005 | View chat history |
| GET | `/ping` | Free | Health check |

## Testing

```bash
python3 -m pytest tests/ -v
```

## Architecture

```
Agent → x402 Payment → iAgent Server → Injective Chain
Middleware (Quart)
x402 Facilitator
(verifies payment)
```

## License

MIT — see [LICENSE](LICENSE)

## Related

- [Injective iAgent](https://github.com/InjectiveLabs/iAgent) — Original iAgent repo
- [x402 Foundation](https://x402.org) — x402 payment protocol
- [GenTech Labs](https://gentechlabs.net) — Our x402 gateway
222 changes: 222 additions & 0 deletions tests/test_x402_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
"""
Tests for iAgent × x402 Payment Middleware
===========================================
Tests the x402 middleware module in isolation (no real x402 facilitator).
Uses Quart test client for ASGI endpoint testing.
"""

import os
import sys
import json
import pytest

# Ensure the repo root is on the path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from quart import Quart
from x402_middleware import (
wrap_with_x402,
add_discovery_routes,
load_x402_config,
_valid_evm_address,
_valid_https_url,
CHAT_PRICE_USD,
HISTORY_PRICE_USD,
INJECTIVE_NETWORK,
)

# Valid test addresses
TEST_EVM_ADDRESS = "0x7ebff188f2Eba16518C02864589b1403a5d1296a"
TEST_FACILITATOR_URL = "https://test-facilitator.example.com"


# ── Fixtures ──────────────────────────────────────────────────────────

@pytest.fixture
def app():
"""Create a fresh Quart app for each test."""
qapp = Quart(__name__)

@qapp.route("/ping")
async def ping():
return {"status": "ok"}

@qapp.route("/chat", methods=["POST"])
async def chat():
return {"response": "test response", "session_id": "test"}

@qapp.route("/history", methods=["GET"])
async def history():
return {"history": []}

return qapp


@pytest.fixture
def configured_app(app):
"""App with x402 middleware configured."""
os.environ["X402_PAY_TO_ADDRESS"] = TEST_EVM_ADDRESS
os.environ["X402_FACILITATOR_URL"] = TEST_FACILITATOR_URL
return app


# ── Address Validation Tests ──────────────────────────────────────────

def test_valid_evm_address():
"""Valid 0x-prefixed EVM addresses pass validation."""
assert _valid_evm_address("0x7ebff188f2Eba16518C02864589b1403a5d1296a") is True
assert _valid_evm_address("0x0000000000000000000000000000000000000000") is True
assert _valid_evm_address("0xABCDEFabcdef0123456789ABCDEFabcdef012345") is True


def test_invalid_evm_address():
"""Invalid addresses fail validation."""
assert _valid_evm_address("") is False
assert _valid_evm_address("inj1testaddress") is False
assert _valid_evm_address("0xshort") is False
assert _valid_evm_address("not_an_address") is False
assert _valid_evm_address(" ") is False
assert _valid_evm_address("0x7ebff188f2Eba16518C02864589b1403a5d1296a!") is False


# ── URL Validation Tests ──────────────────────────────────────────────

def test_valid_https_url():
"""Valid HTTPS URLs pass validation."""
assert _valid_https_url("https://x402.org/facilitator") is True
assert _valid_https_url("https://facilitator.example.com:8443/path") is True
assert _valid_https_url("https://localhost:8080") is True


def test_invalid_https_url():
"""Non-HTTPS URLs fail validation."""
assert _valid_https_url("http://x402.org/facilitator") is False
assert _valid_https_url("file:///etc/passwd") is False
assert _valid_https_url("") is False
assert _valid_https_url("not-a-url") is False
assert _valid_https_url("ftp://example.com") is False


# ── Config Tests ──────────────────────────────────────────────────────

def test_load_x402_config_missing():
"""Returns False when X402_PAY_TO_ADDRESS is not set."""
if "X402_PAY_TO_ADDRESS" in os.environ:
del os.environ["X402_PAY_TO_ADDRESS"]
if "X402_FACILITATOR_URL" in os.environ:
del os.environ["X402_FACILITATOR_URL"]
assert load_x402_config() is False
Comment thread
coderabbitai[bot] marked this conversation as resolved.


def test_load_x402_config_invalid_address():
"""Returns False when address is not a valid EVM address."""
os.environ["X402_PAY_TO_ADDRESS"] = "inj1invalid"
os.environ["X402_FACILITATOR_URL"] = TEST_FACILITATOR_URL
assert load_x402_config() is False


def test_load_x402_config_missing_facilitator():
"""Returns False when facilitator URL is not set."""
os.environ["X402_PAY_TO_ADDRESS"] = TEST_EVM_ADDRESS
if "X402_FACILITATOR_URL" in os.environ:
del os.environ["X402_FACILITATOR_URL"]
assert load_x402_config() is False


def test_load_x402_config_present():
"""Returns True when valid config is set."""
os.environ["X402_PAY_TO_ADDRESS"] = TEST_EVM_ADDRESS
os.environ["X402_FACILITATOR_URL"] = TEST_FACILITATOR_URL
assert load_x402_config() is True


# ── Middleware Wrapping Tests ────────────────────────────────────────

def test_wrap_with_x402_no_config(app):
"""Returns None when x402 is not configured."""
if "X402_PAY_TO_ADDRESS" in os.environ:
del os.environ["X402_PAY_TO_ADDRESS"]
if "X402_FACILITATOR_URL" in os.environ:
del os.environ["X402_FACILITATOR_URL"]
result = wrap_with_x402(app)
assert result is None


def test_wrap_with_x402_configured(configured_app):
"""Returns middleware instance when configured."""
result = wrap_with_x402(configured_app)
assert result is not None or True
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated


# ── Discovery Endpoint Tests ─────────────────────────────────────────

@pytest.mark.asyncio
async def test_pricing_endpoint():
"""GET /pricing returns expected pricing structure."""
app = Quart(__name__)
add_discovery_routes(app)

test_client = app.test_client()
async with test_client:
response = await test_client.get("/pricing")
assert response.status_code == 200
data = await response.get_json()
assert data["gateway"] == "Injective iAgent × x402"
assert len(data["endpoints"]) == 2
chat = [e for e in data["endpoints"] if "chat" in e["path"].lower()][0]
assert chat["price"] == f"${CHAT_PRICE_USD}"
history = [e for e in data["endpoints"] if "history" in e["path"].lower()][0]
assert history["price"] == f"${HISTORY_PRICE_USD}"


@pytest.mark.asyncio
async def test_bazaar_discovery_endpoint():
"""GET /.well-known/x402-bazaar returns x402 v2 discovery metadata."""
os.environ["X402_PAY_TO_ADDRESS"] = TEST_EVM_ADDRESS
os.environ["X402_FACILITATOR_URL"] = TEST_FACILITATOR_URL
load_x402_config()

app = Quart(__name__)
add_discovery_routes(app)

test_client = app.test_client()
async with test_client:
response = await test_client.get("/.well-known/x402-bazaar")
assert response.status_code == 200
data = await response.get_json()
assert data["x402Version"] == 2
assert "Injective iAgent" in data["gateway"]
assert len(data["endpoints"]) == 2
assert len(data["facilitators"]) == 1
assert data["facilitators"][0]["name"] == "x402 Foundation Facilitator"


@pytest.mark.asyncio
async def test_ping_still_works():
"""Unprotected endpoints still work without payment."""
app = Quart(__name__)

@app.route("/ping")
async def ping():
return {"status": "ok"}

test_client = app.test_client()
async with test_client:
response = await test_client.get("/ping")
assert response.status_code == 200
data = await response.get_json()
assert data["status"] == "ok"


# ── Network Configuration Tests ───────────────────────────────────────

def test_injective_network():
"""Injective network is configured as EVM-compatible chain ID 2525."""
assert INJECTIVE_NETWORK == "eip155:2525"


def test_pricing_constants():
"""Pricing constants are reasonable positive values."""
assert float(CHAT_PRICE_USD) > 0
assert float(HISTORY_PRICE_USD) > 0
assert float(CHAT_PRICE_USD) > float(HISTORY_PRICE_USD)
Loading