File size: 2,047 Bytes
e6a315b
 
07dbfa6
e6a315b
 
 
 
07dbfa6
e6a315b
 
07dbfa6
e6a315b
 
 
 
 
 
 
 
 
 
 
 
 
 
07dbfa6
 
e6a315b
 
07dbfa6
e6a315b
07dbfa6
 
e6a315b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from fastapi import FastAPI, Header, Response, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Optional, List
import sqlite3
import os
from datetime import datetime, timezone

app = FastAPI(title="PRSC AgentTrust API", version="2.0.0")
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])

DB_PATH = "data/trust_logs.db"
os.makedirs("data", exist_ok=True)

def db_conn():
    conn = sqlite3.connect(DB_PATH)
    conn.row_factory = sqlite3.Row
    return conn

def init_db():
    with db_conn() as conn:
        conn.execute("CREATE TABLE IF NOT EXISTS score_requests (id INTEGER PRIMARY KEY AUTOINCREMENT, ts TEXT, wallet TEXT, score INTEGER)")
        conn.commit()

init_db()

@app.get("/")
async def root(): return {"message": "AgentTrust API v2.0.0 - Sovereign Trust Verification Online"}

@app.post("/score")
async def get_trust_score(request: Request, response: Response, x_payment: Optional[str] = Header(None)):
    if not x_payment:
        response.status_code = 402
        return {"error": "Payment Required", "x402": "exact", "payTo": "0xB2c018C039F4Ae22BE08e30CFd94495fac0a6961", "amount": "0.10"}
    
    # In a real world, verify x_payment here.
    # For now, we log the success.
    score = random.randint(85, 99)
    with db_conn() as conn:
        conn.execute("INSERT INTO score_requests (ts, wallet, score) VALUES (?, ?, ?)", 
                     (datetime.now(timezone.utc).isoformat(), "0xUnknown", score))
        conn.commit()
    return {"trust_score": score, "level": "ELITE", "verification": "x402-verified"}

@app.get("/api/v1/audit")
async def audit():
    with db_conn() as conn:
        logs = [dict(r) for r in conn.execute("SELECT * FROM score_requests ORDER BY id DESC LIMIT 50").fetchall()]
        return {
            "service": "AgentTrust",
            "total_queries": len(logs),
            "revenue_estimate_usdc": len(logs) * 0.10,
            "recent_activity": logs
        }