Skip to content

Commit

Permalink
merge updates and string check
Browse files Browse the repository at this point in the history
  • Loading branch information
nitro-neal committed Feb 14, 2024
1 parent 720eff6 commit 2eef0b4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
4 changes: 4 additions & 0 deletions packages/credentials/src/verifiable-credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ export class VerifiableCredential {
throw new Error('Issuer and subject must be defined');
}

if(typeof issuer !== 'string' || typeof subject !== 'string') {
throw new Error('Issuer and subject must be of type string');
}

const credentialSubject: CredentialSubject = {
id: subject,
...jsonData
Expand Down
12 changes: 8 additions & 4 deletions packages/credentials/src/verifiable-presentation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PortableDid } from '@web5/dids';
import type { BearerDid } from '@web5/dids';
import type { IPresentation} from '@sphereon/ssi-types';

import { utils as cryptoUtils } from '@web5/crypto';
Expand Down Expand Up @@ -35,7 +35,7 @@ export type VerifiablePresentationCreateOptions = {
* @param did - The holder DID of the presentation, represented as a PortableDid.
*/
export type VerifiablePresentationSignOptions = {
did: PortableDid;
did: BearerDid;
};

/**
Expand Down Expand Up @@ -80,8 +80,8 @@ export class VerifiablePresentation {
signerDid : options.did,
payload : {
vp : this.vpDataModel,
iss : options.did.did,
sub : options.did.did,
iss : options.did.uri,
sub : options.did.uri,
}
});

Expand Down Expand Up @@ -128,6 +128,10 @@ export class VerifiablePresentation {
throw new Error('Holder must be defined');
}

if(typeof holder !== 'string') {
throw new Error('Holder must be of type string');
}

const vpDataModel: VpDataModel = {
'@context' : [DEFAULT_CONTEXT],
type : Array.isArray(type)
Expand Down
18 changes: 18 additions & 0 deletions packages/credentials/tests/verifiable-credential.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ describe('Verifiable Credential Tests', async() => {
}
});

it('should throw an error if issuer is not string', async () => {
const subjectDid = issuerDid.uri;

const anyTypeIssuer: any = DidKey.create();

try {
await VerifiableCredential.create({
type : 'StreetCred',
issuer : anyTypeIssuer,
subject : subjectDid,
data : new StreetCredibility('high', true),
});
expect.fail();
} catch(e: any) {
expect(e.message).to.include('Issuer and subject must be of type string');
}
});

it('should throw an error if data is not parseable into a JSON object', async () => {
const issuerDid = 'did:example:issuer';
const subjectDid = 'did:example:subject';
Expand Down
41 changes: 28 additions & 13 deletions packages/credentials/tests/verifiable-presentation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PortableDid } from '@web5/dids';
import type { BearerDid } from '@web5/dids';

import { expect } from 'chai';
import { DidKeyMethod } from '@web5/dids';
import { DidKey } from '@web5/dids';

import { Jwt } from '../src/jwt.js';
import { VerifiablePresentation } from '../src/verifiable-presentation.js';
Expand All @@ -19,30 +19,30 @@ const validVcJwt = 'eyJraWQiOiJkaWQ6a2V5OnpRM3NoZ0NqVmZucldxOUw3cjFRc3oxcmlRUldv
'BVZaZ9-RqpiAM-fHKrdGUzVyXr77pOl7yGgwIO90g';

describe('Verifiable Credential Tests', () => {
let holderDid: PortableDid;
let holderDid: BearerDid;

beforeEach(async () => {
holderDid = await DidKeyMethod.create();
holderDid = await DidKey.create();
});

describe('Verifiable Presentation (VP)', () => {
it('create simple vp', async () => {
const vcJwts = ['vcjwt1'];

const vp = await VerifiablePresentation.create({
holder : holderDid.did,
holder : holderDid.uri,
vcJwts : vcJwts
});

expect(vp.holder).to.equal(holderDid.did);
expect(vp.holder).to.equal(holderDid.uri);
expect(vp.type).to.equal('VerifiablePresentation');
expect(vp.vpDataModel.verifiableCredential).to.not.be.undefined;
expect(vp.vpDataModel.verifiableCredential).to.deep.equal(vcJwts);
});

it('create and sign vp with did:key', async () => {
const vp = await VerifiablePresentation.create({
holder : holderDid.did,
holder : holderDid.uri,
vcJwts : [validVcJwt]
});

Expand All @@ -53,7 +53,7 @@ describe('Verifiable Credential Tests', () => {
const parsedVp = await VerifiablePresentation.parseJwt({ vpJwt });

expect(vpJwt).to.not.be.undefined;
expect(parsedVp.holder).to.equal(holderDid.did);
expect(parsedVp.holder).to.equal(holderDid.uri);
expect(parsedVp.type).to.equal('VerifiablePresentation');
expect(parsedVp.vpDataModel.verifiableCredential).to.not.be.undefined;
expect(parsedVp.vpDataModel.verifiableCredential).to.deep.equal([validVcJwt]);
Expand All @@ -73,7 +73,7 @@ describe('Verifiable Credential Tests', () => {
};

const vp = await VerifiablePresentation.create({
holder : holderDid.did,
holder : holderDid.uri,
vcJwts : [validVcJwt],
additionalData : {
presentation_submission: presentationSubmission
Expand All @@ -88,7 +88,7 @@ describe('Verifiable Credential Tests', () => {
const parsedVp = await VerifiablePresentation.parseJwt({ vpJwt });

expect(vpJwt).to.not.be.undefined;
expect(parsedVp.holder).to.equal(holderDid.did);
expect(parsedVp.holder).to.equal(holderDid.uri);
expect(parsedVp.type).to.equal('PresentationSubmission');
expect(parsedVp.vpDataModel.verifiableCredential).to.not.be.undefined;
expect(parsedVp.vpDataModel.verifiableCredential).to.deep.equal([validVcJwt]);
Expand All @@ -101,12 +101,12 @@ describe('Verifiable Credential Tests', () => {
});

it('parseJwt checks if missing vp property', async () => {
const did = await DidKeyMethod.create();
const did = await DidKey.create();
const jwt = await Jwt.sign({
signerDid : did,
payload : {
iss : did.did,
sub : did.did
iss : did.uri,
sub : did.uri
}
});

Expand All @@ -127,5 +127,20 @@ describe('Verifiable Credential Tests', () => {
expect(e.message).to.include('Holder must be defined');
}
});

it('should throw an error if holder is not a string', async () => {
const anyTypeHolder: any = DidKey.create();

try {
await VerifiablePresentation.create({
holder : anyTypeHolder,
vcJwts : [validVcJwt]
});

expect.fail();
} catch(e: any) {
expect(e.message).to.include('Holder must be of type string');
}
});
});
});

0 comments on commit 2eef0b4

Please sign in to comment.