Skip to content

Commit

Permalink
feat(vc tests): use the schema validator package (#2502)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalvarezz committed Feb 23, 2024
1 parent e8578bf commit 159570a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 96 deletions.
18 changes: 8 additions & 10 deletions tee-worker/ts-tests/integration-tests/common/utils/assertion.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { Event } from '@polkadot/types/interfaces';
import { hexToU8a, u8aToHex } from '@polkadot/util';
import Ajv from 'ajv';
import { assert } from 'chai';
import * as ed from '@noble/ed25519';
import { parseIdGraph } from './identity-helper';
import { CorePrimitivesIdentity } from 'parachain-api';
import type { IntegrationTestContext } from '../common-types';
import { getIdGraphHash } from '../di-utils';
import type { HexString } from '@polkadot/util/types';
import { jsonSchema } from './vc-helper';
import { aesKey } from '../call';
import colors from 'colors';
import { WorkerRpcReturnValue, StfError } from 'parachain-api';
import { Bytes } from '@polkadot/types-codec';
import { Signer, decryptWithAes } from './crypto';
import { blake2AsHex } from '@polkadot/util-crypto';
import { validateVcSchema } from '@litentry/vc-schema-validator';
import { PalletIdentityManagementTeeIdentityContext } from 'sidechain-api';
import { KeyObject } from 'crypto';
import * as base58 from 'micro-base58';
Expand Down Expand Up @@ -152,15 +151,15 @@ export async function assertIdGraphMutationResult(
return u8aToHex(decodedResult.id_graph_hash);
}

/*
/*
assert vc
steps:
1. check vc status should be Active
2. compare vc payload hash(blake vc payload) with vc hash
3. check subject
4. compare vc index with vcPayload id
5. check vc signature
6. compare vc wtih jsonSchema
6. check vc schema
TODO: This is incomplete; we still need to further check: https://github.com/litentry/litentry-parachain/issues/1873
*/
Expand Down Expand Up @@ -195,8 +194,7 @@ export async function assertVc(context: IntegrationTestContext, subject: CorePri
// step 4
// extrac proof and vc without proof json
const vcPayloadJson = JSON.parse(decryptVcPayload);
console.log('credential: ', vcPayloadJson);
console.log('assertions: ', vcPayloadJson.credentialSubject.assertions);
console.log('credential: ', JSON.stringify(vcPayloadJson, null, 2));
const { proof, ...vcWithoutProof } = vcPayloadJson;

// step 5
Expand Down Expand Up @@ -236,13 +234,13 @@ export async function assertVc(context: IntegrationTestContext, subject: CorePri

// step 9
// validate VC aganist schema
const ajv = new Ajv();

const validate = ajv.compile(jsonSchema);
const schemaResult = await validateVcSchema(vcPayloadJson);

const isValid = validate(vcPayloadJson);
if (schemaResult.errors) console.log('Schema Validation errors: ', schemaResult.errors);

assert.isTrue(schemaResult.isValid, 'Check Vc payload error: vcPayload should be valid');

assert.isTrue(isValid, 'Check Vc payload error: vcPayload should be valid');
assert.equal(
vcWithoutProof.type[0],
'VerifiableCredential',
Expand Down
82 changes: 0 additions & 82 deletions tee-worker/ts-tests/integration-tests/common/utils/vc-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,85 +166,3 @@ export const unconfiguredAssertions = [
},
},
];
export const jsonSchema = {
type: 'object',
properties: {
id: {
type: 'string',
},
type: {
type: 'array',
},
issuer: {
type: 'object',
properties: {
id: {
type: 'string',
},
name: {
type: 'string',
},
shard: {
type: 'string',
},
},
},
issuanceDate: {
type: 'string',
},
credentialSubject: {
type: 'object',
properties: {
id: {
type: 'string',
},
description: {
type: 'string',
},
type: {
type: 'string',
},
tag: {
type: 'array',
},
assertions: {
type: 'array',
items: {
type: 'object',
},
},
values: {
type: 'array',
items: {
type: 'boolean',
},
},
endpoint: {
type: 'string',
},
},
required: ['id', 'description', 'type', 'assertions', 'values', 'endpoint'],
},
proof: {
type: 'object',
properties: {
created: {
type: 'string',
},
type: {
enum: ['Ed25519Signature2020'],
},
proofPurpose: {
enum: ['assertionMethod'],
},
proofValue: {
type: 'string',
},
verificationMethod: {
type: 'string',
},
},
},
},
required: ['id', 'type', 'credentialSubject', 'issuer', 'issuanceDate', 'proof'],
};
2 changes: 1 addition & 1 deletion tee-worker/ts-tests/integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"test": "mocha --exit --sort -r ts-node/register --loader=ts-node/esm"
},
"dependencies": {
"@litentry/vc-schema-validator": "^0.0.1",
"@noble/ed25519": "^1.7.3",
"@polkadot/api": "^10.9.1",
"@polkadot/api-augment": "^10.9.1",
Expand All @@ -25,7 +26,6 @@
"@polkadot/util": "^12.5.1",
"@polkadot/util-crypto": "^12.5.1",
"add": "^2.0.6",
"ajv": "^8.12.0",
"bitcore-lib": "^10.0.21",
"chai": "^4.3.6",
"colors": "^1.4.0",
Expand Down
42 changes: 39 additions & 3 deletions tee-worker/ts-tests/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 159570a

Please sign in to comment.