@coordination-network/credential 0.0.1 → 0.1.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 +17 -45
- package/dist/credential-message.d.ts +5 -1
- package/dist/credential-message.js +17 -1
- package/dist/crypto.d.ts +3 -11
- package/dist/crypto.js +1 -31
- package/dist/errors.d.ts +1 -1
- package/dist/holder-ownership.d.ts +12 -0
- package/dist/holder-ownership.js +26 -0
- package/dist/index.d.ts +1 -5
- package/dist/index.js +1 -5
- package/dist/issue-credential.d.ts +5 -1
- package/dist/issue-credential.js +24 -2
- package/dist/revocation.d.ts +6 -5
- package/dist/revocation.js +9 -12
- package/dist/signer.d.ts +2 -13
- package/dist/types.d.ts +2 -38
- package/dist/zk-proof.d.ts +10 -0
- package/dist/zk-proof.js +14 -3
- package/package.json +6 -9
- package/src/credential-message.ts +22 -2
- package/src/crypto.ts +3 -34
- package/src/errors.ts +6 -1
- package/src/holder-ownership.ts +30 -0
- package/src/index.ts +1 -5
- package/src/issue-credential.ts +43 -6
- package/src/revocation.ts +10 -14
- package/src/signer.ts +2 -14
- package/src/types.ts +2 -36
- package/src/zk-proof.ts +19 -3
- package/dist/decode.d.ts +0 -8
- package/dist/decode.js +0 -27
- package/dist/issue.d.ts +0 -9
- package/dist/issue.js +0 -65
- package/dist/present.d.ts +0 -32
- package/dist/present.js +0 -73
- package/dist/profile.d.ts +0 -32
- package/dist/profile.js +0 -122
- package/dist/verify.d.ts +0 -29
- package/dist/verify.js +0 -102
- package/src/decode.ts +0 -25
- package/src/issue.ts +0 -83
- package/src/present.ts +0 -101
- package/src/profile.ts +0 -133
- package/src/verify.ts +0 -118
package/dist/profile.d.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { ApprovalCredentialClaims, Hex32, SdJwtIssueParams } from "./types";
|
|
2
|
-
/**
|
|
3
|
-
* The claims profile of the delegated user approval credential (spec 07):
|
|
4
|
-
* payload construction, the disclosure frame, and the shape validator that
|
|
5
|
-
* backs the MALFORMED verdict.
|
|
6
|
-
*/
|
|
7
|
-
export declare const APPROVAL_CREDENTIAL_VCT = "urn:cn:credential:approval:v1";
|
|
8
|
-
/** Only the two approvalScope members are disclosable; everything else is plain. */
|
|
9
|
-
export declare const DISCLOSURE_FRAME: {
|
|
10
|
-
readonly approvalScope: {
|
|
11
|
-
readonly _sd: readonly ["programme", "country"];
|
|
12
|
-
};
|
|
13
|
-
};
|
|
14
|
-
export declare function networkIdToUrn(id: Hex32): string;
|
|
15
|
-
export declare function urnToNetworkId(urn: string): Hex32 | null;
|
|
16
|
-
/** Assembles the full signed payload; the disclosure frame is applied at issue time. */
|
|
17
|
-
export declare function buildPayload(params: SdJwtIssueParams, jti: string): ApprovalCredentialClaims;
|
|
18
|
-
type ClaimsCheck = {
|
|
19
|
-
ok: true;
|
|
20
|
-
claims: ApprovalCredentialClaims;
|
|
21
|
-
} | {
|
|
22
|
-
ok: false;
|
|
23
|
-
message: string;
|
|
24
|
-
};
|
|
25
|
-
/**
|
|
26
|
-
* Validates the reconstructed payload of a verified token against the
|
|
27
|
-
* profile. Strict on both sides: a missing required claim and an unknown
|
|
28
|
-
* plain claim both fail (spec 07; a tampered disclosure surfaces here as a
|
|
29
|
-
* missing approvalScope member).
|
|
30
|
-
*/
|
|
31
|
-
export declare function validateClaims(payload: unknown): ClaimsCheck;
|
|
32
|
-
export {};
|
package/dist/profile.js
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DISCLOSURE_FRAME = exports.APPROVAL_CREDENTIAL_VCT = void 0;
|
|
4
|
-
exports.networkIdToUrn = networkIdToUrn;
|
|
5
|
-
exports.urnToNetworkId = urnToNetworkId;
|
|
6
|
-
exports.buildPayload = buildPayload;
|
|
7
|
-
exports.validateClaims = validateClaims;
|
|
8
|
-
const revocation_1 = require("./revocation");
|
|
9
|
-
/**
|
|
10
|
-
* The claims profile of the delegated user approval credential (spec 07):
|
|
11
|
-
* payload construction, the disclosure frame, and the shape validator that
|
|
12
|
-
* backs the MALFORMED verdict.
|
|
13
|
-
*/
|
|
14
|
-
exports.APPROVAL_CREDENTIAL_VCT = "urn:cn:credential:approval:v1";
|
|
15
|
-
/** Only the two approvalScope members are disclosable; everything else is plain. */
|
|
16
|
-
exports.DISCLOSURE_FRAME = {
|
|
17
|
-
approvalScope: { _sd: ["programme", "country"] }
|
|
18
|
-
};
|
|
19
|
-
const NETWORK_ID_RE = /^0x[0-9a-f]{64}$/;
|
|
20
|
-
const NETWORK_URN_RE = /^urn:cn:network:[0-9a-f]{64}$/;
|
|
21
|
-
const CONTRACT_URN_RE = /^urn:cn:contract:.+$/;
|
|
22
|
-
const UUID_URN_RE = /^urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
23
|
-
function networkIdToUrn(id) {
|
|
24
|
-
const normalized = id.toLowerCase();
|
|
25
|
-
if (!NETWORK_ID_RE.test(normalized)) {
|
|
26
|
-
throw new Error("networkId must be a 0x-prefixed 32-byte hex string");
|
|
27
|
-
}
|
|
28
|
-
return `urn:cn:network:${normalized.slice(2)}`;
|
|
29
|
-
}
|
|
30
|
-
function urnToNetworkId(urn) {
|
|
31
|
-
return NETWORK_URN_RE.test(urn) ? `0x${urn.slice("urn:cn:network:".length)}` : null;
|
|
32
|
-
}
|
|
33
|
-
/** Assembles the full signed payload; the disclosure frame is applied at issue time. */
|
|
34
|
-
function buildPayload(params, jti) {
|
|
35
|
-
return {
|
|
36
|
-
vct: exports.APPROVAL_CREDENTIAL_VCT,
|
|
37
|
-
iss: networkIdToUrn(params.issuerNetworkId),
|
|
38
|
-
sub: networkIdToUrn(params.subjectNetworkId),
|
|
39
|
-
jti,
|
|
40
|
-
nbf: params.validFrom,
|
|
41
|
-
exp: params.validUntil,
|
|
42
|
-
cnf: { jwk: params.confirmationKey },
|
|
43
|
-
status: (0, revocation_1.buildStatusClaim)(params.revocation.commitment, params.revocationRegistry),
|
|
44
|
-
approvalScope: { ...params.approvalScope }
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
const ALLOWED_TOP_LEVEL = new Set(["vct", "iss", "sub", "jti", "nbf", "exp", "cnf", "status", "approvalScope"]);
|
|
48
|
-
function isRecord(value) {
|
|
49
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
50
|
-
}
|
|
51
|
-
function isJwk(value) {
|
|
52
|
-
if (!isRecord(value))
|
|
53
|
-
return false;
|
|
54
|
-
const keys = Object.keys(value);
|
|
55
|
-
return (keys.length === 4 &&
|
|
56
|
-
value.kty === "EC" &&
|
|
57
|
-
value.crv === "P-256" &&
|
|
58
|
-
typeof value.x === "string" &&
|
|
59
|
-
typeof value.y === "string");
|
|
60
|
-
}
|
|
61
|
-
function isStatusClaim(value) {
|
|
62
|
-
if (!isRecord(value) || Object.keys(value).length !== 1)
|
|
63
|
-
return false;
|
|
64
|
-
const entry = value.cn_zk_revocation;
|
|
65
|
-
if (!isRecord(entry) || Object.keys(entry).length !== 2)
|
|
66
|
-
return false;
|
|
67
|
-
return (typeof entry.revocationRegistry === "string" &&
|
|
68
|
-
CONTRACT_URN_RE.test(entry.revocationRegistry) &&
|
|
69
|
-
typeof entry.revocationCommitment === "string" &&
|
|
70
|
-
/^[0-9]+$/.test(entry.revocationCommitment));
|
|
71
|
-
}
|
|
72
|
-
function isApprovalScope(value) {
|
|
73
|
-
if (!isRecord(value))
|
|
74
|
-
return false;
|
|
75
|
-
const keys = Object.keys(value);
|
|
76
|
-
return (keys.length === 2 &&
|
|
77
|
-
typeof value.programme === "string" &&
|
|
78
|
-
value.programme.length > 0 &&
|
|
79
|
-
typeof value.country === "string" &&
|
|
80
|
-
/^[A-Z]{3}$/.test(value.country));
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Validates the reconstructed payload of a verified token against the
|
|
84
|
-
* profile. Strict on both sides: a missing required claim and an unknown
|
|
85
|
-
* plain claim both fail (spec 07; a tampered disclosure surfaces here as a
|
|
86
|
-
* missing approvalScope member).
|
|
87
|
-
*/
|
|
88
|
-
function validateClaims(payload) {
|
|
89
|
-
if (!isRecord(payload)) {
|
|
90
|
-
return { ok: false, message: "payload is not an object" };
|
|
91
|
-
}
|
|
92
|
-
for (const key of Object.keys(payload)) {
|
|
93
|
-
if (!ALLOWED_TOP_LEVEL.has(key)) {
|
|
94
|
-
return { ok: false, message: `unknown claim: ${key}` };
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
if (payload.vct !== exports.APPROVAL_CREDENTIAL_VCT) {
|
|
98
|
-
return { ok: false, message: "vct missing or not the approval credential type" };
|
|
99
|
-
}
|
|
100
|
-
if (typeof payload.iss !== "string" || urnToNetworkId(payload.iss) === null) {
|
|
101
|
-
return { ok: false, message: "iss missing or not a urn:cn:network URN" };
|
|
102
|
-
}
|
|
103
|
-
if (typeof payload.sub !== "string" || urnToNetworkId(payload.sub) === null) {
|
|
104
|
-
return { ok: false, message: "sub missing or not a urn:cn:network URN" };
|
|
105
|
-
}
|
|
106
|
-
if (typeof payload.jti !== "string" || !UUID_URN_RE.test(payload.jti)) {
|
|
107
|
-
return { ok: false, message: "jti missing or not a urn:uuid URN" };
|
|
108
|
-
}
|
|
109
|
-
if (typeof payload.nbf !== "number" || typeof payload.exp !== "number") {
|
|
110
|
-
return { ok: false, message: "nbf/exp missing or not NumericDate values" };
|
|
111
|
-
}
|
|
112
|
-
if (!isRecord(payload.cnf) || Object.keys(payload.cnf).length !== 1 || !isJwk(payload.cnf.jwk)) {
|
|
113
|
-
return { ok: false, message: "cnf.jwk missing or not a P-256 JWK" };
|
|
114
|
-
}
|
|
115
|
-
if (!isStatusClaim(payload.status)) {
|
|
116
|
-
return { ok: false, message: "status.cn_zk_revocation missing or malformed" };
|
|
117
|
-
}
|
|
118
|
-
if (!isApprovalScope(payload.approvalScope)) {
|
|
119
|
-
return { ok: false, message: "approvalScope missing programme/country" };
|
|
120
|
-
}
|
|
121
|
-
return { ok: true, claims: payload };
|
|
122
|
-
}
|
package/dist/verify.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { Resolver } from "./resolver";
|
|
2
|
-
import { Hex32, Jwk, VerifyResult } from "./types";
|
|
3
|
-
export interface VerifyOptions {
|
|
4
|
-
/** Verification time, unix seconds. Defaults to now. */
|
|
5
|
-
currentDate?: number;
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* The shared verification pipeline (spec 07, verification flow), check order
|
|
9
|
-
* first-failure-wins: decode/structure -> issuer approved -> key fetch ->
|
|
10
|
-
* signature and disclosure digests -> claims shape -> validity window ->
|
|
11
|
-
* revocation. A disclosure whose digest matches nothing is silently dropped
|
|
12
|
-
* by the library (phase 1 spike), so it surfaces in the claims-shape step as
|
|
13
|
-
* a missing approvalScope member and fails MALFORMED.
|
|
14
|
-
*
|
|
15
|
-
* The library's own nbf/exp check runs against a clamped timestamp so the
|
|
16
|
-
* window verdict (EXPIRED vs NOT_YET_VALID) always comes from our check,
|
|
17
|
-
* after the signature has been verified.
|
|
18
|
-
*/
|
|
19
|
-
declare function runPipeline(token: string, resolver: Resolver, now: number, verifyWithLibrary: (issuerJwk: Jwk, clampedNow: number) => Promise<Record<string, unknown>>): Promise<{
|
|
20
|
-
result: VerifyResult;
|
|
21
|
-
issuerId?: Hex32;
|
|
22
|
-
}>;
|
|
23
|
-
/**
|
|
24
|
-
* Verifies a compact SD-JWT credential with all its disclosures — the
|
|
25
|
-
* holder's verify-on-receipt and any check that does not involve a live
|
|
26
|
-
* presentation. Key binding is not checked here; that is verifyPresentation.
|
|
27
|
-
*/
|
|
28
|
-
export declare function verifySdJwtCredential(token: string, resolver: Resolver, options?: VerifyOptions): Promise<VerifyResult>;
|
|
29
|
-
export { runPipeline };
|
package/dist/verify.js
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.verifySdJwtCredential = verifySdJwtCredential;
|
|
4
|
-
exports.runPipeline = runPipeline;
|
|
5
|
-
const sd_jwt_vc_1 = require("@sd-jwt/sd-jwt-vc");
|
|
6
|
-
const crypto_1 = require("./crypto");
|
|
7
|
-
const profile_1 = require("./profile");
|
|
8
|
-
const fail = (code, message) => ({ ok: false, code, message });
|
|
9
|
-
/**
|
|
10
|
-
* The shared verification pipeline (spec 07, verification flow), check order
|
|
11
|
-
* first-failure-wins: decode/structure -> issuer approved -> key fetch ->
|
|
12
|
-
* signature and disclosure digests -> claims shape -> validity window ->
|
|
13
|
-
* revocation. A disclosure whose digest matches nothing is silently dropped
|
|
14
|
-
* by the library (phase 1 spike), so it surfaces in the claims-shape step as
|
|
15
|
-
* a missing approvalScope member and fails MALFORMED.
|
|
16
|
-
*
|
|
17
|
-
* The library's own nbf/exp check runs against a clamped timestamp so the
|
|
18
|
-
* window verdict (EXPIRED vs NOT_YET_VALID) always comes from our check,
|
|
19
|
-
* after the signature has been verified.
|
|
20
|
-
*/
|
|
21
|
-
async function runPipeline(token, resolver, now, verifyWithLibrary) {
|
|
22
|
-
let payload;
|
|
23
|
-
try {
|
|
24
|
-
const jwt = token.split("~")[0];
|
|
25
|
-
const segments = jwt.split(".");
|
|
26
|
-
if (segments.length !== 3) {
|
|
27
|
-
return { result: fail("MALFORMED", "not a compact SD-JWT") };
|
|
28
|
-
}
|
|
29
|
-
payload = JSON.parse(new TextDecoder().decode((0, crypto_1.base64urlToBytes)(segments[1])));
|
|
30
|
-
if (typeof payload !== "object" || payload === null) {
|
|
31
|
-
return { result: fail("MALFORMED", "JWS payload is not an object") };
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
catch {
|
|
35
|
-
return { result: fail("MALFORMED", "token could not be decoded") };
|
|
36
|
-
}
|
|
37
|
-
const issuerId = typeof payload.iss === "string" ? (0, profile_1.urnToNetworkId)(payload.iss) : null;
|
|
38
|
-
if (issuerId === null) {
|
|
39
|
-
return { result: fail("MALFORMED", "iss missing or not a urn:cn:network URN") };
|
|
40
|
-
}
|
|
41
|
-
if (!(await resolver.isApprovedNetworkMember(issuerId))) {
|
|
42
|
-
return {
|
|
43
|
-
result: fail("ISSUER_NOT_APPROVED", `issuer ${payload.iss} is not an approved network member`),
|
|
44
|
-
issuerId
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
const issuerJwk = await resolver.getCredentialPublicKey(issuerId);
|
|
48
|
-
if (issuerJwk === null) {
|
|
49
|
-
return { result: fail("KEY_NOT_FOUND", `no credential public key published for ${payload.iss}`), issuerId };
|
|
50
|
-
}
|
|
51
|
-
// Clamp the library's clock inside the token's window; our own check below decides the verdict.
|
|
52
|
-
let clampedNow = now;
|
|
53
|
-
if (typeof payload.exp === "number" && clampedNow > payload.exp)
|
|
54
|
-
clampedNow = payload.exp;
|
|
55
|
-
if (typeof payload.nbf === "number" && clampedNow < payload.nbf)
|
|
56
|
-
clampedNow = payload.nbf;
|
|
57
|
-
let reconstructed;
|
|
58
|
-
try {
|
|
59
|
-
reconstructed = await verifyWithLibrary(issuerJwk, clampedNow);
|
|
60
|
-
}
|
|
61
|
-
catch (error) {
|
|
62
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
63
|
-
if (message.includes("not yet valid"))
|
|
64
|
-
return { result: fail("NOT_YET_VALID", message), issuerId };
|
|
65
|
-
if (message.includes("expired"))
|
|
66
|
-
return { result: fail("EXPIRED", message), issuerId };
|
|
67
|
-
return { result: fail("PROOF_INVALID", message), issuerId };
|
|
68
|
-
}
|
|
69
|
-
const checked = (0, profile_1.validateClaims)(reconstructed);
|
|
70
|
-
if (!checked.ok) {
|
|
71
|
-
return { result: fail("MALFORMED", checked.message), issuerId };
|
|
72
|
-
}
|
|
73
|
-
const claims = checked.claims;
|
|
74
|
-
if (now < claims.nbf) {
|
|
75
|
-
return { result: fail("NOT_YET_VALID", "credential is not yet within its validity window"), issuerId };
|
|
76
|
-
}
|
|
77
|
-
if (now > claims.exp) {
|
|
78
|
-
return { result: fail("EXPIRED", "credential validity window has passed"), issuerId };
|
|
79
|
-
}
|
|
80
|
-
if (await resolver.isRevoked(BigInt(claims.status.cn_zk_revocation.revocationCommitment))) {
|
|
81
|
-
return { result: fail("REVOKED", "credential commitment is in the revocation registry"), issuerId };
|
|
82
|
-
}
|
|
83
|
-
return { result: { ok: true, claims }, issuerId };
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Verifies a compact SD-JWT credential with all its disclosures — the
|
|
87
|
-
* holder's verify-on-receipt and any check that does not involve a live
|
|
88
|
-
* presentation. Key binding is not checked here; that is verifyPresentation.
|
|
89
|
-
*/
|
|
90
|
-
async function verifySdJwtCredential(token, resolver, options) {
|
|
91
|
-
const now = options?.currentDate ?? Math.floor(Date.now() / 1000);
|
|
92
|
-
const { result } = await runPipeline(token, resolver, now, async (issuerJwk, clampedNow) => {
|
|
93
|
-
const sdjwt = new sd_jwt_vc_1.SDJwtVcInstance({
|
|
94
|
-
verifier: (0, crypto_1.createSdJwtVerifier)(issuerJwk),
|
|
95
|
-
hasher: crypto_1.sdJwtHasher,
|
|
96
|
-
hashAlg: "sha-256"
|
|
97
|
-
});
|
|
98
|
-
const { payload } = await sdjwt.verify(token, { currentDate: clampedNow });
|
|
99
|
-
return payload;
|
|
100
|
-
});
|
|
101
|
-
return result;
|
|
102
|
-
}
|
package/src/decode.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { decodeSdJwtSync, getClaimsSync } from "@sd-jwt/decode"
|
|
2
|
-
import { sdJwtHasher } from "./crypto"
|
|
3
|
-
import { validateClaims } from "./profile"
|
|
4
|
-
import { ApprovalCredentialClaims } from "./types"
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Decodes a compact SD-JWT and reconstructs the full claim set from its
|
|
8
|
-
* disclosures WITHOUT verifying the signature — for callers that already
|
|
9
|
-
* trust the token, such as the issuer reading back a token it just signed.
|
|
10
|
-
* Untrusted tokens go through verifyCredential/verifyPresentation instead.
|
|
11
|
-
*/
|
|
12
|
-
export function decodeSdJwtCredentialClaims(token: string): ApprovalCredentialClaims {
|
|
13
|
-
let reconstructed: unknown
|
|
14
|
-
try {
|
|
15
|
-
const decoded = decodeSdJwtSync(token, sdJwtHasher)
|
|
16
|
-
reconstructed = getClaimsSync(decoded.jwt.payload, decoded.disclosures, sdJwtHasher)
|
|
17
|
-
} catch {
|
|
18
|
-
throw new Error("token is not a decodable SD-JWT")
|
|
19
|
-
}
|
|
20
|
-
const checked = validateClaims(reconstructed)
|
|
21
|
-
if (!checked.ok) {
|
|
22
|
-
throw new Error(checked.message)
|
|
23
|
-
}
|
|
24
|
-
return checked.claims
|
|
25
|
-
}
|
package/src/issue.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { SDJwtVcInstance, SdJwtVcPayload } from "@sd-jwt/sd-jwt-vc"
|
|
2
|
-
import { DisclosureFrame } from "@sd-jwt/types"
|
|
3
|
-
import { InvalidBindingInputError, verifyCredentialBinding } from "@coordination-network/identity"
|
|
4
|
-
import { createSdJwtSigner, jwkToPublicKeyBytes, randomUuid, sdJwtHasher, sdJwtSaltGenerator } from "./crypto"
|
|
5
|
-
import { IssuanceError } from "./errors"
|
|
6
|
-
import { buildPayload, DISCLOSURE_FRAME } from "./profile"
|
|
7
|
-
import { ApprovalResolver } from "./resolver"
|
|
8
|
-
import { verifyCommitmentOwnership } from "./revocation"
|
|
9
|
-
import { Signer } from "./signer"
|
|
10
|
-
import { Hex32, SdJwtIssueParams } from "./types"
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Issues the delegated user approval credential (spec 07, issuance flow).
|
|
14
|
-
* Throws IssuanceError, without signing anything, if the issuer isn't
|
|
15
|
-
* approved or any challenge signature fails.
|
|
16
|
-
*/
|
|
17
|
-
export async function issueSdJwtCredential(
|
|
18
|
-
params: SdJwtIssueParams,
|
|
19
|
-
signer: Signer,
|
|
20
|
-
approval: ApprovalResolver
|
|
21
|
-
): Promise<string> {
|
|
22
|
-
if (!(await approval.isApprovedNetworkMember(params.issuerNetworkId))) {
|
|
23
|
-
throw new IssuanceError(
|
|
24
|
-
"ISSUER_NOT_APPROVED",
|
|
25
|
-
`issuer ${params.issuerNetworkId} is not an approved network member`
|
|
26
|
-
)
|
|
27
|
-
}
|
|
28
|
-
if (!(params.validFrom < params.validUntil)) {
|
|
29
|
-
throw new IssuanceError("INVALID_PARAMS", "validFrom must be before validUntil")
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
let binding: ReturnType<typeof verifyCredentialBinding>
|
|
33
|
-
try {
|
|
34
|
-
binding = verifyCredentialBinding({
|
|
35
|
-
nonce: params.nonce,
|
|
36
|
-
accountPublicKey: params.accountPublicKey,
|
|
37
|
-
confirmationKey: jwkToPublicKeyBytes(params.confirmationKey),
|
|
38
|
-
accountSignature: params.accountPopSignature,
|
|
39
|
-
deviceSignature: params.devicePopSignature
|
|
40
|
-
})
|
|
41
|
-
} catch (error) {
|
|
42
|
-
if (error instanceof InvalidBindingInputError) {
|
|
43
|
-
throw new IssuanceError("INVALID_PARAMS", `malformed binding input: ${error.message}`)
|
|
44
|
-
}
|
|
45
|
-
throw error
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (binding.networkId !== params.subjectNetworkId.toLowerCase()) {
|
|
49
|
-
throw new IssuanceError("SUBJECT_MISMATCH", "subjectNetworkId does not match the account public key")
|
|
50
|
-
}
|
|
51
|
-
if (!binding.valid) {
|
|
52
|
-
throw new IssuanceError("BINDING_INVALID", "credential binding failed")
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const ownershipOk = verifyCommitmentOwnership({
|
|
56
|
-
commitment: params.revocation.commitment,
|
|
57
|
-
publicKey: params.revocation.publicKey,
|
|
58
|
-
signature: params.revocation.ownershipSignature,
|
|
59
|
-
nonce: params.nonce
|
|
60
|
-
})
|
|
61
|
-
if (!ownershipOk) {
|
|
62
|
-
throw new IssuanceError("COMMITMENT_OWNERSHIP_INVALID", "revocation commitment ownership proof failed")
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const payload = buildPayload(
|
|
66
|
-
{ ...params, subjectNetworkId: binding.networkId as Hex32 },
|
|
67
|
-
`urn:uuid:${randomUuid()}`
|
|
68
|
-
)
|
|
69
|
-
const sdjwt = new SDJwtVcInstance({
|
|
70
|
-
signer: createSdJwtSigner(signer),
|
|
71
|
-
signAlg: "ES256",
|
|
72
|
-
hasher: sdJwtHasher,
|
|
73
|
-
hashAlg: "sha-256",
|
|
74
|
-
saltGenerator: sdJwtSaltGenerator
|
|
75
|
-
})
|
|
76
|
-
// Cast: the library types `status` as its own status-list reference, but
|
|
77
|
-
// our custom cn_zk_revocation member is deliberate (spec 07) and the
|
|
78
|
-
// status-list fetcher only triggers on status.status_list.
|
|
79
|
-
return sdjwt.issue(
|
|
80
|
-
{ ...payload } as unknown as SdJwtVcPayload,
|
|
81
|
-
DISCLOSURE_FRAME as unknown as DisclosureFrame<SdJwtVcPayload>
|
|
82
|
-
)
|
|
83
|
-
}
|
package/src/present.ts
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import { SDJwtVcInstance } from "@sd-jwt/sd-jwt-vc"
|
|
2
|
-
import { JwtPayload, PresentationFrame } from "@sd-jwt/types"
|
|
3
|
-
import { createSdJwtSigner, createSdJwtVerifier, sdJwtHasher, sdJwtSaltGenerator } from "./crypto"
|
|
4
|
-
import { Resolver } from "./resolver"
|
|
5
|
-
import { DeviceSigner } from "./signer"
|
|
6
|
-
import { Jwk, VerifyResult } from "./types"
|
|
7
|
-
import { runPipeline } from "./verify"
|
|
8
|
-
|
|
9
|
-
export interface PresentOptions {
|
|
10
|
-
/** Verifier URN, pinned as the KB-JWT audience. */
|
|
11
|
-
audience: string
|
|
12
|
-
/** Verifier-issued nonce, echoed in the KB-JWT. */
|
|
13
|
-
nonce: string
|
|
14
|
-
/** Which approvalScope members to disclose. Both when omitted. */
|
|
15
|
-
disclose?: ("programme" | "country")[]
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface VerifyPresentationOptions {
|
|
19
|
-
/** The verifier's own URN; the KB-JWT audience must match it. */
|
|
20
|
-
audience: string
|
|
21
|
-
/** The nonce this verifier handed out; must be fresh and match. */
|
|
22
|
-
nonce: string
|
|
23
|
-
/** Verification time, unix seconds. Defaults to now. */
|
|
24
|
-
currentDate?: number
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Builds a presentation from a stored credential: the JWS, the selected
|
|
29
|
-
* disclosures, and a KB-JWT signed by the device key binding the verifier's
|
|
30
|
-
* audience, the nonce and the sd_hash of the exact presented token (spec 07,
|
|
31
|
-
* verification flow step 7).
|
|
32
|
-
*/
|
|
33
|
-
export async function presentSdJwtCredential(
|
|
34
|
-
token: string,
|
|
35
|
-
deviceSigner: DeviceSigner,
|
|
36
|
-
options: PresentOptions
|
|
37
|
-
): Promise<string> {
|
|
38
|
-
const disclose = options.disclose ?? ["programme", "country"]
|
|
39
|
-
const frame = { approvalScope: Object.fromEntries(disclose.map((member) => [member, true])) }
|
|
40
|
-
const sdjwt = new SDJwtVcInstance({
|
|
41
|
-
hasher: sdJwtHasher,
|
|
42
|
-
hashAlg: "sha-256",
|
|
43
|
-
saltGenerator: sdJwtSaltGenerator,
|
|
44
|
-
kbSigner: createSdJwtSigner(deviceSigner),
|
|
45
|
-
kbSignAlg: "ES256"
|
|
46
|
-
})
|
|
47
|
-
return sdjwt.present(token, frame as unknown as PresentationFrame<Record<string, unknown>>, {
|
|
48
|
-
kb: {
|
|
49
|
-
payload: {
|
|
50
|
-
aud: options.audience,
|
|
51
|
-
nonce: options.nonce,
|
|
52
|
-
iat: Math.floor(Date.now() / 1000)
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
})
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Verifies a presentation: the full verifyCredential pipeline, plus the
|
|
60
|
-
* KB-JWT under the credential's cnf.jwk with nonce, sd_hash (both checked by
|
|
61
|
-
* the library) and audience (checked here) pinned.
|
|
62
|
-
*/
|
|
63
|
-
export async function verifySdJwtPresentation(
|
|
64
|
-
presentation: string,
|
|
65
|
-
resolver: Resolver,
|
|
66
|
-
options: VerifyPresentationOptions
|
|
67
|
-
): Promise<VerifyResult> {
|
|
68
|
-
const now = options.currentDate ?? Math.floor(Date.now() / 1000)
|
|
69
|
-
let audience: string | undefined
|
|
70
|
-
|
|
71
|
-
const { result } = await runPipeline(presentation, resolver, now, async (issuerJwk: Jwk, clampedNow: number) => {
|
|
72
|
-
const sdjwt = new SDJwtVcInstance({
|
|
73
|
-
verifier: createSdJwtVerifier(issuerJwk),
|
|
74
|
-
hasher: sdJwtHasher,
|
|
75
|
-
hashAlg: "sha-256",
|
|
76
|
-
kbVerifier: (data: string, signature: string, payload: JwtPayload) => {
|
|
77
|
-
const cnfJwk = (payload as { cnf?: { jwk?: Jwk } }).cnf?.jwk
|
|
78
|
-
if (!cnfJwk) return false
|
|
79
|
-
try {
|
|
80
|
-
return createSdJwtVerifier(cnfJwk)(data, signature)
|
|
81
|
-
} catch {
|
|
82
|
-
return false
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
})
|
|
86
|
-
const { payload, kb } = await sdjwt.verify(presentation, {
|
|
87
|
-
currentDate: clampedNow,
|
|
88
|
-
keyBindingNonce: options.nonce
|
|
89
|
-
})
|
|
90
|
-
audience = kb?.payload.aud
|
|
91
|
-
return payload
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
if (!result.ok) {
|
|
95
|
-
return result
|
|
96
|
-
}
|
|
97
|
-
if (audience !== options.audience) {
|
|
98
|
-
return { ok: false, code: "PROOF_INVALID", message: "KB-JWT audience does not match this verifier" }
|
|
99
|
-
}
|
|
100
|
-
return result
|
|
101
|
-
}
|
package/src/profile.ts
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { ApprovalCredentialClaims, ApprovalScope, Hex32, Jwk, SdJwtIssueParams, StatusClaim } from "./types"
|
|
2
|
-
import { buildStatusClaim } from "./revocation"
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* The claims profile of the delegated user approval credential (spec 07):
|
|
6
|
-
* payload construction, the disclosure frame, and the shape validator that
|
|
7
|
-
* backs the MALFORMED verdict.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
export const APPROVAL_CREDENTIAL_VCT = "urn:cn:credential:approval:v1"
|
|
11
|
-
|
|
12
|
-
/** Only the two approvalScope members are disclosable; everything else is plain. */
|
|
13
|
-
export const DISCLOSURE_FRAME = {
|
|
14
|
-
approvalScope: { _sd: ["programme", "country"] }
|
|
15
|
-
} as const
|
|
16
|
-
|
|
17
|
-
const NETWORK_ID_RE = /^0x[0-9a-f]{64}$/
|
|
18
|
-
const NETWORK_URN_RE = /^urn:cn:network:[0-9a-f]{64}$/
|
|
19
|
-
const CONTRACT_URN_RE = /^urn:cn:contract:.+$/
|
|
20
|
-
const UUID_URN_RE = /^urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
|
|
21
|
-
|
|
22
|
-
export function networkIdToUrn(id: Hex32): string {
|
|
23
|
-
const normalized = id.toLowerCase() as Hex32
|
|
24
|
-
if (!NETWORK_ID_RE.test(normalized)) {
|
|
25
|
-
throw new Error("networkId must be a 0x-prefixed 32-byte hex string")
|
|
26
|
-
}
|
|
27
|
-
return `urn:cn:network:${normalized.slice(2)}`
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function urnToNetworkId(urn: string): Hex32 | null {
|
|
31
|
-
return NETWORK_URN_RE.test(urn) ? (`0x${urn.slice("urn:cn:network:".length)}` as Hex32) : null
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** Assembles the full signed payload; the disclosure frame is applied at issue time. */
|
|
35
|
-
export function buildPayload(params: SdJwtIssueParams, jti: string): ApprovalCredentialClaims {
|
|
36
|
-
return {
|
|
37
|
-
vct: APPROVAL_CREDENTIAL_VCT,
|
|
38
|
-
iss: networkIdToUrn(params.issuerNetworkId),
|
|
39
|
-
sub: networkIdToUrn(params.subjectNetworkId),
|
|
40
|
-
jti,
|
|
41
|
-
nbf: params.validFrom,
|
|
42
|
-
exp: params.validUntil,
|
|
43
|
-
cnf: { jwk: params.confirmationKey },
|
|
44
|
-
status: buildStatusClaim(params.revocation.commitment, params.revocationRegistry),
|
|
45
|
-
approvalScope: { ...params.approvalScope }
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
type ClaimsCheck = { ok: true; claims: ApprovalCredentialClaims } | { ok: false; message: string }
|
|
50
|
-
|
|
51
|
-
const ALLOWED_TOP_LEVEL = new Set(["vct", "iss", "sub", "jti", "nbf", "exp", "cnf", "status", "approvalScope"])
|
|
52
|
-
|
|
53
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
54
|
-
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function isJwk(value: unknown): value is Jwk {
|
|
58
|
-
if (!isRecord(value)) return false
|
|
59
|
-
const keys = Object.keys(value)
|
|
60
|
-
return (
|
|
61
|
-
keys.length === 4 &&
|
|
62
|
-
value.kty === "EC" &&
|
|
63
|
-
value.crv === "P-256" &&
|
|
64
|
-
typeof value.x === "string" &&
|
|
65
|
-
typeof value.y === "string"
|
|
66
|
-
)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function isStatusClaim(value: unknown): value is StatusClaim {
|
|
70
|
-
if (!isRecord(value) || Object.keys(value).length !== 1) return false
|
|
71
|
-
const entry = value.cn_zk_revocation
|
|
72
|
-
if (!isRecord(entry) || Object.keys(entry).length !== 2) return false
|
|
73
|
-
return (
|
|
74
|
-
typeof entry.revocationRegistry === "string" &&
|
|
75
|
-
CONTRACT_URN_RE.test(entry.revocationRegistry) &&
|
|
76
|
-
typeof entry.revocationCommitment === "string" &&
|
|
77
|
-
/^[0-9]+$/.test(entry.revocationCommitment)
|
|
78
|
-
)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function isApprovalScope(value: unknown): value is ApprovalScope {
|
|
82
|
-
if (!isRecord(value)) return false
|
|
83
|
-
const keys = Object.keys(value)
|
|
84
|
-
return (
|
|
85
|
-
keys.length === 2 &&
|
|
86
|
-
typeof value.programme === "string" &&
|
|
87
|
-
value.programme.length > 0 &&
|
|
88
|
-
typeof value.country === "string" &&
|
|
89
|
-
/^[A-Z]{3}$/.test(value.country)
|
|
90
|
-
)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Validates the reconstructed payload of a verified token against the
|
|
95
|
-
* profile. Strict on both sides: a missing required claim and an unknown
|
|
96
|
-
* plain claim both fail (spec 07; a tampered disclosure surfaces here as a
|
|
97
|
-
* missing approvalScope member).
|
|
98
|
-
*/
|
|
99
|
-
export function validateClaims(payload: unknown): ClaimsCheck {
|
|
100
|
-
if (!isRecord(payload)) {
|
|
101
|
-
return { ok: false, message: "payload is not an object" }
|
|
102
|
-
}
|
|
103
|
-
for (const key of Object.keys(payload)) {
|
|
104
|
-
if (!ALLOWED_TOP_LEVEL.has(key)) {
|
|
105
|
-
return { ok: false, message: `unknown claim: ${key}` }
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
if (payload.vct !== APPROVAL_CREDENTIAL_VCT) {
|
|
109
|
-
return { ok: false, message: "vct missing or not the approval credential type" }
|
|
110
|
-
}
|
|
111
|
-
if (typeof payload.iss !== "string" || urnToNetworkId(payload.iss) === null) {
|
|
112
|
-
return { ok: false, message: "iss missing or not a urn:cn:network URN" }
|
|
113
|
-
}
|
|
114
|
-
if (typeof payload.sub !== "string" || urnToNetworkId(payload.sub) === null) {
|
|
115
|
-
return { ok: false, message: "sub missing or not a urn:cn:network URN" }
|
|
116
|
-
}
|
|
117
|
-
if (typeof payload.jti !== "string" || !UUID_URN_RE.test(payload.jti)) {
|
|
118
|
-
return { ok: false, message: "jti missing or not a urn:uuid URN" }
|
|
119
|
-
}
|
|
120
|
-
if (typeof payload.nbf !== "number" || typeof payload.exp !== "number") {
|
|
121
|
-
return { ok: false, message: "nbf/exp missing or not NumericDate values" }
|
|
122
|
-
}
|
|
123
|
-
if (!isRecord(payload.cnf) || Object.keys(payload.cnf).length !== 1 || !isJwk(payload.cnf.jwk)) {
|
|
124
|
-
return { ok: false, message: "cnf.jwk missing or not a P-256 JWK" }
|
|
125
|
-
}
|
|
126
|
-
if (!isStatusClaim(payload.status)) {
|
|
127
|
-
return { ok: false, message: "status.cn_zk_revocation missing or malformed" }
|
|
128
|
-
}
|
|
129
|
-
if (!isApprovalScope(payload.approvalScope)) {
|
|
130
|
-
return { ok: false, message: "approvalScope missing programme/country" }
|
|
131
|
-
}
|
|
132
|
-
return { ok: true, claims: payload as unknown as ApprovalCredentialClaims }
|
|
133
|
-
}
|