@arcblock/vc 1.30.24 → 1.30.25
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 +16 -3
- package/esm/index.mjs +55 -7
- package/lib/index.cjs +54 -6
- package/lib/index.d.cts +16 -3
- package/package.json +5 -5
package/esm/index.d.mts
CHANGED
|
@@ -28,6 +28,15 @@ interface CredentialStatus {
|
|
|
28
28
|
type: string;
|
|
29
29
|
scope: string;
|
|
30
30
|
}
|
|
31
|
+
interface RevocationStatus {
|
|
32
|
+
revoked: boolean;
|
|
33
|
+
reason?: string;
|
|
34
|
+
revokedAt?: string;
|
|
35
|
+
source: 'endpoint' | 'chain' | 'statusList' | 'cache';
|
|
36
|
+
}
|
|
37
|
+
interface RevocationChecker {
|
|
38
|
+
check(vc: VerifiableCredential): Promise<RevocationStatus>;
|
|
39
|
+
}
|
|
31
40
|
interface VerifiableCredential {
|
|
32
41
|
'@context': string | string[];
|
|
33
42
|
id: string;
|
|
@@ -80,7 +89,8 @@ declare function create({
|
|
|
80
89
|
expirationDate,
|
|
81
90
|
tag,
|
|
82
91
|
endpoint,
|
|
83
|
-
endpointScope
|
|
92
|
+
endpointScope,
|
|
93
|
+
credentialStatus
|
|
84
94
|
}: {
|
|
85
95
|
type: string | string[];
|
|
86
96
|
subject: CredentialSubject;
|
|
@@ -90,6 +100,7 @@ declare function create({
|
|
|
90
100
|
tag?: string;
|
|
91
101
|
endpoint?: string;
|
|
92
102
|
endpointScope?: string;
|
|
103
|
+
credentialStatus?: CredentialStatus;
|
|
93
104
|
}): Promise<VerifiableCredential | null>;
|
|
94
105
|
/**
|
|
95
106
|
* Verify that the verifiable credential is valid
|
|
@@ -108,12 +119,14 @@ declare function verify({
|
|
|
108
119
|
vc,
|
|
109
120
|
ownerDid,
|
|
110
121
|
trustedIssuers,
|
|
111
|
-
ignoreExpired
|
|
122
|
+
ignoreExpired,
|
|
123
|
+
revocationChecker
|
|
112
124
|
}: {
|
|
113
125
|
vc: VerifiableCredential | null;
|
|
114
126
|
ownerDid: string;
|
|
115
127
|
trustedIssuers: string | string[];
|
|
116
128
|
ignoreExpired?: boolean;
|
|
129
|
+
revocationChecker?: RevocationChecker;
|
|
117
130
|
}): Promise<boolean>;
|
|
118
131
|
/**
|
|
119
132
|
* Counter-sign an existing verifiable credential
|
|
@@ -176,4 +189,4 @@ declare function verifyCredentialList({
|
|
|
176
189
|
}): Promise<unknown[]>;
|
|
177
190
|
declare const stableStringify: typeof stringify;
|
|
178
191
|
//#endregion
|
|
179
|
-
export { counterSign, create, createCredentialList, proofTypes, stableStringify, verify, verifyCredentialList, verifyPresentation };
|
|
192
|
+
export { type CredentialStatus, type RevocationChecker, type RevocationStatus, type VerifiableCredential, counterSign, create, createCredentialList, proofTypes, stableStringify, verify, verifyCredentialList, verifyPresentation };
|
package/esm/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { name } from "./package.mjs";
|
|
2
2
|
import { fromPublicKeyHash, isFromPublicKey, isValid, toTypeInfo } from "@arcblock/did";
|
|
3
3
|
import { types } from "@ocap/mcrypto";
|
|
4
|
-
import {
|
|
4
|
+
import { fromBase64, toBase58, toBase64 } from "@ocap/util";
|
|
5
5
|
import { fromPublicKey } from "@ocap/wallet";
|
|
6
6
|
import Debug from "debug";
|
|
7
7
|
import stringify from "fast-json-stable-stringify";
|
|
@@ -34,7 +34,7 @@ const proofTypes = {
|
|
|
34
34
|
* @param params.endpointScope - Endpoint scope, either be public or private
|
|
35
35
|
* @returns Promise<object>
|
|
36
36
|
*/
|
|
37
|
-
async function create({ type, subject, issuer, issuanceDate, expirationDate, tag = "", endpoint = "", endpointScope = "public" }) {
|
|
37
|
+
async function create({ type, subject, issuer, issuanceDate, expirationDate, tag = "", endpoint = "", endpointScope = "public", credentialStatus }) {
|
|
38
38
|
const typeArray = Array.isArray(type) ? [...type] : type ? [type] : [];
|
|
39
39
|
if (typeArray.length === 0 || typeArray.some((t) => !t)) throw new Error("Can not create verifiable credential without empty type");
|
|
40
40
|
if (!typeArray.includes("VerifiableCredential")) typeArray.unshift("VerifiableCredential");
|
|
@@ -69,7 +69,8 @@ async function create({ type, subject, issuer, issuanceDate, expirationDate, tag
|
|
|
69
69
|
credentialSubject: subject
|
|
70
70
|
};
|
|
71
71
|
if (tag) vcObj.tag = tag;
|
|
72
|
-
if (
|
|
72
|
+
if (credentialStatus) vcObj.credentialStatus = credentialStatus;
|
|
73
|
+
else if (endpoint) vcObj.credentialStatus = {
|
|
73
74
|
id: endpoint,
|
|
74
75
|
type: "NFTStatusList2021",
|
|
75
76
|
scope: endpointScope || "public"
|
|
@@ -96,6 +97,19 @@ async function create({ type, subject, issuer, issuanceDate, expirationDate, tag
|
|
|
96
97
|
return null;
|
|
97
98
|
}
|
|
98
99
|
/**
|
|
100
|
+
* In chain mode (any proof carries previousProof) proofs are resolved by proof.id, so duplicate
|
|
101
|
+
* ids make the reference ambiguous and are rejected. Set-mode lists (no previousProof) keep
|
|
102
|
+
* allowing repeated ids/signers: existing tests and data treat same-signer multi-proofs as valid.
|
|
103
|
+
*/
|
|
104
|
+
function assertUniqueChainProofIds(proofList) {
|
|
105
|
+
if (!proofList.some((p) => p.previousProof)) return;
|
|
106
|
+
const ids = /* @__PURE__ */ new Set();
|
|
107
|
+
for (const proof of proofList) if (proof.id) {
|
|
108
|
+
if (ids.has(proof.id)) throw new Error("Duplicate proof id");
|
|
109
|
+
ids.add(proof.id);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
99
113
|
* Verify that the verifiable credential is valid
|
|
100
114
|
* - It is signed by a whitelist of issuers
|
|
101
115
|
* - It is owned by the vc.subject.id
|
|
@@ -108,15 +122,22 @@ async function create({ type, subject, issuer, issuanceDate, expirationDate, tag
|
|
|
108
122
|
* @throws Error
|
|
109
123
|
* @returns Promise<boolean>
|
|
110
124
|
*/
|
|
111
|
-
async function verify({ vc, ownerDid, trustedIssuers, ignoreExpired = false }) {
|
|
125
|
+
async function verify({ vc, ownerDid, trustedIssuers, ignoreExpired = false, revocationChecker }) {
|
|
112
126
|
if (!vc) throw new Error("Empty verifiable credential object");
|
|
113
127
|
if (!vc.issuer || !vc.issuer.id || !vc.issuer.pk || !isValid(vc.issuer.id)) throw new Error("Invalid verifiable credential issuer");
|
|
114
128
|
if (!vc.credentialSubject || !vc.credentialSubject.id || !isValid(vc.credentialSubject.id)) throw new Error("Invalid verifiable credential subject");
|
|
115
129
|
const proofList = Array.isArray(vc.proof) ? vc.proof : vc.proof ? [vc.proof] : [];
|
|
116
130
|
if (proofList.length === 0 || proofList.some((p) => !p || !p.jws)) throw new Error("Invalid verifiable credential proof");
|
|
131
|
+
assertUniqueChainProofIds(proofList);
|
|
117
132
|
if (vc.issuanceDate === void 0) throw Error("Invalid verifiable credential issue date");
|
|
118
|
-
|
|
119
|
-
if (
|
|
133
|
+
const issuanceTime = new Date(vc.issuanceDate).getTime();
|
|
134
|
+
if (Number.isNaN(issuanceTime)) throw Error("Invalid verifiable credential issue date");
|
|
135
|
+
if (issuanceTime > Date.now()) throw Error("Verifiable credential has not take effect");
|
|
136
|
+
if (vc.expirationDate !== void 0 && vc.expirationDate !== "") {
|
|
137
|
+
const expirationTime = new Date(vc.expirationDate).getTime();
|
|
138
|
+
if (Number.isNaN(expirationTime)) throw Error("Invalid verifiable credential expiration date");
|
|
139
|
+
if (!ignoreExpired && expirationTime < Date.now()) throw Error("Verifiable credential has expired");
|
|
140
|
+
}
|
|
120
141
|
const issuers = Array.isArray(trustedIssuers) ? trustedIssuers : [trustedIssuers];
|
|
121
142
|
if (!isFromPublicKey(vc.issuer.id, vc.issuer.pk)) throw new Error("Verifiable credential not issuer pk not match with issuer did");
|
|
122
143
|
if (ownerDid !== vc.credentialSubject.id) throw new Error("Verifiable credential not owned by specified owner did");
|
|
@@ -130,12 +151,35 @@ async function verify({ vc, ownerDid, trustedIssuers, ignoreExpired = false }) {
|
|
|
130
151
|
const prevProofRefs = proof.previousProof;
|
|
131
152
|
if (prevProofRefs && (typeof prevProofRefs === "string" || Array.isArray(prevProofRefs) && prevProofRefs.length > 0)) {
|
|
132
153
|
const prevIds = Array.isArray(prevProofRefs) ? prevProofRefs : [prevProofRefs];
|
|
154
|
+
if (proof.id && prevIds.includes(proof.id)) throw new Error("Proof cannot reference itself as previousProof");
|
|
155
|
+
if (new Set(prevIds).size !== prevIds.length) throw new Error("Duplicate previousProof reference");
|
|
133
156
|
const matchingProofs = proofList.filter((p) => p.id && prevIds.includes(p.id));
|
|
134
157
|
if (matchingProofs.length !== prevIds.length) throw new Error("Referenced previous proof not found");
|
|
158
|
+
const verifiedAcyclic = /* @__PURE__ */ new Set();
|
|
159
|
+
const checkCycle = (currId, activePath) => {
|
|
160
|
+
if (activePath.has(currId)) throw new Error("Circular previousProof reference detected");
|
|
161
|
+
if (verifiedAcyclic.has(currId)) return;
|
|
162
|
+
activePath.add(currId);
|
|
163
|
+
const currProof = proofList.find((p) => p.id === currId);
|
|
164
|
+
if (currProof?.previousProof) {
|
|
165
|
+
const nextIds = Array.isArray(currProof.previousProof) ? currProof.previousProof : [currProof.previousProof];
|
|
166
|
+
for (const nextId of nextIds) checkCycle(nextId, activePath);
|
|
167
|
+
}
|
|
168
|
+
activePath.delete(currId);
|
|
169
|
+
verifiedAcyclic.add(currId);
|
|
170
|
+
};
|
|
171
|
+
const initialPath = new Set(proof.id ? [proof.id] : []);
|
|
172
|
+
for (const prevId of prevIds) checkCycle(prevId, initialPath);
|
|
135
173
|
clone.proof = matchingProofs;
|
|
136
174
|
} else delete clone.proof;
|
|
137
175
|
const signedContent = stringify(clone);
|
|
138
176
|
if (await fromPublicKey(proofPk, toTypeInfo(proofSigner)).verify(signedContent, fromBase64(proof.jws)) !== true) throw Error("Verifiable credential signature not valid");
|
|
177
|
+
if (!isFromPublicKey(proofSigner, proofPk)) throw new Error("Proof pk does not match signer");
|
|
178
|
+
}
|
|
179
|
+
if (!proofList.some((p) => (p.signer || vc.issuer.id) === vc.issuer.id)) throw new Error("Verifiable credential must have a proof from issuer");
|
|
180
|
+
if (revocationChecker) {
|
|
181
|
+
const status = await revocationChecker.check(vc);
|
|
182
|
+
if (status.revoked) throw new Error(`Credential has been revoked${status.reason ? `: ${status.reason}` : ""}`);
|
|
139
183
|
}
|
|
140
184
|
return true;
|
|
141
185
|
}
|
|
@@ -199,7 +243,9 @@ async function counterSign({ vc, wallet, mode = "set" }) {
|
|
|
199
243
|
*/
|
|
200
244
|
async function verifyPresentation({ presentation, trustedIssuers, challenge, ignoreExpired = false }) {
|
|
201
245
|
if (!presentation.challenge || challenge !== presentation.challenge) throw Error("Invalid challenge included on vc presentation");
|
|
246
|
+
if (!presentation.verifiableCredential) throw Error("Invalid verifiableCredential in presentation");
|
|
202
247
|
const vcList = Array.isArray(presentation.verifiableCredential) ? presentation.verifiableCredential : [presentation.verifiableCredential];
|
|
248
|
+
if (vcList.length === 0 || vcList.some((v) => !v)) throw Error("Invalid verifiableCredential in presentation");
|
|
203
249
|
const proofList = Array.isArray(presentation.proof) ? presentation.proof : [presentation.proof];
|
|
204
250
|
const clone = structuredClone(presentation);
|
|
205
251
|
delete clone.proof;
|
|
@@ -208,7 +254,7 @@ async function verifyPresentation({ presentation, trustedIssuers, challenge, ign
|
|
|
208
254
|
const proof = proofList.find((x) => isFromPublicKey(vcObj.credentialSubject.id, x.pk));
|
|
209
255
|
if (!proof) throw Error(`VC does not have corresponding proof: ${vcStr}`);
|
|
210
256
|
const signatureStr = proof.jws;
|
|
211
|
-
if (await fromPublicKey(
|
|
257
|
+
if (await fromPublicKey(proof.pk, toTypeInfo(vcObj.credentialSubject.id)).verify(stringify(clone), fromBase64(signatureStr)) !== true) throw Error("Presentation signature invalid");
|
|
212
258
|
await verify({
|
|
213
259
|
vc: vcObj,
|
|
214
260
|
ownerDid: vcObj.credentialSubject.id,
|
|
@@ -272,7 +318,9 @@ async function verifyCredentialList({ credentials, trustedIssuers }) {
|
|
|
272
318
|
if (!issuers.includes(proofSigner)) throw new Error("Proof signer not in trusted issuers");
|
|
273
319
|
if (proof.signer && proof.pk && !isFromPublicKey(proof.signer, proof.pk)) throw new Error("Proof pk does not match signer");
|
|
274
320
|
if (await fromPublicKey(proofPk, toTypeInfo(proofSigner)).verify(signedContent, fromBase64(proof.jws)) !== true) throw Error("Status credential signature not valid");
|
|
321
|
+
if (!isFromPublicKey(proofSigner, proofPk)) throw new Error("Proof pk does not match signer");
|
|
275
322
|
}
|
|
323
|
+
if (!proofList.some((p) => (p.signer || x.issuer.id) === x.issuer.id)) throw new Error("Verifiable credential must have a proof from issuer");
|
|
276
324
|
return x.claim;
|
|
277
325
|
}));
|
|
278
326
|
}
|
package/lib/index.cjs
CHANGED
|
@@ -38,7 +38,7 @@ const proofTypes = {
|
|
|
38
38
|
* @param params.endpointScope - Endpoint scope, either be public or private
|
|
39
39
|
* @returns Promise<object>
|
|
40
40
|
*/
|
|
41
|
-
async function create({ type, subject, issuer, issuanceDate, expirationDate, tag = "", endpoint = "", endpointScope = "public" }) {
|
|
41
|
+
async function create({ type, subject, issuer, issuanceDate, expirationDate, tag = "", endpoint = "", endpointScope = "public", credentialStatus }) {
|
|
42
42
|
const typeArray = Array.isArray(type) ? [...type] : type ? [type] : [];
|
|
43
43
|
if (typeArray.length === 0 || typeArray.some((t) => !t)) throw new Error("Can not create verifiable credential without empty type");
|
|
44
44
|
if (!typeArray.includes("VerifiableCredential")) typeArray.unshift("VerifiableCredential");
|
|
@@ -73,7 +73,8 @@ async function create({ type, subject, issuer, issuanceDate, expirationDate, tag
|
|
|
73
73
|
credentialSubject: subject
|
|
74
74
|
};
|
|
75
75
|
if (tag) vcObj.tag = tag;
|
|
76
|
-
if (
|
|
76
|
+
if (credentialStatus) vcObj.credentialStatus = credentialStatus;
|
|
77
|
+
else if (endpoint) vcObj.credentialStatus = {
|
|
77
78
|
id: endpoint,
|
|
78
79
|
type: "NFTStatusList2021",
|
|
79
80
|
scope: endpointScope || "public"
|
|
@@ -100,6 +101,19 @@ async function create({ type, subject, issuer, issuanceDate, expirationDate, tag
|
|
|
100
101
|
return null;
|
|
101
102
|
}
|
|
102
103
|
/**
|
|
104
|
+
* In chain mode (any proof carries previousProof) proofs are resolved by proof.id, so duplicate
|
|
105
|
+
* ids make the reference ambiguous and are rejected. Set-mode lists (no previousProof) keep
|
|
106
|
+
* allowing repeated ids/signers: existing tests and data treat same-signer multi-proofs as valid.
|
|
107
|
+
*/
|
|
108
|
+
function assertUniqueChainProofIds(proofList) {
|
|
109
|
+
if (!proofList.some((p) => p.previousProof)) return;
|
|
110
|
+
const ids = /* @__PURE__ */ new Set();
|
|
111
|
+
for (const proof of proofList) if (proof.id) {
|
|
112
|
+
if (ids.has(proof.id)) throw new Error("Duplicate proof id");
|
|
113
|
+
ids.add(proof.id);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
103
117
|
* Verify that the verifiable credential is valid
|
|
104
118
|
* - It is signed by a whitelist of issuers
|
|
105
119
|
* - It is owned by the vc.subject.id
|
|
@@ -112,15 +126,22 @@ async function create({ type, subject, issuer, issuanceDate, expirationDate, tag
|
|
|
112
126
|
* @throws Error
|
|
113
127
|
* @returns Promise<boolean>
|
|
114
128
|
*/
|
|
115
|
-
async function verify({ vc, ownerDid, trustedIssuers, ignoreExpired = false }) {
|
|
129
|
+
async function verify({ vc, ownerDid, trustedIssuers, ignoreExpired = false, revocationChecker }) {
|
|
116
130
|
if (!vc) throw new Error("Empty verifiable credential object");
|
|
117
131
|
if (!vc.issuer || !vc.issuer.id || !vc.issuer.pk || !(0, _arcblock_did.isValid)(vc.issuer.id)) throw new Error("Invalid verifiable credential issuer");
|
|
118
132
|
if (!vc.credentialSubject || !vc.credentialSubject.id || !(0, _arcblock_did.isValid)(vc.credentialSubject.id)) throw new Error("Invalid verifiable credential subject");
|
|
119
133
|
const proofList = Array.isArray(vc.proof) ? vc.proof : vc.proof ? [vc.proof] : [];
|
|
120
134
|
if (proofList.length === 0 || proofList.some((p) => !p || !p.jws)) throw new Error("Invalid verifiable credential proof");
|
|
135
|
+
assertUniqueChainProofIds(proofList);
|
|
121
136
|
if (vc.issuanceDate === void 0) throw Error("Invalid verifiable credential issue date");
|
|
122
|
-
|
|
123
|
-
if (
|
|
137
|
+
const issuanceTime = new Date(vc.issuanceDate).getTime();
|
|
138
|
+
if (Number.isNaN(issuanceTime)) throw Error("Invalid verifiable credential issue date");
|
|
139
|
+
if (issuanceTime > Date.now()) throw Error("Verifiable credential has not take effect");
|
|
140
|
+
if (vc.expirationDate !== void 0 && vc.expirationDate !== "") {
|
|
141
|
+
const expirationTime = new Date(vc.expirationDate).getTime();
|
|
142
|
+
if (Number.isNaN(expirationTime)) throw Error("Invalid verifiable credential expiration date");
|
|
143
|
+
if (!ignoreExpired && expirationTime < Date.now()) throw Error("Verifiable credential has expired");
|
|
144
|
+
}
|
|
124
145
|
const issuers = Array.isArray(trustedIssuers) ? trustedIssuers : [trustedIssuers];
|
|
125
146
|
if (!(0, _arcblock_did.isFromPublicKey)(vc.issuer.id, vc.issuer.pk)) throw new Error("Verifiable credential not issuer pk not match with issuer did");
|
|
126
147
|
if (ownerDid !== vc.credentialSubject.id) throw new Error("Verifiable credential not owned by specified owner did");
|
|
@@ -134,12 +155,35 @@ async function verify({ vc, ownerDid, trustedIssuers, ignoreExpired = false }) {
|
|
|
134
155
|
const prevProofRefs = proof.previousProof;
|
|
135
156
|
if (prevProofRefs && (typeof prevProofRefs === "string" || Array.isArray(prevProofRefs) && prevProofRefs.length > 0)) {
|
|
136
157
|
const prevIds = Array.isArray(prevProofRefs) ? prevProofRefs : [prevProofRefs];
|
|
158
|
+
if (proof.id && prevIds.includes(proof.id)) throw new Error("Proof cannot reference itself as previousProof");
|
|
159
|
+
if (new Set(prevIds).size !== prevIds.length) throw new Error("Duplicate previousProof reference");
|
|
137
160
|
const matchingProofs = proofList.filter((p) => p.id && prevIds.includes(p.id));
|
|
138
161
|
if (matchingProofs.length !== prevIds.length) throw new Error("Referenced previous proof not found");
|
|
162
|
+
const verifiedAcyclic = /* @__PURE__ */ new Set();
|
|
163
|
+
const checkCycle = (currId, activePath) => {
|
|
164
|
+
if (activePath.has(currId)) throw new Error("Circular previousProof reference detected");
|
|
165
|
+
if (verifiedAcyclic.has(currId)) return;
|
|
166
|
+
activePath.add(currId);
|
|
167
|
+
const currProof = proofList.find((p) => p.id === currId);
|
|
168
|
+
if (currProof?.previousProof) {
|
|
169
|
+
const nextIds = Array.isArray(currProof.previousProof) ? currProof.previousProof : [currProof.previousProof];
|
|
170
|
+
for (const nextId of nextIds) checkCycle(nextId, activePath);
|
|
171
|
+
}
|
|
172
|
+
activePath.delete(currId);
|
|
173
|
+
verifiedAcyclic.add(currId);
|
|
174
|
+
};
|
|
175
|
+
const initialPath = new Set(proof.id ? [proof.id] : []);
|
|
176
|
+
for (const prevId of prevIds) checkCycle(prevId, initialPath);
|
|
139
177
|
clone.proof = matchingProofs;
|
|
140
178
|
} else delete clone.proof;
|
|
141
179
|
const signedContent = (0, fast_json_stable_stringify.default)(clone);
|
|
142
180
|
if (await (0, _ocap_wallet.fromPublicKey)(proofPk, (0, _arcblock_did.toTypeInfo)(proofSigner)).verify(signedContent, (0, _ocap_util.fromBase64)(proof.jws)) !== true) throw Error("Verifiable credential signature not valid");
|
|
181
|
+
if (!(0, _arcblock_did.isFromPublicKey)(proofSigner, proofPk)) throw new Error("Proof pk does not match signer");
|
|
182
|
+
}
|
|
183
|
+
if (!proofList.some((p) => (p.signer || vc.issuer.id) === vc.issuer.id)) throw new Error("Verifiable credential must have a proof from issuer");
|
|
184
|
+
if (revocationChecker) {
|
|
185
|
+
const status = await revocationChecker.check(vc);
|
|
186
|
+
if (status.revoked) throw new Error(`Credential has been revoked${status.reason ? `: ${status.reason}` : ""}`);
|
|
143
187
|
}
|
|
144
188
|
return true;
|
|
145
189
|
}
|
|
@@ -203,7 +247,9 @@ async function counterSign({ vc, wallet, mode = "set" }) {
|
|
|
203
247
|
*/
|
|
204
248
|
async function verifyPresentation({ presentation, trustedIssuers, challenge, ignoreExpired = false }) {
|
|
205
249
|
if (!presentation.challenge || challenge !== presentation.challenge) throw Error("Invalid challenge included on vc presentation");
|
|
250
|
+
if (!presentation.verifiableCredential) throw Error("Invalid verifiableCredential in presentation");
|
|
206
251
|
const vcList = Array.isArray(presentation.verifiableCredential) ? presentation.verifiableCredential : [presentation.verifiableCredential];
|
|
252
|
+
if (vcList.length === 0 || vcList.some((v) => !v)) throw Error("Invalid verifiableCredential in presentation");
|
|
207
253
|
const proofList = Array.isArray(presentation.proof) ? presentation.proof : [presentation.proof];
|
|
208
254
|
const clone = structuredClone(presentation);
|
|
209
255
|
delete clone.proof;
|
|
@@ -212,7 +258,7 @@ async function verifyPresentation({ presentation, trustedIssuers, challenge, ign
|
|
|
212
258
|
const proof = proofList.find((x) => (0, _arcblock_did.isFromPublicKey)(vcObj.credentialSubject.id, x.pk));
|
|
213
259
|
if (!proof) throw Error(`VC does not have corresponding proof: ${vcStr}`);
|
|
214
260
|
const signatureStr = proof.jws;
|
|
215
|
-
if (await (0, _ocap_wallet.fromPublicKey)(
|
|
261
|
+
if (await (0, _ocap_wallet.fromPublicKey)(proof.pk, (0, _arcblock_did.toTypeInfo)(vcObj.credentialSubject.id)).verify((0, fast_json_stable_stringify.default)(clone), (0, _ocap_util.fromBase64)(signatureStr)) !== true) throw Error("Presentation signature invalid");
|
|
216
262
|
await verify({
|
|
217
263
|
vc: vcObj,
|
|
218
264
|
ownerDid: vcObj.credentialSubject.id,
|
|
@@ -276,7 +322,9 @@ async function verifyCredentialList({ credentials, trustedIssuers }) {
|
|
|
276
322
|
if (!issuers.includes(proofSigner)) throw new Error("Proof signer not in trusted issuers");
|
|
277
323
|
if (proof.signer && proof.pk && !(0, _arcblock_did.isFromPublicKey)(proof.signer, proof.pk)) throw new Error("Proof pk does not match signer");
|
|
278
324
|
if (await (0, _ocap_wallet.fromPublicKey)(proofPk, (0, _arcblock_did.toTypeInfo)(proofSigner)).verify(signedContent, (0, _ocap_util.fromBase64)(proof.jws)) !== true) throw Error("Status credential signature not valid");
|
|
325
|
+
if (!(0, _arcblock_did.isFromPublicKey)(proofSigner, proofPk)) throw new Error("Proof pk does not match signer");
|
|
279
326
|
}
|
|
327
|
+
if (!proofList.some((p) => (p.signer || x.issuer.id) === x.issuer.id)) throw new Error("Verifiable credential must have a proof from issuer");
|
|
280
328
|
return x.claim;
|
|
281
329
|
}));
|
|
282
330
|
}
|
package/lib/index.d.cts
CHANGED
|
@@ -28,6 +28,15 @@ interface CredentialStatus {
|
|
|
28
28
|
type: string;
|
|
29
29
|
scope: string;
|
|
30
30
|
}
|
|
31
|
+
interface RevocationStatus {
|
|
32
|
+
revoked: boolean;
|
|
33
|
+
reason?: string;
|
|
34
|
+
revokedAt?: string;
|
|
35
|
+
source: 'endpoint' | 'chain' | 'statusList' | 'cache';
|
|
36
|
+
}
|
|
37
|
+
interface RevocationChecker {
|
|
38
|
+
check(vc: VerifiableCredential): Promise<RevocationStatus>;
|
|
39
|
+
}
|
|
31
40
|
interface VerifiableCredential {
|
|
32
41
|
'@context': string | string[];
|
|
33
42
|
id: string;
|
|
@@ -80,7 +89,8 @@ declare function create({
|
|
|
80
89
|
expirationDate,
|
|
81
90
|
tag,
|
|
82
91
|
endpoint,
|
|
83
|
-
endpointScope
|
|
92
|
+
endpointScope,
|
|
93
|
+
credentialStatus
|
|
84
94
|
}: {
|
|
85
95
|
type: string | string[];
|
|
86
96
|
subject: CredentialSubject;
|
|
@@ -90,6 +100,7 @@ declare function create({
|
|
|
90
100
|
tag?: string;
|
|
91
101
|
endpoint?: string;
|
|
92
102
|
endpointScope?: string;
|
|
103
|
+
credentialStatus?: CredentialStatus;
|
|
93
104
|
}): Promise<VerifiableCredential | null>;
|
|
94
105
|
/**
|
|
95
106
|
* Verify that the verifiable credential is valid
|
|
@@ -108,12 +119,14 @@ declare function verify({
|
|
|
108
119
|
vc,
|
|
109
120
|
ownerDid,
|
|
110
121
|
trustedIssuers,
|
|
111
|
-
ignoreExpired
|
|
122
|
+
ignoreExpired,
|
|
123
|
+
revocationChecker
|
|
112
124
|
}: {
|
|
113
125
|
vc: VerifiableCredential | null;
|
|
114
126
|
ownerDid: string;
|
|
115
127
|
trustedIssuers: string | string[];
|
|
116
128
|
ignoreExpired?: boolean;
|
|
129
|
+
revocationChecker?: RevocationChecker;
|
|
117
130
|
}): Promise<boolean>;
|
|
118
131
|
/**
|
|
119
132
|
* Counter-sign an existing verifiable credential
|
|
@@ -176,4 +189,4 @@ declare function verifyCredentialList({
|
|
|
176
189
|
}): Promise<unknown[]>;
|
|
177
190
|
declare const stableStringify: typeof stringify;
|
|
178
191
|
//#endregion
|
|
179
|
-
export { counterSign, create, createCredentialList, proofTypes, stableStringify, verify, verifyCredentialList, verifyPresentation };
|
|
192
|
+
export { type CredentialStatus, type RevocationChecker, type RevocationStatus, type VerifiableCredential, counterSign, create, createCredentialList, proofTypes, stableStringify, verify, verifyCredentialList, verifyPresentation };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcblock/vc",
|
|
3
|
-
"version": "1.30.
|
|
3
|
+
"version": "1.30.25",
|
|
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.30.
|
|
75
|
-
"@ocap/mcrypto": "1.30.
|
|
76
|
-
"@ocap/util": "1.30.
|
|
77
|
-
"@ocap/wallet": "1.30.
|
|
74
|
+
"@arcblock/did": "1.30.25",
|
|
75
|
+
"@ocap/mcrypto": "1.30.25",
|
|
76
|
+
"@ocap/util": "1.30.25",
|
|
77
|
+
"@ocap/wallet": "1.30.25",
|
|
78
78
|
"debug": "^4.4.3",
|
|
79
79
|
"is-absolute-url": "^3.0.3",
|
|
80
80
|
"fast-json-stable-stringify": "^2.1.0",
|