Skip to content

Commit

Permalink
fix(verify): vpVerify will always failed when VPType: VP_Digest, VP_S…
Browse files Browse the repository at this point in the history
…electiveDisclosure (#30)

Co-authored-by: github-actions[bot] <action@github.com>
release-as: 0.7.1
  • Loading branch information
zzcwoshizz and actions-user committed Jan 17, 2023
1 parent a81a017 commit 6b5eb85
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 2 deletions.
125 changes: 125 additions & 0 deletions packages/verify/src/vpVerify.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright 2021-2022 zcloak authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { CType } from '@zcloak/ctype/types';

import { initCrypto, mnemonicGenerate, randomAsHex } from '@zcloak/crypto';
import { getPublish } from '@zcloak/ctype/publish';
import { Did, helpers } from '@zcloak/did';
import { MockDidResolver } from '@zcloak/did-resolver';
import { Raw, VerifiableCredentialBuilder, VerifiablePresentationBuilder } from '@zcloak/vc';

import { vpVerify } from './vpVerify';

const resolver = new MockDidResolver();

const CONTENTS1 = {
name: 'zCloak',
age: 19,
birthday: '2022.10.31',
isUser: true
};

describe('VerifiablePresentation', (): void => {
let holder: Did;
let issuer1: Did;
let issuer2: Did;
let issuer3: Did;
let ctype: CType;
let rawCtype: Raw;

beforeAll(async (): Promise<void> => {
await initCrypto();
holder = helpers.createEcdsaFromMnemonic(mnemonicGenerate(12));
issuer1 = helpers.createEcdsaFromMnemonic(mnemonicGenerate(12));
issuer2 = helpers.createEcdsaFromMnemonic(mnemonicGenerate(12));
issuer3 = helpers.createEcdsaFromMnemonic(mnemonicGenerate(12));
resolver.addDocument(holder.getDocument());
resolver.addDocument(issuer1.getDocument());
resolver.addDocument(issuer2.getDocument());
resolver.addDocument(issuer3.getDocument());

ctype = await getPublish(
{
title: 'Test',
description: 'Test',
type: 'object',
properties: {
name: {
type: 'string'
},
age: {
type: 'integer'
},
birthday: {
type: 'string'
},
isUser: {
type: 'boolean'
}
}
},
issuer1
);
rawCtype = new Raw({
contents: CONTENTS1,
owner: holder.id,
ctype,
hashType: 'RescuePrime'
});

rawCtype.calcRootHash();
});

describe('VerifiablePresentation single vc', (): void => {
it('verify vp with VPType: VP', async (): Promise<void> => {
const vc = await VerifiableCredentialBuilder.fromRawCredential(
rawCtype.toRawCredential(),
ctype
)
.setExpirationDate(null)
.build(issuer1);

const vpBuilder = new VerifiablePresentationBuilder(holder);

const challenge = randomAsHex();
const vp = await vpBuilder.addVC(vc, 'VP').build(undefined, challenge);

expect(await vpVerify(vp, resolver)).toBe(true);
});

it('create ctype vp with VPType: VP_Digest', async (): Promise<void> => {
const vc = await VerifiableCredentialBuilder.fromRawCredential(
rawCtype.toRawCredential(),
ctype
)
.setExpirationDate(null)
.build(issuer1);

const vpBuilder = new VerifiablePresentationBuilder(holder);

const challenge = randomAsHex();
const vp = await vpBuilder.addVC(vc, 'VP_Digest').build(undefined, challenge);

expect(await vpVerify(vp, resolver)).toBe(true);
});

it('verify vp with VPType: VP_SelectiveDisclosure', async (): Promise<void> => {
const vc = await VerifiableCredentialBuilder.fromRawCredential(
rawCtype.toRawCredential(),
ctype
)
.setExpirationDate(null)
.build(issuer1);

const vpBuilder = new VerifiablePresentationBuilder(holder);

const challenge = randomAsHex();
const vp = await vpBuilder
.addVC(vc, 'VP_SelectiveDisclosure', ['isUser'])
.build(undefined, challenge);

expect(await vpVerify(vp, resolver)).toBe(true);
});
});
});
4 changes: 2 additions & 2 deletions packages/verify/src/vpVerify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ export async function vpVerify(

// check vc is same did with proof's signer
for (const vc of verifiableCredential) {
if (!isSameUri(vc.issuer, proof.verificationMethod)) {
if (!isSameUri(vc.holder, proof.verificationMethod)) {
return false;
}
}

const proofValid = await proofVerify(id, proof, resolverOrDidDocument);

const results = await Promise.all(
type.map((t, i) => VERIFIERS[t](verifiableCredential[i]), resolverOrDidDocument)
type.map((t, i) => VERIFIERS[t](verifiableCredential[i], resolverOrDidDocument))
);

return idValid && proofValid && !results.includes(false);
Expand Down

0 comments on commit 6b5eb85

Please sign in to comment.