@kya-os/checkpoint-shared 1.7.0 → 1.8.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/CHANGELOG.md +122 -9
- package/README.md +24 -7
- package/dist/beacon-nonce/index.d.mts +1 -0
- package/dist/beacon-nonce/index.d.mts.map +1 -1
- package/dist/beacon-nonce/index.d.ts +1 -0
- package/dist/beacon-nonce/index.d.ts.map +1 -1
- package/dist/beacon-nonce/index.js +9 -3
- package/dist/beacon-nonce/index.mjs +9 -4
- package/dist/beacon-nonce/index.mjs.map +1 -1
- package/dist/beacon-types/index.d.mts +23 -1
- package/dist/beacon-types/index.d.mts.map +1 -1
- package/dist/beacon-types/index.d.ts +23 -1
- package/dist/beacon-types/index.d.ts.map +1 -1
- package/dist/beacon-types/index.js +49 -4
- package/dist/beacon-types/index.mjs +47 -4
- package/dist/beacon-types/index.mjs.map +1 -1
- package/dist/{browser-posture-BLhw-MON.d.mts → browser-posture-5fPrsxx7.d.mts} +6 -6
- package/dist/{browser-posture-BLhw-MON.d.mts.map → browser-posture-5fPrsxx7.d.mts.map} +1 -1
- package/dist/{browser-posture-B8YiPa5c.d.ts → browser-posture-CCR1kh5e.d.ts} +6 -6
- package/dist/{browser-posture-B8YiPa5c.d.ts.map → browser-posture-CCR1kh5e.d.ts.map} +1 -1
- package/dist/browser-posture-proof.d.mts +16 -2
- package/dist/browser-posture-proof.d.mts.map +1 -1
- package/dist/browser-posture-proof.d.ts +16 -2
- package/dist/browser-posture-proof.d.ts.map +1 -1
- package/dist/browser-posture-proof.js +73 -17
- package/dist/browser-posture-proof.mjs +71 -17
- package/dist/browser-posture-proof.mjs.map +1 -1
- package/dist/browser-posture.d.mts +1 -1
- package/dist/browser-posture.d.ts +1 -1
- package/dist/browser-posture.js +6 -5
- package/dist/browser-posture.mjs +4 -3
- package/dist/browser-posture.mjs.map +1 -1
- package/dist/browser.d.mts +25 -4
- package/dist/browser.d.mts.map +1 -1
- package/dist/browser.d.ts +25 -4
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +1 -1
- package/dist/browser.mjs +1 -1
- package/dist/browser.mjs.map +1 -1
- package/dist/canonical-signer/index.js +30 -28
- package/dist/canonical-signer/index.mjs +31 -1
- package/dist/canonical-signer/index.mjs.map +1 -1
- package/dist/clearance.d.mts +1 -1
- package/dist/clearance.d.ts +1 -1
- package/dist/clearance.js +2 -2
- package/dist/clearance.mjs +2 -2
- package/dist/client-ip-B7w-uHfS.d.mts +55 -0
- package/dist/client-ip-B7w-uHfS.d.mts.map +1 -0
- package/dist/client-ip-B7w-uHfS.d.ts +55 -0
- package/dist/client-ip-B7w-uHfS.d.ts.map +1 -0
- package/dist/{client-ip-CW6pRyLf.js → client-ip-B8wFzJ5f.js} +117 -7
- package/dist/{client-ip-B2Xeo-GK.mjs → client-ip-DIJyr6px.mjs} +113 -9
- package/dist/client-ip-DIJyr6px.mjs.map +1 -0
- package/dist/client-ip.d.mts +2 -2
- package/dist/client-ip.d.ts +2 -2
- package/dist/client-ip.js +3 -2
- package/dist/client-ip.mjs +2 -2
- package/dist/detection-reason-mapping-B9RSkOs8.js +277 -0
- package/dist/detection-reason-mapping-BGymWIPl.mjs +249 -0
- package/dist/detection-reason-mapping-BGymWIPl.mjs.map +1 -0
- package/dist/detection-reason-mapping-CQexNvR7.d.mts +52 -0
- package/dist/detection-reason-mapping-CQexNvR7.d.mts.map +1 -0
- package/dist/detection-reason-mapping-CQexNvR7.d.ts +52 -0
- package/dist/detection-reason-mapping-CQexNvR7.d.ts.map +1 -0
- package/dist/engine-enforcing-AYEcgNsb.js +39 -0
- package/dist/engine-enforcing-B5eNfCpG.mjs +29 -0
- package/dist/engine-enforcing-B5eNfCpG.mjs.map +1 -0
- package/dist/engine-enforcing-DiOclGqf.d.mts +11 -0
- package/dist/engine-enforcing-DiOclGqf.d.mts.map +1 -0
- package/dist/engine-enforcing-DiOclGqf.d.ts +11 -0
- package/dist/engine-enforcing-DiOclGqf.d.ts.map +1 -0
- package/dist/engine-info.d.mts +2 -0
- package/dist/engine-info.d.ts +2 -0
- package/dist/engine-info.js +4 -0
- package/dist/engine-info.mjs +3 -0
- package/dist/errors/index.d.mts +5 -0
- package/dist/errors/index.d.mts.map +1 -1
- package/dist/errors/index.d.ts +5 -0
- package/dist/errors/index.d.ts.map +1 -1
- package/dist/errors/index.js +23 -4
- package/dist/errors/index.mjs +23 -4
- package/dist/errors/index.mjs.map +1 -1
- package/dist/{external-B7WI9FyQ.d.ts → external-m3Kmafwj.d.mts} +2 -2
- package/dist/{external-B7WI9FyQ.d.mts.map → external-m3Kmafwj.d.mts.map} +1 -1
- package/dist/{external-B7WI9FyQ.d.mts → external-m3Kmafwj.d.ts} +2 -2
- package/dist/{external-B7WI9FyQ.d.ts.map → external-m3Kmafwj.d.ts.map} +1 -1
- package/dist/fetcher-CivUCbhL.d.ts +44 -0
- package/dist/fetcher-CivUCbhL.d.ts.map +1 -0
- package/dist/fetcher-cXQsbm6s.d.mts +44 -0
- package/dist/fetcher-cXQsbm6s.d.mts.map +1 -0
- package/dist/gateway-client-ip.d.mts +1 -1
- package/dist/gateway-client-ip.d.ts +1 -1
- package/dist/gateway-client-ip.js +2 -2
- package/dist/gateway-client-ip.mjs +2 -2
- package/dist/host-adapter/index.d.mts +2 -2
- package/dist/host-adapter/index.d.ts +2 -2
- package/dist/host-adapter/index.js +2 -1
- package/dist/host-adapter/index.mjs +2 -2
- package/dist/{host-adapter-CwW9YB0U.js → host-adapter-0F8EpbD6.js} +59 -0
- package/dist/{host-adapter-C-4IxmqM.mjs → host-adapter-DHyISsIf.mjs} +55 -2
- package/dist/{host-adapter-C-4IxmqM.mjs.map → host-adapter-DHyISsIf.mjs.map} +1 -1
- package/dist/index-B3cHxr3O.d.mts +156 -0
- package/dist/index-B3cHxr3O.d.mts.map +1 -0
- package/dist/index-CXbOY7gB.d.ts +156 -0
- package/dist/index-CXbOY7gB.d.ts.map +1 -0
- package/dist/index.d.mts +8331 -12630
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +8331 -12630
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2515 -6663
- package/dist/index.mjs +2438 -6371
- package/dist/index.mjs.map +1 -1
- package/dist/kya-http.d.mts +2 -2
- package/dist/kya-http.d.ts +2 -2
- package/dist/kya-http.js +9 -1
- package/dist/kya-http.mjs +3 -2
- package/dist/mcpi/dashboard-config/index.d.mts.map +1 -1
- package/dist/mcpi/dashboard-config/index.d.ts.map +1 -1
- package/dist/mcpi/status-list/index.d.mts +72 -0
- package/dist/mcpi/status-list/index.d.mts.map +1 -0
- package/dist/mcpi/status-list/index.d.ts +72 -0
- package/dist/mcpi/status-list/index.d.ts.map +1 -0
- package/dist/mcpi/status-list/index.js +703 -0
- package/dist/mcpi/status-list/index.mjs +684 -0
- package/dist/mcpi/status-list/index.mjs.map +1 -0
- package/dist/patterns-B2r7M9wR.js +243 -0
- package/dist/patterns-B7dQoeE3.mjs +173 -0
- package/dist/patterns-B7dQoeE3.mjs.map +1 -0
- package/dist/patterns-Bp40cwh2.d.mts +110 -0
- package/dist/patterns-Bp40cwh2.d.mts.map +1 -0
- package/dist/patterns-Bp40cwh2.d.ts +110 -0
- package/dist/patterns-Bp40cwh2.d.ts.map +1 -0
- package/dist/patterns.d.mts +2 -0
- package/dist/patterns.d.ts +2 -0
- package/dist/patterns.js +15 -0
- package/dist/patterns.mjs +3 -0
- package/dist/policy/enforcement.d.mts +3 -2
- package/dist/policy/enforcement.d.ts +3 -2
- package/dist/policy/enforcement.js +8 -2
- package/dist/policy/enforcement.mjs +3 -2
- package/dist/policy/fetcher.d.mts +2 -0
- package/dist/policy/fetcher.d.ts +2 -0
- package/dist/policy/fetcher.js +240 -0
- package/dist/policy/fetcher.mjs +238 -0
- package/dist/policy/fetcher.mjs.map +1 -0
- package/dist/policy/schemas.d.mts +3 -0
- package/dist/policy/schemas.d.ts +3 -0
- package/dist/policy/schemas.js +27 -0
- package/dist/policy/schemas.mjs +4 -0
- package/dist/{posture-origins-Ba83O0cD.mjs → posture-origins-Ci6k4Zh1.js} +24 -173
- package/dist/{posture-origins-CdVOdiMT.js → posture-origins-DGyS4Vuk.mjs} +2 -267
- package/dist/posture-origins-DGyS4Vuk.mjs.map +1 -0
- package/dist/{project-verification-key-BccT12tQ.js → project-verification-key-B-L3D2nC.js} +1 -1
- package/dist/{project-verification-key-66N4edKn.mjs → project-verification-key-CWXdu3-n.mjs} +2 -2
- package/dist/{project-verification-key-66N4edKn.mjs.map → project-verification-key-CWXdu3-n.mjs.map} +1 -1
- package/dist/provider-registry.d.mts +2 -0
- package/dist/provider-registry.d.ts +2 -0
- package/dist/provider-registry.js +51 -0
- package/dist/provider-registry.mjs +3 -0
- package/dist/reason-codes.d.mts +2 -0
- package/dist/reason-codes.d.ts +2 -0
- package/dist/reason-codes.js +8 -0
- package/dist/reason-codes.mjs +3 -0
- package/dist/{registered-sid-hUl-bvM5.mjs → registered-sid-26hNxp6t.mjs} +11 -2
- package/dist/registered-sid-26hNxp6t.mjs.map +1 -0
- package/dist/registered-sid-D6KCaT1E.d.mts +33 -0
- package/dist/registered-sid-D6KCaT1E.d.mts.map +1 -0
- package/dist/registered-sid-D6KCaT1E.d.ts +33 -0
- package/dist/registered-sid-D6KCaT1E.d.ts.map +1 -0
- package/dist/{registered-sid-eLCNZvIF.js → registered-sid-DVGTSH0L.js} +15 -0
- package/dist/ruleset-hash-Cmv8MSO5.js +34 -0
- package/dist/ruleset-hash-IRN5eI9H.d.mts +5 -0
- package/dist/ruleset-hash-IRN5eI9H.d.mts.map +1 -0
- package/dist/ruleset-hash-IRN5eI9H.d.ts +5 -0
- package/dist/ruleset-hash-IRN5eI9H.d.ts.map +1 -0
- package/dist/ruleset-hash-QQIaLuPi.mjs +30 -0
- package/dist/ruleset-hash-QQIaLuPi.mjs.map +1 -0
- package/dist/{index-HaqfPtaL.d.ts → schemas-02CI7jgm.d.mts} +11 -183
- package/dist/schemas-02CI7jgm.d.mts.map +1 -0
- package/dist/{schemas-YhagYDx_.js → schemas-BlScS1it.js} +0 -20
- package/dist/{index-B7rTovHa.d.mts → schemas-C_3C72SR.d.ts} +11 -183
- package/dist/schemas-C_3C72SR.d.ts.map +1 -0
- package/dist/schemas-Cm4C4Z5G.js +703 -0
- package/dist/schemas-DPIT8usF.mjs +525 -0
- package/dist/schemas-DPIT8usF.mjs.map +1 -0
- package/dist/{schemas-7qodsG-Z.mjs → schemas-IHAhH3gV.mjs} +2 -16
- package/dist/schemas-IHAhH3gV.mjs.map +1 -0
- package/dist/{step-up-COlmWJN8.mjs → step-up-BK0oBi11.mjs} +91 -41
- package/dist/step-up-BK0oBi11.mjs.map +1 -0
- package/dist/{step-up-C2d8C20x.js → step-up-Bp8RAQwv.js} +117 -55
- package/dist/{step-up-BjP--hG-.d.mts → step-up-Bz12gYKT.d.mts} +28 -12
- package/dist/step-up-Bz12gYKT.d.mts.map +1 -0
- package/dist/{step-up-BjP--hG-.d.ts → step-up-Bz12gYKT.d.ts} +28 -12
- package/dist/step-up-Bz12gYKT.d.ts.map +1 -0
- package/dist/test-fixtures/index.d.mts +4 -4
- package/dist/test-fixtures/index.d.mts.map +1 -1
- package/dist/test-fixtures/index.d.ts +4 -4
- package/dist/test-fixtures/index.d.ts.map +1 -1
- package/dist/test-fixtures/index.js +4 -4
- package/dist/test-fixtures/index.mjs +4 -4
- package/dist/test-fixtures/index.mjs.map +1 -1
- package/dist/verification-BAw9N8Pt.js +181 -0
- package/dist/verification-Cvvv86TT.mjs +123 -0
- package/dist/verification-Cvvv86TT.mjs.map +1 -0
- package/fixtures/client-ip-resolution.n01.json +101 -0
- package/fixtures/reviewer-grant-revocation.v1.json +53 -81
- package/package.json +37 -2
- package/dist/client-ip-B2Xeo-GK.mjs.map +0 -1
- package/dist/client-ip-DFamiNoz.d.mts +0 -34
- package/dist/client-ip-DFamiNoz.d.mts.map +0 -1
- package/dist/client-ip-DFamiNoz.d.ts +0 -34
- package/dist/client-ip-DFamiNoz.d.ts.map +0 -1
- package/dist/index-B7rTovHa.d.mts.map +0 -1
- package/dist/index-HaqfPtaL.d.ts.map +0 -1
- package/dist/posture-origins-Ba83O0cD.mjs.map +0 -1
- package/dist/registered-sid-D3g-gZQF.d.mts +0 -11
- package/dist/registered-sid-D3g-gZQF.d.mts.map +0 -1
- package/dist/registered-sid-D3g-gZQF.d.ts +0 -11
- package/dist/registered-sid-D3g-gZQF.d.ts.map +0 -1
- package/dist/registered-sid-hUl-bvM5.mjs.map +0 -1
- package/dist/schemas-7qodsG-Z.mjs.map +0 -1
- package/dist/step-up-BjP--hG-.d.mts.map +0 -1
- package/dist/step-up-BjP--hG-.d.ts.map +0 -1
- package/dist/step-up-COlmWJN8.mjs.map +0 -1
|
@@ -0,0 +1,684 @@
|
|
|
1
|
+
import { verifyAsync } from "@noble/ed25519";
|
|
2
|
+
|
|
3
|
+
//#region src/mcpi/org-did.ts
|
|
4
|
+
/**
|
|
5
|
+
* Canonical KYA-OS per-organization did:web derivation.
|
|
6
|
+
*
|
|
7
|
+
* The org slug becomes a single path segment in
|
|
8
|
+
* `https://kya.vouched.id/orgs/<slug>/did.json`, so validation here is a
|
|
9
|
+
* security boundary shared by issuers and verifiers.
|
|
10
|
+
*/
|
|
11
|
+
/** did:web host for org issuer DIDs. */
|
|
12
|
+
const MCPI_ORG_DID_HOST = "kya.vouched.id";
|
|
13
|
+
const ORG_SLUG_PATTERN = /^[A-Za-z0-9](?:[A-Za-z0-9._-]{0,253}[A-Za-z0-9])?$/;
|
|
14
|
+
/** Build the org's `did:web` issuer DID from its slug. Throws on invalid slugs. */
|
|
15
|
+
function deriveMcpiOrgDid(slug) {
|
|
16
|
+
if (!ORG_SLUG_PATTERN.test(slug)) throw new Error(`Invalid org slug for did:web derivation: ${JSON.stringify(slug)}`);
|
|
17
|
+
return `did:web:${MCPI_ORG_DID_HOST}:orgs:${slug}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/mcpi/status-list/wire.ts
|
|
22
|
+
/**
|
|
23
|
+
* The per-issuer status list wire contract: where an org's lists live, and
|
|
24
|
+
* the two credential envelopes one list is published in.
|
|
25
|
+
*
|
|
26
|
+
* One bitstring, two envelopes. The envelope follows the credential's
|
|
27
|
+
* profile: a base-profile VC-JWT (KYA-OS SPEC §6.6) points at the
|
|
28
|
+
* StatusList2021 credential, a card-profile credential (SPEC §6.10) at a
|
|
29
|
+
* Bitstring Status List credential secured with a JwtProof2020 JWS. Both
|
|
30
|
+
* carry the same `encodedList`.
|
|
31
|
+
*
|
|
32
|
+
* Both envelopes are JSON: the credential, plus a `JwtProof2020` proof whose
|
|
33
|
+
* `jwt` is a compact JWS (EdDSA, `typ: JWT`, `kid` = a verification method in
|
|
34
|
+
* the issuer's DID document) over `{ iss, sub, iat, exp, vc }`, where `vc` is
|
|
35
|
+
* the credential exactly as served. A reader that ignores proofs reads the
|
|
36
|
+
* top-level copy; a verifier reads the signed `vc` and rejects the envelope
|
|
37
|
+
* when the two differ (`verifyStatusList`).
|
|
38
|
+
*
|
|
39
|
+
* Pure and runtime-agnostic: no I/O, no signing, no compression.
|
|
40
|
+
*/
|
|
41
|
+
/**
|
|
42
|
+
* Longest time a signed list is trusted after its `iat`. The publisher signs
|
|
43
|
+
* each response with `exp = iat + STATUS_LIST_TTL_SECONDS`, and a verifier
|
|
44
|
+
* never extends trust past it whatever the envelope claims.
|
|
45
|
+
*/
|
|
46
|
+
const STATUS_LIST_TTL_SECONDS = 300;
|
|
47
|
+
/** Tolerated verifier clock lag when checking `iat`. */
|
|
48
|
+
const STATUS_LIST_CLOCK_SKEW_SECONDS = 60;
|
|
49
|
+
/** The `proof.type` both envelopes carry. */
|
|
50
|
+
const STATUS_LIST_PROOF_TYPE = "JwtProof2020";
|
|
51
|
+
const LIST_ID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
52
|
+
const BITSTRING_SUFFIX = "/bitstring";
|
|
53
|
+
/** True when `listId` has the shape of a list id (a lowercase UUID). */
|
|
54
|
+
function isStatusListId(listId) {
|
|
55
|
+
return LIST_ID_PATTERN.test(listId);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The HTTPS origin and path of an org DID: `did:web:<host>:orgs:<slug>` →
|
|
59
|
+
* `https://<host>/orgs/<slug>`. A list lives under its issuer's DID path, so
|
|
60
|
+
* its host is the issuer DID's did:web host, never a separate literal.
|
|
61
|
+
*/
|
|
62
|
+
function orgDidBase(issuer) {
|
|
63
|
+
const [, , host, ...path] = issuer.split(":");
|
|
64
|
+
return `https://${host}/${path.join("/")}`;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The public URL of one envelope of an org's list:
|
|
68
|
+
* `https://<host>/orgs/<slug>/status-lists/<listId>` for StatusList2021,
|
|
69
|
+
* with `/bitstring` appended for the Bitstring Status List credential.
|
|
70
|
+
* Throws on an invalid slug or list id.
|
|
71
|
+
*/
|
|
72
|
+
function orgStatusListUrl(slug, listId, envelope = "StatusList2021") {
|
|
73
|
+
const issuer = deriveMcpiOrgDid(slug);
|
|
74
|
+
if (!isStatusListId(listId)) throw new Error(`Invalid status list id: ${JSON.stringify(listId)}`);
|
|
75
|
+
const base = `${orgDidBase(issuer)}/status-lists/${listId}`;
|
|
76
|
+
return envelope === "BitstringStatusList" ? base + BITSTRING_SUFFIX : base;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Parse an org status-list URL produced by {@link orgStatusListUrl}. Returns
|
|
80
|
+
* null for anything else (another host, a query or fragment, a bad slug or
|
|
81
|
+
* list id), so a verifier can refuse a list that is not an org's own. The
|
|
82
|
+
* URL must sit exactly under the DID path of the org its slug names, so the
|
|
83
|
+
* list is bound to that issuer.
|
|
84
|
+
*/
|
|
85
|
+
function parseOrgStatusListUrl(url) {
|
|
86
|
+
const match = /^https:\/\/[^/?#]+\/orgs\/([^/?#]+)\/status-lists\/([^/?#]+)(\/bitstring)?$/.exec(url);
|
|
87
|
+
if (!match) return null;
|
|
88
|
+
const [, slug, listId, bitstring] = match;
|
|
89
|
+
let issuer;
|
|
90
|
+
try {
|
|
91
|
+
issuer = deriveMcpiOrgDid(slug);
|
|
92
|
+
} catch {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
if (!isStatusListId(listId)) return null;
|
|
96
|
+
if (!url.startsWith(`${orgDidBase(issuer)}/status-lists/`)) return null;
|
|
97
|
+
return {
|
|
98
|
+
slug,
|
|
99
|
+
listId,
|
|
100
|
+
envelope: bitstring ? "BitstringStatusList" : "StatusList2021",
|
|
101
|
+
issuer
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const ENVELOPE_SHAPES = {
|
|
105
|
+
StatusList2021: {
|
|
106
|
+
context: ["https://www.w3.org/2018/credentials/v1", "https://w3id.org/vc/status-list/2021/v1"],
|
|
107
|
+
credentialType: "StatusList2021Credential",
|
|
108
|
+
subjectType: "StatusList2021"
|
|
109
|
+
},
|
|
110
|
+
BitstringStatusList: {
|
|
111
|
+
context: ["https://www.w3.org/ns/credentials/v2"],
|
|
112
|
+
credentialType: "BitstringStatusListCredential",
|
|
113
|
+
subjectType: "BitstringStatusList"
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
/** The credential and subject `type` values of an envelope. */
|
|
117
|
+
function statusListTypes(envelope) {
|
|
118
|
+
const { credentialType, subjectType } = ENVELOPE_SHAPES[envelope];
|
|
119
|
+
return {
|
|
120
|
+
credentialType,
|
|
121
|
+
subjectType
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function isoSeconds(seconds) {
|
|
125
|
+
return (/* @__PURE__ */ new Date(seconds * 1e3)).toISOString().replace(".000Z", "Z");
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Build one envelope's credential and the claims to sign. The StatusList2021
|
|
129
|
+
* credential carries `issuanceDate` and `expirationDate`; the Bitstring
|
|
130
|
+
* Status List credential carries `validUntil`. The credential's expiry and the
|
|
131
|
+
* JWT `exp` are the same instant.
|
|
132
|
+
*/
|
|
133
|
+
function buildStatusListCredential(input) {
|
|
134
|
+
const shape = ENVELOPE_SHAPES[input.envelope];
|
|
135
|
+
const expiresAt = input.issuedAt + 300;
|
|
136
|
+
const listRef = `${input.url}#list`;
|
|
137
|
+
const validity = input.envelope === "StatusList2021" ? {
|
|
138
|
+
issuanceDate: isoSeconds(input.issuedAt),
|
|
139
|
+
expirationDate: isoSeconds(expiresAt)
|
|
140
|
+
} : { validUntil: isoSeconds(expiresAt) };
|
|
141
|
+
const credential = {
|
|
142
|
+
"@context": [...shape.context],
|
|
143
|
+
id: input.url,
|
|
144
|
+
type: ["VerifiableCredential", shape.credentialType],
|
|
145
|
+
issuer: input.issuer,
|
|
146
|
+
...validity,
|
|
147
|
+
credentialSubject: {
|
|
148
|
+
id: listRef,
|
|
149
|
+
type: shape.subjectType,
|
|
150
|
+
statusPurpose: input.purpose,
|
|
151
|
+
encodedList: input.encodedList
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
return {
|
|
155
|
+
credential,
|
|
156
|
+
claims: {
|
|
157
|
+
iss: input.issuer,
|
|
158
|
+
sub: listRef,
|
|
159
|
+
iat: input.issuedAt,
|
|
160
|
+
exp: expiresAt,
|
|
161
|
+
vc: credential
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
/** The served envelope: the credential plus its `JwtProof2020` proof. */
|
|
166
|
+
function statusListEnvelopeBody(credential, jwt) {
|
|
167
|
+
return {
|
|
168
|
+
...credential,
|
|
169
|
+
proof: {
|
|
170
|
+
type: STATUS_LIST_PROOF_TYPE,
|
|
171
|
+
jwt
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region src/mcpi/status-list/bits.ts
|
|
178
|
+
/**
|
|
179
|
+
* The status bitstring and its `encodedList` form.
|
|
180
|
+
*
|
|
181
|
+
* Bit order is the W3C status-list one (StatusList2021, Bitstring Status List):
|
|
182
|
+
* index `i` is bit `7 - i % 8` of byte `floor(i / 8)`, most significant bit
|
|
183
|
+
* first. `encodedList` is the multibase base64url (`u` prefix) of the GZIP of
|
|
184
|
+
* the raw bitstring.
|
|
185
|
+
*
|
|
186
|
+
* Decoding is bounded and strict, like the Rust engine's `gzip.rs`: the payload
|
|
187
|
+
* is exactly ONE gzip member (RFC 1952). Its header is parsed here (magic,
|
|
188
|
+
* method, reserved flags, optional fields, header CRC16); only the raw deflate
|
|
189
|
+
* stream goes to the WHATWG `DecompressionStream`, so the same code runs in
|
|
190
|
+
* Node, Workers and browsers. The deflate stream must end exactly where the
|
|
191
|
+
* 8-byte trailer starts (a second member or trailing bytes reject), the
|
|
192
|
+
* inflated output must be exactly `size / 8` bytes (inflation stops at the
|
|
193
|
+
* first byte past that), and the trailer's CRC32 and ISIZE must match.
|
|
194
|
+
*/
|
|
195
|
+
/**
|
|
196
|
+
* Bits per org status list: 131,072 (16 KiB), the W3C-recommended minimum for
|
|
197
|
+
* herd privacy. Every org list has this size, so a verifier knows exactly how
|
|
198
|
+
* many bytes a list inflates to.
|
|
199
|
+
*/
|
|
200
|
+
const STATUS_LIST_SIZE = 131072;
|
|
201
|
+
const MULTIBASE_BASE64URL = "u";
|
|
202
|
+
const BASE64URL_PATTERN = /^[A-Za-z0-9_-]*$/;
|
|
203
|
+
/** Thrown when a bitstring or `encodedList` cannot be decoded. */
|
|
204
|
+
var StatusBitstringError = class extends Error {
|
|
205
|
+
constructor(message) {
|
|
206
|
+
super(message);
|
|
207
|
+
this.name = "StatusBitstringError";
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
/** Byte offset and mask of status index `index`. */
|
|
211
|
+
function locate(bits, index) {
|
|
212
|
+
if (!Number.isSafeInteger(index) || index < 0 || index >= bits.length * 8) throw new StatusBitstringError(`status index ${index} is outside a ${bits.length * 8}-bit list`);
|
|
213
|
+
return {
|
|
214
|
+
byte: index >> 3,
|
|
215
|
+
mask: 128 >> (index & 7)
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
/** Whether status index `index` is set. Throws when it is outside the list. */
|
|
219
|
+
function isStatusBitSet(bits, index) {
|
|
220
|
+
const { byte, mask } = locate(bits, index);
|
|
221
|
+
return (bits[byte] & mask) !== 0;
|
|
222
|
+
}
|
|
223
|
+
/** A copy of `bits` with status index `index` set. */
|
|
224
|
+
function withStatusBitSet(bits, index) {
|
|
225
|
+
const { byte, mask } = locate(bits, index);
|
|
226
|
+
const next = new Uint8Array(bits);
|
|
227
|
+
next[byte] |= mask;
|
|
228
|
+
return next;
|
|
229
|
+
}
|
|
230
|
+
/** Every set status index, ascending. */
|
|
231
|
+
function setStatusIndices(bits) {
|
|
232
|
+
const indices = [];
|
|
233
|
+
bits.forEach((value, byte) => {
|
|
234
|
+
for (let bit = 0; bit < 8; bit++) if (value & 128 >> bit) indices.push(byte * 8 + bit);
|
|
235
|
+
});
|
|
236
|
+
return indices;
|
|
237
|
+
}
|
|
238
|
+
/** A zeroed bitstring of `size` bits with `indices` set. */
|
|
239
|
+
function statusBitsFrom(size, indices) {
|
|
240
|
+
if (!Number.isSafeInteger(size) || size <= 0 || size % 8 !== 0) throw new StatusBitstringError(`status list size ${size} is not a positive multiple of 8`);
|
|
241
|
+
let bits = new Uint8Array(size / 8);
|
|
242
|
+
for (const index of indices) bits = withStatusBitSet(bits, index);
|
|
243
|
+
return bits;
|
|
244
|
+
}
|
|
245
|
+
function toBase64Url(bytes) {
|
|
246
|
+
let binary = "";
|
|
247
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
248
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
249
|
+
}
|
|
250
|
+
function fromBase64Url(text) {
|
|
251
|
+
if (!BASE64URL_PATTERN.test(text) || text.length % 4 === 1) throw new StatusBitstringError("encodedList is not base64url");
|
|
252
|
+
const padded = text.replace(/-/g, "+").replace(/_/g, "/") + "=".repeat((4 - text.length % 4) % 4);
|
|
253
|
+
const binary = atob(padded);
|
|
254
|
+
const bytes = new Uint8Array(binary.length);
|
|
255
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
256
|
+
return bytes;
|
|
257
|
+
}
|
|
258
|
+
let crcTable;
|
|
259
|
+
/** CRC-32 (IEEE 802.3, the gzip trailer's checksum). */
|
|
260
|
+
function crc32(bytes) {
|
|
261
|
+
if (!crcTable) {
|
|
262
|
+
crcTable = /* @__PURE__ */ new Uint32Array(256);
|
|
263
|
+
for (let n = 0; n < 256; n++) {
|
|
264
|
+
let c = n;
|
|
265
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
266
|
+
crcTable[n] = c >>> 0;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
let crc = 4294967295;
|
|
270
|
+
for (const byte of bytes) crc = crcTable[(crc ^ byte) & 255] ^ crc >>> 8;
|
|
271
|
+
return (crc ^ 4294967295) >>> 0;
|
|
272
|
+
}
|
|
273
|
+
const GZIP_FHCRC = 2;
|
|
274
|
+
const GZIP_FEXTRA = 4;
|
|
275
|
+
const GZIP_FNAME = 8;
|
|
276
|
+
const GZIP_FCOMMENT = 16;
|
|
277
|
+
const GZIP_FLAG_RESERVED = 224;
|
|
278
|
+
const GZIP_FIXED_HEADER_LENGTH = 10;
|
|
279
|
+
const GZIP_TRAILER_LENGTH = 8;
|
|
280
|
+
const truncatedMember = () => new StatusBitstringError("the gzip member is truncated");
|
|
281
|
+
const notGzip = () => new StatusBitstringError("not a gzip member");
|
|
282
|
+
const le16 = (bytes, at) => bytes[at] | bytes[at + 1] << 8;
|
|
283
|
+
const le32 = (bytes, at) => (bytes[at] | bytes[at + 1] << 8 | bytes[at + 2] << 16 | bytes[at + 3] << 24) >>> 0;
|
|
284
|
+
/** Length of a gzip member's header (fixed part plus its optional fields), after checking it. */
|
|
285
|
+
function gzipHeaderLength(member) {
|
|
286
|
+
if (member.length < GZIP_FIXED_HEADER_LENGTH) throw truncatedMember();
|
|
287
|
+
if (member[0] !== 31 || member[1] !== 139 || member[2] !== 8) throw notGzip();
|
|
288
|
+
const flags = member[3];
|
|
289
|
+
if (flags & GZIP_FLAG_RESERVED) throw notGzip();
|
|
290
|
+
let position = GZIP_FIXED_HEADER_LENGTH;
|
|
291
|
+
if (flags & GZIP_FEXTRA) {
|
|
292
|
+
if (position + 2 > member.length) throw truncatedMember();
|
|
293
|
+
position += 2 + le16(member, position);
|
|
294
|
+
if (position > member.length) throw truncatedMember();
|
|
295
|
+
}
|
|
296
|
+
for (const flag of [GZIP_FNAME, GZIP_FCOMMENT]) if (flags & flag) {
|
|
297
|
+
const nul = member.indexOf(0, position);
|
|
298
|
+
if (nul < 0) throw truncatedMember();
|
|
299
|
+
position = nul + 1;
|
|
300
|
+
}
|
|
301
|
+
if (flags & GZIP_FHCRC) {
|
|
302
|
+
if (position + 2 > member.length) throw truncatedMember();
|
|
303
|
+
if (le16(member, position) !== (crc32(member.subarray(0, position)) & 65535)) throw new StatusBitstringError("the gzip header CRC does not match");
|
|
304
|
+
position += 2;
|
|
305
|
+
}
|
|
306
|
+
return position;
|
|
307
|
+
}
|
|
308
|
+
/** Whether `deflated` inflates as one complete raw deflate stream (within `maxOutputBytes`). */
|
|
309
|
+
async function inflatesCompletely(deflated, maxOutputBytes) {
|
|
310
|
+
try {
|
|
311
|
+
await pipeThrough(deflated, new DecompressionStream("deflate-raw"), maxOutputBytes);
|
|
312
|
+
return true;
|
|
313
|
+
} catch {
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Inflate the single gzip member `member` to exactly `expectedLength` bytes.
|
|
319
|
+
* The deflate stream must end exactly at the trailer: `DecompressionStream`
|
|
320
|
+
* may ignore bytes after a stream's final block, so the stream is also
|
|
321
|
+
* inflated without its last byte, which must then fail. If it still
|
|
322
|
+
* completes, bytes (a second member, junk) sit between the stream and the
|
|
323
|
+
* trailer.
|
|
324
|
+
*/
|
|
325
|
+
async function gunzipExact(member, expectedLength) {
|
|
326
|
+
const bodyStart = gzipHeaderLength(member);
|
|
327
|
+
if (member.length - bodyStart < GZIP_TRAILER_LENGTH) throw truncatedMember();
|
|
328
|
+
const trailerStart = member.length - GZIP_TRAILER_LENGTH;
|
|
329
|
+
const deflated = member.subarray(bodyStart, trailerStart);
|
|
330
|
+
const bits = await pipeThrough(deflated, new DecompressionStream("deflate-raw"), expectedLength);
|
|
331
|
+
if (deflated.length > 0 && await inflatesCompletely(deflated.subarray(0, -1), expectedLength)) throw new StatusBitstringError("data follows the deflate stream before the gzip trailer");
|
|
332
|
+
if (le32(member, trailerStart) !== crc32(bits)) throw new StatusBitstringError("the gzip CRC32 does not match");
|
|
333
|
+
if (le32(member, trailerStart + 4) !== bits.length >>> 0) throw new StatusBitstringError("the gzip ISIZE does not match");
|
|
334
|
+
return bits;
|
|
335
|
+
}
|
|
336
|
+
async function pipeThrough(bytes, transform, maxOutputBytes) {
|
|
337
|
+
const reader = new Blob([new Uint8Array(bytes)]).stream().pipeThrough(transform).getReader();
|
|
338
|
+
const chunks = [];
|
|
339
|
+
let total = 0;
|
|
340
|
+
try {
|
|
341
|
+
for (;;) {
|
|
342
|
+
const { done, value } = await reader.read();
|
|
343
|
+
if (done) break;
|
|
344
|
+
total += value.length;
|
|
345
|
+
if (total > maxOutputBytes) throw new StatusBitstringError(`bitstring inflates past ${maxOutputBytes} bytes`);
|
|
346
|
+
chunks.push(value);
|
|
347
|
+
}
|
|
348
|
+
} catch (error) {
|
|
349
|
+
await reader.cancel().catch(() => void 0);
|
|
350
|
+
if (error instanceof StatusBitstringError) throw error;
|
|
351
|
+
throw new StatusBitstringError(`bitstring is not a valid compressed stream: ${error instanceof Error ? error.message : String(error)}`);
|
|
352
|
+
}
|
|
353
|
+
const out = new Uint8Array(total);
|
|
354
|
+
let offset = 0;
|
|
355
|
+
for (const chunk of chunks) {
|
|
356
|
+
out.set(chunk, offset);
|
|
357
|
+
offset += chunk.length;
|
|
358
|
+
}
|
|
359
|
+
return out;
|
|
360
|
+
}
|
|
361
|
+
/** The `encodedList` of a raw bitstring: `u` + base64url(gzip(bits)). */
|
|
362
|
+
async function encodeStatusBits(bits) {
|
|
363
|
+
const gzipped = await pipeThrough(bits, new CompressionStream("gzip"), Number.MAX_SAFE_INTEGER);
|
|
364
|
+
return MULTIBASE_BASE64URL + toBase64Url(gzipped);
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Decode an `encodedList` to the raw bitstring of a `size`-bit list. Rejects
|
|
368
|
+
* with {@link StatusBitstringError} unless the payload is multibase base64url
|
|
369
|
+
* of exactly one complete gzip member, nothing after it, whose CRC32 and ISIZE
|
|
370
|
+
* check out and which inflates to exactly `size / 8` bytes (inflation stops at
|
|
371
|
+
* the first byte past that).
|
|
372
|
+
*/
|
|
373
|
+
async function decodeStatusBits(encodedList, size = STATUS_LIST_SIZE) {
|
|
374
|
+
if (!Number.isSafeInteger(size) || size <= 0 || size % 8 !== 0) throw new StatusBitstringError(`status list size ${size} is not a positive multiple of 8`);
|
|
375
|
+
if (!encodedList.startsWith(MULTIBASE_BASE64URL)) throw new StatusBitstringError("encodedList lacks the multibase base64url prefix");
|
|
376
|
+
const bits = await gunzipExact(fromBase64Url(encodedList.slice(1)), size / 8);
|
|
377
|
+
if (bits.length !== size / 8) throw new StatusBitstringError(`bitstring is ${bits.length} bytes, expected ${size / 8}`);
|
|
378
|
+
return bits;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
//#endregion
|
|
382
|
+
//#region ../../node_modules/.pnpm/canonicalize@3.0.0/node_modules/canonicalize/lib/canonicalize.js
|
|
383
|
+
function canonicalize(object, seen = /* @__PURE__ */ new Set()) {
|
|
384
|
+
if (typeof object === "number" && isNaN(object)) throw new Error("NaN is not allowed");
|
|
385
|
+
if (typeof object === "number" && !isFinite(object)) throw new Error("Infinity is not allowed");
|
|
386
|
+
if (object === null || typeof object !== "object") return JSON.stringify(object);
|
|
387
|
+
if (typeof object.toJSON === "function") {
|
|
388
|
+
if (seen.has(object)) throw new Error("Circular reference detected");
|
|
389
|
+
seen.add(object);
|
|
390
|
+
const result = canonicalize(object.toJSON(), seen);
|
|
391
|
+
seen.delete(object);
|
|
392
|
+
return result;
|
|
393
|
+
}
|
|
394
|
+
if (seen.has(object)) throw new Error("Circular reference detected");
|
|
395
|
+
seen.add(object);
|
|
396
|
+
let result;
|
|
397
|
+
if (Array.isArray(object)) result = `[${object.map((cv) => {
|
|
398
|
+
return canonicalize(cv === void 0 || typeof cv === "symbol" ? null : cv, seen);
|
|
399
|
+
}).join(",")}]`;
|
|
400
|
+
else {
|
|
401
|
+
const parts = [];
|
|
402
|
+
for (const key of Object.keys(object).sort()) {
|
|
403
|
+
if (object[key] === void 0 || typeof object[key] === "symbol") continue;
|
|
404
|
+
parts.push(`${canonicalize(key)}:${canonicalize(object[key], seen)}`);
|
|
405
|
+
}
|
|
406
|
+
result = `{${parts.join(",")}}`;
|
|
407
|
+
}
|
|
408
|
+
seen.delete(object);
|
|
409
|
+
return result;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
//#endregion
|
|
413
|
+
//#region src/mcpi/status-list/verify.ts
|
|
414
|
+
/**
|
|
415
|
+
* Verify a signed status list before trusting a bit (design §3.5).
|
|
416
|
+
*
|
|
417
|
+
* `verifyStatusList` accepts either envelope (see `./wire`) and returns the
|
|
418
|
+
* bitstring only when every check below passes. It reads the bits from the
|
|
419
|
+
* SIGNED `vc`, never from the unsigned copy beside it, and rejects the
|
|
420
|
+
* envelope when the two differ. Any failure rejects with a
|
|
421
|
+
* {@link StatusListVerificationError}; a caller never treats a rejection as
|
|
422
|
+
* "not revoked".
|
|
423
|
+
*
|
|
424
|
+
* The engine's `verify_status_list` (`kya-os-engine` `status_list`, bound as
|
|
425
|
+
* `engineVerifyStatusList` in `@kya-os/checkpoint-wasm-runtime`) is the
|
|
426
|
+
* verifier for every SDK host. This TypeScript verifier is the parity oracle
|
|
427
|
+
* the engine is checked against
|
|
428
|
+
* (`tests/cross-runtime-parity/status-list/fixtures/vectors.json`). Two
|
|
429
|
+
* production callers still use it until they move onto the engine: the Fly
|
|
430
|
+
* gateway (`apps/gateway/src/lib/did-kid-refresh.ts`) and the dashboard's
|
|
431
|
+
* signed-list reader (`apps/web/lib/authz/adapters/signed-status-list-reader.ts`).
|
|
432
|
+
* New callers use the engine's binding.
|
|
433
|
+
*
|
|
434
|
+
* Checks, in order (the shared parity vectors pin the error code of each):
|
|
435
|
+
* 1. `malformed`: the body is an object with `proof.type: JwtProof2020` and a
|
|
436
|
+
* compact JWS of at most 256 KiB whose header is `alg: EdDSA`, `typ: JWT`,
|
|
437
|
+
* a string `kid` and no `crit`.
|
|
438
|
+
* 2. `issuer_mismatch`: the JWT `iss` is the expected issuer (checked before
|
|
439
|
+
* any DID resolution, so a list never makes us resolve a foreign DID).
|
|
440
|
+
* 3. `did_unresolved`: the issuer's DID document resolves and names itself.
|
|
441
|
+
* 4. `unknown_kid`: `kid` is a DID URL under the issuer naming a verification
|
|
442
|
+
* method the document lists for `assertionMethod`, whose `publicKeyJwk` is
|
|
443
|
+
* an Ed25519 key (`publicKeyMultibase` and other encodings are refused).
|
|
444
|
+
* 5. `bad_signature`: the JWS verifies with that key under strict RFC 8032
|
|
445
|
+
* rules (canonical encodings, small-order keys refused), as the Rust
|
|
446
|
+
* engine's `verify_strict` does.
|
|
447
|
+
* 6. `malformed`: `vc` is one of the two envelope credentials.
|
|
448
|
+
* 7. `issuer_mismatch`: `vc.issuer` is the issuer.
|
|
449
|
+
* 8. `url_mismatch`: `vc.id` is the URL the credential named (any
|
|
450
|
+
* `#fragment` stripped), and `sub` and `vc.credentialSubject.id` are that
|
|
451
|
+
* URL + `#list`.
|
|
452
|
+
* 9. `purpose_mismatch`: `statusPurpose` is the expected purpose.
|
|
453
|
+
* 10. `malformed` / `not_yet_valid` / `expired`: `iat` and `exp` are integers
|
|
454
|
+
* with `exp > iat`; every present validity field (`validFrom`,
|
|
455
|
+
* `issuanceDate`, `validUntil`, `expirationDate`) is a strict RFC 3339
|
|
456
|
+
* date-time ({@link parseRfc3339}); the latest of `iat` and the two start
|
|
457
|
+
* fields is at most 60 s ahead of now, and now is before the earliest of
|
|
458
|
+
* `exp`, `iat + 300 s` and the two end fields.
|
|
459
|
+
* 11. `copy_mismatch`: the envelope without `proof` equals the signed `vc`.
|
|
460
|
+
* 12. `decode_failed`: the signed `encodedList` decodes to exactly `size / 8`
|
|
461
|
+
* bytes (multibase, ONE gzip member and nothing after it, CRC32 and ISIZE,
|
|
462
|
+
* no bomb).
|
|
463
|
+
*
|
|
464
|
+
* A signature segment or JWK `x` that is not base64url (e.g. a length of 1 mod
|
|
465
|
+
* 4) is `bad_signature` / `unknown_kid`, never an uncaught decoding error.
|
|
466
|
+
*/
|
|
467
|
+
/** A rejected status list. `code` names the first check that failed. */
|
|
468
|
+
var StatusListVerificationError = class extends Error {
|
|
469
|
+
code;
|
|
470
|
+
constructor(code, message) {
|
|
471
|
+
super(`status list rejected (${code}): ${message}`);
|
|
472
|
+
this.code = code;
|
|
473
|
+
this.name = "StatusListVerificationError";
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
const MAX_JWS_LENGTH = 262144;
|
|
477
|
+
const JWS_SEGMENT = /^[A-Za-z0-9_-]+$/;
|
|
478
|
+
function reject(code, message) {
|
|
479
|
+
throw new StatusListVerificationError(code, message);
|
|
480
|
+
}
|
|
481
|
+
function isObject(value) {
|
|
482
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* The bytes of an unpadded base64url string, or null when it is not one (a
|
|
486
|
+
* length of 1 mod 4 encodes no whole byte). Non-zero trailing bits are
|
|
487
|
+
* accepted, as `atob` and the Rust engine's decoder both do.
|
|
488
|
+
*/
|
|
489
|
+
function base64UrlBytes(segment) {
|
|
490
|
+
if (!JWS_SEGMENT.test(segment) || segment.length % 4 === 1) return null;
|
|
491
|
+
const padded = segment.replace(/-/g, "+").replace(/_/g, "/") + "=".repeat((4 - segment.length % 4) % 4);
|
|
492
|
+
let binary;
|
|
493
|
+
try {
|
|
494
|
+
binary = atob(padded);
|
|
495
|
+
} catch {
|
|
496
|
+
return null;
|
|
497
|
+
}
|
|
498
|
+
const bytes = new Uint8Array(binary.length);
|
|
499
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
500
|
+
return bytes;
|
|
501
|
+
}
|
|
502
|
+
function decodeSegment(segment) {
|
|
503
|
+
const bytes = base64UrlBytes(segment);
|
|
504
|
+
let parsed;
|
|
505
|
+
try {
|
|
506
|
+
if (!bytes) throw new Error("not base64url");
|
|
507
|
+
parsed = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
508
|
+
} catch {
|
|
509
|
+
return reject("malformed", "a JWS segment is not base64url JSON");
|
|
510
|
+
}
|
|
511
|
+
if (!isObject(parsed)) reject("malformed", "a JWS segment is not a JSON object");
|
|
512
|
+
return parsed;
|
|
513
|
+
}
|
|
514
|
+
function hasType(value, wanted) {
|
|
515
|
+
return Array.isArray(value) && value.includes(wanted);
|
|
516
|
+
}
|
|
517
|
+
function idOf(value) {
|
|
518
|
+
return isObject(value) ? value.id : value;
|
|
519
|
+
}
|
|
520
|
+
/** Days since 1970-01-01 of a proleptic Gregorian date (H. Hinnant's `days_from_civil`). */
|
|
521
|
+
function daysFromCivil(year, month, day) {
|
|
522
|
+
const y = month <= 2 ? year - 1 : year;
|
|
523
|
+
const era = Math.floor(y / 400);
|
|
524
|
+
const yearOfEra = y - era * 400;
|
|
525
|
+
const dayOfYear = Math.floor((153 * ((month + 9) % 12) + 2) / 5) + day - 1;
|
|
526
|
+
const dayOfEra = yearOfEra * 365 + Math.floor(yearOfEra / 4) - Math.floor(yearOfEra / 100) + dayOfYear;
|
|
527
|
+
return era * 146097 + dayOfEra - 719468;
|
|
528
|
+
}
|
|
529
|
+
const RFC3339 = /^(\d{4})-(\d{2})-(\d{2})[Tt](\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:([Zz])|([+-])(\d{2}):(\d{2}))$/;
|
|
530
|
+
/**
|
|
531
|
+
* Unix seconds of an RFC 3339 date-time (the `xsd:dateTime` form VC validity
|
|
532
|
+
* fields use), or null. Only the full form is accepted: date, `T`, time, an
|
|
533
|
+
* optional fraction (floored to the second) and `Z` or a `±HH:MM` offset.
|
|
534
|
+
* Impossible dates and times (Feb 29 of a common year, hour 24, second 60) are
|
|
535
|
+
* refused. Matches the Rust engine's `instant::parse_rfc3339` exactly;
|
|
536
|
+
* `Date.parse` is not used because it accepts date-only, offset-less (host
|
|
537
|
+
* local time), rolled-over and RFC 2822 values.
|
|
538
|
+
*/
|
|
539
|
+
function parseRfc3339(text) {
|
|
540
|
+
const match = RFC3339.exec(text);
|
|
541
|
+
if (!match) return null;
|
|
542
|
+
const [year, month, day, hour, minute, second] = match.slice(1, 7).map(Number);
|
|
543
|
+
const daysInMonth = [
|
|
544
|
+
31,
|
|
545
|
+
year % 4 === 0 && year % 100 !== 0 || year % 400 === 0 ? 29 : 28,
|
|
546
|
+
31,
|
|
547
|
+
30,
|
|
548
|
+
31,
|
|
549
|
+
30,
|
|
550
|
+
31,
|
|
551
|
+
31,
|
|
552
|
+
30,
|
|
553
|
+
31,
|
|
554
|
+
30,
|
|
555
|
+
31
|
|
556
|
+
][month - 1];
|
|
557
|
+
if (daysInMonth === void 0 || day < 1 || day > daysInMonth || hour > 23 || minute > 59 || second > 59) return null;
|
|
558
|
+
let offset = 0;
|
|
559
|
+
if (!match[7]) {
|
|
560
|
+
const offsetHours = Number(match[9]);
|
|
561
|
+
const offsetMinutes = Number(match[10]);
|
|
562
|
+
if (offsetHours > 23 || offsetMinutes > 59) return null;
|
|
563
|
+
offset = (match[8] === "+" ? 1 : -1) * (offsetHours * 3600 + offsetMinutes * 60);
|
|
564
|
+
}
|
|
565
|
+
return daysFromCivil(year, month, day) * 86400 + hour * 3600 + minute * 60 + second - offset;
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* The instant in the credential's validity field `field`, or undefined when it
|
|
569
|
+
* is absent or null. A present value that is not an RFC 3339 date-time is
|
|
570
|
+
* `malformed`: a validity bound the verifier cannot read is never ignored.
|
|
571
|
+
*/
|
|
572
|
+
function validityInstant(vc, field) {
|
|
573
|
+
const value = vc[field];
|
|
574
|
+
if (value === void 0 || value === null) return void 0;
|
|
575
|
+
const instant = typeof value === "string" ? parseRfc3339(value) : null;
|
|
576
|
+
if (instant === null) reject("malformed", `${field} is not an RFC 3339 date-time`);
|
|
577
|
+
return instant;
|
|
578
|
+
}
|
|
579
|
+
/** The Ed25519 public key `kid` names, when the document lists it for assertions. */
|
|
580
|
+
function assertionKey(document, issuer, kid) {
|
|
581
|
+
const absolute = (id) => typeof id === "string" ? id.startsWith("#") ? issuer + id : id : void 0;
|
|
582
|
+
const methods = Array.isArray(document.verificationMethod) ? document.verificationMethod : [];
|
|
583
|
+
const listed = (Array.isArray(document.assertionMethod) ? document.assertionMethod : []).find((entry) => absolute(idOf(entry)) === kid);
|
|
584
|
+
if (listed === void 0) return null;
|
|
585
|
+
const method = isObject(listed) ? listed : methods.find((m) => isObject(m) && absolute(m.id) === kid);
|
|
586
|
+
if (!isObject(method)) return null;
|
|
587
|
+
const jwk = method.publicKeyJwk;
|
|
588
|
+
if (!isObject(jwk) || jwk.kty !== "OKP" || jwk.crv !== "Ed25519") return null;
|
|
589
|
+
if (typeof jwk.x !== "string") return null;
|
|
590
|
+
const key = base64UrlBytes(jwk.x);
|
|
591
|
+
return key?.length === 32 ? key : null;
|
|
592
|
+
}
|
|
593
|
+
function envelopeOf(vc) {
|
|
594
|
+
const subject = vc.credentialSubject;
|
|
595
|
+
if (!hasType(vc.type, "VerifiableCredential") || !isObject(subject)) return reject("malformed", "vc is not a status list credential");
|
|
596
|
+
for (const envelope of ["StatusList2021", "BitstringStatusList"]) {
|
|
597
|
+
const { credentialType, subjectType } = statusListTypes(envelope);
|
|
598
|
+
if (hasType(vc.type, credentialType) && subject.type === subjectType) return envelope;
|
|
599
|
+
}
|
|
600
|
+
return reject("malformed", "vc is neither a StatusList2021 nor a Bitstring Status List credential");
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Verify a fetched status-list envelope against its issuer's DID document and
|
|
604
|
+
* return its bits. Rejects with {@link StatusListVerificationError}.
|
|
605
|
+
*/
|
|
606
|
+
async function verifyStatusList(body, options) {
|
|
607
|
+
const { issuer } = options;
|
|
608
|
+
const fragment = options.url.indexOf("#");
|
|
609
|
+
const url = fragment >= 0 ? options.url.slice(0, fragment) : options.url;
|
|
610
|
+
const purpose = options.purpose ?? "revocation";
|
|
611
|
+
const size = options.size ?? 131072;
|
|
612
|
+
const now = options.now ?? Math.floor(Date.now() / 1e3);
|
|
613
|
+
if (!isObject(body) || !isObject(body.proof)) reject("malformed", "no proof object");
|
|
614
|
+
const { proof, ...unsigned } = body;
|
|
615
|
+
if (proof.type !== "JwtProof2020" || typeof proof.jwt !== "string") reject("malformed", `proof is not a ${STATUS_LIST_PROOF_TYPE}`);
|
|
616
|
+
const jws = proof.jwt;
|
|
617
|
+
const segments = jws.split(".");
|
|
618
|
+
if (jws.length > MAX_JWS_LENGTH || segments.length !== 3 || !segments.every((s) => JWS_SEGMENT.test(s))) reject("malformed", "proof.jwt is not a compact JWS");
|
|
619
|
+
const [headerSegment, payloadSegment, signatureSegment] = segments;
|
|
620
|
+
const header = decodeSegment(headerSegment);
|
|
621
|
+
const payload = decodeSegment(payloadSegment);
|
|
622
|
+
if (header.alg !== "EdDSA" || header.typ !== "JWT" || "crit" in header || typeof header.kid !== "string") reject("malformed", "JWS header must be alg EdDSA, typ JWT, a kid and no crit");
|
|
623
|
+
const kid = header.kid;
|
|
624
|
+
if (payload.iss !== issuer) reject("issuer_mismatch", "the list is not signed by the issuer");
|
|
625
|
+
if (!kid.startsWith(`${issuer}#`)) reject("unknown_kid", "kid is not under the issuer DID");
|
|
626
|
+
let document;
|
|
627
|
+
try {
|
|
628
|
+
document = await options.resolveDidDocument(issuer);
|
|
629
|
+
} catch {
|
|
630
|
+
document = null;
|
|
631
|
+
}
|
|
632
|
+
if (!isObject(document) || document.id !== issuer) reject("did_unresolved", "the issuer DID document did not resolve");
|
|
633
|
+
const key = assertionKey(document, issuer, kid);
|
|
634
|
+
if (!key) reject("unknown_kid", "kid names no Ed25519 assertion key of the issuer");
|
|
635
|
+
const signature = base64UrlBytes(signatureSegment);
|
|
636
|
+
const signingInput = new TextEncoder().encode(`${headerSegment}.${payloadSegment}`);
|
|
637
|
+
let valid = false;
|
|
638
|
+
try {
|
|
639
|
+
valid = signature?.length === 64 && await verifyAsync(signature, signingInput, key, { zip215: false });
|
|
640
|
+
} catch {
|
|
641
|
+
valid = false;
|
|
642
|
+
}
|
|
643
|
+
if (!valid) reject("bad_signature", "the JWS signature does not verify");
|
|
644
|
+
const vc = payload.vc;
|
|
645
|
+
if (!isObject(vc)) return reject("malformed", "no vc claim");
|
|
646
|
+
const envelope = envelopeOf(vc);
|
|
647
|
+
const subject = vc.credentialSubject;
|
|
648
|
+
if (idOf(vc.issuer) !== issuer) reject("issuer_mismatch", "vc.issuer is not the issuer");
|
|
649
|
+
if (vc.id !== url || payload.sub !== `${url}#list` || subject.id !== `${url}#list`) reject("url_mismatch", "the list is not the one the credential names");
|
|
650
|
+
if (subject.statusPurpose !== purpose) reject("purpose_mismatch", `statusPurpose is not ${purpose}`);
|
|
651
|
+
const { iat, exp } = payload;
|
|
652
|
+
if (!Number.isSafeInteger(iat) || !Number.isSafeInteger(exp) || exp <= iat) reject("malformed", "iat and exp must be integers with exp after iat");
|
|
653
|
+
const issuedAt = iat;
|
|
654
|
+
const [validFrom, issuanceDate, validUntil, expirationDate] = [
|
|
655
|
+
"validFrom",
|
|
656
|
+
"issuanceDate",
|
|
657
|
+
"validUntil",
|
|
658
|
+
"expirationDate"
|
|
659
|
+
].map((field) => validityInstant(vc, field));
|
|
660
|
+
if (Math.max(issuedAt, validFrom ?? issuedAt, issuanceDate ?? issuedAt) > now + 60) reject("not_yet_valid", "the list is not valid yet");
|
|
661
|
+
const expiresAt = Math.min(exp, issuedAt + 300, validUntil ?? Number.MAX_SAFE_INTEGER, expirationDate ?? Number.MAX_SAFE_INTEGER);
|
|
662
|
+
if (now >= expiresAt) reject("expired", "the list has expired");
|
|
663
|
+
if (canonicalize(unsigned) !== canonicalize(vc)) reject("copy_mismatch", "the unsigned credential differs from the signed one");
|
|
664
|
+
if (typeof subject.encodedList !== "string") reject("malformed", "no encodedList");
|
|
665
|
+
let bits;
|
|
666
|
+
try {
|
|
667
|
+
bits = await decodeStatusBits(subject.encodedList, size);
|
|
668
|
+
} catch (error) {
|
|
669
|
+
if (error instanceof StatusBitstringError) reject("decode_failed", error.message);
|
|
670
|
+
throw error;
|
|
671
|
+
}
|
|
672
|
+
return {
|
|
673
|
+
envelope,
|
|
674
|
+
url,
|
|
675
|
+
issuer,
|
|
676
|
+
issuedAt,
|
|
677
|
+
expiresAt,
|
|
678
|
+
bits
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
//#endregion
|
|
683
|
+
export { STATUS_LIST_CLOCK_SKEW_SECONDS, STATUS_LIST_PROOF_TYPE, STATUS_LIST_SIZE, STATUS_LIST_TTL_SECONDS, StatusBitstringError, StatusListVerificationError, buildStatusListCredential, decodeStatusBits, encodeStatusBits, isStatusBitSet, isStatusListId, orgStatusListUrl, parseOrgStatusListUrl, parseRfc3339, setStatusIndices, statusBitsFrom, statusListEnvelopeBody, statusListTypes, verifyStatusList, withStatusBitSet };
|
|
684
|
+
//# sourceMappingURL=index.mjs.map
|