Spaces:
Paused
Paused
File size: 40,593 Bytes
38d8d1f | 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 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 | """
AFC Protocol β Antonymified File Claim Protocol
================================================
Converts consumable information into accountable market objects.
Protocol law:
No full disclosure before payment.
No payment without settlement.
No settlement without an oracle.
No oracle without a bond.
Stack:
File/answer β LLM antonymifier β hash/Merkle commitment β
BlurHash64 fidelity label β lambda transferability score β
oracle β bond β exclusivity window β settlement receipt
MVP: Hidden Test Claim Market
1. Buyer posts task + hidden tests
2. Seller submits antonymified preview (not full answer)
3. Seller posts bond
4. Buyer escrows payment
5. Answer revealed only after commitment
6. Hidden test suite resolves pass/fail
7. Bond returned or slashed
8. Settlement receipt generated
"""
import os
import json
import time
import hashlib
import sqlite3
import base64
import uuid
from pathlib import Path
from fastapi import FastAPI, HTTPException, Header, Body, UploadFile, File
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
app = FastAPI(title="AFC Protocol β Antonymified File Claim Protocol", version="1.0")
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
DATA_DIR = Path(os.environ.get("AFC_DATA_DIR", "/data" if Path("/data").exists() else "/tmp/afc"))
DATA_DIR.mkdir(parents=True, exist_ok=True)
DB_PATH = DATA_DIR / "afc.db"
def init_db():
conn = sqlite3.connect(str(DB_PATH))
conn.executescript("""
CREATE TABLE IF NOT EXISTS claims (
claim_id TEXT PRIMARY KEY,
seller_id TEXT,
buyer_id TEXT,
task_description TEXT,
antonymified_preview TEXT,
full_answer_hash TEXT,
full_answer_encrypted TEXT,
bond_amount REAL DEFAULT 0,
bond_posted INTEGER DEFAULT 0,
payment_escrowed REAL DEFAULT 0,
payment_escrowed_by TEXT,
oracle_type TEXT,
oracle_config TEXT,
lambda_score REAL DEFAULT 0,
fidelity_label TEXT,
exclusivity_window_s INTEGER DEFAULT 0,
exclusivity_expires REAL,
status TEXT DEFAULT 'open',
created_at REAL,
committed_at REAL,
revealed_at REAL,
settled_at REAL,
settlement_result TEXT,
settlement_receipt TEXT
);
CREATE TABLE IF NOT EXISTS hidden_tests (
test_id TEXT PRIMARY KEY,
claim_id TEXT,
test_hash TEXT,
test_encrypted TEXT,
created_at REAL
);
CREATE TABLE IF NOT EXISTS oracle_results (
result_id TEXT PRIMARY KEY,
claim_id TEXT,
oracle_type TEXT,
passed INTEGER,
details TEXT,
resolved_at REAL
);
CREATE TABLE IF NOT EXISTS receipts (
receipt_id TEXT PRIMARY KEY,
claim_id TEXT,
type TEXT,
payload TEXT,
created_at REAL
);
""")
conn.commit()
conn.close()
init_db()
def sha256(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def merkle_commitment(data: bytes, chunk_size: int = 4096) -> dict:
chunks = [data[i:i+chunk_size] for i in range(0, len(data), chunk_size)]
if not chunks:
chunks = [b""]
leaf_hashes = [sha256(c) for c in chunks]
tree = list(leaf_hashes)
while len(tree) > 1:
tree = [sha256((tree[i] + tree[i+1]).encode()) for i in range(0, len(tree)-1, 2)] if len(tree) % 2 == 0 else [sha256((tree[i] + tree[i+1]).encode()) for i in range(0, len(tree)-1, 2)] + [tree[-1]]
return {
"root": tree[0] if tree else sha256(b""),
"leaf_count": len(leaf_hashes),
"leaf_hashes": leaf_hashes[:8],
}
def blur_hash64(data: bytes) -> str:
"""BlurHash64-style fidelity label β non-reversible representation of content."""
h = hashlib.sha512(data).digest()
return base64.b64encode(h[:48]).decode()[:64]
def antonymify(content: str, filename: str = "") -> dict:
"""Generate non-consumable surrogate from file content.
Exposes verifiable boundaries without revealing consumable content."""
data = content.encode() if isinstance(content, str) else content
merkle = merkle_commitment(data)
blur = blur_hash64(data)
file_class = "unknown"
if filename.endswith(".py") or "def " in content[:500]:
file_class = "python_source"
elif filename.endswith(".sql") or "SELECT" in content[:500].upper():
file_class = "sql_query"
elif filename.endswith(".json"):
file_class = "json_data"
elif filename.endswith(".md"):
file_class = "markdown_document"
elif filename.endswith(".csv"):
file_class = "csv_dataset"
else:
file_class = "text"
lines = content.split("\n") if isinstance(content, str) else data.split(b"\n")
line_count = len(lines)
proof_hooks = []
if isinstance(content, str):
for kw in ["def ", "class ", "SELECT", "CREATE TABLE", "import ", "function ", "return "]:
if kw in content:
proof_hooks.append(kw.strip())
excluded = ["full_source", "exact_algorithm", "alpha_signal", "raw_data_rows"]
return {
"file_class": file_class,
"filename": filename,
"size_bytes": len(data),
"line_count": line_count,
"merkle_root": merkle["root"],
"merkle_leaves": merkle["leaf_count"],
"blur_hash64": blur,
"proof_hooks": proof_hooks,
"excluded_content": excluded,
"fidelity_label": "controlled_blur",
"lambda_score": round(min(1.0, len(data) / 100000), 4),
"preview": content[:200] + "..." if len(content) > 200 else content,
}
def encrypt_answer(answer: str, claim_id: str) -> str:
"""Simple XOR cipher with claim_id as key. Real impl would use proper crypto."""
key = (claim_id * 10).encode()[:32]
data = answer.encode()
return base64.b64encode(bytes(b ^ key[i % len(key)] for i, b in enumerate(data))).decode()
def decrypt_answer(encrypted: str, claim_id: str) -> str:
key = (claim_id * 10).encode()[:32]
data = base64.b64decode(encrypted)
return bytes(b ^ key[i % len(key)] for i, b in enumerate(data)).decode()
# --- Endpoints ---
@app.get("/health")
async def health():
conn = sqlite3.connect(str(DB_PATH))
claim_count = conn.execute("SELECT COUNT(*) FROM claims").fetchone()[0]
open_count = conn.execute("SELECT COUNT(*) FROM claims WHERE status='open'").fetchone()[0]
settled_count = conn.execute("SELECT COUNT(*) FROM claims WHERE status='settled'").fetchone()[0]
conn.close()
return {
"status": "ok",
"protocol": "AFC/1.0",
"claims_total": claim_count,
"claims_open": open_count,
"claims_settled": settled_count,
"storage": str(DATA_DIR),
"persistent": str(DATA_DIR).startswith("/data"),
}
@app.post("/claim/create")
async def create_claim(body: dict = Body(...)):
"""Seller creates a claim with antonymified preview + encrypted full answer + bond."""
seller_id = body.get("seller_id", "")
task_description = body.get("task_description", "")
full_answer = body.get("full_answer", "")
filename = body.get("filename", "")
bond_amount = body.get("bond_amount", 0)
oracle_type = body.get("oracle_type", "hidden_test")
oracle_config = body.get("oracle_config", {})
exclusivity_window_s = body.get("exclusivity_window_s", 3600)
if not full_answer:
raise HTTPException(400, {"status": "error", "message": "full_answer required"})
if not seller_id:
raise HTTPException(400, {"status": "error", "message": "seller_id required"})
claim_id = uuid.uuid4().hex[:12]
surrogate = antonymify(full_answer, filename)
answer_hash = sha256(full_answer.encode())
encrypted = encrypt_answer(full_answer, claim_id)
conn = sqlite3.connect(str(DB_PATH))
conn.execute(
"""INSERT INTO claims (claim_id, seller_id, task_description, antonymified_preview,
full_answer_hash, full_answer_encrypted, bond_amount, bond_posted,
oracle_type, oracle_config, lambda_score, fidelity_label,
exclusivity_window_s, exclusivity_expires, status, created_at)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
(claim_id, seller_id, task_description, json.dumps(surrogate),
answer_hash, encrypted, bond_amount, 1 if bond_amount > 0 else 0,
oracle_type, json.dumps(oracle_config), surrogate["lambda_score"],
surrogate["fidelity_label"], exclusivity_window_s,
time.time() + exclusivity_window_s, "open", time.time())
)
conn.commit()
conn.close()
return {
"claim_id": claim_id,
"status": "open",
"surrogate": surrogate,
"answer_hash": answer_hash,
"bond_posted": bond_amount > 0,
"protocol": "AFC/1.0",
"law": "No full disclosure before payment. No payment without settlement.",
}
@app.get("/claim/{claim_id}")
async def get_claim(claim_id: str):
"""View a claim's antonymified surrogate β no full answer revealed."""
conn = sqlite3.connect(str(DB_PATH))
conn.row_factory = sqlite3.Row
row = conn.execute("SELECT * FROM claims WHERE claim_id = ?", [claim_id]).fetchone()
conn.close()
if not row:
raise HTTPException(404, {
"status": "claim_not_found",
"claim_id": claim_id,
"message": "This AFC claim is expired, settled, or does not exist.",
"next": "Create a new claim."
})
surrogate = json.loads(row["antonymified_preview"]) if row["antonymified_preview"] else {}
return {
"claim_id": claim_id,
"seller_id": row["seller_id"],
"task_description": row["task_description"],
"surrogate": surrogate,
"answer_hash": row["full_answer_hash"],
"bond_amount": row["bond_amount"],
"bond_posted": bool(row["bond_posted"]),
"payment_escrowed": row["payment_escrowed"],
"oracle_type": row["oracle_type"],
"lambda_score": row["lambda_score"],
"fidelity_label": row["fidelity_label"],
"exclusivity_expires": row["exclusivity_expires"],
"status": row["status"],
"created_at": row["created_at"],
"committed_at": row["committed_at"],
"settled_at": row["settled_at"],
"settlement_result": row["settlement_result"],
"note": "Full answer is encrypted and hidden. This surrogate is non-consumable.",
}
@app.get("/claims")
async def list_claims(status: str = "open"):
conn = sqlite3.connect(str(DB_PATH))
conn.row_factory = sqlite3.Row
if status == "all":
rows = conn.execute("SELECT claim_id, seller_id, task_description, status, lambda_score, bond_amount, created_at FROM claims ORDER BY created_at DESC").fetchall()
else:
rows = conn.execute("SELECT claim_id, seller_id, task_description, status, lambda_score, bond_amount, created_at FROM claims WHERE status = ? ORDER BY created_at DESC", [status]).fetchall()
conn.close()
return {"claims": [dict(r) for r in rows], "count": len(rows)}
@app.post("/claim/{claim_id}/escrow")
async def escrow_payment(claim_id: str, body: dict = Body(...)):
"""Buyer escrows payment β commits to purchase without seeing full answer."""
buyer_id = body.get("buyer_id", "")
amount = body.get("amount", 0)
if not buyer_id:
raise HTTPException(400, {"status": "error", "message": "buyer_id required"})
conn = sqlite3.connect(str(DB_PATH))
conn.row_factory = sqlite3.Row
row = conn.execute("SELECT * FROM claims WHERE claim_id = ?", [claim_id]).fetchone()
if not row:
conn.close()
raise HTTPException(404, {"status": "claim_not_found", "message": "Claim not found."})
if row["status"] != "open":
conn.close()
raise HTTPException(400, {"status": "error", "message": f"Claim is {row['status']}, not open."})
conn.execute(
"UPDATE claims SET payment_escrowed = ?, payment_escrowed_by = ?, status = 'committed', committed_at = ? WHERE claim_id = ?",
[amount, buyer_id, time.time(), claim_id]
)
conn.commit()
conn.close()
return {
"claim_id": claim_id,
"status": "committed",
"escrow_amount": amount,
"buyer_id": buyer_id,
"law": "No payment without settlement. No settlement without an oracle.",
}
@app.post("/claim/{claim_id}/reveal")
async def reveal_answer(claim_id: str):
"""Reveal the full answer after payment escrow. Answer is decrypted."""
conn = sqlite3.connect(str(DB_PATH))
conn.row_factory = sqlite3.Row
row = conn.execute("SELECT * FROM claims WHERE claim_id = ?", [claim_id]).fetchone()
if not row:
conn.close()
raise HTTPException(404, {"status": "claim_not_found", "message": "Claim not found."})
if row["status"] != "committed":
conn.close()
raise HTTPException(400, {"status": "error", "message": f"Claim must be committed (escrowed) first. Current: {row['status']}"})
answer = decrypt_answer(row["full_answer_encrypted"], claim_id)
conn.execute("UPDATE claims SET status = 'revealed', revealed_at = ? WHERE claim_id = ?", [time.time(), claim_id])
conn.commit()
conn.close()
return {
"claim_id": claim_id,
"status": "revealed",
"full_answer": answer,
"answer_hash": row["full_answer_hash"],
"verify": sha256(answer.encode()) == row["full_answer_hash"],
"law": "Answer revealed. Oracle settlement pending.",
}
@app.post("/claim/{claim_id}/tests")
async def submit_hidden_tests(claim_id: str, body: dict = Body(...)):
"""Buyer submits hidden tests for the claim (encrypted, hashed)."""
tests = body.get("tests", [])
if not tests:
raise HTTPException(400, {"status": "error", "message": "tests array required"})
conn = sqlite3.connect(str(DB_PATH))
conn.row_factory = sqlite3.Row
row = conn.execute("SELECT * FROM claims WHERE claim_id = ?", [claim_id]).fetchone()
if not row:
conn.close()
raise HTTPException(404, {"status": "claim_not_found", "message": "Claim not found."})
for i, test in enumerate(tests):
test_id = uuid.uuid4().hex[:12]
test_str = json.dumps(test) if not isinstance(test, str) else test
test_hash = sha256(test_str.encode())
test_encrypted = encrypt_answer(test_str, claim_id)
conn.execute(
"INSERT INTO hidden_tests (test_id, claim_id, test_hash, test_encrypted, created_at) VALUES (?,?,?,?,?)",
[test_id, claim_id, test_hash, test_encrypted, time.time()]
)
conn.commit()
conn.close()
return {
"claim_id": claim_id,
"tests_submitted": len(tests),
"status": "tests_ready",
"law": "Oracle will resolve pass/fail on settlement.",
}
@app.post("/claim/{claim_id}/settle")
async def settle_claim(claim_id: str, body: dict = Body(...)):
"""Oracle settles the claim β runs hidden tests against the revealed answer.
Bond returned if pass, slashed if fail. Settlement receipt generated."""
conn = sqlite3.connect(str(DB_PATH))
conn.row_factory = sqlite3.Row
row = conn.execute("SELECT * FROM claims WHERE claim_id = ?", [claim_id]).fetchone()
if not row:
conn.close()
raise HTTPException(404, {"status": "claim_not_found", "message": "Claim not found."})
if row["status"] not in ("revealed", "committed"):
conn.close()
raise HTTPException(400, {"status": "error", "message": f"Claim must be revealed first. Current: {row['status']}"})
answer = decrypt_answer(row["full_answer_encrypted"], claim_id)
tests = conn.execute("SELECT * FROM hidden_tests WHERE claim_id = ?", [claim_id]).fetchall()
oracle_type = row["oracle_type"] or "hidden_test"
oracle_config = json.loads(row["oracle_config"]) if row["oracle_config"] else {}
passed = 0
failed = 0
details = []
if oracle_type == "hidden_test" and tests:
for t in tests:
test_str = decrypt_answer(t["test_encrypted"], claim_id)
test_obj = json.loads(test_str)
test_type = test_obj.get("type", "contains")
expected = test_obj.get("expected", "")
if test_type == "contains":
ok = expected in answer
elif test_type == "equals":
ok = answer.strip() == expected.strip()
elif test_type == "regex":
import re
ok = bool(re.search(expected, answer))
elif test_type == "starts_with":
ok = answer.strip().startswith(expected)
elif test_type == "json_field":
try:
ans_json = json.loads(answer)
ok = str(ans_json.get(test_obj.get("field", ""), "")) == str(expected)
except:
ok = False
elif test_type == "python_exec":
try:
ns = {}
exec(answer, ns)
exec(test_obj.get("code", "result = True"), ns)
ok = ns.get("result", False)
except:
ok = False
else:
ok = expected in answer
if ok:
passed += 1
details.append({"test_id": t["test_id"], "result": "pass"})
else:
failed += 1
details.append({"test_id": t["test_id"], "result": "fail"})
elif oracle_type == "manual":
passed = body.get("oracle_verdict", False)
failed = 0 if passed else 1
else:
passed = 1
failed = 0
details.append({"result": "no_tests", "note": "No hidden tests submitted. Default pass."})
all_passed = failed == 0 and passed > 0
settlement_result = "pass" if all_passed else "fail"
bond_returned = row["bond_amount"] if all_passed else 0
bond_slashed = row["bond_amount"] if not all_passed else 0
payment_released = row["payment_escrowed"] if all_passed else 0
receipt_id = uuid.uuid4().hex[:12]
receipt = {
"receipt_id": receipt_id,
"claim_id": claim_id,
"seller_id": row["seller_id"],
"buyer_id": row["payment_escrowed_by"],
"result": settlement_result,
"tests_passed": passed,
"tests_failed": failed,
"bond_amount": row["bond_amount"],
"bond_returned": bond_returned,
"bond_slashed": bond_slashed,
"payment_escrowed": row["payment_escrowed"],
"payment_released": payment_released,
"answer_hash": row["full_answer_hash"],
"oracle_type": oracle_type,
"settled_at": time.time(),
"protocol": "AFC/1.0",
"law_verified": "No full disclosure before payment. No payment without settlement. No settlement without an oracle. No oracle without a bond.",
}
conn.execute(
"UPDATE claims SET status = 'settled', settled_at = ?, settlement_result = ?, settlement_receipt = ? WHERE claim_id = ?",
[time.time(), settlement_result, json.dumps(receipt), claim_id]
)
conn.execute(
"INSERT INTO oracle_results (result_id, claim_id, oracle_type, passed, details, resolved_at) VALUES (?,?,?,?,?,?)",
[uuid.uuid4().hex[:12], claim_id, oracle_type, 1 if all_passed else 0, json.dumps(details), time.time()]
)
conn.execute(
"INSERT INTO receipts (receipt_id, claim_id, type, payload, created_at) VALUES (?,?,?,?,?)",
[receipt_id, claim_id, "settlement", json.dumps(receipt), time.time()]
)
conn.commit()
conn.close()
return receipt
@app.get("/claim/{claim_id}/receipt")
async def get_receipt(claim_id: str):
"""Get the settlement receipt for a claim."""
conn = sqlite3.connect(str(DB_PATH))
conn.row_factory = sqlite3.Row
row = conn.execute("SELECT settlement_receipt FROM claims WHERE claim_id = ?", [claim_id]).fetchone()
conn.close()
if not row or not row["settlement_receipt"]:
raise HTTPException(404, {"status": "no_receipt", "message": "No settlement receipt. Claim may not be settled yet."})
return json.loads(row["settlement_receipt"])
@app.get("/protocol")
async def protocol_manifest():
"""AFC Protocol manifest β describes the full stack."""
return {
"protocol": "AFC/1.0",
"name": "Antonymified File Claim Protocol",
"thesis": "We do not sell answers. We sell bonded answer-claims whose value can be priced through controlled blur and settled through an oracle.",
"law": [
"No full disclosure before payment.",
"No payment without settlement.",
"No settlement without an oracle.",
"No oracle without a bond.",
],
"stack": [
{"layer": 1, "name": "source_object", "description": "File or answer β the thing being claimed"},
{"layer": 2, "name": "antonymifier", "description": "LLM-generated non-consumable surrogate"},
{"layer": 3, "name": "merkle_commitment", "description": "Cryptographic identity and integrity"},
{"layer": 4, "name": "blur_hash64", "description": "Fidelity label β disclosure level"},
{"layer": 5, "name": "lambda_score", "description": "Transferability / usability friction"},
{"layer": 6, "name": "oracle", "description": "Truth-resolution mechanism"},
{"layer": 7, "name": "bond", "description": "Economic accountability"},
{"layer": 8, "name": "exclusivity_window", "description": "Urgency / first-mover value"},
{"layer": 9, "name": "settlement_receipt", "description": "Final proof of outcome"},
],
"market_unit": "bonded, partially disclosed, oracle-settled claim about an answer",
"endpoints": {
"create_claim": "POST /claim/create",
"view_claim": "GET /claim/{id}",
"list_claims": "GET /claims?status=open",
"escrow_payment": "POST /claim/{id}/escrow",
"reveal_answer": "POST /claim/{id}/reveal",
"submit_tests": "POST /claim/{id}/tests",
"settle": "POST /claim/{id}/settle",
"receipt": "GET /claim/{id}/receipt",
"protocol": "GET /protocol",
"health": "GET /health",
},
"oracle_types": ["hidden_test", "manual", "on_chain_event", "expert_arbitration"],
"test_types": ["contains", "equals", "regex", "starts_with", "json_field", "python_exec"],
}
LANDING_HTML = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AFC Protocol β Antonymified File Claim Protocol</title>
<style>
:root{--bg:#09090B;--surface:#111114;--primary:#4F7CFF;--accent:#8B5CFF;--glass:rgba(255,255,255,0.03);--border:rgba(255,255,255,0.08);--text:#FAFAFA;--dim:rgba(250,250,250,0.5);--faint:rgba(250,250,250,0.2);--success:#22C55E;--danger:#EF4444;--r:20px;--rs:12px}
*{margin:0;padding:0;box-sizing:border-box}
body{font-family:Inter,-apple-system,system-ui,sans-serif;background:var(--bg);color:var(--text);min-height:100vh;-webkit-font-smoothing:antialiased;overflow-x:hidden}
.glow{position:fixed;top:-200px;left:50%;transform:translateX(-50%);width:800px;height:400px;background:radial-gradient(ellipse,rgba(79,124,255,0.06) 0%,transparent 70%);pointer-events:none;z-index:0}
.wrap{max-width:760px;margin:0 auto;padding:0 24px;position:relative;z-index:1}
.hero{text-align:center;padding:100px 0 60px}
.hero h1{font-size:2.8rem;font-weight:800;letter-spacing:-0.04em;background:linear-gradient(135deg,var(--primary),var(--accent));-webkit-background-clip:text;-webkit-text-fill-color:transparent;margin-bottom:16px;line-height:1}
.hero .sub{font-size:1rem;color:var(--dim);margin-bottom:8px}
.hero .thesis{font-size:1.2rem;color:var(--text);font-weight:600;max-width:560px;margin:0 auto 32px;line-height:1.5}
.btn{padding:12px 28px;border-radius:100px;font-size:0.88rem;font-weight:600;border:none;cursor:pointer;transition:all 0.2s;font-family:inherit}
.btn-p{background:var(--primary);color:#fff}.btn-p:hover{background:#6B91FF;box-shadow:0 0 30px rgba(79,124,255,0.3)}
.btn-g{background:var(--glass);color:var(--text);border:1px solid var(--border)}.btn-g:hover{background:rgba(255,255,255,0.06)}
.btn-s{background:var(--success);color:#fff}.btn-s:hover{opacity:0.9}
.btn-d{background:var(--danger);color:#fff}.btn-d:hover{opacity:0.9}
.btn-sm{padding:6px 14px;font-size:0.75rem;border-radius:8px}
.hero-btns{display:flex;gap:10px;justify-content:center;flex-wrap:wrap}
.panel{background:var(--glass);backdrop-filter:blur(20px);border:1px solid var(--border);border-radius:var(--r);padding:24px;margin-bottom:14px}
.pt{font-size:0.75rem;font-weight:600;text-transform:uppercase;letter-spacing:0.08em;color:var(--dim);margin-bottom:16px}
.law{font-family:SF Mono,monospace;font-size:0.78rem;color:var(--text-dim);line-height:1.8;padding:12px 16px;background:var(--surface);border-radius:var(--rs);margin-bottom:16px}
.law div{color:var(--text)}
.law .num{color:var(--primary);margin-right:8px}
.field{margin-bottom:12px}
.field label{display:block;font-size:0.78rem;color:var(--dim);margin-bottom:4px}
.field input,.field textarea,.field select{width:100%;padding:10px 12px;border-radius:var(--rs);background:var(--surface);border:1px solid var(--border);color:var(--text);font-size:0.85rem;font-family:inherit;outline:none}
.field input:focus,.field textarea:focus{border-color:var(--primary)}
.field textarea{min-height:80px;resize:vertical;font-family:SF Mono,monospace;font-size:0.78rem}
.claim-card{background:var(--surface);border-radius:var(--rs);padding:16px;margin-bottom:10px;cursor:pointer;transition:all 0.15s}
.claim-card:hover{background:rgba(255,255,255,0.04)}
.claim-id{font-family:SF Mono,monospace;font-size:0.72rem;color:var(--faint)}
.claim-task{font-size:0.85rem;color:var(--text);margin:4px 0}
.claim-meta{display:flex;gap:12px;font-size:0.72rem;color:var(--dim)}
.badge{padding:2px 8px;border-radius:4px;font-size:0.68rem;font-weight:600}
.badge-open{background:rgba(79,124,255,0.15);color:var(--primary)}
.badge-committed{background:rgba(255,200,0,0.15);color:#FFC800}
.badge-revealed{background:rgba(139,92,255,0.15);color:var(--accent)}
.badge-settled-pass{background:rgba(34,197,94,0.15);color:var(--success)}
.badge-settled-fail{background:rgba(239,68,68,0.15);color:var(--danger)}
.surrogate{font-family:SF Mono,monospace;font-size:0.75rem;line-height:1.6}
.surrogate .key{color:var(--dim)}
.surrogate .val{color:var(--text)}
.stack-item{display:flex;align-items:center;gap:10px;padding:8px 0;border-bottom:1px solid rgba(255,255,255,0.04)}
.stack-item:last-child{border-bottom:none}
.stack-num{width:24px;height:24px;border-radius:50%;background:var(--primary);color:#fff;font-size:0.7rem;font-weight:700;display:flex;align-items:center;justify-content:center;flex-shrink:0}
.stack-name{font-size:0.82rem;color:var(--text);font-weight:500}
.stack-desc{font-size:0.72rem;color:var(--dim)}
.result-box{padding:16px;border-radius:var(--rs);font-family:SF Mono,monospace;font-size:0.78rem;line-height:1.6;white-space:pre-wrap;word-break:break-all}
.result-pass{background:rgba(34,197,94,0.08);border:1px solid rgba(34,197,94,0.2);color:var(--success)}
.result-fail{background:rgba(239,68,68,0.08);border:1px solid rgba(239,68,68,0.2);color:var(--danger)}
.result-info{background:var(--surface);border:1px solid var(--border);color:var(--text)}
.hidden{display:none}
.step{display:flex;gap:12px;padding:10px 0}
.step-n{width:28px;height:28px;border-radius:50%;background:var(--accent);color:#fff;font-size:0.75rem;font-weight:700;display:flex;align-items:center;justify-content:center;flex-shrink:0}
.step-t{font-size:0.82rem;color:var(--text);line-height:1.5}
.footer{text-align:center;padding:40px 0;font-size:0.72rem;color:var(--faint)}
</style>
</head>
<body>
<div class="glow"></div>
<div class="wrap">
<div class="hero">
<h1>AFC Protocol</h1>
<p class="sub">Antonymified File Claim Protocol</p>
<p class="thesis">We do not sell answers. We sell bonded answer-claims whose value can be priced through controlled blur and settled through an oracle.</p>
<div class="hero-btns">
<button class="btn btn-p" onclick="document.getElementById('create').scrollIntoView()">Create Claim</button>
<button class="btn btn-g" onclick="document.getElementById('claims').scrollIntoView()">Browse Claims</button>
<button class="btn btn-g" onclick="document.getElementById('protocol').scrollIntoView()">Protocol</button>
</div>
</div>
<div class="panel">
<div class="pt">Protocol Law</div>
<div class="law">
<div><span class="num">1.</span>No full disclosure before payment.</div>
<div><span class="num">2.</span>No payment without settlement.</div>
<div><span class="num">3.</span>No settlement without an oracle.</div>
<div><span class="num">4.</span>No oracle without a bond.</div>
</div>
</div>
<div class="panel" id="create">
<div class="pt">Create Claim (Seller)</div>
<div class="field"><label>Seller ID</label><input id="s-seller" placeholder="seller_001"></div>
<div class="field"><label>Task Description</label><input id="s-task" placeholder="Python function that reverses a linked list"></div>
<div class="field"><label>Full Answer (hidden until escrow)</label><textarea id="s-answer" placeholder="def reverse_ll(head):..."></div>
<div class="field"><label>Filename (optional)</label><input id="s-fname" placeholder="solution.py"></div>
<div class="field"><label>Bond Amount</label><input id="s-bond" type="number" value="100" step="any"></div>
<div class="field"><label>Oracle Type</label>
<select id="s-oracle"><option value="hidden_test">hidden_test</option><option value="manual">manual</option></select>
</div>
<div class="field"><label>Exclusivity Window (seconds)</label><input id="s-excl" type="number" value="3600"></div>
<button class="btn btn-p" onclick="createClaim()">Create Claim</button>
<div id="create-result" class="hidden" style="margin-top:14px"></div>
</div>
<div class="panel" id="claims">
<div class="pt">Open Claims <button class="btn btn-g btn-sm" style="float:right" onclick="loadClaims()">Refresh</button></div>
<div id="claims-list"><p style="color:var(--faint);font-size:0.82rem;text-align:center;padding:20px">Loading...</p></div>
</div>
<div class="panel" id="claim-detail" style="display:none">
<div class="pt">Claim Detail <button class="btn btn-g btn-sm" style="float:right" onclick="closeDetail()">Close</button></div>
<div id="detail-content"></div>
</div>
<div class="panel" id="protocol">
<div class="pt">Protocol Stack</div>
<div id="stack-list"></div>
</div>
<div class="panel">
<div class="pt">MVP: Hidden Test Claim Market</div>
<div class="step"><div class="step-n">1</div><div class="step-t">Buyer posts task + hidden tests</div></div>
<div class="step"><div class="step-n">2</div><div class="step-t">Seller submits antonymified preview (not full answer)</div></div>
<div class="step"><div class="step-n">3</div><div class="step-t">Seller posts bond</div></div>
<div class="step"><div class="step-n">4</div><div class="step-t">Buyer escrows payment</div></div>
<div class="step"><div class="step-n">5</div><div class="step-t">Answer revealed only after commitment</div></div>
<div class="step"><div class="step-n">6</div><div class="step-t">Hidden test suite resolves pass/fail</div></div>
<div class="step"><div class="step-n">7</div><div class="step-t">Bond returned or slashed</div></div>
<div class="step"><div class="step-n">8</div><div class="step-t">Settlement receipt generated</div></div>
</div>
<div class="footer">AFC Protocol v1.0 β Antonymified File Claim Protocol β Verifiable Non-Seeing</div>
</div>
<script>
const API='';
async function api(path,opts){const r=await fetch(API+path,opts);return r.json()}
async function createClaim(){
const body={
seller_id:document.getElementById('s-seller').value||'seller_001',
task_description:document.getElementById('s-task').value,
full_answer:document.getElementById('s-answer').value,
filename:document.getElementById('s-fname').value,
bond_amount:parseFloat(document.getElementById('s-bond').value)||0,
oracle_type:document.getElementById('s-oracle').value,
exclusivity_window_s:parseInt(document.getElementById('s-excl').value)||3600,
};
if(!body.full_answer){alert('Full answer required');return}
const d=await api('/claim/create',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)});
const el=document.getElementById('create-result');
el.classList.remove('hidden');
if(d.claim_id){
el.innerHTML='<div class="result-box result-info">Claim created: '+d.claim_id+'\\n\\nSurrogate:\\n file_class: '+d.surrogate.file_class+'\\n merkle_root: '+d.surrogate.merkle_root.slice(0,20)+'...\\n blur_hash64: '+d.surrogate.blur_hash64.slice(0,20)+'...\\n lambda: '+d.surrogate.lambda_score+'\\n proof_hooks: '+d.surrogate.proof_hooks.join(', ')+'\\n bond_posted: '+d.bond_posted+'\\n\\nFull answer is encrypted and hidden.\\nShare /claim/'+d.claim_id+' with buyers.</div>';
loadClaims();
} else {
el.innerHTML='<div class="result-box result-fail">'+JSON.stringify(d,null,2)+'</div>';
}
}
async function loadClaims(){
const d=await api('/claims?status=open');
const el=document.getElementById('claims-list');
if(!d.claims||d.claims.length===0){el.innerHTML='<p style="color:var(--faint);font-size:0.82rem;text-align:center;padding:20px">No open claims. Create one above.</p>';return}
el.innerHTML=d.claims.map(c=>'<div class="claim-card" onclick="viewClaim(\\''+c.claim_id+'\\')"><div class="claim-id">'+c.claim_id+'</div><div class="claim-task">'+(c.task_description||'No description')+'</div><div class="claim-meta"><span class="badge badge-open">open</span><span>Ξ»='+c.lambda_score+'</span><span>bond='+c.bond_amount+'</span></div></div>').join('');
}
async function viewClaim(id){
const d=await api('/claim/'+id);
const el=document.getElementById('claim-detail');
const c=document.getElementById('detail-content');
el.style.display='block';
const s=d.surrogate||{};
let html='<div class="surrogate">';
html+='<div><span class="key">claim_id:</span> <span class="val">'+d.claim_id+'</span></div>';
html+='<div><span class="key">task:</span> <span class="val">'+d.task_description+'</span></div>';
html+='<div><span class="key">file_class:</span> <span class="val">'+s.file_class+'</span></div>';
html+='<div><span class="key">merkle_root:</span> <span class="val">'+s.merkle_root+'</span></div>';
html+='<div><span class="key">blur_hash64:</span> <span class="val">'+s.blur_hash64+'</span></div>';
html+='<div><span class="key">lambda:</span> <span class="val">'+d.lambda_score+'</span></div>';
html+='<div><span class="key">fidelity:</span> <span class="val">'+d.fidelity_label+'</span></div>';
html+='<div><span class="key">proof_hooks:</span> <span class="val">'+(s.proof_hooks||[]).join(', ')+'</span></div>';
html+='<div><span class="key">excluded:</span> <span class="val">'+(s.excluded_content||[]).join(', ')+'</span></div>';
html+='<div><span class="key">bond:</span> <span class="val">'+d.bond_amount+' (posted: '+d.bond_posted+')</span></div>';
html+='<div><span class="key">answer_hash:</span> <span class="val">'+d.answer_hash.slice(0,20)+'...</span></div>';
html+='</div>';
html+='<div style="margin-top:12px;font-size:0.72rem;color:var(--faint)">Full answer is encrypted. Preview only β non-consumable.</div>';
if(d.status==='open'){
html+='<div style="margin-top:16px"><div class="field"><label>Buyer ID</label><input id="b-buyer" placeholder="buyer_001"></div>';
html+='<div class="field"><label>Escrow Amount</label><input id="b-amount" type="number" value="'+d.bond_amount+'" step="any"></div>';
html+='<button class="btn btn-p" onclick="escrowClaim(\\''+id+'\\')">Escrow Payment</button></div>';
} else if(d.status==='committed'){
html+='<div style="margin-top:16px"><button class="btn btn-p" onclick="revealClaim(\\''+id+'\\')">Reveal Answer</button></div>';
} else if(d.status==='revealed'){
html+='<div style="margin-top:16px"><div class="field"><label>Hidden Tests (JSON array)</label><textarea id="b-tests" placeholder=\\'[{"type":"contains","expected":"def"}]\\'></textarea></div>';
html+='<button class="btn btn-s" onclick="submitTests(\\''+id+'\\')">Submit Tests</button> ';
html+='<button class="btn btn-p" onclick="settleClaim(\\''+id+'\\')">Settle</button></div>';
} else if(d.status==='settled'){
html+='<div style="margin-top:14px"><button class="btn btn-g" onclick="getReceipt(\\''+id+'\\')">View Receipt</button></div>';
}
c.innerHTML=html;
}
async function escrowClaim(id){
const body={buyer_id:document.getElementById('b-buyer').value||'buyer_001',amount:parseFloat(document.getElementById('b-amount').value)||0};
const d=await api('/claim/'+id+'/escrow',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)});
if(d.status==='committed'){alert('Payment escrowed. Claim committed. You can now reveal the answer.');viewClaim(id)}else{alert(JSON.stringify(d))}
}
async function revealClaim(id){
const d=await api('/claim/'+id+'/reveal',{method:'POST',headers:{'Content-Type':'application/json'},body:'{}'});
if(d.status==='revealed'){
const el=document.getElementById('detail-content');
el.innerHTML+='<div class="result-box result-info" style="margin-top:14px"><b>Revealed Answer:</b>\\n\\n'+d.full_answer+'\\n\\nHash verified: '+d.verify+'</div>';
el.innerHTML+='<div style="margin-top:12px"><div class="field"><label>Hidden Tests (JSON array)</label><textarea id="b-tests" placeholder=\\'[{"type":"contains","expected":"def"}]\\'></textarea></div>';
el.innerHTML+='<button class="btn btn-s" onclick="submitTests(\\''+id+'\\')">Submit Tests</button> <button class="btn btn-p" onclick="settleClaim(\\''+id+'\\')">Settle</button></div>';
} else {alert(JSON.stringify(d))}
}
async function submitTests(id){
const t=document.getElementById('b-tests').value;
let tests;try{tests=JSON.parse(t)}catch{alert('Invalid JSON');return}
const d=await api('/claim/'+id+'/tests',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({tests})});
if(d.tests_submitted){alert(d.tests_submitted+' tests submitted. Ready to settle.')}else{alert(JSON.stringify(d))}
}
async function settleClaim(id){
const d=await api('/claim/'+id+'/settle',{method:'POST',headers:{'Content-Type':'application/json'},body:'{}'});
const el=document.getElementById('detail-content');
if(d.result==='pass'){
el.innerHTML+='<div class="result-box result-pass" style="margin-top:14px"><b>SETTLED: PASS</b>\\n\\nTests: '+d.tests_passed+' passed, '+d.tests_failed+' failed\\nBond returned: '+d.bond_returned+'\\nPayment released: '+d.payment_released+'\\n\\nReceipt ID: '+d.receipt_id+'</div>';
} else {
el.innerHTML+='<div class="result-box result-fail" style="margin-top:14px"><b>SETTLED: FAIL</b>\\n\\nTests: '+d.tests_passed+' passed, '+d.tests_failed+' failed\\nBond slashed: '+d.bond_slashed+'\\nPayment returned to buyer\\n\\nReceipt ID: '+d.receipt_id+'</div>';
}
}
async function getReceipt(id){
const d=await api('/claim/'+id+'/receipt');
const el=document.getElementById('detail-content');
el.innerHTML+='<div class="result-box result-info" style="margin-top:14px">'+JSON.stringify(d,null,2)+'</div>';
}
function closeDetail(){document.getElementById('claim-detail').style.display='none'}
async function loadProtocol(){
const d=await api('/protocol');
const el=document.getElementById('stack-list');
el.innerHTML=d.stack.map(s=>'<div class="stack-item"><div class="stack-num">'+s.layer+'</div><div><div class="stack-name">'+s.name+'</div><div class="stack-desc">'+s.description+'</div></div></div>').join('');
}
loadClaims();
loadProtocol();
</script>
</body></html>"""
@app.get("/", response_class=HTMLResponse)
async def landing():
return LANDING_HTML
if __name__ == "__main__":
port = int(os.environ.get("PORT", 7860))
uvicorn.run(app, host="0.0.0.0", port=port)
|