diff --git a/packages/credentials/src/verifiable-credential.ts b/packages/credentials/src/verifiable-credential.ts index d96690792..ea338c1bf 100644 --- a/packages/credentials/src/verifiable-credential.ts +++ b/packages/credentials/src/verifiable-credential.ts @@ -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 diff --git a/packages/credentials/src/verifiable-presentation.ts b/packages/credentials/src/verifiable-presentation.ts index 5e5a61af5..a74e48df5 100644 --- a/packages/credentials/src/verifiable-presentation.ts +++ b/packages/credentials/src/verifiable-presentation.ts @@ -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'; @@ -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; }; /** @@ -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, } }); @@ -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) diff --git a/packages/credentials/tests/verifiable-credential.spec.ts b/packages/credentials/tests/verifiable-credential.spec.ts index 00beb160b..629428542 100644 --- a/packages/credentials/tests/verifiable-credential.spec.ts +++ b/packages/credentials/tests/verifiable-credential.spec.ts @@ -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'; diff --git a/packages/credentials/tests/verifiable-presentation.spec.ts b/packages/credentials/tests/verifiable-presentation.spec.ts index 2779a50bd..d9bbaccd9 100644 --- a/packages/credentials/tests/verifiable-presentation.spec.ts +++ b/packages/credentials/tests/verifiable-presentation.spec.ts @@ -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'; @@ -19,10 +19,10 @@ 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)', () => { @@ -30,11 +30,11 @@ describe('Verifiable Credential Tests', () => { 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); @@ -42,7 +42,7 @@ describe('Verifiable Credential Tests', () => { it('create and sign vp with did:key', async () => { const vp = await VerifiablePresentation.create({ - holder : holderDid.did, + holder : holderDid.uri, vcJwts : [validVcJwt] }); @@ -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]); @@ -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 @@ -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]); @@ -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 } }); @@ -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'); + } + }); }); }); \ No newline at end of file