@arcblock/vc 1.18.166 → 1.19.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/lib/index.js +53 -48
  2. package/package.json +6 -6
package/lib/index.js CHANGED
@@ -33,9 +33,9 @@ const proofTypes = {
33
33
  * @param {Date} params.expirationDate
34
34
  * @param {String} params.endpoint - Status endpoint url
35
35
  * @param {String} params.endpointScope - Endpoint scope, either be public or private
36
- * @returns {object}
36
+ * @returns {Promise<object>}
37
37
  */
38
- function create({
38
+ async function create({
39
39
  type,
40
40
  subject,
41
41
  issuer,
@@ -124,7 +124,7 @@ function create({
124
124
 
125
125
  debug('create', result);
126
126
 
127
- if (verify({ vc: result, ownerDid: subject.id, trustedIssuers: [issuerDid] })) {
127
+ if (await verify({ vc: result, ownerDid: subject.id, trustedIssuers: [issuerDid] })) {
128
128
  return result;
129
129
  }
130
130
 
@@ -142,9 +142,9 @@ function create({
142
142
  * @param {string} ownerDid - vc holder/owner did
143
143
  * @param {Array} trustedIssuers - list of issuer did
144
144
  * @throws {Error}
145
- * @returns {boolean}
145
+ * @returns {Promise<boolean>}
146
146
  */
147
- function verify({ vc, ownerDid, trustedIssuers, ignoreExpired = false }) {
147
+ async function verify({ vc, ownerDid, trustedIssuers, ignoreExpired = false }) {
148
148
  // Integrity check
149
149
  if (!vc) {
150
150
  throw new Error('Empty verifiable credential object');
@@ -196,7 +196,7 @@ function verify({ vc, ownerDid, trustedIssuers, ignoreExpired = false }) {
196
196
  delete clone.signature;
197
197
 
198
198
  // Verify signature
199
- if (issuer.verify(stringify(clone), fromBase64(signature)) !== true) {
199
+ if ((await issuer.verify(stringify(clone), fromBase64(signature))) !== true) {
200
200
  throw Error('Verifiable credential signature not valid');
201
201
  }
202
202
 
@@ -216,9 +216,9 @@ function verify({ vc, ownerDid, trustedIssuers, ignoreExpired = false }) {
216
216
  * @param {Array} trustedIssuers - list of issuer did
217
217
  * @param {String} challenge - Random byte you want
218
218
  * @throws {Error}
219
- * @returns {boolean}
219
+ * @returns {Promise<boolean>}
220
220
  */
221
- function verifyPresentation({ presentation, trustedIssuers, challenge, ignoreExpired = false }) {
221
+ async function verifyPresentation({ presentation, trustedIssuers, challenge, ignoreExpired = false }) {
222
222
  if (!presentation.challenge || challenge !== presentation.challenge) {
223
223
  throw Error('Invalid challenge included on vc presentation');
224
224
  }
@@ -231,22 +231,24 @@ function verifyPresentation({ presentation, trustedIssuers, challenge, ignoreExp
231
231
  const clone = cloneDeep(presentation);
232
232
  delete clone.proof;
233
233
 
234
- vcList.forEach((vcStr) => {
235
- const vcObj = JSON.parse(vcStr);
236
- const proof = proofList.find((x) => isFromPublicKey(vcObj.credentialSubject.id, x.pk));
234
+ await Promise.all(
235
+ vcList.map(async (vcStr) => {
236
+ const vcObj = JSON.parse(vcStr);
237
+ const proof = proofList.find((x) => isFromPublicKey(vcObj.credentialSubject.id, x.pk));
237
238
 
238
- if (!proof) {
239
- throw Error(`VC does not have corresponding proof: ${vcStr}`);
240
- }
239
+ if (!proof) {
240
+ throw Error(`VC does not have corresponding proof: ${vcStr}`);
241
+ }
241
242
 
242
- const signature = proof.jws;
243
- const holder = fromPublicKey(fromBase58(proof.pk), toTypeInfo(vcObj.credentialSubject.id));
244
- if (holder.verify(stringify(clone), fromBase64(signature)) !== true) {
245
- throw Error('Presentation signature invalid');
246
- }
243
+ const signature = proof.jws;
244
+ const holder = fromPublicKey(fromBase58(proof.pk), toTypeInfo(vcObj.credentialSubject.id));
245
+ if ((await holder.verify(stringify(clone), fromBase64(signature))) !== true) {
246
+ throw Error('Presentation signature invalid');
247
+ }
247
248
 
248
- verify({ vc: vcObj, ownerDid: vcObj.credentialSubject.id, trustedIssuers, ignoreExpired });
249
- });
249
+ await verify({ vc: vcObj, ownerDid: vcObj.credentialSubject.id, trustedIssuers, ignoreExpired });
250
+ })
251
+ );
250
252
 
251
253
  return true;
252
254
  }
@@ -292,37 +294,40 @@ function createCredentialList({ claims, issuer, issuanceDate }) {
292
294
  });
293
295
  }
294
296
 
295
- function verifyCredentialList({ credentials, trustedIssuers }) {
297
+ // eslint-disable-next-line require-await
298
+ async function verifyCredentialList({ credentials, trustedIssuers }) {
296
299
  if (!credentials || !Array.isArray(credentials)) {
297
300
  throw new Error('Can not verify with empty credentials list');
298
301
  }
299
302
 
300
- return credentials.map((x) => {
301
- // Verify issuer
302
- const issuers = Array.isArray(trustedIssuers) ? trustedIssuers : [trustedIssuers];
303
- const issuerDid = issuers.find((d) => d === x.issuer.id);
304
- if (!issuerDid) {
305
- throw new Error('Credential not issued by trusted issuers');
306
- }
307
- if (!isFromPublicKey(issuerDid, x.issuer.pk)) {
308
- throw new Error('Credential not issuer pk not match with issuer did');
309
- }
310
-
311
- // Construct the issuer wallet
312
- const issuer = fromPublicKey(x.issuer.pk, toTypeInfo(issuerDid));
313
-
314
- // NOTE: we are ignoring other fields of the proof
315
- const clone = cloneDeep(x);
316
- const signature = clone.proof.jws;
317
- delete clone.proof;
318
-
319
- // Verify signature
320
- if (issuer.verify(stringify(clone), fromBase64(signature)) !== true) {
321
- throw Error('Status credential signature not valid');
322
- }
323
-
324
- return x.claim;
325
- });
303
+ return Promise.all(
304
+ credentials.map(async (x) => {
305
+ // Verify issuer
306
+ const issuers = Array.isArray(trustedIssuers) ? trustedIssuers : [trustedIssuers];
307
+ const issuerDid = issuers.find((d) => d === x.issuer.id);
308
+ if (!issuerDid) {
309
+ throw new Error('Credential not issued by trusted issuers');
310
+ }
311
+ if (!isFromPublicKey(issuerDid, x.issuer.pk)) {
312
+ throw new Error('Credential not issuer pk not match with issuer did');
313
+ }
314
+
315
+ // Construct the issuer wallet
316
+ const issuer = fromPublicKey(x.issuer.pk, toTypeInfo(issuerDid));
317
+
318
+ // NOTE: we are ignoring other fields of the proof
319
+ const clone = cloneDeep(x);
320
+ const signature = clone.proof.jws;
321
+ delete clone.proof;
322
+
323
+ // Verify signature
324
+ if ((await issuer.verify(stringify(clone), fromBase64(signature))) !== true) {
325
+ throw Error('Status credential signature not valid');
326
+ }
327
+
328
+ return x.claim;
329
+ })
330
+ );
326
331
  }
327
332
 
328
333
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcblock/vc",
3
- "version": "1.18.166",
3
+ "version": "1.19.1",
4
4
  "description": "Javascript lib to work with ArcBlock Verifiable Credentials",
5
5
  "keywords": [
6
6
  "arcblock",
@@ -47,14 +47,14 @@
47
47
  "url": "https://github.com/ArcBlock/blockchain/issues"
48
48
  },
49
49
  "dependencies": {
50
- "@arcblock/did": "1.18.166",
51
- "@ocap/mcrypto": "1.18.166",
52
- "@ocap/util": "1.18.166",
53
- "@ocap/wallet": "1.18.166",
50
+ "@arcblock/did": "1.19.1",
51
+ "@ocap/mcrypto": "1.19.1",
52
+ "@ocap/util": "1.19.1",
53
+ "@ocap/wallet": "1.19.1",
54
54
  "debug": "^4.3.6",
55
55
  "is-absolute-url": "^3.0.3",
56
56
  "json-stable-stringify": "^1.0.1",
57
57
  "lodash": "^4.17.21"
58
58
  },
59
- "gitHead": "58c8356b3b8c238728560e4c3fef6ed1704d3ac4"
59
+ "gitHead": "21184488172c6c824ebd1714f728ff2aee4a3ac0"
60
60
  }