@bedrock/vc-verifier 14.0.0 → 14.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.
Files changed (2) hide show
  1. package/lib/http.js +52 -7
  2. package/package.json +3 -2
package/lib/http.js CHANGED
@@ -16,6 +16,7 @@ import {checkStatus} from './status.js';
16
16
  import cors from 'cors';
17
17
  import {createDocumentLoader} from './documentLoader.js';
18
18
  import {createSuites} from './suites.js';
19
+ import {serializeError} from 'serialize-error';
19
20
  import {createValidateMiddleware as validate} from '@bedrock/validation';
20
21
 
21
22
  const {util: {BedrockError}} = bedrock;
@@ -93,6 +94,29 @@ export async function addRoutes({app, service} = {}) {
93
94
  checkStatus: checks.includes('credentialStatus') ?
94
95
  checkStatus : () => ({verified: true})
95
96
  });
97
+ // if proof should have been checked but wasn't due to an error,
98
+ // try to run the check again using the VC's issuance date
99
+ if(checks.includes('proof') &&
100
+ result.error && !result.proof && result.results[0] &&
101
+ typeof credential.issuanceDate === 'string') {
102
+ const proofResult = await vc.verifyCredential({
103
+ credential,
104
+ documentLoader,
105
+ suite,
106
+ now: new Date(credential.issuanceDate),
107
+ // only check credential status when option is set
108
+ checkStatus: checks.includes('credentialStatus') ?
109
+ checkStatus : () => ({verified: true})
110
+ });
111
+ if(proofResult.verified) {
112
+ // overlay original (failed) results on top of proof results
113
+ result.results[0] = {
114
+ ...proofResult.results[0],
115
+ ...result.results[0],
116
+ proofVerified: true
117
+ };
118
+ }
119
+ }
96
120
  response = _createResponse({credential, result, checks});
97
121
  } catch(e) {
98
122
  response = _createResponse({error: e});
@@ -222,11 +246,22 @@ function _createResponse({credential, result, challengeUses, error, checks}) {
222
246
  const results = result.results ||
223
247
  (result.presentationResult && result.presentationResult.results);
224
248
  if(results && results.length > 0) {
249
+ for(const r of results) {
250
+ // ensure error is serializable
251
+ if(r.error) {
252
+ r.error = _serializeError({error: r.error});
253
+ }
254
+ }
225
255
  const [{proof}] = results;
226
- ({verificationMethod} = proof);
256
+ if(proof) {
257
+ ({verificationMethod} = proof);
258
+ }
227
259
  }
228
260
 
229
- if(!result.error) {
261
+ if(result.error) {
262
+ // ensure error is serializable
263
+ error = result.error = _serializeError({error: result.error});
264
+ } else {
230
265
  // try to get error from credential results
231
266
  const firstCredentialResult = (result.presentationResult &&
232
267
  result.credentialResults && result.credentialResults[0]) ||
@@ -249,11 +284,8 @@ function _createResponse({credential, result, challengeUses, error, checks}) {
249
284
  error = firstCredentialResult.error;
250
285
  }
251
286
  error = error || {message: 'Verification error.'};
252
- } else {
253
- error = result.error;
254
- if(!error.message) {
255
- error.message = 'Verification error.';
256
- }
287
+ // ensure error is serializable
288
+ error = _serializeError({error});
257
289
  }
258
290
 
259
291
  let message = error.message;
@@ -274,3 +306,16 @@ function _createResponse({credential, result, challengeUses, error, checks}) {
274
306
  }
275
307
  return response;
276
308
  }
309
+
310
+ function _serializeError({error}) {
311
+ // ensure error is serializable; do not include stack
312
+ error = serializeError(error);
313
+ if(!error.name || error.name === 'Error') {
314
+ error.name = 'VerificationError';
315
+ }
316
+ if(!error.message) {
317
+ error.message = 'Verification error.';
318
+ }
319
+ delete error.stack;
320
+ return error;
321
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrock/vc-verifier",
3
- "version": "14.0.0",
3
+ "version": "14.1.0",
4
4
  "type": "module",
5
5
  "description": "Bedrock VC Verifier",
6
6
  "main": "./lib/index.js",
@@ -35,7 +35,8 @@
35
35
  "assert-plus": "^1.0.0",
36
36
  "bnid": "^3.0.0",
37
37
  "body-parser": "^1.20.0",
38
- "cors": "^2.8.5"
38
+ "cors": "^2.8.5",
39
+ "serialize-error": "^11.0.0"
39
40
  },
40
41
  "peerDependencies": {
41
42
  "@bedrock/core": "^6.0.1",