@arcblock/vc 1.30.26 → 1.31.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/esm/index.d.mts +2 -1
- package/esm/index.mjs +2 -1
- package/esm/sd-vc.d.mts +100 -0
- package/esm/sd-vc.mjs +352 -0
- package/lib/index.cjs +9 -1
- package/lib/index.d.cts +2 -1
- package/lib/sd-vc.cjs +360 -0
- package/lib/sd-vc.d.cts +100 -0
- package/package.json +5 -5
package/esm/index.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SD_HASH_ALG, SdDisclosure, SdKeyBindingProof, SdPredicate, SdPresentation, computeSdDigest, createSelectiveDisclosureVc, decodeSdDisclosure, deriveSelectiveDisclosurePresentation, encodeSdDisclosure, verifySelectiveDisclosurePresentation } from "./sd-vc.mjs";
|
|
1
2
|
import { WalletObject } from "@ocap/wallet";
|
|
2
3
|
import stringify from "fast-json-stable-stringify";
|
|
3
4
|
|
|
@@ -189,4 +190,4 @@ declare function verifyCredentialList({
|
|
|
189
190
|
}): Promise<unknown[]>;
|
|
190
191
|
declare const stableStringify: typeof stringify;
|
|
191
192
|
//#endregion
|
|
192
|
-
export { type CredentialStatus, type RevocationChecker, type RevocationStatus, type VerifiableCredential, counterSign, create, createCredentialList, proofTypes, stableStringify, verify, verifyCredentialList, verifyPresentation };
|
|
193
|
+
export { type CredentialStatus, type RevocationChecker, type RevocationStatus, SD_HASH_ALG, type SdDisclosure, type SdKeyBindingProof, type SdPredicate, type SdPresentation, type VerifiableCredential, computeSdDigest, counterSign, create, createCredentialList, createSelectiveDisclosureVc, decodeSdDisclosure, deriveSelectiveDisclosurePresentation, encodeSdDisclosure, proofTypes, stableStringify, verify, verifyCredentialList, verifyPresentation, verifySelectiveDisclosurePresentation };
|
package/esm/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { name } from "./package.mjs";
|
|
2
|
+
import { SD_HASH_ALG, computeSdDigest, createSelectiveDisclosureVc, decodeSdDisclosure, deriveSelectiveDisclosurePresentation, encodeSdDisclosure, verifySelectiveDisclosurePresentation } from "./sd-vc.mjs";
|
|
2
3
|
import { fromPublicKeyHash, isFromPublicKey, isValid, toTypeInfo } from "@arcblock/did";
|
|
3
4
|
import { types } from "@ocap/mcrypto";
|
|
4
5
|
import { fromBase64, toBase58, toBase64 } from "@ocap/util";
|
|
@@ -327,4 +328,4 @@ async function verifyCredentialList({ credentials, trustedIssuers }) {
|
|
|
327
328
|
const stableStringify = stringify;
|
|
328
329
|
|
|
329
330
|
//#endregion
|
|
330
|
-
export { counterSign, create, createCredentialList, proofTypes, stableStringify, verify, verifyCredentialList, verifyPresentation };
|
|
331
|
+
export { SD_HASH_ALG, computeSdDigest, counterSign, create, createCredentialList, createSelectiveDisclosureVc, decodeSdDisclosure, deriveSelectiveDisclosurePresentation, encodeSdDisclosure, proofTypes, stableStringify, verify, verifyCredentialList, verifyPresentation, verifySelectiveDisclosurePresentation };
|
package/esm/sd-vc.d.mts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { VerifiableCredential } from "./index.mjs";
|
|
2
|
+
import { WalletObject } from "@ocap/wallet";
|
|
3
|
+
|
|
4
|
+
//#region src/sd-vc.d.ts
|
|
5
|
+
declare const SD_HASH_ALG: "sha3-256";
|
|
6
|
+
interface SdDisclosure {
|
|
7
|
+
/** High-entropy 128-bit+ random salt (hex) */
|
|
8
|
+
salt: string;
|
|
9
|
+
/** Claim property name */
|
|
10
|
+
claimName: string;
|
|
11
|
+
/** Original claim value */
|
|
12
|
+
claimValue: unknown;
|
|
13
|
+
/** Base64 encoded canonical `[salt, claimName, claimValue]` tuple */
|
|
14
|
+
encoded: string;
|
|
15
|
+
/** Quantum-safe SHA3-256 commitment digest stored in `credentialSubject._sd` */
|
|
16
|
+
digest: string;
|
|
17
|
+
}
|
|
18
|
+
interface SdKeyBindingProof {
|
|
19
|
+
holderDid: string;
|
|
20
|
+
holderPk: string;
|
|
21
|
+
challenge: string;
|
|
22
|
+
domain: string;
|
|
23
|
+
sdHash: string;
|
|
24
|
+
created: string;
|
|
25
|
+
signature: string;
|
|
26
|
+
}
|
|
27
|
+
interface SdPresentation {
|
|
28
|
+
verifiableCredential: VerifiableCredential;
|
|
29
|
+
/** Subset of disclosures chosen by the holder for selective presentation */
|
|
30
|
+
disclosures: string[];
|
|
31
|
+
/** Optional Holder Key-Binding Proof preventing replay and MITM correlation */
|
|
32
|
+
keyBindingProof?: SdKeyBindingProof;
|
|
33
|
+
}
|
|
34
|
+
interface SdPredicate {
|
|
35
|
+
field: string;
|
|
36
|
+
op: 'eq' | 'neq' | 'gt' | 'gte' | 'lt' | 'lte' | 'in';
|
|
37
|
+
value: unknown;
|
|
38
|
+
}
|
|
39
|
+
declare function computeSdDigest(encodedDisclosure: string): string;
|
|
40
|
+
declare function encodeSdDisclosure(salt: string, claimName: string, claimValue: unknown): SdDisclosure;
|
|
41
|
+
declare function decodeSdDisclosure(encoded: string): SdDisclosure;
|
|
42
|
+
/**
|
|
43
|
+
* Create a Selective Disclosure Verifiable Credential (`SD-VC`, inspired by IETF RFC 9901 SD-JWT VC).
|
|
44
|
+
* Replaces sensitive claims listed in `selectiveFields` with salted SHA3-256 hash commitments (`_sd`),
|
|
45
|
+
* signs the credential with the issuer's wallet, and returns `{ vc, disclosures }`.
|
|
46
|
+
*/
|
|
47
|
+
declare function createSelectiveDisclosureVc(params: {
|
|
48
|
+
type: string | string[];
|
|
49
|
+
subject: {
|
|
50
|
+
id: string;
|
|
51
|
+
[key: string]: unknown;
|
|
52
|
+
};
|
|
53
|
+
selectiveFields: string[];
|
|
54
|
+
issuer: {
|
|
55
|
+
wallet: WalletObject;
|
|
56
|
+
name?: string;
|
|
57
|
+
};
|
|
58
|
+
issuanceDate?: string;
|
|
59
|
+
expirationDate?: string;
|
|
60
|
+
tag?: string;
|
|
61
|
+
}): Promise<{
|
|
62
|
+
vc: VerifiableCredential;
|
|
63
|
+
disclosures: SdDisclosure[];
|
|
64
|
+
}>;
|
|
65
|
+
/**
|
|
66
|
+
* Derive a minimal-disclosure `SdPresentation` exposing ONLY `discloseFields`
|
|
67
|
+
* and binding the presentation to the verifier's `challenge` and `domain` with the holder's key.
|
|
68
|
+
*/
|
|
69
|
+
declare function deriveSelectiveDisclosurePresentation(params: {
|
|
70
|
+
vc: VerifiableCredential;
|
|
71
|
+
disclosures: Array<SdDisclosure | string>;
|
|
72
|
+
discloseFields: string[];
|
|
73
|
+
holderWallet?: WalletObject;
|
|
74
|
+
challenge?: string;
|
|
75
|
+
domain?: string;
|
|
76
|
+
}): Promise<SdPresentation>;
|
|
77
|
+
/**
|
|
78
|
+
* Verify a Selective Disclosure Presentation (`SdPresentation`):
|
|
79
|
+
* 1. Verifies the Issuer's cryptographic signature on `verifiableCredential`.
|
|
80
|
+
* 2. Verifies each disclosed `[salt, claimName, claimValue]` against `credentialSubject._sd` digests.
|
|
81
|
+
* 3. Rejects duplicate disclosures or tampered digests.
|
|
82
|
+
* 4. Verifies the Holder Key-Binding Proof (`challenge` / `domain` / `sdHash`) if required or present.
|
|
83
|
+
* 5. Evaluates optional `predicates` over the reconstructed claims.
|
|
84
|
+
*/
|
|
85
|
+
declare function verifySelectiveDisclosurePresentation(params: {
|
|
86
|
+
presentation: SdPresentation;
|
|
87
|
+
trustedIssuers: string | string[];
|
|
88
|
+
ownerDid?: string;
|
|
89
|
+
requireKeyBinding?: boolean;
|
|
90
|
+
expectedChallenge?: string;
|
|
91
|
+
expectedDomain?: string;
|
|
92
|
+
predicates?: SdPredicate[];
|
|
93
|
+
}): Promise<{
|
|
94
|
+
valid: boolean;
|
|
95
|
+
disclosedSubject: Record<string, unknown>;
|
|
96
|
+
undisclosedCount: number;
|
|
97
|
+
error?: string;
|
|
98
|
+
}>;
|
|
99
|
+
//#endregion
|
|
100
|
+
export { SD_HASH_ALG, SdDisclosure, SdKeyBindingProof, SdPredicate, SdPresentation, computeSdDigest, createSelectiveDisclosureVc, decodeSdDisclosure, deriveSelectiveDisclosurePresentation, encodeSdDisclosure, verifySelectiveDisclosurePresentation };
|
package/esm/sd-vc.mjs
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import { create, verify } from "./index.mjs";
|
|
2
|
+
import { isFromPublicKey, isValid, toTypeInfo } from "@arcblock/did";
|
|
3
|
+
import { Hasher, getRandomBytes } from "@ocap/mcrypto";
|
|
4
|
+
import { fromBase64, toBase58, toBase64 } from "@ocap/util";
|
|
5
|
+
import { fromPublicKey } from "@ocap/wallet";
|
|
6
|
+
import stringify from "fast-json-stable-stringify";
|
|
7
|
+
|
|
8
|
+
//#region src/sd-vc.ts
|
|
9
|
+
const SD_HASH_ALG = "sha3-256";
|
|
10
|
+
const FORBIDDEN_CLAIM_KEYS = new Set([
|
|
11
|
+
"id",
|
|
12
|
+
"_sd",
|
|
13
|
+
"_sd_alg",
|
|
14
|
+
"__proto__",
|
|
15
|
+
"constructor",
|
|
16
|
+
"prototype"
|
|
17
|
+
]);
|
|
18
|
+
const CANONICAL_SIGNED_INT_REGEX = /^-?\d+$/;
|
|
19
|
+
const CANONICAL_DECIMAL_REGEX = /^-?\d+(\.\d+)?$/;
|
|
20
|
+
function computeSdDigest(encodedDisclosure) {
|
|
21
|
+
const bytes = Uint8Array.from(Buffer.from(encodedDisclosure, "utf8"));
|
|
22
|
+
return Hasher.SHA3.hash256(bytes);
|
|
23
|
+
}
|
|
24
|
+
function encodeSdDisclosure(salt, claimName, claimValue) {
|
|
25
|
+
if (!claimName || FORBIDDEN_CLAIM_KEYS.has(claimName)) throw new Error(`Invalid or reserved selective disclosure claim name: ${claimName}`);
|
|
26
|
+
const canonicalTuple = stringify([
|
|
27
|
+
salt,
|
|
28
|
+
claimName,
|
|
29
|
+
claimValue
|
|
30
|
+
]);
|
|
31
|
+
const encoded = toBase64(Buffer.from(canonicalTuple, "utf8"));
|
|
32
|
+
return {
|
|
33
|
+
salt,
|
|
34
|
+
claimName,
|
|
35
|
+
claimValue,
|
|
36
|
+
encoded,
|
|
37
|
+
digest: computeSdDigest(encoded)
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function decodeSdDisclosure(encoded) {
|
|
41
|
+
const rawJson = Buffer.from(fromBase64(encoded)).toString("utf8");
|
|
42
|
+
const parsed = JSON.parse(rawJson);
|
|
43
|
+
if (!Array.isArray(parsed) || parsed.length !== 3) throw new Error("Invalid SD-VC disclosure tuple");
|
|
44
|
+
const [salt, claimName, claimValue] = parsed;
|
|
45
|
+
if (typeof salt !== "string" || !salt || typeof claimName !== "string" || !claimName) throw new Error("Malformed SD-VC disclosure salt or claimName");
|
|
46
|
+
if (FORBIDDEN_CLAIM_KEYS.has(claimName)) throw new Error(`Forbidden claimName in SD-VC disclosure: ${claimName}`);
|
|
47
|
+
return {
|
|
48
|
+
salt,
|
|
49
|
+
claimName,
|
|
50
|
+
claimValue,
|
|
51
|
+
encoded,
|
|
52
|
+
digest: computeSdDigest(encoded)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Create a Selective Disclosure Verifiable Credential (`SD-VC`, inspired by IETF RFC 9901 SD-JWT VC).
|
|
57
|
+
* Replaces sensitive claims listed in `selectiveFields` with salted SHA3-256 hash commitments (`_sd`),
|
|
58
|
+
* signs the credential with the issuer's wallet, and returns `{ vc, disclosures }`.
|
|
59
|
+
*/
|
|
60
|
+
async function createSelectiveDisclosureVc(params) {
|
|
61
|
+
const { type, subject, selectiveFields, issuer, issuanceDate, expirationDate, tag } = params;
|
|
62
|
+
if (!subject || typeof subject !== "object" || !subject.id) throw new Error("Cannot create SD-VC without valid subject.id");
|
|
63
|
+
const uniqueFields = Array.from(new Set(selectiveFields || []));
|
|
64
|
+
for (const field of uniqueFields) {
|
|
65
|
+
if (FORBIDDEN_CLAIM_KEYS.has(field)) throw new Error(`Field "${field}" cannot be selectively concealed`);
|
|
66
|
+
if (!(field in subject)) throw new Error(`Selective field "${field}" does not exist on subject`);
|
|
67
|
+
}
|
|
68
|
+
const publicSubject = {
|
|
69
|
+
id: subject.id,
|
|
70
|
+
_sd: [],
|
|
71
|
+
_sd_alg: SD_HASH_ALG
|
|
72
|
+
};
|
|
73
|
+
const disclosures = [];
|
|
74
|
+
for (const [key, val] of Object.entries(subject)) {
|
|
75
|
+
if (key === "id") continue;
|
|
76
|
+
if (FORBIDDEN_CLAIM_KEYS.has(key)) throw new Error(`Reserved property "${key}" is not allowed in SD-VC subject`);
|
|
77
|
+
if (uniqueFields.includes(key)) {
|
|
78
|
+
const disclosure = encodeSdDisclosure(Buffer.from(getRandomBytes(16)).toString("hex"), key, val);
|
|
79
|
+
disclosures.push(disclosure);
|
|
80
|
+
publicSubject._sd.push(disclosure.digest);
|
|
81
|
+
} else publicSubject[key] = val;
|
|
82
|
+
}
|
|
83
|
+
publicSubject._sd.sort();
|
|
84
|
+
const typeList = Array.isArray(type) ? [...type] : [type];
|
|
85
|
+
if (!typeList.includes("SelectiveDisclosureCredential")) typeList.push("SelectiveDisclosureCredential");
|
|
86
|
+
const vc = await create({
|
|
87
|
+
type: typeList,
|
|
88
|
+
subject: publicSubject,
|
|
89
|
+
issuer,
|
|
90
|
+
issuanceDate,
|
|
91
|
+
expirationDate,
|
|
92
|
+
tag
|
|
93
|
+
});
|
|
94
|
+
if (!vc) throw new Error("Failed to create and sign Selective Disclosure VC");
|
|
95
|
+
return {
|
|
96
|
+
vc,
|
|
97
|
+
disclosures
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Derive a minimal-disclosure `SdPresentation` exposing ONLY `discloseFields`
|
|
102
|
+
* and binding the presentation to the verifier's `challenge` and `domain` with the holder's key.
|
|
103
|
+
*/
|
|
104
|
+
async function deriveSelectiveDisclosurePresentation(params) {
|
|
105
|
+
const { vc, disclosures, discloseFields, holderWallet, challenge = "", domain = "" } = params;
|
|
106
|
+
const rawSd = Array.isArray(vc.credentialSubject?._sd) ? vc.credentialSubject._sd.map(String) : [];
|
|
107
|
+
const commitmentSet = new Set(rawSd);
|
|
108
|
+
const decodedList = disclosures.map((d) => decodeSdDisclosure(typeof d === "string" ? d : d.encoded));
|
|
109
|
+
const wantedSet = new Set(discloseFields);
|
|
110
|
+
const selectedEncoded = [];
|
|
111
|
+
for (const field of wantedSet) {
|
|
112
|
+
const match = decodedList.find((d) => d.claimName === field);
|
|
113
|
+
if (!match) throw new Error(`Requested disclosure field "${field}" not found in holder disclosures`);
|
|
114
|
+
if (!commitmentSet.has(match.digest)) throw new Error(`Disclosure digest for "${field}" is not present in vc.credentialSubject._sd`);
|
|
115
|
+
selectedEncoded.push(match.encoded);
|
|
116
|
+
}
|
|
117
|
+
let keyBindingProof;
|
|
118
|
+
if (holderWallet) {
|
|
119
|
+
const holderDid = holderWallet.address;
|
|
120
|
+
if (vc.credentialSubject?.id && vc.credentialSubject.id !== holderDid) throw new Error("holderWallet address does not match vc.credentialSubject.id");
|
|
121
|
+
const sdHash = Hasher.SHA3.hash256(Uint8Array.from(Buffer.from(stringify({
|
|
122
|
+
vcId: vc.id,
|
|
123
|
+
vcProof: vc.proof,
|
|
124
|
+
disclosures: selectedEncoded
|
|
125
|
+
}), "utf8")));
|
|
126
|
+
const created = (/* @__PURE__ */ new Date()).toISOString();
|
|
127
|
+
const kbPayload = stringify({
|
|
128
|
+
holderDid,
|
|
129
|
+
challenge,
|
|
130
|
+
domain,
|
|
131
|
+
sdHash,
|
|
132
|
+
created
|
|
133
|
+
});
|
|
134
|
+
const sig = await holderWallet.sign(kbPayload);
|
|
135
|
+
keyBindingProof = {
|
|
136
|
+
holderDid,
|
|
137
|
+
holderPk: toBase58(holderWallet.publicKey),
|
|
138
|
+
challenge,
|
|
139
|
+
domain,
|
|
140
|
+
sdHash,
|
|
141
|
+
created,
|
|
142
|
+
signature: toBase64(sig)
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
verifiableCredential: structuredClone(vc),
|
|
147
|
+
disclosures: selectedEncoded,
|
|
148
|
+
...keyBindingProof ? { keyBindingProof } : {}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function compareSdNumeric(actual, expected, op) {
|
|
152
|
+
const isIntLike = (v) => typeof v === "bigint" || typeof v === "number" && Number.isFinite(v) && Number.isInteger(v) || typeof v === "string" && CANONICAL_SIGNED_INT_REGEX.test(v);
|
|
153
|
+
if (isIntLike(actual) && isIntLike(expected)) {
|
|
154
|
+
const aBig = BigInt(String(actual));
|
|
155
|
+
const bBig = BigInt(String(expected));
|
|
156
|
+
if (op === "gt") return aBig > bBig;
|
|
157
|
+
if (op === "gte") return aBig >= bBig;
|
|
158
|
+
if (op === "lt") return aBig < bBig;
|
|
159
|
+
return aBig <= bBig;
|
|
160
|
+
}
|
|
161
|
+
const toFiniteNumber = (v) => {
|
|
162
|
+
if (typeof v === "number" && Number.isFinite(v)) return v;
|
|
163
|
+
if (typeof v === "string" && CANONICAL_DECIMAL_REGEX.test(v)) {
|
|
164
|
+
const n = Number(v);
|
|
165
|
+
return Number.isFinite(n) ? n : null;
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
};
|
|
169
|
+
const aNum = toFiniteNumber(actual);
|
|
170
|
+
const bNum = toFiniteNumber(expected);
|
|
171
|
+
if (aNum === null || bNum === null) return false;
|
|
172
|
+
if (op === "gt") return aNum > bNum;
|
|
173
|
+
if (op === "gte") return aNum >= bNum;
|
|
174
|
+
if (op === "lt") return aNum < bNum;
|
|
175
|
+
return aNum <= bNum;
|
|
176
|
+
}
|
|
177
|
+
function evaluateSdPredicate(pred, claims) {
|
|
178
|
+
if (FORBIDDEN_CLAIM_KEYS.has(pred.field) && pred.field !== "id") return false;
|
|
179
|
+
if (!Object.prototype.hasOwnProperty.call(claims, pred.field)) return false;
|
|
180
|
+
const actual = claims[pred.field];
|
|
181
|
+
if (actual === void 0) return false;
|
|
182
|
+
switch (pred.op) {
|
|
183
|
+
case "eq": return actual === pred.value;
|
|
184
|
+
case "neq": return actual !== pred.value;
|
|
185
|
+
case "gt":
|
|
186
|
+
case "gte":
|
|
187
|
+
case "lt":
|
|
188
|
+
case "lte": return compareSdNumeric(actual, pred.value, pred.op);
|
|
189
|
+
case "in": return Array.isArray(pred.value) && pred.value.includes(actual);
|
|
190
|
+
default: return false;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Verify a Selective Disclosure Presentation (`SdPresentation`):
|
|
195
|
+
* 1. Verifies the Issuer's cryptographic signature on `verifiableCredential`.
|
|
196
|
+
* 2. Verifies each disclosed `[salt, claimName, claimValue]` against `credentialSubject._sd` digests.
|
|
197
|
+
* 3. Rejects duplicate disclosures or tampered digests.
|
|
198
|
+
* 4. Verifies the Holder Key-Binding Proof (`challenge` / `domain` / `sdHash`) if required or present.
|
|
199
|
+
* 5. Evaluates optional `predicates` over the reconstructed claims.
|
|
200
|
+
*/
|
|
201
|
+
async function verifySelectiveDisclosurePresentation(params) {
|
|
202
|
+
const { presentation, trustedIssuers, ownerDid, requireKeyBinding = false, expectedChallenge, expectedDomain, predicates = [] } = params;
|
|
203
|
+
try {
|
|
204
|
+
if (!presentation || !presentation.verifiableCredential) return {
|
|
205
|
+
valid: false,
|
|
206
|
+
disclosedSubject: {},
|
|
207
|
+
undisclosedCount: 0,
|
|
208
|
+
error: "Missing verifiableCredential"
|
|
209
|
+
};
|
|
210
|
+
const vc = presentation.verifiableCredential;
|
|
211
|
+
const holderDid = ownerDid || vc.credentialSubject?.id;
|
|
212
|
+
if (!holderDid || !isValid(holderDid)) return {
|
|
213
|
+
valid: false,
|
|
214
|
+
disclosedSubject: {},
|
|
215
|
+
undisclosedCount: 0,
|
|
216
|
+
error: "Invalid holder DID"
|
|
217
|
+
};
|
|
218
|
+
if (!await verify({
|
|
219
|
+
vc,
|
|
220
|
+
ownerDid: holderDid,
|
|
221
|
+
trustedIssuers
|
|
222
|
+
})) return {
|
|
223
|
+
valid: false,
|
|
224
|
+
disclosedSubject: {},
|
|
225
|
+
undisclosedCount: 0,
|
|
226
|
+
error: "Base VC signature verification failed"
|
|
227
|
+
};
|
|
228
|
+
const rawSd = vc.credentialSubject?._sd;
|
|
229
|
+
if (!Array.isArray(rawSd) || vc.credentialSubject?._sd_alg !== SD_HASH_ALG || rawSd.some((d) => typeof d !== "string" || !d)) return {
|
|
230
|
+
valid: false,
|
|
231
|
+
disclosedSubject: {},
|
|
232
|
+
undisclosedCount: 0,
|
|
233
|
+
error: "Missing or unsupported _sd commitments"
|
|
234
|
+
};
|
|
235
|
+
const commitmentSet = new Set(rawSd);
|
|
236
|
+
if (commitmentSet.size !== rawSd.length) return {
|
|
237
|
+
valid: false,
|
|
238
|
+
disclosedSubject: {},
|
|
239
|
+
undisclosedCount: 0,
|
|
240
|
+
error: "Duplicate _sd commitments in credential"
|
|
241
|
+
};
|
|
242
|
+
const seenDigests = /* @__PURE__ */ new Set();
|
|
243
|
+
const seenClaimNames = /* @__PURE__ */ new Set();
|
|
244
|
+
const disclosedSubject = {};
|
|
245
|
+
for (const [k, v] of Object.entries(vc.credentialSubject)) {
|
|
246
|
+
if (k === "_sd" || k === "_sd_alg") continue;
|
|
247
|
+
if (!FORBIDDEN_CLAIM_KEYS.has(k) || k === "id") disclosedSubject[k] = v;
|
|
248
|
+
}
|
|
249
|
+
for (const encoded of presentation.disclosures || []) {
|
|
250
|
+
const decoded = decodeSdDisclosure(encoded);
|
|
251
|
+
if (seenDigests.has(decoded.digest) || seenClaimNames.has(decoded.claimName)) return {
|
|
252
|
+
valid: false,
|
|
253
|
+
disclosedSubject: {},
|
|
254
|
+
undisclosedCount: 0,
|
|
255
|
+
error: `Duplicate disclosure for claim ${decoded.claimName}`
|
|
256
|
+
};
|
|
257
|
+
if (!commitmentSet.has(decoded.digest)) return {
|
|
258
|
+
valid: false,
|
|
259
|
+
disclosedSubject: {},
|
|
260
|
+
undisclosedCount: 0,
|
|
261
|
+
error: `Disclosure digest mismatch for claim ${decoded.claimName}`
|
|
262
|
+
};
|
|
263
|
+
if (decoded.claimName in disclosedSubject) return {
|
|
264
|
+
valid: false,
|
|
265
|
+
disclosedSubject: {},
|
|
266
|
+
undisclosedCount: 0,
|
|
267
|
+
error: `Claim ${decoded.claimName} collides with public claim`
|
|
268
|
+
};
|
|
269
|
+
seenDigests.add(decoded.digest);
|
|
270
|
+
seenClaimNames.add(decoded.claimName);
|
|
271
|
+
disclosedSubject[decoded.claimName] = decoded.claimValue;
|
|
272
|
+
}
|
|
273
|
+
if (Boolean(requireKeyBinding || expectedChallenge !== void 0 || expectedDomain !== void 0) && !presentation.keyBindingProof) return {
|
|
274
|
+
valid: false,
|
|
275
|
+
disclosedSubject: {},
|
|
276
|
+
undisclosedCount: 0,
|
|
277
|
+
error: "Missing required keyBindingProof"
|
|
278
|
+
};
|
|
279
|
+
if (presentation.keyBindingProof) {
|
|
280
|
+
const kb = presentation.keyBindingProof;
|
|
281
|
+
if (kb.holderDid !== holderDid) return {
|
|
282
|
+
valid: false,
|
|
283
|
+
disclosedSubject: {},
|
|
284
|
+
undisclosedCount: 0,
|
|
285
|
+
error: "keyBindingProof holderDid mismatch"
|
|
286
|
+
};
|
|
287
|
+
if (!isFromPublicKey(kb.holderDid, kb.holderPk)) return {
|
|
288
|
+
valid: false,
|
|
289
|
+
disclosedSubject: {},
|
|
290
|
+
undisclosedCount: 0,
|
|
291
|
+
error: "keyBindingProof pk mismatch"
|
|
292
|
+
};
|
|
293
|
+
if (expectedChallenge !== void 0 && kb.challenge !== expectedChallenge) return {
|
|
294
|
+
valid: false,
|
|
295
|
+
disclosedSubject: {},
|
|
296
|
+
undisclosedCount: 0,
|
|
297
|
+
error: "keyBindingProof challenge mismatch"
|
|
298
|
+
};
|
|
299
|
+
if (expectedDomain !== void 0 && kb.domain !== expectedDomain) return {
|
|
300
|
+
valid: false,
|
|
301
|
+
disclosedSubject: {},
|
|
302
|
+
undisclosedCount: 0,
|
|
303
|
+
error: "keyBindingProof domain mismatch"
|
|
304
|
+
};
|
|
305
|
+
const expectedSdHash = Hasher.SHA3.hash256(Uint8Array.from(Buffer.from(stringify({
|
|
306
|
+
vcId: vc.id,
|
|
307
|
+
vcProof: vc.proof,
|
|
308
|
+
disclosures: presentation.disclosures || []
|
|
309
|
+
}), "utf8")));
|
|
310
|
+
if (kb.sdHash !== expectedSdHash) return {
|
|
311
|
+
valid: false,
|
|
312
|
+
disclosedSubject: {},
|
|
313
|
+
undisclosedCount: 0,
|
|
314
|
+
error: "keyBindingProof sdHash mismatch"
|
|
315
|
+
};
|
|
316
|
+
const kbPayload = stringify({
|
|
317
|
+
holderDid: kb.holderDid,
|
|
318
|
+
challenge: kb.challenge,
|
|
319
|
+
domain: kb.domain,
|
|
320
|
+
sdHash: kb.sdHash,
|
|
321
|
+
created: kb.created
|
|
322
|
+
});
|
|
323
|
+
if (!await fromPublicKey(kb.holderPk, toTypeInfo(kb.holderDid)).verify(kbPayload, fromBase64(kb.signature))) return {
|
|
324
|
+
valid: false,
|
|
325
|
+
disclosedSubject: {},
|
|
326
|
+
undisclosedCount: 0,
|
|
327
|
+
error: "Invalid keyBindingProof signature"
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
for (const pred of predicates) if (!evaluateSdPredicate(pred, disclosedSubject)) return {
|
|
331
|
+
valid: false,
|
|
332
|
+
disclosedSubject: {},
|
|
333
|
+
undisclosedCount: 0,
|
|
334
|
+
error: `Predicate failed: ${pred.field} ${pred.op} ${JSON.stringify(pred.value)}`
|
|
335
|
+
};
|
|
336
|
+
return {
|
|
337
|
+
valid: true,
|
|
338
|
+
disclosedSubject,
|
|
339
|
+
undisclosedCount: commitmentSet.size - seenDigests.size
|
|
340
|
+
};
|
|
341
|
+
} catch (err) {
|
|
342
|
+
return {
|
|
343
|
+
valid: false,
|
|
344
|
+
disclosedSubject: {},
|
|
345
|
+
undisclosedCount: 0,
|
|
346
|
+
error: err?.message || "SD-VC verification error"
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
//#endregion
|
|
352
|
+
export { SD_HASH_ALG, computeSdDigest, createSelectiveDisclosureVc, decodeSdDisclosure, deriveSelectiveDisclosurePresentation, encodeSdDisclosure, verifySelectiveDisclosurePresentation };
|
package/lib/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
2
|
const require_package = require('./package.cjs');
|
|
3
|
+
const require_sd_vc = require('./sd-vc.cjs');
|
|
3
4
|
let _arcblock_did = require("@arcblock/did");
|
|
4
5
|
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
5
6
|
let _ocap_util = require("@ocap/util");
|
|
@@ -331,11 +332,18 @@ async function verifyCredentialList({ credentials, trustedIssuers }) {
|
|
|
331
332
|
const stableStringify = fast_json_stable_stringify.default;
|
|
332
333
|
|
|
333
334
|
//#endregion
|
|
335
|
+
exports.SD_HASH_ALG = require_sd_vc.SD_HASH_ALG;
|
|
336
|
+
exports.computeSdDigest = require_sd_vc.computeSdDigest;
|
|
334
337
|
exports.counterSign = counterSign;
|
|
335
338
|
exports.create = create;
|
|
336
339
|
exports.createCredentialList = createCredentialList;
|
|
340
|
+
exports.createSelectiveDisclosureVc = require_sd_vc.createSelectiveDisclosureVc;
|
|
341
|
+
exports.decodeSdDisclosure = require_sd_vc.decodeSdDisclosure;
|
|
342
|
+
exports.deriveSelectiveDisclosurePresentation = require_sd_vc.deriveSelectiveDisclosurePresentation;
|
|
343
|
+
exports.encodeSdDisclosure = require_sd_vc.encodeSdDisclosure;
|
|
337
344
|
exports.proofTypes = proofTypes;
|
|
338
345
|
exports.stableStringify = stableStringify;
|
|
339
346
|
exports.verify = verify;
|
|
340
347
|
exports.verifyCredentialList = verifyCredentialList;
|
|
341
|
-
exports.verifyPresentation = verifyPresentation;
|
|
348
|
+
exports.verifyPresentation = verifyPresentation;
|
|
349
|
+
exports.verifySelectiveDisclosurePresentation = require_sd_vc.verifySelectiveDisclosurePresentation;
|
package/lib/index.d.cts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SD_HASH_ALG, SdDisclosure, SdKeyBindingProof, SdPredicate, SdPresentation, computeSdDigest, createSelectiveDisclosureVc, decodeSdDisclosure, deriveSelectiveDisclosurePresentation, encodeSdDisclosure, verifySelectiveDisclosurePresentation } from "./sd-vc.cjs";
|
|
1
2
|
import { WalletObject } from "@ocap/wallet";
|
|
2
3
|
import stringify from "fast-json-stable-stringify";
|
|
3
4
|
|
|
@@ -189,4 +190,4 @@ declare function verifyCredentialList({
|
|
|
189
190
|
}): Promise<unknown[]>;
|
|
190
191
|
declare const stableStringify: typeof stringify;
|
|
191
192
|
//#endregion
|
|
192
|
-
export { type CredentialStatus, type RevocationChecker, type RevocationStatus, type VerifiableCredential, counterSign, create, createCredentialList, proofTypes, stableStringify, verify, verifyCredentialList, verifyPresentation };
|
|
193
|
+
export { type CredentialStatus, type RevocationChecker, type RevocationStatus, SD_HASH_ALG, type SdDisclosure, type SdKeyBindingProof, type SdPredicate, type SdPresentation, type VerifiableCredential, computeSdDigest, counterSign, create, createCredentialList, createSelectiveDisclosureVc, decodeSdDisclosure, deriveSelectiveDisclosurePresentation, encodeSdDisclosure, proofTypes, stableStringify, verify, verifyCredentialList, verifyPresentation, verifySelectiveDisclosurePresentation };
|
package/lib/sd-vc.cjs
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_index = require('./index.cjs');
|
|
3
|
+
let _arcblock_did = require("@arcblock/did");
|
|
4
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
5
|
+
let _ocap_util = require("@ocap/util");
|
|
6
|
+
let _ocap_wallet = require("@ocap/wallet");
|
|
7
|
+
let fast_json_stable_stringify = require("fast-json-stable-stringify");
|
|
8
|
+
fast_json_stable_stringify = require_rolldown_runtime.__toESM(fast_json_stable_stringify);
|
|
9
|
+
|
|
10
|
+
//#region src/sd-vc.ts
|
|
11
|
+
const SD_HASH_ALG = "sha3-256";
|
|
12
|
+
const FORBIDDEN_CLAIM_KEYS = new Set([
|
|
13
|
+
"id",
|
|
14
|
+
"_sd",
|
|
15
|
+
"_sd_alg",
|
|
16
|
+
"__proto__",
|
|
17
|
+
"constructor",
|
|
18
|
+
"prototype"
|
|
19
|
+
]);
|
|
20
|
+
const CANONICAL_SIGNED_INT_REGEX = /^-?\d+$/;
|
|
21
|
+
const CANONICAL_DECIMAL_REGEX = /^-?\d+(\.\d+)?$/;
|
|
22
|
+
function computeSdDigest(encodedDisclosure) {
|
|
23
|
+
const bytes = Uint8Array.from(Buffer.from(encodedDisclosure, "utf8"));
|
|
24
|
+
return _ocap_mcrypto.Hasher.SHA3.hash256(bytes);
|
|
25
|
+
}
|
|
26
|
+
function encodeSdDisclosure(salt, claimName, claimValue) {
|
|
27
|
+
if (!claimName || FORBIDDEN_CLAIM_KEYS.has(claimName)) throw new Error(`Invalid or reserved selective disclosure claim name: ${claimName}`);
|
|
28
|
+
const canonicalTuple = (0, fast_json_stable_stringify.default)([
|
|
29
|
+
salt,
|
|
30
|
+
claimName,
|
|
31
|
+
claimValue
|
|
32
|
+
]);
|
|
33
|
+
const encoded = (0, _ocap_util.toBase64)(Buffer.from(canonicalTuple, "utf8"));
|
|
34
|
+
return {
|
|
35
|
+
salt,
|
|
36
|
+
claimName,
|
|
37
|
+
claimValue,
|
|
38
|
+
encoded,
|
|
39
|
+
digest: computeSdDigest(encoded)
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function decodeSdDisclosure(encoded) {
|
|
43
|
+
const rawJson = Buffer.from((0, _ocap_util.fromBase64)(encoded)).toString("utf8");
|
|
44
|
+
const parsed = JSON.parse(rawJson);
|
|
45
|
+
if (!Array.isArray(parsed) || parsed.length !== 3) throw new Error("Invalid SD-VC disclosure tuple");
|
|
46
|
+
const [salt, claimName, claimValue] = parsed;
|
|
47
|
+
if (typeof salt !== "string" || !salt || typeof claimName !== "string" || !claimName) throw new Error("Malformed SD-VC disclosure salt or claimName");
|
|
48
|
+
if (FORBIDDEN_CLAIM_KEYS.has(claimName)) throw new Error(`Forbidden claimName in SD-VC disclosure: ${claimName}`);
|
|
49
|
+
return {
|
|
50
|
+
salt,
|
|
51
|
+
claimName,
|
|
52
|
+
claimValue,
|
|
53
|
+
encoded,
|
|
54
|
+
digest: computeSdDigest(encoded)
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Create a Selective Disclosure Verifiable Credential (`SD-VC`, inspired by IETF RFC 9901 SD-JWT VC).
|
|
59
|
+
* Replaces sensitive claims listed in `selectiveFields` with salted SHA3-256 hash commitments (`_sd`),
|
|
60
|
+
* signs the credential with the issuer's wallet, and returns `{ vc, disclosures }`.
|
|
61
|
+
*/
|
|
62
|
+
async function createSelectiveDisclosureVc(params) {
|
|
63
|
+
const { type, subject, selectiveFields, issuer, issuanceDate, expirationDate, tag } = params;
|
|
64
|
+
if (!subject || typeof subject !== "object" || !subject.id) throw new Error("Cannot create SD-VC without valid subject.id");
|
|
65
|
+
const uniqueFields = Array.from(new Set(selectiveFields || []));
|
|
66
|
+
for (const field of uniqueFields) {
|
|
67
|
+
if (FORBIDDEN_CLAIM_KEYS.has(field)) throw new Error(`Field "${field}" cannot be selectively concealed`);
|
|
68
|
+
if (!(field in subject)) throw new Error(`Selective field "${field}" does not exist on subject`);
|
|
69
|
+
}
|
|
70
|
+
const publicSubject = {
|
|
71
|
+
id: subject.id,
|
|
72
|
+
_sd: [],
|
|
73
|
+
_sd_alg: SD_HASH_ALG
|
|
74
|
+
};
|
|
75
|
+
const disclosures = [];
|
|
76
|
+
for (const [key, val] of Object.entries(subject)) {
|
|
77
|
+
if (key === "id") continue;
|
|
78
|
+
if (FORBIDDEN_CLAIM_KEYS.has(key)) throw new Error(`Reserved property "${key}" is not allowed in SD-VC subject`);
|
|
79
|
+
if (uniqueFields.includes(key)) {
|
|
80
|
+
const disclosure = encodeSdDisclosure(Buffer.from((0, _ocap_mcrypto.getRandomBytes)(16)).toString("hex"), key, val);
|
|
81
|
+
disclosures.push(disclosure);
|
|
82
|
+
publicSubject._sd.push(disclosure.digest);
|
|
83
|
+
} else publicSubject[key] = val;
|
|
84
|
+
}
|
|
85
|
+
publicSubject._sd.sort();
|
|
86
|
+
const typeList = Array.isArray(type) ? [...type] : [type];
|
|
87
|
+
if (!typeList.includes("SelectiveDisclosureCredential")) typeList.push("SelectiveDisclosureCredential");
|
|
88
|
+
const vc = await require_index.create({
|
|
89
|
+
type: typeList,
|
|
90
|
+
subject: publicSubject,
|
|
91
|
+
issuer,
|
|
92
|
+
issuanceDate,
|
|
93
|
+
expirationDate,
|
|
94
|
+
tag
|
|
95
|
+
});
|
|
96
|
+
if (!vc) throw new Error("Failed to create and sign Selective Disclosure VC");
|
|
97
|
+
return {
|
|
98
|
+
vc,
|
|
99
|
+
disclosures
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Derive a minimal-disclosure `SdPresentation` exposing ONLY `discloseFields`
|
|
104
|
+
* and binding the presentation to the verifier's `challenge` and `domain` with the holder's key.
|
|
105
|
+
*/
|
|
106
|
+
async function deriveSelectiveDisclosurePresentation(params) {
|
|
107
|
+
const { vc, disclosures, discloseFields, holderWallet, challenge = "", domain = "" } = params;
|
|
108
|
+
const rawSd = Array.isArray(vc.credentialSubject?._sd) ? vc.credentialSubject._sd.map(String) : [];
|
|
109
|
+
const commitmentSet = new Set(rawSd);
|
|
110
|
+
const decodedList = disclosures.map((d) => decodeSdDisclosure(typeof d === "string" ? d : d.encoded));
|
|
111
|
+
const wantedSet = new Set(discloseFields);
|
|
112
|
+
const selectedEncoded = [];
|
|
113
|
+
for (const field of wantedSet) {
|
|
114
|
+
const match = decodedList.find((d) => d.claimName === field);
|
|
115
|
+
if (!match) throw new Error(`Requested disclosure field "${field}" not found in holder disclosures`);
|
|
116
|
+
if (!commitmentSet.has(match.digest)) throw new Error(`Disclosure digest for "${field}" is not present in vc.credentialSubject._sd`);
|
|
117
|
+
selectedEncoded.push(match.encoded);
|
|
118
|
+
}
|
|
119
|
+
let keyBindingProof;
|
|
120
|
+
if (holderWallet) {
|
|
121
|
+
const holderDid = holderWallet.address;
|
|
122
|
+
if (vc.credentialSubject?.id && vc.credentialSubject.id !== holderDid) throw new Error("holderWallet address does not match vc.credentialSubject.id");
|
|
123
|
+
const sdHash = _ocap_mcrypto.Hasher.SHA3.hash256(Uint8Array.from(Buffer.from((0, fast_json_stable_stringify.default)({
|
|
124
|
+
vcId: vc.id,
|
|
125
|
+
vcProof: vc.proof,
|
|
126
|
+
disclosures: selectedEncoded
|
|
127
|
+
}), "utf8")));
|
|
128
|
+
const created = (/* @__PURE__ */ new Date()).toISOString();
|
|
129
|
+
const kbPayload = (0, fast_json_stable_stringify.default)({
|
|
130
|
+
holderDid,
|
|
131
|
+
challenge,
|
|
132
|
+
domain,
|
|
133
|
+
sdHash,
|
|
134
|
+
created
|
|
135
|
+
});
|
|
136
|
+
const sig = await holderWallet.sign(kbPayload);
|
|
137
|
+
keyBindingProof = {
|
|
138
|
+
holderDid,
|
|
139
|
+
holderPk: (0, _ocap_util.toBase58)(holderWallet.publicKey),
|
|
140
|
+
challenge,
|
|
141
|
+
domain,
|
|
142
|
+
sdHash,
|
|
143
|
+
created,
|
|
144
|
+
signature: (0, _ocap_util.toBase64)(sig)
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
return {
|
|
148
|
+
verifiableCredential: structuredClone(vc),
|
|
149
|
+
disclosures: selectedEncoded,
|
|
150
|
+
...keyBindingProof ? { keyBindingProof } : {}
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function compareSdNumeric(actual, expected, op) {
|
|
154
|
+
const isIntLike = (v) => typeof v === "bigint" || typeof v === "number" && Number.isFinite(v) && Number.isInteger(v) || typeof v === "string" && CANONICAL_SIGNED_INT_REGEX.test(v);
|
|
155
|
+
if (isIntLike(actual) && isIntLike(expected)) {
|
|
156
|
+
const aBig = BigInt(String(actual));
|
|
157
|
+
const bBig = BigInt(String(expected));
|
|
158
|
+
if (op === "gt") return aBig > bBig;
|
|
159
|
+
if (op === "gte") return aBig >= bBig;
|
|
160
|
+
if (op === "lt") return aBig < bBig;
|
|
161
|
+
return aBig <= bBig;
|
|
162
|
+
}
|
|
163
|
+
const toFiniteNumber = (v) => {
|
|
164
|
+
if (typeof v === "number" && Number.isFinite(v)) return v;
|
|
165
|
+
if (typeof v === "string" && CANONICAL_DECIMAL_REGEX.test(v)) {
|
|
166
|
+
const n = Number(v);
|
|
167
|
+
return Number.isFinite(n) ? n : null;
|
|
168
|
+
}
|
|
169
|
+
return null;
|
|
170
|
+
};
|
|
171
|
+
const aNum = toFiniteNumber(actual);
|
|
172
|
+
const bNum = toFiniteNumber(expected);
|
|
173
|
+
if (aNum === null || bNum === null) return false;
|
|
174
|
+
if (op === "gt") return aNum > bNum;
|
|
175
|
+
if (op === "gte") return aNum >= bNum;
|
|
176
|
+
if (op === "lt") return aNum < bNum;
|
|
177
|
+
return aNum <= bNum;
|
|
178
|
+
}
|
|
179
|
+
function evaluateSdPredicate(pred, claims) {
|
|
180
|
+
if (FORBIDDEN_CLAIM_KEYS.has(pred.field) && pred.field !== "id") return false;
|
|
181
|
+
if (!Object.prototype.hasOwnProperty.call(claims, pred.field)) return false;
|
|
182
|
+
const actual = claims[pred.field];
|
|
183
|
+
if (actual === void 0) return false;
|
|
184
|
+
switch (pred.op) {
|
|
185
|
+
case "eq": return actual === pred.value;
|
|
186
|
+
case "neq": return actual !== pred.value;
|
|
187
|
+
case "gt":
|
|
188
|
+
case "gte":
|
|
189
|
+
case "lt":
|
|
190
|
+
case "lte": return compareSdNumeric(actual, pred.value, pred.op);
|
|
191
|
+
case "in": return Array.isArray(pred.value) && pred.value.includes(actual);
|
|
192
|
+
default: return false;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Verify a Selective Disclosure Presentation (`SdPresentation`):
|
|
197
|
+
* 1. Verifies the Issuer's cryptographic signature on `verifiableCredential`.
|
|
198
|
+
* 2. Verifies each disclosed `[salt, claimName, claimValue]` against `credentialSubject._sd` digests.
|
|
199
|
+
* 3. Rejects duplicate disclosures or tampered digests.
|
|
200
|
+
* 4. Verifies the Holder Key-Binding Proof (`challenge` / `domain` / `sdHash`) if required or present.
|
|
201
|
+
* 5. Evaluates optional `predicates` over the reconstructed claims.
|
|
202
|
+
*/
|
|
203
|
+
async function verifySelectiveDisclosurePresentation(params) {
|
|
204
|
+
const { presentation, trustedIssuers, ownerDid, requireKeyBinding = false, expectedChallenge, expectedDomain, predicates = [] } = params;
|
|
205
|
+
try {
|
|
206
|
+
if (!presentation || !presentation.verifiableCredential) return {
|
|
207
|
+
valid: false,
|
|
208
|
+
disclosedSubject: {},
|
|
209
|
+
undisclosedCount: 0,
|
|
210
|
+
error: "Missing verifiableCredential"
|
|
211
|
+
};
|
|
212
|
+
const vc = presentation.verifiableCredential;
|
|
213
|
+
const holderDid = ownerDid || vc.credentialSubject?.id;
|
|
214
|
+
if (!holderDid || !(0, _arcblock_did.isValid)(holderDid)) return {
|
|
215
|
+
valid: false,
|
|
216
|
+
disclosedSubject: {},
|
|
217
|
+
undisclosedCount: 0,
|
|
218
|
+
error: "Invalid holder DID"
|
|
219
|
+
};
|
|
220
|
+
if (!await require_index.verify({
|
|
221
|
+
vc,
|
|
222
|
+
ownerDid: holderDid,
|
|
223
|
+
trustedIssuers
|
|
224
|
+
})) return {
|
|
225
|
+
valid: false,
|
|
226
|
+
disclosedSubject: {},
|
|
227
|
+
undisclosedCount: 0,
|
|
228
|
+
error: "Base VC signature verification failed"
|
|
229
|
+
};
|
|
230
|
+
const rawSd = vc.credentialSubject?._sd;
|
|
231
|
+
if (!Array.isArray(rawSd) || vc.credentialSubject?._sd_alg !== SD_HASH_ALG || rawSd.some((d) => typeof d !== "string" || !d)) return {
|
|
232
|
+
valid: false,
|
|
233
|
+
disclosedSubject: {},
|
|
234
|
+
undisclosedCount: 0,
|
|
235
|
+
error: "Missing or unsupported _sd commitments"
|
|
236
|
+
};
|
|
237
|
+
const commitmentSet = new Set(rawSd);
|
|
238
|
+
if (commitmentSet.size !== rawSd.length) return {
|
|
239
|
+
valid: false,
|
|
240
|
+
disclosedSubject: {},
|
|
241
|
+
undisclosedCount: 0,
|
|
242
|
+
error: "Duplicate _sd commitments in credential"
|
|
243
|
+
};
|
|
244
|
+
const seenDigests = /* @__PURE__ */ new Set();
|
|
245
|
+
const seenClaimNames = /* @__PURE__ */ new Set();
|
|
246
|
+
const disclosedSubject = {};
|
|
247
|
+
for (const [k, v] of Object.entries(vc.credentialSubject)) {
|
|
248
|
+
if (k === "_sd" || k === "_sd_alg") continue;
|
|
249
|
+
if (!FORBIDDEN_CLAIM_KEYS.has(k) || k === "id") disclosedSubject[k] = v;
|
|
250
|
+
}
|
|
251
|
+
for (const encoded of presentation.disclosures || []) {
|
|
252
|
+
const decoded = decodeSdDisclosure(encoded);
|
|
253
|
+
if (seenDigests.has(decoded.digest) || seenClaimNames.has(decoded.claimName)) return {
|
|
254
|
+
valid: false,
|
|
255
|
+
disclosedSubject: {},
|
|
256
|
+
undisclosedCount: 0,
|
|
257
|
+
error: `Duplicate disclosure for claim ${decoded.claimName}`
|
|
258
|
+
};
|
|
259
|
+
if (!commitmentSet.has(decoded.digest)) return {
|
|
260
|
+
valid: false,
|
|
261
|
+
disclosedSubject: {},
|
|
262
|
+
undisclosedCount: 0,
|
|
263
|
+
error: `Disclosure digest mismatch for claim ${decoded.claimName}`
|
|
264
|
+
};
|
|
265
|
+
if (decoded.claimName in disclosedSubject) return {
|
|
266
|
+
valid: false,
|
|
267
|
+
disclosedSubject: {},
|
|
268
|
+
undisclosedCount: 0,
|
|
269
|
+
error: `Claim ${decoded.claimName} collides with public claim`
|
|
270
|
+
};
|
|
271
|
+
seenDigests.add(decoded.digest);
|
|
272
|
+
seenClaimNames.add(decoded.claimName);
|
|
273
|
+
disclosedSubject[decoded.claimName] = decoded.claimValue;
|
|
274
|
+
}
|
|
275
|
+
if (Boolean(requireKeyBinding || expectedChallenge !== void 0 || expectedDomain !== void 0) && !presentation.keyBindingProof) return {
|
|
276
|
+
valid: false,
|
|
277
|
+
disclosedSubject: {},
|
|
278
|
+
undisclosedCount: 0,
|
|
279
|
+
error: "Missing required keyBindingProof"
|
|
280
|
+
};
|
|
281
|
+
if (presentation.keyBindingProof) {
|
|
282
|
+
const kb = presentation.keyBindingProof;
|
|
283
|
+
if (kb.holderDid !== holderDid) return {
|
|
284
|
+
valid: false,
|
|
285
|
+
disclosedSubject: {},
|
|
286
|
+
undisclosedCount: 0,
|
|
287
|
+
error: "keyBindingProof holderDid mismatch"
|
|
288
|
+
};
|
|
289
|
+
if (!(0, _arcblock_did.isFromPublicKey)(kb.holderDid, kb.holderPk)) return {
|
|
290
|
+
valid: false,
|
|
291
|
+
disclosedSubject: {},
|
|
292
|
+
undisclosedCount: 0,
|
|
293
|
+
error: "keyBindingProof pk mismatch"
|
|
294
|
+
};
|
|
295
|
+
if (expectedChallenge !== void 0 && kb.challenge !== expectedChallenge) return {
|
|
296
|
+
valid: false,
|
|
297
|
+
disclosedSubject: {},
|
|
298
|
+
undisclosedCount: 0,
|
|
299
|
+
error: "keyBindingProof challenge mismatch"
|
|
300
|
+
};
|
|
301
|
+
if (expectedDomain !== void 0 && kb.domain !== expectedDomain) return {
|
|
302
|
+
valid: false,
|
|
303
|
+
disclosedSubject: {},
|
|
304
|
+
undisclosedCount: 0,
|
|
305
|
+
error: "keyBindingProof domain mismatch"
|
|
306
|
+
};
|
|
307
|
+
const expectedSdHash = _ocap_mcrypto.Hasher.SHA3.hash256(Uint8Array.from(Buffer.from((0, fast_json_stable_stringify.default)({
|
|
308
|
+
vcId: vc.id,
|
|
309
|
+
vcProof: vc.proof,
|
|
310
|
+
disclosures: presentation.disclosures || []
|
|
311
|
+
}), "utf8")));
|
|
312
|
+
if (kb.sdHash !== expectedSdHash) return {
|
|
313
|
+
valid: false,
|
|
314
|
+
disclosedSubject: {},
|
|
315
|
+
undisclosedCount: 0,
|
|
316
|
+
error: "keyBindingProof sdHash mismatch"
|
|
317
|
+
};
|
|
318
|
+
const kbPayload = (0, fast_json_stable_stringify.default)({
|
|
319
|
+
holderDid: kb.holderDid,
|
|
320
|
+
challenge: kb.challenge,
|
|
321
|
+
domain: kb.domain,
|
|
322
|
+
sdHash: kb.sdHash,
|
|
323
|
+
created: kb.created
|
|
324
|
+
});
|
|
325
|
+
if (!await (0, _ocap_wallet.fromPublicKey)(kb.holderPk, (0, _arcblock_did.toTypeInfo)(kb.holderDid)).verify(kbPayload, (0, _ocap_util.fromBase64)(kb.signature))) return {
|
|
326
|
+
valid: false,
|
|
327
|
+
disclosedSubject: {},
|
|
328
|
+
undisclosedCount: 0,
|
|
329
|
+
error: "Invalid keyBindingProof signature"
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
for (const pred of predicates) if (!evaluateSdPredicate(pred, disclosedSubject)) return {
|
|
333
|
+
valid: false,
|
|
334
|
+
disclosedSubject: {},
|
|
335
|
+
undisclosedCount: 0,
|
|
336
|
+
error: `Predicate failed: ${pred.field} ${pred.op} ${JSON.stringify(pred.value)}`
|
|
337
|
+
};
|
|
338
|
+
return {
|
|
339
|
+
valid: true,
|
|
340
|
+
disclosedSubject,
|
|
341
|
+
undisclosedCount: commitmentSet.size - seenDigests.size
|
|
342
|
+
};
|
|
343
|
+
} catch (err) {
|
|
344
|
+
return {
|
|
345
|
+
valid: false,
|
|
346
|
+
disclosedSubject: {},
|
|
347
|
+
undisclosedCount: 0,
|
|
348
|
+
error: err?.message || "SD-VC verification error"
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
//#endregion
|
|
354
|
+
exports.SD_HASH_ALG = SD_HASH_ALG;
|
|
355
|
+
exports.computeSdDigest = computeSdDigest;
|
|
356
|
+
exports.createSelectiveDisclosureVc = createSelectiveDisclosureVc;
|
|
357
|
+
exports.decodeSdDisclosure = decodeSdDisclosure;
|
|
358
|
+
exports.deriveSelectiveDisclosurePresentation = deriveSelectiveDisclosurePresentation;
|
|
359
|
+
exports.encodeSdDisclosure = encodeSdDisclosure;
|
|
360
|
+
exports.verifySelectiveDisclosurePresentation = verifySelectiveDisclosurePresentation;
|
package/lib/sd-vc.d.cts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { VerifiableCredential } from "./index.cjs";
|
|
2
|
+
import { WalletObject } from "@ocap/wallet";
|
|
3
|
+
|
|
4
|
+
//#region src/sd-vc.d.ts
|
|
5
|
+
declare const SD_HASH_ALG: "sha3-256";
|
|
6
|
+
interface SdDisclosure {
|
|
7
|
+
/** High-entropy 128-bit+ random salt (hex) */
|
|
8
|
+
salt: string;
|
|
9
|
+
/** Claim property name */
|
|
10
|
+
claimName: string;
|
|
11
|
+
/** Original claim value */
|
|
12
|
+
claimValue: unknown;
|
|
13
|
+
/** Base64 encoded canonical `[salt, claimName, claimValue]` tuple */
|
|
14
|
+
encoded: string;
|
|
15
|
+
/** Quantum-safe SHA3-256 commitment digest stored in `credentialSubject._sd` */
|
|
16
|
+
digest: string;
|
|
17
|
+
}
|
|
18
|
+
interface SdKeyBindingProof {
|
|
19
|
+
holderDid: string;
|
|
20
|
+
holderPk: string;
|
|
21
|
+
challenge: string;
|
|
22
|
+
domain: string;
|
|
23
|
+
sdHash: string;
|
|
24
|
+
created: string;
|
|
25
|
+
signature: string;
|
|
26
|
+
}
|
|
27
|
+
interface SdPresentation {
|
|
28
|
+
verifiableCredential: VerifiableCredential;
|
|
29
|
+
/** Subset of disclosures chosen by the holder for selective presentation */
|
|
30
|
+
disclosures: string[];
|
|
31
|
+
/** Optional Holder Key-Binding Proof preventing replay and MITM correlation */
|
|
32
|
+
keyBindingProof?: SdKeyBindingProof;
|
|
33
|
+
}
|
|
34
|
+
interface SdPredicate {
|
|
35
|
+
field: string;
|
|
36
|
+
op: 'eq' | 'neq' | 'gt' | 'gte' | 'lt' | 'lte' | 'in';
|
|
37
|
+
value: unknown;
|
|
38
|
+
}
|
|
39
|
+
declare function computeSdDigest(encodedDisclosure: string): string;
|
|
40
|
+
declare function encodeSdDisclosure(salt: string, claimName: string, claimValue: unknown): SdDisclosure;
|
|
41
|
+
declare function decodeSdDisclosure(encoded: string): SdDisclosure;
|
|
42
|
+
/**
|
|
43
|
+
* Create a Selective Disclosure Verifiable Credential (`SD-VC`, inspired by IETF RFC 9901 SD-JWT VC).
|
|
44
|
+
* Replaces sensitive claims listed in `selectiveFields` with salted SHA3-256 hash commitments (`_sd`),
|
|
45
|
+
* signs the credential with the issuer's wallet, and returns `{ vc, disclosures }`.
|
|
46
|
+
*/
|
|
47
|
+
declare function createSelectiveDisclosureVc(params: {
|
|
48
|
+
type: string | string[];
|
|
49
|
+
subject: {
|
|
50
|
+
id: string;
|
|
51
|
+
[key: string]: unknown;
|
|
52
|
+
};
|
|
53
|
+
selectiveFields: string[];
|
|
54
|
+
issuer: {
|
|
55
|
+
wallet: WalletObject;
|
|
56
|
+
name?: string;
|
|
57
|
+
};
|
|
58
|
+
issuanceDate?: string;
|
|
59
|
+
expirationDate?: string;
|
|
60
|
+
tag?: string;
|
|
61
|
+
}): Promise<{
|
|
62
|
+
vc: VerifiableCredential;
|
|
63
|
+
disclosures: SdDisclosure[];
|
|
64
|
+
}>;
|
|
65
|
+
/**
|
|
66
|
+
* Derive a minimal-disclosure `SdPresentation` exposing ONLY `discloseFields`
|
|
67
|
+
* and binding the presentation to the verifier's `challenge` and `domain` with the holder's key.
|
|
68
|
+
*/
|
|
69
|
+
declare function deriveSelectiveDisclosurePresentation(params: {
|
|
70
|
+
vc: VerifiableCredential;
|
|
71
|
+
disclosures: Array<SdDisclosure | string>;
|
|
72
|
+
discloseFields: string[];
|
|
73
|
+
holderWallet?: WalletObject;
|
|
74
|
+
challenge?: string;
|
|
75
|
+
domain?: string;
|
|
76
|
+
}): Promise<SdPresentation>;
|
|
77
|
+
/**
|
|
78
|
+
* Verify a Selective Disclosure Presentation (`SdPresentation`):
|
|
79
|
+
* 1. Verifies the Issuer's cryptographic signature on `verifiableCredential`.
|
|
80
|
+
* 2. Verifies each disclosed `[salt, claimName, claimValue]` against `credentialSubject._sd` digests.
|
|
81
|
+
* 3. Rejects duplicate disclosures or tampered digests.
|
|
82
|
+
* 4. Verifies the Holder Key-Binding Proof (`challenge` / `domain` / `sdHash`) if required or present.
|
|
83
|
+
* 5. Evaluates optional `predicates` over the reconstructed claims.
|
|
84
|
+
*/
|
|
85
|
+
declare function verifySelectiveDisclosurePresentation(params: {
|
|
86
|
+
presentation: SdPresentation;
|
|
87
|
+
trustedIssuers: string | string[];
|
|
88
|
+
ownerDid?: string;
|
|
89
|
+
requireKeyBinding?: boolean;
|
|
90
|
+
expectedChallenge?: string;
|
|
91
|
+
expectedDomain?: string;
|
|
92
|
+
predicates?: SdPredicate[];
|
|
93
|
+
}): Promise<{
|
|
94
|
+
valid: boolean;
|
|
95
|
+
disclosedSubject: Record<string, unknown>;
|
|
96
|
+
undisclosedCount: number;
|
|
97
|
+
error?: string;
|
|
98
|
+
}>;
|
|
99
|
+
//#endregion
|
|
100
|
+
export { SD_HASH_ALG, SdDisclosure, SdKeyBindingProof, SdPredicate, SdPresentation, computeSdDigest, createSelectiveDisclosureVc, decodeSdDisclosure, deriveSelectiveDisclosurePresentation, encodeSdDisclosure, verifySelectiveDisclosurePresentation };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcblock/vc",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.31.0",
|
|
4
4
|
"description": "TypeScript lib to work with ArcBlock Verifiable Credentials",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"arcblock",
|
|
@@ -71,10 +71,10 @@
|
|
|
71
71
|
"url": "https://github.com/ArcBlock/blockchain/issues"
|
|
72
72
|
},
|
|
73
73
|
"dependencies": {
|
|
74
|
-
"@arcblock/did": "1.
|
|
75
|
-
"@ocap/mcrypto": "1.
|
|
76
|
-
"@ocap/util": "1.
|
|
77
|
-
"@ocap/wallet": "1.
|
|
74
|
+
"@arcblock/did": "1.31.0",
|
|
75
|
+
"@ocap/mcrypto": "1.31.0",
|
|
76
|
+
"@ocap/util": "1.31.0",
|
|
77
|
+
"@ocap/wallet": "1.31.0",
|
|
78
78
|
"debug": "^4.4.3",
|
|
79
79
|
"is-absolute-url": "^3.0.3",
|
|
80
80
|
"fast-json-stable-stringify": "^2.1.0",
|