-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththoughtproof-verify-example.js
More file actions
209 lines (179 loc) · 6.75 KB
/
Copy paththoughtproof-verify-example.js
File metadata and controls
209 lines (179 loc) · 6.75 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
198
199
200
201
202
203
204
205
206
207
208
209
/**
* ThoughtProof Attestation Example
*
* Demonstrates how to:
* 1. Call the ThoughtProof /v1/check API
* 2. Receive a reasoning verification verdict (ALLOW/HOLD)
* 3. Verify the attestation signature against ThoughtProof's JWKS
*
* ThoughtProof verifies whether an AI agent's reasoning is sound
* before the agent acts — adversarial multi-model critique where
* independent models challenge each other's conclusions.
*
* JWKS: https://api.thoughtproof.ai/.well-known/jwks.json
* Algorithm: EdDSA (Ed25519)
* Key ID: tp-attestor-v1
*
* Usage:
* node thoughtproof-verify-example.js
*/
const crypto = require("crypto");
const https = require("https");
// --- Helpers ---
function fetchJSON(url) {
return new Promise((resolve, reject) => {
const req = https.get(url, (res) => {
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(new Error(`HTTP ${res.statusCode} from ${url}`));
}
let data = "";
res.on("data", (chunk) => (data += chunk));
res.on("end", () => resolve(JSON.parse(data)));
});
req.on("error", reject);
req.setTimeout(10000, () => {
req.destroy();
reject(new Error(`Timeout fetching ${url}`));
});
});
}
// --- ThoughtProof Attestation Format ---
/**
* Example attestation payload from ThoughtProof.
*
* In production, this comes from POST /v1/check after x402 payment.
* For this demo, we use a pre-signed example.
*/
const exampleAttestation = {
issuer: "https://api.thoughtproof.ai",
type: "reasoning_integrity",
kid: "tp-attestor-v1",
alg: "EdDSA",
jwks: "https://api.thoughtproof.ai/.well-known/jwks.json",
signed: {
verdict: "ALLOW",
confidence: 0.82,
mdi: 0.67,
claimHash: "sha256:a1b2c3d4e5f6",
domain: "financial",
stakeLevel: "medium",
timestamp: new Date().toISOString(),
},
// sig would be populated by the API — omitted in this demo
sig: null,
};
// --- Verify JWKS endpoint is reachable ---
async function verifyJWKS() {
console.log("ThoughtProof Attestation Verification Example\n");
console.log("1. Fetching JWKS from api.thoughtproof.ai...\n");
const jwks = await fetchJSON(
"https://api.thoughtproof.ai/.well-known/jwks.json"
);
const key = jwks.keys.find((k) => k.kid === "tp-attestor-v1");
if (!key) {
console.log(" ❌ Key tp-attestor-v1 not found in JWKS");
return;
}
console.log(" ✅ JWKS fetched successfully");
console.log(` Key ID: ${key.kid}`);
console.log(` Algorithm: ${key.alg}`);
console.log(` Curve: ${key.crv}`);
console.log(` Type: ${key.kty}\n`);
// Import the public key
const publicKey = crypto.createPublicKey({ key, format: "jwk" });
console.log(" ✅ Public key imported\n");
// --- Show attestation format ---
console.log("2. ThoughtProof Attestation Format:\n");
console.log(` Type: ${exampleAttestation.type}`);
console.log(` Verdict: ${exampleAttestation.signed.verdict}`);
console.log(` Confidence: ${exampleAttestation.signed.confidence}`);
console.log(` MDI (Model Diversity): ${exampleAttestation.signed.mdi}`);
console.log(` Domain: ${exampleAttestation.signed.domain}`);
console.log(` Stake Level: ${exampleAttestation.signed.stakeLevel}`);
console.log(` Claim Hash: ${exampleAttestation.signed.claimHash}`);
console.log(
` (non-deterministic by design — adversarial critique is probabilistic)\n`
);
// --- Show how it fits in multi-attestation array ---
console.log("3. In a multi-attestation payload:\n");
console.log(" {");
console.log(' "attestations": [');
console.log(
' { "type": "wallet_state", "issuer": "InsumerAPI", ... },'
);
console.log(
' { "type": "reasoning_integrity", "issuer": "ThoughtProof", "signed": { "verdict": "ALLOW", "confidence": 0.82, "mdi": 0.67 }, ... },'
);
console.log(
' { "type": "behavioral_trust", "issuer": "RNWY", ... },'
);
console.log(
' { "type": "job_performance", "issuer": "Maiat", ... }'
);
console.log(" ]");
console.log(" }\n");
console.log(" Relying party picks which dimensions to verify.");
console.log(
' A high-value trade might require all four. A simple transfer might only check wallet_state.\n'
);
// --- Integration ---
console.log("4. Integration:\n");
console.log(" API: POST https://api.thoughtproof.ai/v1/check");
console.log(' Payment: x402 (USDC on Base) or MPP (Tempo stablecoin)');
console.log(" JWKS: https://api.thoughtproof.ai/.well-known/jwks.json");
console.log(" Skill: https://thoughtproof.ai/skill.md");
console.log(
" Combined format: github.com/douglasborthwick-crypto/insumer-examples/issues/1\n"
);
console.log("Done ✅\n");
}
// --- Live wallet-bound verification (shipped 2026-04-11, no API key) ---
/**
* Recursive sorted-key compact JSON — matches Python
* json.dumps(obj, sort_keys=True, separators=(",", ":")), which is what
* ThoughtProof signs over for the wallet-indexed envelope.
*/
function canonicalJSON(obj) {
if (obj === null || typeof obj !== "object") return JSON.stringify(obj);
if (Array.isArray(obj)) return "[" + obj.map(canonicalJSON).join(",") + "]";
return (
"{" +
Object.keys(obj)
.sort()
.map((k) => JSON.stringify(k) + ":" + canonicalJSON(obj[k]))
.join(",") +
"}"
);
}
/**
* The reasoning_integrity signal is also available wallet-bound:
* GET /v1/issuer/wallet/{wallet} returns a wallet_reasoning_integrity/v1
* envelope with the wallet inside the signed bytes, signed with a detached
* EdDSA signature (no API key needed). This verifies it live.
*/
async function verifyWalletBound() {
console.log("5. Wallet-bound lookup (GET /v1/issuer/wallet/{wallet}, no key):\n");
const demoWallet = "0x0000000000000000000000000000000000001004";
const env = await fetchJSON(
"https://api.thoughtproof.ai/v1/issuer/wallet/" + demoWallet
);
console.log(` wallet: ${env.wallet}`);
console.log(` found: ${env.found}`);
console.log(` verdict: ${env.verdict}`);
console.log(` kid: ${env.signature.kid}\n`);
const jwks = await fetchJSON(
"https://api.thoughtproof.ai/.well-known/jwks.json"
);
const key = jwks.keys.find((k) => k.kid === env.signature.kid);
const publicKey = crypto.createPublicKey({ key, format: "jwk" });
// The detached signature covers the payload minus the `signature` object.
const { signature, ...payload } = env;
const message = Buffer.from(canonicalJSON(payload));
const sig = Buffer.from(signature.value, "base64url");
const ok = crypto.verify(null, message, publicKey, sig);
console.log(
` ${ok ? "✅" : "❌"} EdDSA signature ${ok ? "verified" : "INVALID"} — the wallet is in the signed bytes\n`
);
console.log("Done ✅");
}
verifyJWKS().then(verifyWalletBound).catch(console.error);