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

Adding builder for VerifiableCredential #125

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions package-lock.json

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

11 changes: 11 additions & 0 deletions packages/credentials/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function getCurrentTimestamp() : string {
return new Date().toISOString();
nitro-neal marked this conversation as resolved.
Show resolved Hide resolved
}

export function isRFC3339Timestamp(timestamp: string): boolean {
nitro-neal marked this conversation as resolved.
Show resolved Hide resolved
if (typeof timestamp !== 'string') {
return false;
}

return !isNaN(Date.parse(timestamp));
nitro-neal marked this conversation as resolved.
Show resolved Hide resolved
}
96 changes: 96 additions & 0 deletions packages/credentials/src/verifiable-credential-builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import type { VerifiableCredential, CredentialSubject, Issuer } from './types.js';
import { getCurrentTimestamp, isRFC3339Timestamp } from './utils.js';

export class VerifiableCredentialBuilder {
verifiableCredential: VerifiableCredential;

constructor(credentialSubject: CredentialSubject | CredentialSubject[], issuer: Issuer) {
const contexts = ['https://www.w3.org/2018/credentials/v1'];
const types: string[] = ['VerifiableCredential'];

this.verifiableCredential = {
'@context' : contexts as ['https://www.w3.org/2018/credentials/v1', ...string[]],
credentialSubject : credentialSubject,
issuer : issuer,
type : types,
issuanceDate : getCurrentTimestamp(),
};
}

addContext(context: string[] | string): VerifiableCredentialBuilder {
if (!context || context === '' || context.length === 0) {
throw new Error('context cannot be empty');
}

let contextArray = Array.isArray(context) ? context : [context];
this.verifiableCredential['@context'] = [
...new Set([...this.verifiableCredential['@context'], ...contextArray])
] as ['https://www.w3.org/2018/credentials/v1', ...string[]];

return this;
}

addType(type: string[] | string): VerifiableCredentialBuilder {
if (!type || type === '' || type.length === 0) {
throw new Error('type cannot be empty');
}

let typeArray = Array.isArray(type) ? type : [type];

if (!Array.isArray(this.verifiableCredential.type)) {
this.verifiableCredential.type = [this.verifiableCredential.type] as string[];
}

this.verifiableCredential.type = [...new Set([...this.verifiableCredential.type, ...typeArray])];
return this;
}

setID(id: string): VerifiableCredentialBuilder {
nitro-neal marked this conversation as resolved.
Show resolved Hide resolved
if (!id || id === '') {
throw new Error('id cannot be empty');

}
this.verifiableCredential.id = id;
return this;
}

setIssuer(issuer: Issuer): VerifiableCredentialBuilder {
if (!issuer || Object.keys(issuer).length === 0) {
throw new Error('issuer must be a non-empty object');
}

this.verifiableCredential.issuer = issuer;
return this;
}

setIssuanceDate(issuanceDate: string): VerifiableCredentialBuilder {
if (!issuanceDate || issuanceDate === '') {
throw new Error('issuanceDate cannot be empty');
}

if (isRFC3339Timestamp(issuanceDate) === false) {
throw new Error('issuanceDate is not a valid RFC3339 timestamp');
}

this.verifiableCredential.issuanceDate = issuanceDate;
return this;
}

setExpirationDate(expirationDate: string): VerifiableCredentialBuilder {
if (!expirationDate || expirationDate === '') {
throw new Error('expirationDate cannot be empty');
}

if (isRFC3339Timestamp(expirationDate) === false) {
throw new Error('expirationDate is not a valid RFC3339 timestamp');
}

this.verifiableCredential.expirationDate = expirationDate;
return this;
}


build(): VerifiableCredential {
return this.verifiableCredential;
}
}
130 changes: 130 additions & 0 deletions packages/credentials/tests/verifiable-credential-builder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { expect } from 'chai';

import type { CredentialSubject, Issuer } from '../src/types.js';
import {VerifiableCredentialBuilder} from '../src/verifiable-credential-builder.js';
import { DidKeyApi } from '../../dids/src/did-key.js';


describe('VCBuilder', async () => {
nitro-neal marked this conversation as resolved.
Show resolved Hide resolved
const didKey = new DidKeyApi();
const did = await didKey.create();

const credentialSubject: CredentialSubject = {
id: did.id
};

const issuer: Issuer = {
id: did.id
};

describe('VCBuilder builds VC()', () => {
nitro-neal marked this conversation as resolved.
Show resolved Hide resolved
it('can build a valid vc', async () => {
nitro-neal marked this conversation as resolved.
Show resolved Hide resolved
const vcBuilder = new VerifiableCredentialBuilder(credentialSubject, issuer);
const vc = vcBuilder.build();

expect(vc).to.exist;

expect(vc['@context']).to.include('https://www.w3.org/2018/credentials/v1');
expect(vc.credentialSubject).to.equal(credentialSubject);
expect(vc.issuer).to.equal(issuer);
expect(vc.type).to.include('VerifiableCredential');
expect(vc.issuanceDate).to.exist;

expect(vc.id).to.not.exist;
expect(vc.credentialStatus).to.not.exist;
expect(vc.expirationDate).to.not.exist;


});

it('can set the ID', () => {
const vcBuilder = new VerifiableCredentialBuilder(credentialSubject, issuer);
vcBuilder.setID('customID');
const vc = vcBuilder.build();

expect(vc.id).to.equal('customID');
});

it('throws an error when setting an empty ID', () => {
const vcBuilder = new VerifiableCredentialBuilder(credentialSubject, issuer);

expect(() => vcBuilder.setID('')).to.throw('id cannot be empty');
});

it('can add new context', () => {
const vcBuilder = new VerifiableCredentialBuilder(credentialSubject, issuer);
vcBuilder.addContext(['newContext']);
const vc = vcBuilder.build();

expect(vc['@context']).to.include('https://www.w3.org/2018/credentials/v1');
expect(vc['@context']).to.include('newContext');
expect(vc['@context'].length).to.equal(2);
});

it('can add new type', () => {
const vcBuilder = new VerifiableCredentialBuilder(credentialSubject, issuer);
vcBuilder.addType('newType');
const vc = vcBuilder.build();

expect(vc.type).to.include('newType');
expect(vc.type).to.include('VerifiableCredential');
expect(vc.type!.length).to.equal(2);
});


it('can set the issuer', () => {
const vcBuilder = new VerifiableCredentialBuilder(credentialSubject, issuer);
vcBuilder.setIssuer({ id: 'customIssuer' });
const vc = vcBuilder.build();

expect(vc.issuer).to.deep.equal({ id: 'customIssuer' });
});

it('throws an error when setting an empty issuer', () => {
const vcBuilder = new VerifiableCredentialBuilder(credentialSubject, issuer);

expect(() => vcBuilder.setIssuer({} as Issuer)).to.throw('issuer must be a non-empty object');
});

it('can set the issuance date', () => {
const vcBuilder = new VerifiableCredentialBuilder(credentialSubject, issuer);
vcBuilder.setIssuanceDate('2023-12-31T23:59:59Z');
const vc = vcBuilder.build();

expect(vc.issuanceDate).to.equal('2023-12-31T23:59:59Z');
});

it('throws an error when setting an empty issuance date', () => {
const vcBuilder = new VerifiableCredentialBuilder(credentialSubject, issuer);

expect(() => vcBuilder.setIssuanceDate('')).to.throw('issuanceDate cannot be empty');
});

it('throws an error when setting an invalid issuance date', () => {
const vcBuilder = new VerifiableCredentialBuilder(credentialSubject, issuer);

expect(() => vcBuilder.setIssuanceDate('not-a-date')).to.throw('issuanceDate is not a valid RFC3339 timestamp');
});

it('can set the expiration date', () => {
const vcBuilder = new VerifiableCredentialBuilder(credentialSubject, issuer);
vcBuilder.setExpirationDate('2024-12-31T23:59:59Z');
const vc = vcBuilder.build();

expect(vc.expirationDate).to.equal('2024-12-31T23:59:59Z');
});

it('throws an error when setting an empty expiration date', () => {
const vcBuilder = new VerifiableCredentialBuilder(credentialSubject, issuer);

expect(() => vcBuilder.setExpirationDate('')).to.throw('expirationDate cannot be empty');
});

it('throws an error when setting an invalid expiration date', () => {
const vcBuilder = new VerifiableCredentialBuilder(credentialSubject, issuer);

expect(() => vcBuilder.setExpirationDate('not-a-date')).to.throw('expirationDate is not a valid RFC3339 timestamp');
});

});
});