@coderifts/sdk 3.13.2 → 3.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -0
- package/dist/cjs/authorize.d.ts +176 -0
- package/dist/cjs/authorize.d.ts.map +1 -0
- package/dist/cjs/authorize.js +139 -0
- package/dist/cjs/authorize.js.map +1 -0
- package/dist/cjs/client.d.ts +31 -3
- package/dist/cjs/client.d.ts.map +1 -1
- package/dist/cjs/client.js +35 -5
- package/dist/cjs/client.js.map +1 -1
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +22 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/vendor/package.json +1 -0
- package/dist/cjs/vendor/receipt-verifier/VENDOR.sha256 +41 -0
- package/dist/cjs/vendor/receipt-verifier/arity.js +53 -0
- package/dist/cjs/vendor/receipt-verifier/evidence-root.js +188 -0
- package/dist/cjs/vendor/receipt-verifier/verified-execution-binding.js +507 -0
- package/dist/cjs/vendor/receipt-verifier/verify-evidence.js +410 -0
- package/dist/cjs/vendor/receipt-verifier/verify-grant.js +597 -0
- package/dist/cjs/vendor/receipt-verifier/verify-prove-transcript.js +72 -0
- package/dist/cjs/vendor/receipt-verifier/verify.d.ts +31 -0
- package/dist/cjs/vendor/receipt-verifier/verify.js +454 -0
- package/dist/cjs/verify-receipt-local.d.ts +69 -0
- package/dist/cjs/verify-receipt-local.d.ts.map +1 -0
- package/dist/cjs/verify-receipt-local.js +103 -0
- package/dist/cjs/verify-receipt-local.js.map +1 -0
- package/dist/esm/authorize.d.ts +176 -0
- package/dist/esm/authorize.d.ts.map +1 -0
- package/dist/esm/authorize.js +135 -0
- package/dist/esm/authorize.js.map +1 -0
- package/dist/esm/client.d.ts +31 -3
- package/dist/esm/client.d.ts.map +1 -1
- package/dist/esm/client.js +35 -5
- package/dist/esm/client.js.map +1 -1
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +18 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/vendor/package.json +1 -0
- package/dist/esm/vendor/receipt-verifier/VENDOR.sha256 +41 -0
- package/dist/esm/vendor/receipt-verifier/arity.js +53 -0
- package/dist/esm/vendor/receipt-verifier/evidence-root.js +188 -0
- package/dist/esm/vendor/receipt-verifier/verified-execution-binding.js +507 -0
- package/dist/esm/vendor/receipt-verifier/verify-evidence.js +410 -0
- package/dist/esm/vendor/receipt-verifier/verify-grant.js +597 -0
- package/dist/esm/vendor/receipt-verifier/verify-prove-transcript.js +72 -0
- package/dist/esm/vendor/receipt-verifier/verify.d.ts +31 -0
- package/dist/esm/vendor/receipt-verifier/verify.js +454 -0
- package/dist/esm/verify-receipt-local.d.ts +69 -0
- package/dist/esm/verify-receipt-local.d.ts.map +1 -0
- package/dist/esm/verify-receipt-local.js +97 -0
- package/dist/esm/verify-receipt-local.js.map +1 -0
- package/package.json +4 -3
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for the vendored `verify.js`, written HERE rather than in the vendored file.
|
|
3
|
+
*
|
|
4
|
+
* The vendored bytes must stay byte-identical to receipt-verifier's — a pin that survives an edit
|
|
5
|
+
* is not a pin — so the declaration lives beside them instead of inside them.
|
|
6
|
+
*
|
|
7
|
+
* Only the two entry points this SDK uses are declared. Declaring the rest would invite callers to
|
|
8
|
+
* reach into the core through this package, and the core is vendored for internal use, not
|
|
9
|
+
* re-exported as an API this SDK then owns.
|
|
10
|
+
*/
|
|
11
|
+
export interface CoreReceiptVerdict {
|
|
12
|
+
valid: boolean;
|
|
13
|
+
status: string;
|
|
14
|
+
reason?: string;
|
|
15
|
+
payload?: Record<string, unknown>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The DEFAULT export — `module.exports` whole.
|
|
19
|
+
*
|
|
20
|
+
* Declared this way because node's cjs-module-lexer does not surface every name on this module to
|
|
21
|
+
* an ESM importer (measured: `verifyReceipt` yes, `keyringFromDocument` no). Named declarations
|
|
22
|
+
* here would type-check and then fail at load in the ESM build.
|
|
23
|
+
*/
|
|
24
|
+
declare const core: {
|
|
25
|
+
verifyReceipt(
|
|
26
|
+
token: string,
|
|
27
|
+
opts: { ctx: { keyring: unknown; expectedKid: string | null }; now?: number },
|
|
28
|
+
): CoreReceiptVerdict;
|
|
29
|
+
keyringFromDocument(doc: unknown): Map<string, unknown> | { byKid: Map<string, unknown> };
|
|
30
|
+
};
|
|
31
|
+
export default core;
|
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* CodeRifts chain-receipt verifier — PURE LIBRARY. Node >= 20, zero dependencies (node:crypto only).
|
|
5
|
+
*
|
|
6
|
+
* Verify the receipt yourself — offline, no live CodeRifts API call needed.
|
|
7
|
+
* The reference format is frozen in ./RECEIPT_FORMAT.md.
|
|
8
|
+
*
|
|
9
|
+
* THE SPLIT (1282-A'). This file used to be both the library and the command. The command half
|
|
10
|
+
* carried the parts a library must not have: a shebang, a `require.main` block, argument parsing,
|
|
11
|
+
* and `fetchKeyInfo` — a network call whose LEGACY single-key branch returns a key document with
|
|
12
|
+
* no status field, so a receipt signed by a key that was later revoked verified as current. Four
|
|
13
|
+
* repositories vendor this file as `src/verify.js`; every one of them was carrying that command,
|
|
14
|
+
* and none of them could ever run it.
|
|
15
|
+
*
|
|
16
|
+
* The command now lives in ./cli.js. `node cli.js <receipt> …` is the same CLI it always was.
|
|
17
|
+
* Library behaviour is BYTE-IDENTICAL: the cross-language corpus and the envelope-step vectors
|
|
18
|
+
* pass unmodified, which is the proof that this was a move and not a rewrite.
|
|
19
|
+
*
|
|
20
|
+
* WHAT STAYED, and why it is not an inconsistency: `loadKeyring` reads a registry from a URL **or
|
|
21
|
+
* a local file**, and three of the four vendoring repos call it with a FILE path — it is part of
|
|
22
|
+
* the library surface they depend on. `fetchKeyInfo` has no file branch and no consumer outside
|
|
23
|
+
* the command, so it left.
|
|
24
|
+
*
|
|
25
|
+
* CLI usage now:
|
|
26
|
+
* node cli.js <receipt> [--key pub.pem | --keys <url|file>] [--kid <kid>] [--fetch <url>] [--refresh-keys]
|
|
27
|
+
* node cli.js --chain receipts.txt [--key pub.pem | --keys <url|file>] [--kid <kid>] [--fetch <url>] [--refresh-keys]
|
|
28
|
+
*
|
|
29
|
+
* Key discovery: with no --key/--keys/--fetch/--refresh-keys, the command loads the
|
|
30
|
+
* vendored snapshot at keys/coderifts-keys.json (offline). Live fetch is opt-in
|
|
31
|
+
* (--refresh-keys or --fetch <url> / --keys <url>). DEFAULT_FETCH_URL is the well-known
|
|
32
|
+
* registry used by those opt-in flags. This library never fetches.
|
|
33
|
+
* --keys resolves each receipt's key by kid from a registry
|
|
34
|
+
* ({ keys: [{ kid, public_key_pem, status, valid_from, retired_at }] }); accepts a URL or file.
|
|
35
|
+
*
|
|
36
|
+
* Output: JSON { valid, reason?, payload?, chain? } to stdout.
|
|
37
|
+
* Exit codes: 0 valid, 1 invalid, 2 usage error.
|
|
38
|
+
*
|
|
39
|
+
* Verification order (matches the reference taxonomy exactly):
|
|
40
|
+
* structure -> json -> kid -> signature
|
|
41
|
+
* Reasons: malformed_structure | bad_json | unknown_kid | signature_error | signature_mismatch
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
const crypto = require('node:crypto');
|
|
45
|
+
const fs = require('node:fs');
|
|
46
|
+
const { split3ary } = require('./arity');
|
|
47
|
+
|
|
48
|
+
const DEFAULT_FETCH_URL = 'https://app.coderifts.com/.well-known/coderifts-keys.json';
|
|
49
|
+
const SIGNING_PREFIX = 'crchain.v1';
|
|
50
|
+
const MAX_SUPPORTED_V = 4;
|
|
51
|
+
/** ID104 — verification expiry leeway (ms). `exp + leeway < now` → VERIFIED_EXPIRED. */
|
|
52
|
+
const CLOCK_SKEW_LEEWAY_MS = 30_000;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 0s grace only when context DECLARES destructive AND production.
|
|
56
|
+
* Public verifier has `--environment` / envelope.environment; no `destructive`
|
|
57
|
+
* / `operation_class` field — never guess from operation labels.
|
|
58
|
+
*/
|
|
59
|
+
function expiryLeewayMs(_context) {
|
|
60
|
+
return CLOCK_SKEW_LEEWAY_MS;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function isExpiredAt(expiresAtMs, nowMs, context) {
|
|
64
|
+
if (!Number.isFinite(expiresAtMs) || !Number.isFinite(nowMs)) return false;
|
|
65
|
+
return (expiresAtMs + expiryLeewayMs(context)) < nowMs;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Signed fields per the max version — the anti-downgrade delimiter guard rejects any that contain '|'.
|
|
69
|
+
const SIGNED_FIELDS = ['kid', 'fp', 'prev', 'caller', 'ts', 'reg', 'ir', 'expires_at', 'bh'];
|
|
70
|
+
|
|
71
|
+
function sha256hex(str) {
|
|
72
|
+
return crypto.createHash('sha256').update(String(str), 'utf8').digest('hex');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* RFC 8785 (JCS) canonical JSON for our data domain — MUST match the issuer's src/canonical-json.js
|
|
77
|
+
* and verify.py's canonical_json byte-for-byte (ASCII keys, JSON.stringify scalars, sorted keys, no
|
|
78
|
+
* whitespace, reject NaN/Infinity/undefined). Used to recompute decision_body_hash from --envelope.
|
|
79
|
+
*/
|
|
80
|
+
function canonicalJson(value) {
|
|
81
|
+
if (value === null) return 'null';
|
|
82
|
+
const t = typeof value;
|
|
83
|
+
if (t === 'boolean' || t === 'string') return JSON.stringify(value);
|
|
84
|
+
if (t === 'number') {
|
|
85
|
+
if (!Number.isFinite(value)) throw new TypeError('canonicalJson: non-finite number');
|
|
86
|
+
return JSON.stringify(value);
|
|
87
|
+
}
|
|
88
|
+
if (t === 'undefined') throw new TypeError('canonicalJson: undefined');
|
|
89
|
+
if (Array.isArray(value)) return `[${value.map(canonicalJson).join(',')}]`;
|
|
90
|
+
if (t === 'object') {
|
|
91
|
+
const keys = Object.keys(value).sort();
|
|
92
|
+
return `{${keys.map((k) => `${JSON.stringify(k)}:${canonicalJson(value[k])}`).join(',')}}`;
|
|
93
|
+
}
|
|
94
|
+
throw new TypeError(`canonicalJson: unsupported type ${t}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Reconstruct the exact signed bytes from a parsed receipt body.
|
|
99
|
+
* v1 (v absent or 1): the base string.
|
|
100
|
+
* v2 (v === 2): base + '|' + reg.
|
|
101
|
+
* v3 (v === 3): base + '|' + reg + '|' + ir.
|
|
102
|
+
* v4 (v === 4): base + '|' + reg + '|' + ir + '|' + expires_at + '|' + bh.
|
|
103
|
+
* Field order matches the CodeRifts issuer exactly (chain-attestation.js signingInputV4).
|
|
104
|
+
* This string must be byte-identical to the Python implementation.
|
|
105
|
+
*/
|
|
106
|
+
function reconstructSignedInput(payload) {
|
|
107
|
+
const base = `${SIGNING_PREFIX}|${payload.kid}|${payload.fp}|${payload.prev}|${payload.caller}|${payload.ts}`;
|
|
108
|
+
if (payload.v === 4) return `${base}|${payload.reg}|${payload.ir}|${payload.expires_at}|${payload.bh}`;
|
|
109
|
+
if (payload.v === 3) return `${base}|${payload.reg}|${payload.ir}`;
|
|
110
|
+
if (payload.v === 2) return `${base}|${payload.reg}`;
|
|
111
|
+
return base;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Resolve the verification key for a parsed payload.
|
|
116
|
+
* Two modes:
|
|
117
|
+
* - keyring (from --keys): pick the entry whose kid matches payload.kid; an
|
|
118
|
+
* unlisted kid resolves to null (=> unknown_kid). A retired key still verifies
|
|
119
|
+
* so receipts issued before a rotation stay checkable.
|
|
120
|
+
* - single key (default): the one loaded key, gated by expectedKid when known.
|
|
121
|
+
* Returns a KeyObject, or null when the kid is not accepted.
|
|
122
|
+
*/
|
|
123
|
+
/**
|
|
124
|
+
* 1306(a) — accept the SERVED registry shape, not only a Map.
|
|
125
|
+
*
|
|
126
|
+
* MEASURED black-box 2026-09-02. `https://app.coderifts.com/.well-known/coderifts-keys.json`
|
|
127
|
+
* serves `{keys:[{kid, public_key_pem, status, ...}]}`. Passing that document straight into
|
|
128
|
+
* `verifyReceipt(token, {ctx:{keyring}})` threw `TypeError: ctx.keyring.get is not a function` —
|
|
129
|
+
* the library path rejected the exact bytes this project publishes for it, while the CLI's
|
|
130
|
+
* `--keys <url>` worked because it converts first.
|
|
131
|
+
*
|
|
132
|
+
* The fix is here rather than in a caller: every consumer that fetches the public registry would
|
|
133
|
+
* otherwise have to know to convert, and the ones that do not find out with a TypeError rather
|
|
134
|
+
* than a verdict. `expectedKid` is a separate trap on the same path and is NOT papered over —
|
|
135
|
+
* `{keyring}` with no `expectedKid` still resolves to null by design (see resolveEntry), because
|
|
136
|
+
* silently defaulting a kid gate is a different and worse behaviour than a clear TypeError.
|
|
137
|
+
*/
|
|
138
|
+
function coerceKeyring(k, source) {
|
|
139
|
+
if (!k) return k;
|
|
140
|
+
if (typeof k.get === 'function') return k; // already a Map
|
|
141
|
+
if (Array.isArray(k.keys) || Array.isArray(k)) {
|
|
142
|
+
return keyringFromDocument(Array.isArray(k) ? { keys: k } : k, source || 'ctx.keyring');
|
|
143
|
+
}
|
|
144
|
+
return k;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function resolveEntry(ctx, payload) {
|
|
148
|
+
if (ctx.keyring) {
|
|
149
|
+
const entry = ctx.keyring.get(payload.kid);
|
|
150
|
+
if (!entry) return null;
|
|
151
|
+
if (ctx.expectedKid !== null && payload.kid !== ctx.expectedKid) return null;
|
|
152
|
+
return entry; // { publicKey, status, retired_at }
|
|
153
|
+
}
|
|
154
|
+
if (ctx.expectedKid !== null && payload.kid !== ctx.expectedKid) return null;
|
|
155
|
+
return { publicKey: ctx.publicKey, status: null, retired_at: null, revoked_at: null, compromised_at: null };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Derive the 12-status taxonomy verdict for an already-signature-valid receipt.
|
|
160
|
+
* RETIRED_KEY_VALID_AT_ISSUE — retired key, receipt ts predates retired_at (else INVALID_SIGNATURE).
|
|
161
|
+
* UNSUPPORTED_VERSION — payload.v beyond MAX_SUPPORTED_V.
|
|
162
|
+
* VERIFIED_EXPIRED — v4 receipt whose signed expires_at + 30s leeway is in the past.
|
|
163
|
+
* VERIFIED_WRONG_AUDIENCE / _WRONG_ENVIRONMENT — dormant: only when --envelope carries the field
|
|
164
|
+
* AND a check input (--audience/--environment) is supplied.
|
|
165
|
+
* VERIFIED_SUPERSEDED / _SCOPE_MISMATCH — dormant: no check input defined this round.
|
|
166
|
+
* VERIFIED_CURRENT — otherwise.
|
|
167
|
+
*/
|
|
168
|
+
function deriveStatus(payload, entry, opts) {
|
|
169
|
+
if (typeof payload.v === 'number' && payload.v > MAX_SUPPORTED_V) return 'UNSUPPORTED_VERSION';
|
|
170
|
+
// FAIL CLOSED ON A STATUS WE DO NOT UNDERSTAND.
|
|
171
|
+
//
|
|
172
|
+
// MEASURED 2026-08-26: a registry entry with status "revoked" returned
|
|
173
|
+
// { valid: true, status: "VERIFIED_CURRENT" } here — the status was read for 'retired' and
|
|
174
|
+
// otherwise ignored, so anything else fell through to the healthy path. An operator who marked a
|
|
175
|
+
// stolen key revoked would have believed they had acted while this verifier kept accepting it.
|
|
176
|
+
// The app kernel and verify-attest/verify-toolset already reject an unknown status; these two
|
|
177
|
+
// did not, so the fleet disagreed about the same registry.
|
|
178
|
+
//
|
|
179
|
+
// This is a bug fix, not revocation: the revocation RULE (compromised_at, REVOKED_KEY /
|
|
180
|
+
// REVOKED_KEY_UNDECIDABLE) is a separate, larger change across eight verifiers. What lands here
|
|
181
|
+
// is only the direction of the unknown case. Safe by measurement: the live registry publishes
|
|
182
|
+
// 'active' only, so no real consumer changes behaviour.
|
|
183
|
+
const KNOWN_STATUSES = new Set(['active', 'retired', 'revoked', null, undefined]);
|
|
184
|
+
if (!KNOWN_STATUSES.has(entry.status)) {
|
|
185
|
+
return 'UNKNOWN_KEY_STATUS';
|
|
186
|
+
}
|
|
187
|
+
// 1079 B — OPTIONAL timestamps, additive. Absent both fields → this function continues
|
|
188
|
+
// exactly as before. Signing time is payload.ts (receipts have no iat).
|
|
189
|
+
// revoked_at = compromise: EVERY receipt under the key is invalid, including those
|
|
190
|
+
// whose ts predates revoked_at (the attacker chooses ts).
|
|
191
|
+
// retired_at = planned rotation: ts < retired_at stays on the existing path;
|
|
192
|
+
// ts >= retired_at is KEY_RETIRED_AFTER_SIGNING.
|
|
193
|
+
if (typeof entry.revoked_at === 'string' && entry.revoked_at.length > 0) {
|
|
194
|
+
return 'KEY_REVOKED';
|
|
195
|
+
}
|
|
196
|
+
if (typeof entry.retired_at === 'string' && entry.retired_at.length > 0 && payload.ts) {
|
|
197
|
+
const issued = Date.parse(payload.ts);
|
|
198
|
+
const retired = Date.parse(entry.retired_at);
|
|
199
|
+
if (Number.isFinite(issued) && Number.isFinite(retired) && issued >= retired) {
|
|
200
|
+
return 'KEY_RETIRED_AFTER_SIGNING';
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
// REVOKED — RECEIPT_FORMAT.md §7.1 (normative). The attacker chooses ts, so no timestamp may
|
|
204
|
+
// rehabilitate a revoked key's signature: BOTH outcomes are valid:false. UNDECIDABLE is not a
|
|
205
|
+
// softer valid; it reports that we cannot tell a legitimate pre-compromise receipt from a
|
|
206
|
+
// backdated forgery. A missing compromised_at means the whole key history is suspect.
|
|
207
|
+
if (entry.status === 'revoked') {
|
|
208
|
+
const at = entry.compromised_at;
|
|
209
|
+
if (typeof at !== 'string' || at.length === 0) return 'REVOKED_KEY_UNDECIDABLE';
|
|
210
|
+
const boundary = Date.parse(at);
|
|
211
|
+
const issued = Date.parse(payload.ts);
|
|
212
|
+
if (!Number.isFinite(boundary) || !Number.isFinite(issued)) return 'REVOKED_KEY_UNDECIDABLE';
|
|
213
|
+
return issued >= boundary ? 'REVOKED_KEY' : 'REVOKED_KEY_UNDECIDABLE';
|
|
214
|
+
}
|
|
215
|
+
if (entry.status === 'retired') {
|
|
216
|
+
if (entry.retired_at && payload.ts
|
|
217
|
+
&& Date.parse(payload.ts) < Date.parse(entry.retired_at)) {
|
|
218
|
+
return 'RETIRED_KEY_VALID_AT_ISSUE';
|
|
219
|
+
}
|
|
220
|
+
return 'INVALID_SIGNATURE'; // signed by a key already retired at issue -> reject
|
|
221
|
+
}
|
|
222
|
+
const now = opts.now != null ? opts.now : Date.now();
|
|
223
|
+
if (payload.v === 4 && typeof payload.expires_at === 'string') {
|
|
224
|
+
const exp = Date.parse(payload.expires_at);
|
|
225
|
+
const context = opts.envelope || { environment: opts.expectedEnvironment };
|
|
226
|
+
if (isExpiredAt(exp, now, context)) return 'VERIFIED_EXPIRED';
|
|
227
|
+
}
|
|
228
|
+
if (opts.envelope) {
|
|
229
|
+
const env = opts.envelope;
|
|
230
|
+
if (opts.expectedAudience != null && env.audience != null && env.audience !== opts.expectedAudience) {
|
|
231
|
+
return 'VERIFIED_WRONG_AUDIENCE';
|
|
232
|
+
}
|
|
233
|
+
if (opts.expectedEnvironment != null && env.environment != null && env.environment !== opts.expectedEnvironment) {
|
|
234
|
+
return 'VERIFIED_WRONG_ENVIRONMENT';
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return 'VERIFIED_CURRENT';
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Verify a single receipt token against a public key (or keyring) + expected kid.
|
|
242
|
+
* @param {string} token
|
|
243
|
+
* @param {{ publicKey?: import('crypto').KeyObject, keyring?: Map<string,{publicKey:import('crypto').KeyObject}>, expectedKid: (string|null) }} ctx
|
|
244
|
+
* @returns {{ valid: boolean, reason?: string, payload?: object }}
|
|
245
|
+
*/
|
|
246
|
+
function verifyReceiptInner(token, ctx, opts = {}) {
|
|
247
|
+
// 1. structure
|
|
248
|
+
if (typeof token !== 'string' || token.length === 0) {
|
|
249
|
+
return { valid: false, status: 'MALFORMED', reason: 'malformed_structure' };
|
|
250
|
+
}
|
|
251
|
+
const segments = token.split('.');
|
|
252
|
+
if (segments.length !== 2 || segments.some((s) => !s)) {
|
|
253
|
+
return { valid: false, status: 'MALFORMED', reason: 'malformed_structure' };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// 2. json
|
|
257
|
+
let payload;
|
|
258
|
+
try {
|
|
259
|
+
payload = JSON.parse(Buffer.from(segments[0], 'base64url').toString('utf8'));
|
|
260
|
+
} catch (_) {
|
|
261
|
+
return { valid: false, status: 'MALFORMED', reason: 'bad_json' };
|
|
262
|
+
}
|
|
263
|
+
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
|
|
264
|
+
return { valid: false, status: 'MALFORMED', reason: 'bad_json' };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// 3. kid -- resolve the entry by kid (keyring) or gate the single key by expectedKid.
|
|
268
|
+
// 1306(a): a served `{keys:[...]}` document is accepted here as well as a Map.
|
|
269
|
+
const entry = resolveEntry(
|
|
270
|
+
ctx.keyring ? { ...ctx, keyring: coerceKeyring(ctx.keyring, 'ctx.keyring') } : ctx,
|
|
271
|
+
payload,
|
|
272
|
+
);
|
|
273
|
+
if (!entry) {
|
|
274
|
+
return { valid: false, status: 'UNKNOWN_KEY', reason: 'unknown_kid', payload };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// 4. signature (raw Ed25519 over the reconstructed UTF-8 bytes)
|
|
278
|
+
const sig = Buffer.from(segments[1], 'base64url');
|
|
279
|
+
let ok = false;
|
|
280
|
+
try {
|
|
281
|
+
ok = crypto.verify(null, Buffer.from(reconstructSignedInput(payload), 'utf8'), entry.publicKey, sig);
|
|
282
|
+
} catch (_) {
|
|
283
|
+
return { valid: false, status: 'INVALID_SIGNATURE', reason: 'signature_error', payload };
|
|
284
|
+
}
|
|
285
|
+
if (!ok) return { valid: false, status: 'INVALID_SIGNATURE', reason: 'signature_mismatch', payload };
|
|
286
|
+
|
|
287
|
+
// 5. anti-downgrade delimiter guard — a signed field containing '|' could re-split into a lower
|
|
288
|
+
// version whose reconstructed bytes collide with these. Legitimate fields never contain '|'.
|
|
289
|
+
for (const k of SIGNED_FIELDS) {
|
|
290
|
+
if (typeof payload[k] === 'string' && payload[k].includes('|')) {
|
|
291
|
+
return { valid: false, status: 'INVALID_SIGNATURE', reason: 'delimiter_in_field', payload };
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// 6. envelope binding (v4): a supplied envelope's canonical body_hash MUST equal payload.bh.
|
|
296
|
+
if (opts.envelope && payload.v === 4) {
|
|
297
|
+
const rest = { ...opts.envelope };
|
|
298
|
+
delete rest.receipt;
|
|
299
|
+
delete rest.decision_body_hash;
|
|
300
|
+
const recomputed = 'sha256:' + sha256hex(canonicalJson(rest));
|
|
301
|
+
if (recomputed !== payload.bh) {
|
|
302
|
+
return { valid: false, status: 'INVALID_SIGNATURE', reason: 'body_hash_mismatch', payload };
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// 7. taxonomy status (freshness / retirement / dormant field checks).
|
|
307
|
+
const status = deriveStatus(payload, entry, opts);
|
|
308
|
+
if (status === 'KEY_REVOKED') {
|
|
309
|
+
return { valid: false, status, reason: 'KEY_REVOKED', payload };
|
|
310
|
+
}
|
|
311
|
+
if (status === 'KEY_RETIRED_AFTER_SIGNING') {
|
|
312
|
+
return { valid: false, status, reason: 'KEY_RETIRED_AFTER_SIGNING', payload };
|
|
313
|
+
}
|
|
314
|
+
if (status === 'INVALID_SIGNATURE') {
|
|
315
|
+
return { valid: false, status, reason: 'retired_key_after_issue', payload };
|
|
316
|
+
}
|
|
317
|
+
const valid = status === 'VERIFIED_CURRENT' || status === 'RETIRED_KEY_VALID_AT_ISSUE';
|
|
318
|
+
// UNKNOWN_KEY_STATUS is deliberately absent from the valid set — see deriveStatus.
|
|
319
|
+
return { valid, status, payload };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Verify a chain of tokens (oldest first): every signature valid AND every non-genesis
|
|
324
|
+
* link's prev == 'sha256:' + sha256hex(previous token string).
|
|
325
|
+
*/
|
|
326
|
+
function verifyChainInner(tokens, ctx, opts = {}) {
|
|
327
|
+
const links = [];
|
|
328
|
+
let allValid = true;
|
|
329
|
+
let first = null;
|
|
330
|
+
|
|
331
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
332
|
+
const token = tokens[i];
|
|
333
|
+
const res = verifyReceiptInner(token, ctx, opts);
|
|
334
|
+
const link = { index: i, signature_valid: res.valid };
|
|
335
|
+
if (!res.valid) link.reason = res.reason;
|
|
336
|
+
const prev = res.payload ? res.payload.prev : undefined;
|
|
337
|
+
if (res.payload) link.prev = prev;
|
|
338
|
+
|
|
339
|
+
if (i === 0) {
|
|
340
|
+
first = prev === 'null' ? 'genesis' : 'continuation';
|
|
341
|
+
link.role = first;
|
|
342
|
+
// A genesis needs prev === 'null'; a continuation links to a token we do not hold.
|
|
343
|
+
link.prev_ok = prev === 'null' ? true : null;
|
|
344
|
+
} else {
|
|
345
|
+
const expected = `sha256:${sha256hex(tokens[i - 1])}`;
|
|
346
|
+
link.expected_prev = expected;
|
|
347
|
+
link.prev_ok = prev === expected;
|
|
348
|
+
if (!link.prev_ok) allValid = false;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
if (!res.valid) allValid = false;
|
|
352
|
+
links.push(link);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return { valid: allValid, chain: { length: tokens.length, first, links } };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// ---------------------------------------------------------------------------
|
|
359
|
+
// Key loading
|
|
360
|
+
// ---------------------------------------------------------------------------
|
|
361
|
+
|
|
362
|
+
function keyFromPem(pem) {
|
|
363
|
+
return crypto.createPublicKey(pem);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Build a kid -> key map from a registry document
|
|
368
|
+
* ({ keys: [{ kid, public_key_pem, status, valid_from, retired_at }] }).
|
|
369
|
+
* Returns null when `keys` is missing/empty so the caller can try the legacy
|
|
370
|
+
* single-key body. --keys still requires a non-empty keys[] (throws).
|
|
371
|
+
*/
|
|
372
|
+
function keyringFromDocument(doc, source) {
|
|
373
|
+
const keys = doc && Array.isArray(doc.keys) ? doc.keys : null;
|
|
374
|
+
if (!keys || keys.length === 0) return null;
|
|
375
|
+
const keyring = new Map();
|
|
376
|
+
for (const k of keys) {
|
|
377
|
+
if (!k || !k.kid || !k.public_key_pem) throw new Error(`registry entry missing kid/public_key_pem in ${source}`);
|
|
378
|
+
// compromised_at MUST be carried through: deriveStatus reads it for the revoked rule
|
|
379
|
+
// (RECEIPT_FORMAT.md 7.1). Dropping it here made the rule inert — every revoked key
|
|
380
|
+
// returned UNDECIDABLE regardless of ts, which looks implemented and decides nothing.
|
|
381
|
+
keyring.set(k.kid, {
|
|
382
|
+
publicKey: keyFromPem(k.public_key_pem),
|
|
383
|
+
status: k.status || null,
|
|
384
|
+
retired_at: k.retired_at || null,
|
|
385
|
+
revoked_at: k.revoked_at || null,
|
|
386
|
+
compromised_at: k.compromised_at || null,
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
return keyring;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function pickActiveFromKeyring(keyring) {
|
|
393
|
+
for (const [kid, entry] of keyring) {
|
|
394
|
+
if (entry.status === 'active') return { kid, entry };
|
|
395
|
+
}
|
|
396
|
+
const first = keyring.entries().next().value;
|
|
397
|
+
return first ? { kid: first[0], entry: first[1] } : null;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Build a kid -> key map from a CodeRifts key registry
|
|
402
|
+
* ({ keys: [{ kid, public_key_pem, status, valid_from, retired_at }] }). A --keys source may be
|
|
403
|
+
* an http(s) URL or a local file path. Both active and retired keys are loaded.
|
|
404
|
+
*/
|
|
405
|
+
async function loadKeyring(source) {
|
|
406
|
+
let text;
|
|
407
|
+
if (/^https?:\/\//i.test(source)) {
|
|
408
|
+
const res = await fetch(source, { headers: { Accept: 'application/json' } });
|
|
409
|
+
if (!res.ok) throw new Error(`fetch ${source} -> HTTP ${res.status}`);
|
|
410
|
+
text = await res.text();
|
|
411
|
+
} else {
|
|
412
|
+
text = fs.readFileSync(source, 'utf8');
|
|
413
|
+
}
|
|
414
|
+
const doc = JSON.parse(text);
|
|
415
|
+
const keyring = keyringFromDocument(doc, source);
|
|
416
|
+
if (!keyring) throw new Error(`no keys[] in registry ${source}`);
|
|
417
|
+
return keyring;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function verifyReceipt(token, second, third) {
|
|
421
|
+
const { ctx, opts } = split3ary('verifyReceipt', arguments.length, second, third);
|
|
422
|
+
return verifyReceiptInner(token, ctx, opts);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function verifyChain(tokens, second, third) {
|
|
426
|
+
const { ctx, opts } = split3ary('verifyChain', arguments.length, second, third);
|
|
427
|
+
return verifyChainInner(tokens, ctx, opts);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// Reusable API — require('./verify') imports the pure verify logic WITHOUT running the CLI. The
|
|
431
|
+
// GitHub Action + other embedders use these directly (verifyReceipt/verifyChain/deriveStatus/…);
|
|
432
|
+
// the receipt format + taxonomy are frozen in RECEIPT_FORMAT.md.
|
|
433
|
+
module.exports = {
|
|
434
|
+
verifyReceipt,
|
|
435
|
+
verifyChain,
|
|
436
|
+
deriveStatus,
|
|
437
|
+
resolveEntry,
|
|
438
|
+
reconstructSignedInput,
|
|
439
|
+
canonicalJson,
|
|
440
|
+
loadKeyring,
|
|
441
|
+
keyFromPem,
|
|
442
|
+
keyringFromDocument,
|
|
443
|
+
// Pure keyring helper. Exported for the command (cli.js fetchKeyInfo) after the split —
|
|
444
|
+
// it takes a keyring and returns the active entry; it performs no I/O.
|
|
445
|
+
pickActiveFromKeyring,
|
|
446
|
+
sha256hex,
|
|
447
|
+
DEFAULT_FETCH_URL,
|
|
448
|
+
SIGNING_PREFIX,
|
|
449
|
+
MAX_SUPPORTED_V,
|
|
450
|
+
SIGNED_FIELDS,
|
|
451
|
+
CLOCK_SKEW_LEEWAY_MS,
|
|
452
|
+
expiryLeewayMs,
|
|
453
|
+
isExpiredAt,
|
|
454
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LOCAL receipt verification — the proof, and the only thing on this surface that is one.
|
|
3
|
+
*
|
|
4
|
+
* ── WHY THIS EXISTS ─────────────────────────────────────────────────────────────────────────
|
|
5
|
+
*
|
|
6
|
+
* MEASURED before it was written: this SDK's only `verifyReceipt` was
|
|
7
|
+
* `client.verifyReceipt()` — an HTTP POST to `/api/v1/verify-receipt`. It answers well and it is
|
|
8
|
+
* useful, and it is not an offline verification: the caller learns what CodeRifts says, over a
|
|
9
|
+
* network, about bytes CodeRifts was handed. A reader told "verify offline, without us" and given
|
|
10
|
+
* only that method has been told something the package could not do.
|
|
11
|
+
*
|
|
12
|
+
* This runs the SAME vendored core the public receipt-verifier and the conformance CLI run, in
|
|
13
|
+
* process, over bytes already in memory plus a keyring the CALLER supplies. No API key, no
|
|
14
|
+
* network, full Ed25519.
|
|
15
|
+
*
|
|
16
|
+
* ── WHAT IT IS NOT ──────────────────────────────────────────────────────────────────────────
|
|
17
|
+
*
|
|
18
|
+
* It is not authorization. A valid signature says a holder of the named key signed these bytes and
|
|
19
|
+
* that the receipt has not expired; it does not say the receipt permits the action you are about
|
|
20
|
+
* to take. `client.verifyReceipt(token, intended)` answers that second question, on the server,
|
|
21
|
+
* and `authorize()` answers the execution-binding one. Those are different questions and this
|
|
22
|
+
* module deliberately answers only the first.
|
|
23
|
+
*
|
|
24
|
+
* It also cannot see REVOCATION or the issuer's clock. A key compromised five minutes ago still
|
|
25
|
+
* verifies here, because nothing local can know. That is the whole reason
|
|
26
|
+
* `verifyReceiptViaServer` exists beside it — as a mirror, never as the proof.
|
|
27
|
+
*/
|
|
28
|
+
import type { PinnedKeyring } from './authorize.js';
|
|
29
|
+
/** The verdict, in the vocabulary the receipt-verifier and the conformance CLI already use. */
|
|
30
|
+
export interface LocalReceiptVerdict {
|
|
31
|
+
/** True only when the signature verified against the pinned key and the receipt is current. */
|
|
32
|
+
valid: boolean;
|
|
33
|
+
/** VERIFIED_CURRENT · INVALID_SIGNATURE · MALFORMED · UNKNOWN_KEY · EXPIRED · … */
|
|
34
|
+
status: string;
|
|
35
|
+
/** Present on a refusal; the specific cause, not a class. */
|
|
36
|
+
reason?: string;
|
|
37
|
+
/** The decoded body, when the token decoded at all. Never evidence on its own. */
|
|
38
|
+
payload?: Record<string, unknown>;
|
|
39
|
+
/**
|
|
40
|
+
* Said on every result, so a caller cannot read this as more than it is.
|
|
41
|
+
*
|
|
42
|
+
* A verdict that travels without its ceiling gets quoted without it.
|
|
43
|
+
*/
|
|
44
|
+
does_not_prove: string[];
|
|
45
|
+
}
|
|
46
|
+
export interface VerifyReceiptLocalOptions {
|
|
47
|
+
/**
|
|
48
|
+
* THE KEYS YOU PIN, and the reason this is required rather than fetched.
|
|
49
|
+
*
|
|
50
|
+
* A verifier that downloads the key it is about to trust has not verified anything an attacker
|
|
51
|
+
* on the path could not arrange. Fetching the keyring is a deployment decision — make it once,
|
|
52
|
+
* pin the result, and hand it in.
|
|
53
|
+
*/
|
|
54
|
+
keyring: PinnedKeyring;
|
|
55
|
+
/** Refuse a token whose `kid` is not this one. Null (default) accepts any pinned kid. */
|
|
56
|
+
expectedKid?: string | null;
|
|
57
|
+
/** Clock injection. Absent = the host clock. */
|
|
58
|
+
now?: number;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Verify a chain receipt LOCALLY. No network, no API key, full Ed25519.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* import { verifyReceipt } from '@coderifts/sdk';
|
|
65
|
+
* const r = verifyReceipt(token, { keyring: pinnedKeys });
|
|
66
|
+
* if (!r.valid) throw new Error(`${r.status}: ${r.reason}`);
|
|
67
|
+
*/
|
|
68
|
+
export declare function verifyReceipt(token: string, options: VerifyReceiptLocalOptions): LocalReceiptVerdict;
|
|
69
|
+
//# sourceMappingURL=verify-receipt-local.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verify-receipt-local.d.ts","sourceRoot":"","sources":["../../src/verify-receipt-local.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAkBpD,+FAA+F;AAC/F,MAAM,WAAW,mBAAmB;IAChC,+FAA+F;IAC/F,KAAK,EAAE,OAAO,CAAC;IACf,mFAAmF;IACnF,MAAM,EAAE,MAAM,CAAC;IACf,6DAA6D;IAC7D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,kFAAkF;IAClF,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC;;;;OAIG;IACH,cAAc,EAAE,MAAM,EAAE,CAAC;CAC5B;AAED,MAAM,WAAW,yBAAyB;IACtC;;;;;;OAMG;IACH,OAAO,EAAE,aAAa,CAAC;IACvB,yFAAyF;IACzF,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CAChB;AAmCD;;;;;;;GAOG;AACH,wBAAgB,aAAa,CACzB,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,yBAAyB,GACnC,mBAAmB,CAarB"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* LOCAL receipt verification — the proof, and the only thing on this surface that is one.
|
|
4
|
+
*
|
|
5
|
+
* ── WHY THIS EXISTS ─────────────────────────────────────────────────────────────────────────
|
|
6
|
+
*
|
|
7
|
+
* MEASURED before it was written: this SDK's only `verifyReceipt` was
|
|
8
|
+
* `client.verifyReceipt()` — an HTTP POST to `/api/v1/verify-receipt`. It answers well and it is
|
|
9
|
+
* useful, and it is not an offline verification: the caller learns what CodeRifts says, over a
|
|
10
|
+
* network, about bytes CodeRifts was handed. A reader told "verify offline, without us" and given
|
|
11
|
+
* only that method has been told something the package could not do.
|
|
12
|
+
*
|
|
13
|
+
* This runs the SAME vendored core the public receipt-verifier and the conformance CLI run, in
|
|
14
|
+
* process, over bytes already in memory plus a keyring the CALLER supplies. No API key, no
|
|
15
|
+
* network, full Ed25519.
|
|
16
|
+
*
|
|
17
|
+
* ── WHAT IT IS NOT ──────────────────────────────────────────────────────────────────────────
|
|
18
|
+
*
|
|
19
|
+
* It is not authorization. A valid signature says a holder of the named key signed these bytes and
|
|
20
|
+
* that the receipt has not expired; it does not say the receipt permits the action you are about
|
|
21
|
+
* to take. `client.verifyReceipt(token, intended)` answers that second question, on the server,
|
|
22
|
+
* and `authorize()` answers the execution-binding one. Those are different questions and this
|
|
23
|
+
* module deliberately answers only the first.
|
|
24
|
+
*
|
|
25
|
+
* It also cannot see REVOCATION or the issuer's clock. A key compromised five minutes ago still
|
|
26
|
+
* verifies here, because nothing local can know. That is the whole reason
|
|
27
|
+
* `verifyReceiptViaServer` exists beside it — as a mirror, never as the proof.
|
|
28
|
+
*/
|
|
29
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
30
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
31
|
+
};
|
|
32
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
33
|
+
exports.verifyReceipt = verifyReceipt;
|
|
34
|
+
// ── A STATIC IMPORT, AND THAT IS THE POINT ──────────────────────────────────────────────────
|
|
35
|
+
//
|
|
36
|
+
// MEASURED on the built package: `authorize()` loads the same core with a bare `require(...)`, and
|
|
37
|
+
// `dist/esm/authorize.js` therefore throws `require is not defined` for every ESM consumer. The
|
|
38
|
+
// tests never saw it because they load the CJS source. A call-time `require` is invisible to the
|
|
39
|
+
// compiler in exactly the build where it cannot work.
|
|
40
|
+
//
|
|
41
|
+
// A static import is compiled to `require` for the CJS output and left as an import for the ESM
|
|
42
|
+
// one, so this module works in both — and the compiler, not a runtime, is what checks it.
|
|
43
|
+
//
|
|
44
|
+
// The DEFAULT import, not named ones. MEASURED on the built ESM package: node's cjs-module-lexer
|
|
45
|
+
// resolved `verifyReceipt` from the vendored CommonJS and NOT `keyringFromDocument`, so a named
|
|
46
|
+
// import compiled fine and threw at load time — "does not provide an export named". Taking
|
|
47
|
+
// `module.exports` whole and destructuring at runtime is interop that does not depend on a lexer
|
|
48
|
+
// guessing right about bytes this package is forbidden to edit.
|
|
49
|
+
const verify_js_1 = __importDefault(require("./vendor/receipt-verifier/verify.js"));
|
|
50
|
+
const DOES_NOT_PROVE = Object.freeze([
|
|
51
|
+
'that the receipt AUTHORIZES the action you are about to take — a valid signature is '
|
|
52
|
+
+ 'authenticity, not authorization. Pass an intended context to the server verify, or use '
|
|
53
|
+
+ 'authorize() for the execution binding',
|
|
54
|
+
'that the signing key is still trusted — REVOCATION and the issuer\'s clock are not visible '
|
|
55
|
+
+ 'to any local verifier, including this one. A key compromised a minute ago still verifies here',
|
|
56
|
+
'that the bytes came from the run you think they did — this checks ONE token, and "these '
|
|
57
|
+
+ 'tokens are one run" is a property of a set (see cr.evidence.root.v1)',
|
|
58
|
+
]);
|
|
59
|
+
function toKeyringMap(keyring) {
|
|
60
|
+
if (!keyring || !Array.isArray(keyring.keys) || keyring.keys.length === 0) {
|
|
61
|
+
throw new TypeError('verifyReceipt: a keyring with at least one key is required — this '
|
|
62
|
+
+ 'verifier does not fetch keys, because a verifier that downloads the key it is about '
|
|
63
|
+
+ 'to trust has verified nothing an attacker on the path could not arrange');
|
|
64
|
+
}
|
|
65
|
+
// THE CORE'S OWN keyring parser, not a second one written here.
|
|
66
|
+
//
|
|
67
|
+
// `keyringFromDocument` is the function the receipt-verifier CLI and the conformance measure
|
|
68
|
+
// both use. Re-implementing the parse in this file would be a second idea of what a keyring
|
|
69
|
+
// is — the exact shape 1423 came from, where conformance had its own notion of checking and it
|
|
70
|
+
// was weaker than prove's.
|
|
71
|
+
const parsed = verify_js_1.default.keyringFromDocument(keyring);
|
|
72
|
+
// The core returns either a Map or a `{ byKid }` wrapper depending on the document shape it
|
|
73
|
+
// was handed. Both are accepted here rather than the caller being told which to produce.
|
|
74
|
+
const m = (parsed && parsed.byKid)
|
|
75
|
+
|| parsed;
|
|
76
|
+
if (!m || typeof m.get !== 'function' || m.size === 0) {
|
|
77
|
+
throw new TypeError('verifyReceipt: the keyring contained no usable {kid, public_key_pem}');
|
|
78
|
+
}
|
|
79
|
+
return m;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Verify a chain receipt LOCALLY. No network, no API key, full Ed25519.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* import { verifyReceipt } from '@coderifts/sdk';
|
|
86
|
+
* const r = verifyReceipt(token, { keyring: pinnedKeys });
|
|
87
|
+
* if (!r.valid) throw new Error(`${r.status}: ${r.reason}`);
|
|
88
|
+
*/
|
|
89
|
+
function verifyReceipt(token, options) {
|
|
90
|
+
const ring = toKeyringMap(options && options.keyring);
|
|
91
|
+
const r = verify_js_1.default.verifyReceipt(token, {
|
|
92
|
+
ctx: { keyring: ring, expectedKid: options.expectedKid ?? null },
|
|
93
|
+
...(Number.isFinite(options.now) ? { now: options.now } : {}),
|
|
94
|
+
});
|
|
95
|
+
return {
|
|
96
|
+
valid: r.valid === true,
|
|
97
|
+
status: r.status,
|
|
98
|
+
...(r.reason ? { reason: r.reason } : {}),
|
|
99
|
+
...(r.payload ? { payload: r.payload } : {}),
|
|
100
|
+
does_not_prove: [...DOES_NOT_PROVE],
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=verify-receipt-local.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verify-receipt-local.js","sourceRoot":"","sources":["../../src/verify-receipt-local.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;;;;;AA8FH,sCAgBC;AA3GD,+FAA+F;AAC/F,EAAE;AACF,mGAAmG;AACnG,gGAAgG;AAChG,iGAAiG;AACjG,sDAAsD;AACtD,EAAE;AACF,gGAAgG;AAChG,0FAA0F;AAC1F,EAAE;AACF,iGAAiG;AACjG,gGAAgG;AAChG,2FAA2F;AAC3F,iGAAiG;AACjG,gEAAgE;AAChE,oFAAuD;AAmCvD,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,CAAC;IACjC,sFAAsF;UACpF,yFAAyF;UACzF,uCAAuC;IACzC,6FAA6F;UAC3F,+FAA+F;IACjG,0FAA0F;UACxF,sEAAsE;CAC3E,CAAC,CAAC;AAEH,SAAS,YAAY,CAAC,OAAsB;IACxC,IAAI,CAAC,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxE,MAAM,IAAI,SAAS,CAAC,oEAAoE;cAClF,sFAAsF;cACtF,yEAAyE,CAAC,CAAC;IACrF,CAAC;IACD,gEAAgE;IAChE,EAAE;IACF,6FAA6F;IAC7F,4FAA4F;IAC5F,+FAA+F;IAC/F,2BAA2B;IAC3B,MAAM,MAAM,GAAG,mBAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC;IACjD,4FAA4F;IAC5F,yFAAyF;IACzF,MAAM,CAAC,GAAG,CAAC,MAAM,IAAK,MAA2C,CAAC,KAAK,CAAC;WAChE,MAA+B,CAAC;IACxC,IAAI,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,GAAG,KAAK,UAAU,IAAI,CAAC,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;QACpD,MAAM,IAAI,SAAS,CAAC,sEAAsE,CAAC,CAAC;IAChG,CAAC;IACD,OAAO,CAAC,CAAC;AACb,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,aAAa,CACzB,KAAa,EACb,OAAkC;IAElC,MAAM,IAAI,GAAG,YAAY,CAAC,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IACtD,MAAM,CAAC,GAAG,mBAAI,CAAC,aAAa,CAAC,KAAK,EAAE;QAChC,GAAG,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,CAAC,WAAW,IAAI,IAAI,EAAE;QAChE,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAChE,CAAC,CAAC;IACH,OAAO;QACH,KAAK,EAAE,CAAC,CAAC,KAAK,KAAK,IAAI;QACvB,MAAM,EAAE,CAAC,CAAC,MAAM;QAChB,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACzC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC5C,cAAc,EAAE,CAAC,GAAG,cAAc,CAAC;KACtC,CAAC;AACN,CAAC"}
|