Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Credential Schema VcDataModel Impl #491

Merged
merged 7 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/seven-horses-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web5/credentials": patch
---

Added credentialSchema to the VcDataModel
13 changes: 12 additions & 1 deletion packages/credentials/src/verifiable-credential.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BearerDid } from '@web5/dids';

Check warning on line 1 in packages/credentials/src/verifiable-credential.ts

View workflow job for this annotation

GitHub Actions / tbdocs-reporter

extractor: typedoc:missing-docs

CredentialSchema.__type.id (Property) does not have any documentation.

Check warning on line 1 in packages/credentials/src/verifiable-credential.ts

View workflow job for this annotation

GitHub Actions / tbdocs-reporter

extractor: typedoc:missing-docs

CredentialSchema.__type.type (Property) does not have any documentation.
import type { ICredential, ICredentialSubject} from '@sphereon/ssi-types';

import { utils as cryptoUtils } from '@web5/crypto';
Expand All @@ -19,6 +19,14 @@
*/
export type VcDataModel = ICredential;

/**
* A credential schema defines the structure and content of the data, enabling verifiers to assess if the data adheres to the established schema.
*/
export type CredentialSchema = {
id: string;
type: string;
};

/**
* Options for creating a verifiable credential.
* @param type Optional. The type of the credential, can be a string or an array of strings.
Expand All @@ -43,6 +51,8 @@
issuanceDate?: string;
/** The expiration date of the credential, as a string. */
expirationDate?: string;
/** The credential schema of the credential */
credentialSchema?: CredentialSchema;
/** The evidence of the credential, as an array of any. */
evidence?: any[];
};
Expand Down Expand Up @@ -147,7 +157,7 @@
* @returns A [VerifiableCredential] instance.
*/
public static async create(options: VerifiableCredentialCreateOptions): Promise<VerifiableCredential> {
const { type, issuer, subject, data, issuanceDate, expirationDate, evidence } = options;
const { type, issuer, subject, data, issuanceDate, expirationDate, credentialSchema, evidence } = options;

const jsonData = JSON.parse(JSON.stringify(data));

Expand Down Expand Up @@ -179,6 +189,7 @@
credentialSubject : credentialSubject,
// Include optional properties only if they have values
...(expirationDate && { expirationDate }),
...(credentialSchema && { credentialSchema }),
...(evidence && { evidence }),
};

Expand Down
42 changes: 30 additions & 12 deletions packages/credentials/tests/verifiable-credential.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DidDht, DidKey, DidIon, DidJwk } from '@web5/dids';
import { Jwt } from '../src/jwt.js';
import { VerifiableCredential } from '../src/verifiable-credential.js';
import CredentialsVerifyTestVector from '../../../web5-spec/test-vectors/credentials/verify.json' assert { type: 'json' };
import { getCurrentXmlSchema112Timestamp, getXmlSchema112Timestamp } from '../src/utils.js';
import { getCurrentXmlSchema112Timestamp } from '../src/utils.js';

describe('Verifiable Credential Tests', async() => {
let issuerDid: BearerDid;
Expand Down Expand Up @@ -92,28 +92,46 @@ describe('Verifiable Credential Tests', async() => {
});

it('create and sign kyc vc with did:jwk', async () => {
const did = await DidJwk.create();
const subjectDid = await DidJwk.create();
const issuerDid = await DidJwk.create();

const vc = await VerifiableCredential.create({
type : 'KnowYourCustomerCred',
subject : did.uri,
issuer : did.uri,
expirationDate : getXmlSchema112Timestamp(2687920690), // 2055-03-05
subject : subjectDid.uri,
issuer : issuerDid.uri,
issuanceDate : '2023-05-19T08:02:04Z',
expirationDate : `2055-05-19T08:02:04Z`,
data : {
country: 'us'
}
id : subjectDid.uri,
country_of_residence : 'US',
tier : 'Tier 1'
},
credentialSchema: {
id : ' https://schema.org/PFI',
type : 'JsonSchema'
},
evidence: [
{ kind: 'document_verification', checks: ['passport', 'utility_bill'] },
{ kind: 'sanctions_check', checks: ['daily'] }
]
});

const vcJwt = await vc.sign({ did });
const vcJwt = await vc.sign({ did: issuerDid });

await VerifiableCredential.verify({ vcJwt });

for( const currentVc of [vc, VerifiableCredential.parseJwt({ vcJwt })]){
expect(currentVc.issuer).to.equal(did.uri);
expect(currentVc.subject).to.equal(did.uri);
expect(currentVc.issuer).to.equal(issuerDid.uri);
expect(currentVc.subject).to.equal(subjectDid.uri);
expect(currentVc.type).to.equal('KnowYourCustomerCred');
expect(currentVc.vcDataModel.issuanceDate).to.not.be.undefined;
expect(currentVc.vcDataModel.credentialSubject).to.deep.equal({ id: did.uri, country: 'us'});
expect(currentVc.vcDataModel.issuanceDate).to.equal('2023-05-19T08:02:04Z');
expect(currentVc.vcDataModel.expirationDate).to.equal('2055-05-19T08:02:04Z');
expect(currentVc.vcDataModel.credentialSubject).to.deep.equal({ id: subjectDid.uri, country_of_residence: 'US', tier: 'Tier 1'});
expect(currentVc.vcDataModel.credentialSchema).to.deep.equal({ id: ' https://schema.org/PFI', type: 'JsonSchema'});
expect(currentVc.vcDataModel.evidence).to.deep.equal([
{ kind: 'document_verification', checks: ['passport', 'utility_bill'] },
{ kind: 'sanctions_check', checks: ['daily'] }
]);
}
});

Expand Down
Loading