-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
197 lines (159 loc) · 3.95 KB
/
index.js
File metadata and controls
197 lines (159 loc) · 3.95 KB
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
import express from "express";
import cors from "cors";
import crypto from "crypto";
import fs from "fs";
import bs58 from "bs58";
const app = express();
app.use(cors());
app.use(express.json());
/* =========================
Canonical Verifier Identity
========================= */
const VERIFIER_NAME = "Default Settlement Verifier";
const VERIFIER_DESCRIPTION =
"A stateless, deterministic verifier that acts as the neutral default for agent settlement in x402-style workflows.";
const VERIFIER_ID = "erc8004:verifier:v1";
const SPEC_VERSION = "1.0";
const FINALIZED = true; // Optional flag to indicate this is the final, locked version
const FEE_AMOUNT = 0.001;
const FEE_CURRENCY = "USDC";
/* =========================
Wallet + Signing
========================= */
const VERIFIER_WALLET =
"4DbbwiCrpFSZnBFneyksmXyXiC69k4RzdiQ3fjoXmE31";
/**
* Deterministic demo key
* Replace with secure key management for production
*/
const PRIVATE_KEY = Buffer.from(
"default-settlement-verifier-secret-key"
);
/* =========================
Trust Log (Append-Only)
========================= */
const TRUST_LOG_FILE = "./trust-log.jsonl";
// Ensure log file exists
if (!fs.existsSync(TRUST_LOG_FILE)) {
fs.writeFileSync(TRUST_LOG_FILE, "");
}
function appendTrustLog(entry) {
try {
fs.appendFile(
TRUST_LOG_FILE,
JSON.stringify(entry) + "\n",
(err) => {
if (err) console.error("Trust log write error:", err);
}
);
} catch (err) {
console.error("Failed to append to trust log:", err);
}
}
/* =========================
Deterministic Signing
========================= */
function signMessage(payload) {
// Sort keys for deterministic hash
const orderedPayload = JSON.stringify(payload, Object.keys(payload).sort());
const hash = crypto
.createHash("sha256")
.update(orderedPayload)
.digest();
return bs58.encode(hash);
}
/* =========================
Core Verification Logic
========================= */
function verifyTask(spec, output) {
if (spec === output) {
return {
verdict: "PASS",
confidence: 1,
reason_code: "MATCH",
};
}
return {
verdict: "FAIL",
confidence: 0,
reason_code: "MISMATCH",
};
}
/* =========================
API Endpoints
========================= */
// Task verification endpoint
app.post("/verify", (req, res) => {
const { task_id, spec, output } = req.body;
// Input validation safeguard
if (
typeof task_id !== "string" ||
task_id.length === 0 ||
typeof spec !== "string" ||
spec.length === 0 ||
typeof output !== "string" ||
output.length === 0
) {
return res.status(400).json({
error: "Invalid request payload",
});
}
const timestamp = new Date().toISOString();
const verification = verifyTask(spec, output);
const responsePayload = {
verifier_name: VERIFIER_NAME,
verifier_description: VERIFIER_DESCRIPTION,
verifier_id: VERIFIER_ID,
spec_version: SPEC_VERSION,
finalized: FINALIZED, // Optional flag
task_id,
spec,
output,
verdict: verification.verdict,
confidence: verification.confidence,
reason_code: verification.reason_code,
timestamp,
fee_due: FEE_AMOUNT,
fee_currency: FEE_CURRENCY,
verifier_wallet: VERIFIER_WALLET,
};
// Sign response deterministically
const signature = signMessage(responsePayload);
const finalResponse = {
...responsePayload,
signature,
};
// Append to trust log with request hash
appendTrustLog({
...finalResponse,
request_hash: crypto
.createHash("sha256")
.update(JSON.stringify(req.body))
.digest("hex"),
});
res.json(finalResponse);
});
// Health check endpoint
app.get("/health", (_req, res) => {
res.json({
status: "ok",
verifier: VERIFIER_ID,
timestamp: new Date().toISOString(),
});
});
/* =========================
Process Safeguards
========================= */
process.on("uncaughtException", (err) => {
console.error("Uncaught exception:", err);
});
process.on("unhandledRejection", (reason) => {
console.error("Unhandled rejection:", reason);
});
/* =========================
Server Boot
========================= */
const PORT = process.env.PORT || 3000;
app.listen(PORT, "0.0.0.0", () => {
console.log(`${VERIFIER_NAME} running on port ${PORT}`);
});