Repository navigation
Expand file tree
/
Copy pathnotary_worker.js
More file actions
267 lines (238 loc) · 9.96 KB
/
Copy pathnotary_worker.js
File metadata and controls
267 lines (238 loc) · 9.96 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
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
/**
* SELF-RADIANCE AGENTIC NOTARY v2.1 (ASSET & PRICE ENCEP COMPLIANCE)
*
* THE SOVEREIGN STANDARD:
* 1. IDEMPOTENT: Uses SHA-256 for deterministic, non-replicable Receipt IDs.
* 2. PROVENANCE: Includes the SHA-256 Fingerprint of the digital asset.
* 3. IDENTITY: Salted, hashed email to protect buyer privacy.
* 4. LIFECYCLE: Standardized 'kid' (Key ID) for rotating signatures.
* 5. CANONICAL: Recursive key-sorting serializer for deterministic verification signatures.
* 6. HARDENED: Rigorous parameter sanitization protecting against SSRF & path traversal.
* 7. PRICE-COMPLIANT: Enforces exact Price-to-Asset verification checks to prevent voucher recycling.
*/
// Authoritative Inventory Matrix (SHA-256 re-baselined 2026-06-11 against live specs/*.json; synchronized with x402-license-gateway assets.ts)
const ASSET_DIRECTORY = {
"vq00-zion-skank": {
price_usd: 1.00,
sha256: "578027543b0e645f663cf9a042b08c84db01e98c55df26e2fab08eb9cf7e56f7"
},
"vq01-restarules": {
price_usd: 1.00,
sha256: "4b355e67369c14365d10411439542d2178971df16a4b7d24b40c91c120d2846e"
},
"vq02-m2a-handshake": {
price_usd: 1.00,
sha256: "8989fb16726481527ff5587986d2885bff8974716201b31353489b855aefd396"
},
"vq03-trust-receipts": {
price_usd: 7.00,
sha256: "e858201865ef1f03f018b18ae9107b19a8bdff099cee6a290871972c2191e57a"
},
"vq04-rateguard": {
price_usd: 4.00,
sha256: "2b7bee8de92164552c002ad67e6230023d1e8affe7ac8a5d83668734e3a45a62"
},
"vq05-safecard": {
price_usd: 3.00,
sha256: "139299f626b00e5e593872bce21df0945286f8cd942eb36eb98d421fd5f0acf6"
},
"vq06-consent-block": {
price_usd: 5.00,
sha256: "eb1eb4a7abe417079105ab0a37e5244376cd80f7e958e20406c320bbcb37e0a3"
},
"vq07-balance-proof": {
price_usd: 4.00,
sha256: "09902631859daede8b6253a57fc5ce47f6aa101b69c25caa1cd81b38d5ca4115"
},
"vq08-asset-spec": {
price_usd: 2.00,
sha256: "6d414c503463e5fd57152097ccfed805e301c46fd782f8c84062062fba10ac1a"
},
"vq09-auth-header": {
price_usd: 7.00,
sha256: "00cb211955b18f590a8a8ee53f759bd6f333f25b800a4f4b1a144eee4b849e29"
},
"vq10-context-anchor": {
price_usd: 6.00,
sha256: "a27a543c735b59be550350362c0bd25eea8244936684dca9125d50d901ade23a"
},
"vq11-loop-shield": {
price_usd: 8.00,
sha256: "6b93e7797f2c58a197f241945263774c7afbae741d3058fd74658ec133897522"
},
"vq12-agent-vcard": {
price_usd: 6.00,
sha256: "20a14ad2bbc609f55bea4414b805023f8ed524db5cd32b0fd6e1a625a93afc24"
},
"vq13-oauth-delegation": {
price_usd: 7.00,
sha256: "0f28c4d468d5d1c49ea54d00d93bf1ef3085fc80b76fa95f17c1ed4b3e7f6cec"
},
"vq14-state-bridge": {
price_usd: 6.00,
sha256: "5caf575b45b60fe781176355b0895761b37c8640e613594a0c4e6eeae8a7b119"
},
"vq15-scope-discovery": {
price_usd: 5.00,
sha256: "7ccb447f1d4bbb665853912773886060f0c8a9d3e91bbb3af6268c05c64f2ca3"
},
"vq16-notary-proof": {
price_usd: 8.00,
sha256: "7c82a6de77da3e8bd97bdb7e9cc74e9f71f5f4c2be51ac00ba9dda5f6238f896"
},
"vq17-verifiable-intent": {
price_usd: 5.00,
sha256: "c5536858244aa01ce552c86b8e753af0ec14e30f53a679e13ffb21e969a6dcb7"
},
"vq18-message-signature": {
price_usd: 6.00,
sha256: "ef4707a106af58c194b41cba49239c0e8c039c6f3b8c38832133b57539b032b3"
},
"vq19-nanopay-session": {
price_usd: 7.00,
sha256: "4f13874cd1d38e7f7830b02bd186a4db3ac8dc1ebbf7516c93ae82b522627a61"
},
};
export default {
async fetch(request, env) {
const url = new URL(request.url);
const sessionId = url.searchParams.get('session_id');
const assetIdHint = url.searchParams.get('asset_id');
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, OPTIONS",
"Content-Type": "application/json",
"X-Content-Type-Options": "nosniff",
"Cache-Control": "no-store",
};
if (request.method === "OPTIONS") return new Response(null, { headers: corsHeaders });
// Step 1: Put a "Bouncer" at the Door (Stripe Session ID format/path traversal check)
const sessionRegex = /^cs_(test|live)_[a-zA-Z0-9_]+$/;
if (!sessionId || !sessionRegex.test(sessionId)) {
return new Response(JSON.stringify({
error: "Verification Failed",
details: "Invalid or malformed session_id format. Suspicious patterns rejected."
}), { status: 400, headers: corsHeaders });
}
try {
// 1. STRIPE VERIFICATION (THE GUARD)
const stripeResponse = await fetch(`https://api.stripe.com/v1/checkout/sessions/${sessionId}`, {
headers: { 'Authorization': `Bearer ${env.STRIPE_SECRET_KEY}` }
});
if (!stripeResponse.ok) throw new Error("Stripe Session Not Found");
const session = await stripeResponse.json();
if (session.payment_status !== 'paid') throw new Error("Payment incomplete");
// 2. AUTHORITATIVE PRICE & ASSET METADATA MATCHING (THE PRICE SHIELD)
// Extract asset ID from checkout metadata, falling back securely to public URL parameter only if confirmed.
const assetIdRaw = session.metadata?.asset_id || assetIdHint;
if (!assetIdRaw) {
throw new Error("Receipt Generation Aborted: Session missing designated asset_id context.");
}
// Safeguard against string formatting differences by trimming the asset ID
const assetId = assetIdRaw.trim().toLowerCase();
// Look up our registered product definition inside our system catalog
const assetDefinition = ASSET_DIRECTORY[assetId];
if (!assetDefinition) {
throw new Error(`Receipt Generation Aborted: Unknown asset ID '${assetId}' requested.`);
}
// Verify transaction amount matches registered specification valuation exactly.
// Stripe amount total is parsed in cents (e.g. $1.00 USD is parsed as 100 cents)
const paidAmount = session.amount_total / 100;
const expectedPrice = assetDefinition.price_usd;
if (paidAmount < expectedPrice) {
throw new Error(`Transaction Fraud Blocked: Paid amount of $${paidAmount.toFixed(2)} is insufficient. Expected valuation is $${expectedPrice.toFixed(2)}.`);
}
const assetHash = assetDefinition.sha256;
// Step 3: Salted Email Hash (Privacy-preserving Proof of Identity with Secret Salt)
const email = session.customer_details?.email || "anonymous";
const salt = env.SALT_SECRET || "default_self_radiance_notary_static_salt_2026"; // Secure salt fallback
const emailHash = await hashString(`${email}:${salt}`);
// 3. GENERATE RECEIPT
const receiptData = {
schema: "agentic-receipt-v2.1",
kid: "selfradiance-ed25519-2026-01",
issuer: "https://selfradiance.github.io",
asset: {
id: assetId,
sha256: assetHash,
version: session.metadata?.asset_version || "1.0.0"
},
payment: {
session: session.id,
buyer_hash: emailHash,
amount: paidAmount,
currency: session.currency.toUpperCase()
},
issued_at: new Date().toISOString(),
algorithm: "Ed25519"
};
// 4. DETERMINISTIC RECEIPT ID (The No-Replay Shield)
const deterministicSeed = `${session.id}:${assetId}:${receiptData.kid}`;
const receiptIdRaw = await hashString(deterministicSeed);
receiptData.receipt_id = `SR-${receiptIdRaw.slice(0, 16).toUpperCase()}`;
// 5. SIGNING (The Authority Stamp with Step 2 Deterministic Canonical Serialization)
const privateKeyRaw = base64ToArrayBuffer(env.PRIVATE_KEY);
const pkcs8Envelope = new Uint8Array([
0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20,
...new Uint8Array(privateKeyRaw)
]);
const key = await crypto.subtle.importKey("pkcs8", pkcs8Envelope, "Ed25519", false, ["sign"]);
const encoder = new TextEncoder();
// Run signature on recursively-sorted JSON string guarantee
const document = encoder.encode(canonicalStringify(receiptData));
const sigBuffer = await crypto.subtle.sign("Ed25519", key, document);
const signatureB64 = arrayBufferToBase64(sigBuffer);
return new Response(JSON.stringify({
status: "verified",
certification: "Self-Radiance Sovereign Asset License",
receipt: receiptData,
signature: signatureB64,
verification_key: "LLU9AQt4chCkV6/TBAUxeUSc4nbkN5pBKrZ9V7MYedQ=",
audit: "v2.1-sovereign-standard"
}, null, 2), { headers: corsHeaders });
} catch (err) {
return new Response(JSON.stringify({ error: "Verification Failed", details: err.message }), {
status: 500,
headers: corsHeaders
});
}
}
};
/**
* Step 2: Recursive alphabetical keys serialization scheme to ensure
* byte-by-byte exact JSON representation across all engines and languages natively.
*/
function canonicalStringify(obj) {
if (obj === null) return "null";
if (typeof obj !== "object") return JSON.stringify(obj);
if (Array.isArray(obj)) {
return "[" + obj.map(item => canonicalStringify(item)).join(",") + "]";
}
const sortedKeys = Object.keys(obj).sort();
const keyValuePairs = sortedKeys.map(key => {
return JSON.stringify(key) + ":" + canonicalStringify(obj[key]);
});
return "{" + keyValuePairs.join(",") + "}";
}
async function hashString(message) {
const msgUint8 = new TextEncoder().encode(message);
const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}
function base64ToArrayBuffer(base64) {
const binaryString = atob(base64);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;
}
function arrayBufferToBase64(buffer) {
let binary = "";
const bytes = new Uint8Array(buffer);
for (let i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}