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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
"""
RMI Developer Tier System
==========================
Free developer API keys with 100 calls/day. No payment required.
Designed to get bot developers hooked on RMI tools before upgrading to paid.

Tiers:
  FREE: 100 calls/day, rate-limited 10/min, all tools accessible
  TRIAL: 3-5 calls per tool (existing system), no key needed
  PAID: Pay-per-use via x402, no rate limit beyond burst protection
  PRO: $29/month, 5000 calls/day, priority queue, webhook support

Author: RMI Development
Date: 2026-06-05
"""

import json
import logging
import time
from datetime import UTC, datetime, timedelta
from typing import Any

from fastapi import APIRouter, Request
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field

logger = logging.getLogger("developer_tier")

router = APIRouter(prefix="/api/v1/developer", tags=["developer-tier"])

# ── EVM Signature Verification ───────────────────────────────────


def verify_evm_signature(address: str, message: str, signature: str) -> bool:
    """Verify an EVM wallet signature.

    Uses web3.py to recover the signer from the signature and compare
    with the claimed address. This proves wallet ownership.
    """
    try:
        from eth_account import Account
        from eth_account.messages import encode_defunct

        encoded = encode_defunct(text=message)
        recovered = Account.recover_message(encoded, signature=signature)
        return recovered.lower() == address.lower()
    except ImportError:
        # eth_account not available β€” fail closed
        return False
    except Exception:
        return False


def verify_solana_signature(address: str, message: str, signature: str) -> bool:
    """Verify a Solana wallet signature."""
    try:
        import base64

        from nacl.signing import VerifyKey

        from app.core.redis import get_redis

        pubkey = VerifyKey(base64.b58decode(address))
        msg_bytes = message.encode("utf-8")
        sig_bytes = base64.b64decode(signature)
        pubkey.verify(msg_bytes, sig_bytes)
        return True
    except ImportError:
        return False
    except Exception:
        return False


# ── Tier Definitions ──────────────────────────────────────────────

TIERS = {
    "free": {
        "daily_limit": 100,
        "rate_limit_per_min": 10,
        "rate_limit_per_hour": 200,
        "tools": "all",
        "price_usd": 0,
        "trial_free": 0,
        "webhook_support": False,
        "priority_queue": False,
        "analytics_access": False,
    },
    "trial": {
        "daily_limit": 20,
        "rate_limit_per_min": 5,
        "rate_limit_per_hour": 50,
        "tools": "all",
        "price_usd": 0,
        "trial_free": 3,
        "webhook_support": False,
        "priority_queue": False,
        "analytics_access": False,
    },
    "paid": {
        "daily_limit": 10000,
        "rate_limit_per_min": 60,
        "rate_limit_per_hour": 2000,
        "tools": "all",
        "price_usd": "per_call",
        "trial_free": 0,
        "webhook_support": True,
        "priority_queue": False,
        "analytics_access": True,
    },
    "pro": {
        "daily_limit": 5000,
        "rate_limit_per_min": 120,
        "rate_limit_per_hour": 5000,
        "tools": "all",
        "price_usd": 29.00,
        "trial_free": 0,
        "webhook_support": True,
        "priority_queue": True,
        "analytics_access": True,
    },
}


# ── Redis Helper ─────────────────────────────────────────────────


def create_tier(tier: str) -> dict[str, Any] | None:
    """Create a new developer API key."""
    r = get_redis()
    if not r:
        return None

    if tier not in TIERS:
        return None

    key = generate_api_key()
    hashed = hash_api_key(key)
    created_at = datetime.now(UTC).isoformat()

    key_info = {
        "key": key,  # Only returned once, never stored in plaintext
        "hashed_key": hashed,
        "email": email,
        "tier": tier,
        "wallet_address": wallet_address,
        "created_at": created_at,
        "status": "active",
        "total_calls": 0,
        "total_revenue_usd": 0,
    }

    # Store in Redis (no TTL for free keys, 90-day TTL for trial)
    r.set(
        f"rmi:dev:key:{hashed}",
        json.dumps(key_info),
        ex=86400 * 90 if tier == "trial" else None,
    )

    # Index by email for lookup
    r.sadd(f"rmi:dev:email:{email}", hashed)

    # Track total keys created
    r.incr("rmi:dev:stats:total_keys")
    r.incr(f"rmi:dev:stats:tier:{tier}")

    return key_info


def get_usage_for_key(hashed_key: str) -> dict[str, Any]:
    """Get usage stats for an API key."""
    r = get_redis()
    if not r:
        return {"error": "redis_unavailable"}

    today = datetime.now(UTC).strftime("%Y-%m-%d")
    today_calls = int(r.get(f"rmi:dev:usage:{hashed_key}:{today}") or 0)
    total_calls = int(r.get(f"rmi:dev:usage:{hashed_key}:total") or 0)

    # Get key info
    key_data = r.get(f"rmi:dev:key:{hashed_key}")
    if not key_data:
        return {"error": "key_not_found"}

    key_info = json.loads(key_data)
    tier_config = TIERS.get(key_info.get("tier", "free"), TIERS["free"])

    return {
        "tier": key_info.get("tier", "free"),
        "email": key_info.get("email", ""),
        "status": key_info.get("status", "active"),
        "today_calls": today_calls,
        "daily_limit": tier_config["daily_limit"],
        "remaining_today": max(0, tier_config["daily_limit"] - today_calls),
        "total_calls": total_calls,
        "created_at": key_info.get("created_at", ""),
        "rate_limit_per_min": tier_config["rate_limit_per_min"],
    }


# ── Request/Response Models ──────────────────────────────────────


class RegisterRequest(BaseModel):
    email: str = Field(..., min_length=3, max_length=255)
    tier: str = Field(default="free")
    wallet_address: str | None = None


class VerifyRequest(BaseModel):
    api_key: str


class UsageRequest(BaseModel):
    api_key: str


# ── Endpoints ────────────────────────────────────────────────────


@router.post("/register")
async def register_developer(req: RegisterRequest):
    """Register for a free developer API key. 100 calls/day, no payment needed."""
    # Validate tier
    if req.tier not in TIERS:
        return JSONResponse(
            status_code=400,
            content={
                "error": f"Invalid tier. Available: {', '.join(TIERS.keys())}",
                "tiers": {k: {"daily_limit": v["daily_limit"], "price": v["price_usd"]} for k, v in TIERS.items()},
            },
        )

    # Validate email
    if "@" not in req.email or "." not in req.email.split("@")[-1]:
        return JSONResponse(status_code=400, content={"error": "Invalid email address"})

    # Check if email already has a key
    r = get_redis()
    if r:
        existing = r.smembers(f"rmi:dev:email:{req.email}")
        if existing:
            return JSONResponse(
                status_code=409,
                content={
                    "error": "Email already registered",
                    "message": "You already have an API key. Use /verify to check your key status.",
                },
            )

    # Create key
    result = create_api_key(req.email, req.tier, req.wallet_address)
    if not result:
        return JSONResponse(status_code=500, content={"error": "Failed to create API key"})

    # Return key info (only show key once)
    return JSONResponse(
        status_code=201,
        content={
            "success": True,
            "message": "Developer API key created. Save it β€” it won't be shown again.",
            "api_key": result["key"],
            "tier": result["tier"],
            "daily_limit": TIERS[result["tier"]]["daily_limit"],
            "rate_limit_per_min": TIERS[result["tier"]]["rate_limit_per_min"],
            "documentation": "https://rugmunch.io/mcp-docs",
            "quickstart": {
                "curl": f'curl -X POST https://mcp.rugmunch.io/api/v1/x402-tools/audit \\n  -H "Content-Type: application/json" \\n  -H "X-RMI-Dev-Key: {result["key"]}" \\n  -d \'{{"address": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"}}\'',
                "python": f"""from rmi import RMI
rmi = RMI(api_key="{result["key"]}")
result = rmi.scan("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")
""",
            },
        },
    )


@router.post("/verify")
async def verify_key(req: VerifyRequest):
    """Verify an API key and get usage stats."""
    if not req.api_key:
        return JSONResponse(status_code=400, content={"error": "api_key required"})

    hashed = hash_api_key(req.api_key)
    usage = get_usage_for_key(hashed)

    if "error" in usage:
        return JSONResponse(status_code=401, content={"error": "Invalid API key"})

    return JSONResponse(content={"valid": True, **usage})


@router.post("/usage")
async def get_usage(req: UsageRequest):
    """Get detailed usage for an API key."""
    if not req.api_key:
        return JSONResponse(status_code=400, content={"error": "api_key required"})

    hashed = hash_api_key(req.api_key)
    r = get_redis()
    if not r:
        return JSONResponse(status_code=500, content={"error": "redis_unavailable"})

    # Verify key exists
    key_data = r.get(f"rmi:dev:key:{hashed}")
    if not key_data:
        return JSONResponse(status_code=401, content={"error": "Invalid API key"})

    # Get usage for last 7 days
    today = datetime.now(UTC)
    daily_usage = []
    for i in range(7):
        day = (today - timedelta(days=i)).strftime("%Y-%m-%d")
        calls = int(r.get(f"rmi:dev:usage:{hashed}:{day}") or 0)
        daily_usage.append({"date": day, "calls": calls})

    # Get per-tool usage
    tool_usage = {}
    tool_keys = r.keys(f"rmi:dev:tool:{hashed}:*")
    for tk in tool_keys[:50]:  # Limit to 50 tools
        tool_name = tk.decode().split(":")[-1]
        count = int(r.get(tk) or 0)
        if count > 0:
            tool_usage[tool_name] = count

    return JSONResponse(
        content={
            "tier": json.loads(key_data).get("tier", "free"),
            "daily_usage": daily_usage,
            "tool_usage": dict(sorted(tool_usage.items(), key=lambda x: -x[1])[:20]),
        }
    )


@router.get("/tiers")
async def list_tiers():
    """List available developer tiers."""
    return JSONResponse(
        content={
            "tiers": {
                name: {
                    "daily_limit": config["daily_limit"],
                    "rate_limit_per_min": config["rate_limit_per_min"],
                    "rate_limit_per_hour": config["rate_limit_per_hour"],
                    "price": config["price_usd"],
                    "webhook_support": config["webhook_support"],
                    "priority_queue": config["priority_queue"],
                    "analytics_access": config["analytics_access"],
                }
                for name, config in TIERS.items()
            },
            "note": "FREE tier: 100 calls/day, no payment required. Upgrade to PAID for unlimited pay-per-use.",
        }
    )


# ── Wallet Identity Verification ─────────────────────────────────


class WalletVerifyRequest(BaseModel):
    address: str
    signature: str
    chain: str = "evm"  # evm or solana


@router.post("/wallet/verify")
async def verify_wallet_identity(req: WalletVerifyRequest):
    """Verify wallet ownership via signature.

    Grants a persistent wallet-based identity for trials that survives IP changes.
    Users sign a challenge message with their wallet to prove ownership.
    """
    if not req.address or not req.signature:
        return JSONResponse(status_code=400, content={"error": "address and signature required"})

    # Generate challenge message
    challenge = f"RMI identity verification: {req.address} at {int(time.time()) // 3600}"

    # Verify signature
    if req.chain == "evm":
        valid = verify_evm_signature(req.address, challenge, req.signature)
    elif req.chain == "solana":
        valid = verify_solana_signature(req.address, challenge, req.signature)
    else:
        return JSONResponse(status_code=400, content={"error": "chain must be 'evm' or 'solana'"})

    if not valid:
        return JSONResponse(status_code=401, content={"error": "Invalid signature β€” wallet ownership not proven"})

    # Store wallet identity in Redis
    r = get_redis()
    if not r:
        return JSONResponse(status_code=500, content={"error": "redis_unavailable"})

    wallet_id = f"wallet:{req.chain}:{req.address.lower()}"

    # Grant enhanced trial limits for verified wallets
    wallet_data = {
        "address": req.address.lower(),
        "chain": req.chain,
        "verified_at": datetime.now(UTC).isoformat(),
        "trial_multiplier": 3,  # 3x normal trial limits
        "daily_free_limit": 50,  # 50 free calls/day for verified wallets
        "status": "active",
    }

    r.set(f"rmi:wallet:{wallet_id}", json.dumps(wallet_data), ex=86400 * 30)  # 30 day TTL
    r.incr("rmi:wallet:stats:total_verified")

    return JSONResponse(
        content={
            "verified": True,
            "wallet_id": wallet_id,
            "benefits": {
                "trial_multiplier": 3,
                "daily_free_limit": 50,
                "persistent_identity": True,
            },
            "message": "Wallet verified! You now get 3x trial limits and 50 free calls/day.",
        }
    )


@router.post("/wallet/get_challenge")
async def get_wallet_challenge(req: VerifyRequest):
    """Get a challenge message to sign with your wallet."""
    # If they have an API key, generate a challenge for that key
    if req.api_key:
        hashed = hash_api_key(req.api_key)
        key_data = get_redis().get(f"rmi:dev:key:{hashed}") if get_redis() else None
        if key_data:
            info = json.loads(key_data)
            address = info.get("wallet_address", "")
            if address:
                challenge = f"RMI identity verification: {address} at {int(time.time()) // 3600}"
                return JSONResponse(
                    content={
                        "message": challenge,
                        "address": address,
                        "instruction": "Sign this message with your wallet to verify ownership.",
                    }
                )

    # Generic challenge
    challenge = f"RMI identity verification at {int(time.time()) // 3600}"
    return JSONResponse(
        content={
            "message": challenge,
            "instruction": "Sign this message with your wallet. Include your wallet address in the /wallet/verify call.",
        }
    )


@router.get("/wallet/status/{chain}/{address}")
async def wallet_status(chain: str, address: str):
    """Check wallet verification status and benefits."""
    r = get_redis()
    if not r:
        return JSONResponse(status_code=500, content={"error": "redis_unavailable"})

    wallet_id = f"rmi:wallet:wallet:{chain}:{address.lower()}"
    data = r.get(wallet_id)
    if not data:
        return JSONResponse(
            content={
                "verified": False,
                "message": "Wallet not verified. Use POST /wallet/verify to verify.",
            }
        )

    wallet_data = json.loads(data)
    return JSONResponse(
        content={
            "verified": True,
            **wallet_data,
        }
    )


# ── Middleware Helper ─────────────────────────────────────────────


async def check_developer_key(request: Request) -> dict[str, Any] | None:
    """Check if request has a valid developer API key.

    Returns tier info if valid, None if not a dev key request.
    This is called by the enforcement middleware BEFORE payment checks.
    """
    dev_key = request.headers.get("X-RMI-Dev-Key", "") or request.headers.get("x-rmi-dev-key", "")
    if not dev_key:
        return None

    hashed = hash_api_key(dev_key)
    r = get_redis()
    if not r:
        return {"error": "redis_unavailable", "deny": True}

    # Look up key
    key_data = r.get(f"rmi:dev:key:{hashed}")
    if not key_data:
        return {"error": "invalid_api_key", "deny": True, "status_code": 401}

    key_info = json.loads(key_data)

    # Check status
    if key_info.get("status") != "active":
        return {"error": "key_suspended", "deny": True, "status_code": 403}

    # Check daily limit
    today = datetime.now(UTC).strftime("%Y-%m-%d")
    today_calls = int(r.get(f"rmi:dev:usage:{hashed}:{today}") or 0)
    tier_config = TIERS.get(key_info.get("tier", "free"), TIERS["free"])

    if today_calls >= tier_config["daily_limit"]:
        return {
            "error": "daily_limit_exceeded",
            "deny": True,
            "status_code": 429,
            "daily_limit": tier_config["daily_limit"],
            "calls_today": today_calls,
        }

    # Check per-minute rate limit
    minute_key = f"rmi:dev:ratelimit:{hashed}:{int(time.time()) // 60}"
    minute_calls = int(r.get(minute_key) or 0)
    if minute_calls >= tier_config["rate_limit_per_min"]:
        return {
            "error": "rate_limit_exceeded",
            "deny": True,
            "status_code": 429,
            "rate_limit_per_min": tier_config["rate_limit_per_min"],
            "retry_after": 60,
        }

    # All checks passed β€” consume the call
    pipe = r.pipeline()
    pipe.incr(f"rmi:dev:usage:{hashed}:{today}")
    pipe.incr(f"rmi:dev:usage:{hashed}:total")
    pipe.incr(minute_key)
    pipe.expire(minute_key, 120)  # 2 minute TTL for rate limit key
    pipe.incr("rmi:dev:stats:total_calls")
    pipe.execute()

    return {
        "valid": True,
        "tier": key_info.get("tier", "free"),
        "email": key_info.get("email", ""),
        "remaining_today": tier_config["daily_limit"] - today_calls - 1,
        "deny": False,
    }