diff --git a/packages/issuer/src/issuer.ts b/packages/issuer/src/issuer.ts index 7648b1e..fc9d316 100644 --- a/packages/issuer/src/issuer.ts +++ b/packages/issuer/src/issuer.ts @@ -526,11 +526,9 @@ export class DcxIssuer implements DcxManager { const applicationVP: CredentialApplicationVP = await record.data.json(); Logger.info('Processing vp credential application ...', stringifier(applicationVP)); + const { verifiableCredential, credential_application: application } = applicationVP; // Select valid credentials against the manifest - const vcJwts = this.selectCredentials({ - manifest, - verifiableCredential : applicationVP.verifiableCredential, - }); + const vcJwts = this.selectCredentials({ manifest, verifiableCredential }); Logger.info(`Selected ${vcJwts.length} credentials`, stringifier(vcJwts)); const applicant = record.author; @@ -541,15 +539,10 @@ export class DcxIssuer implements DcxManager { const data = await this.requestCredentialData({ body: { vcs: verified }, id: providerId}); Logger.info('Requested data from provider', stringifier(data)); - const application = applicationVP.credential_application; const { signedVc } = await this.createCredential({ data, manifest, application }); Logger.info('Created credential', signedVc); - const response = this.createCredentialResponseVP({ - manifest, - application, - verifiableCredential : [signedVc], - }); + const response = this.createCredentialResponseVP({ manifest, application, verifiableCredential: [signedVc] }); Logger.info('Created credential response', stringifier(response)); const issuance = await this.createResponseRecord({ response, applicant }); diff --git a/packages/server/src/issuer-server.ts b/packages/server/src/issuer-server.ts index 91ea82a..a5cc943 100644 --- a/packages/server/src/issuer-server.ts +++ b/packages/server/src/issuer-server.ts @@ -1,4 +1,5 @@ import { + CredentialApplicationVP, CredentialManifest, DcxPath, FileSystem, @@ -72,82 +73,86 @@ export class IssuerServer implements IServer { * Listens for incoming records from the DWN */ public async listen(params: SleepTime = { ms: '2m' }): Promise { - await this.sync(); - this.server.listening = true; - Logger.log('IssuerServer listening ...'); - const milliseconds = ms(params.ms); - const CURSOR = this.issuer.config.cursorFile; - let cursor = await FileSystem.readToJson(CURSOR); const pagination = Objects.isEmpty(cursor) ? {} : { cursor }; + Logger.log('IssuerServer listening ...'); while (this.server.listening) { - const { records = [] } = await this.issuer.web5.dwn.records.query({ - from : this.issuer.agent.agentDid.uri, - message : { - pagination, - filter : { protocol: issuer.protocol }, - }, - }); - - const recordIds = records.map((record: Record) => record.id); - - const recordReads: Record[] = await Promise.all( - recordIds.map(async (recordId: string) => { - const { record }: { record: Record } = await this.issuer.web5.dwn.records.read({ - from : this.issuer.agent.agentDid.uri, - message : { - filter : { - recordId, + try { + + await this.sync(); + + const { records = [] } = await this.issuer.web5.dwn.records.query({ + from : this.issuer.agent.agentDid.uri, + message : { + pagination, + filter : { protocol: issuer.protocol }, + }, + }); + + const recordIds = records.map((record: Record) => record.id); + + const reads: Record[] = await Promise.all( + recordIds.map(async (recordId: string) => { + const { record }: { record: Record } = await this.issuer.web5.dwn.records.read({ + from : this.issuer.agent.agentDid.uri, + message : { + filter : { + recordId, + }, }, - }, - }); - return record; - }), - ); - - if (this.server.testing) { - Logger.log('Test Complete! Stopping DCX server ...'); - this.stop(); - } + }); + return record; + }), + ); + + if (this.server.testing) { + Logger.log('Test Complete! Stopping DCX server ...'); + this.stop(); + } - if (!recordReads.length) { - Logger.log('No records found!', recordReads.length); - await Time.sleep(milliseconds); - } + if (!reads.length) { + Logger.log('No records read!', reads.length); + this.sleepServer(milliseconds); + } - for (const record of recordReads) { - if (record.protocolPath === 'application') { - const data = await record.data.json(); - Logger.debug(`Found application with id: ${record.id}`, data); + for (const read of reads) { + if (read.protocolPath === 'application') { + const data: CredentialApplicationVP = await read.data.json(); + Logger.debug(`Found application with id: ${read.id}`, data); - const manifest = this.issuer.config.manifests.find( - (manifest: CredentialManifest) => - manifest.id.toLowerCase() === data.vpDataModel.credential_application.manifest_id, - ); + const manifest = this.issuer.config.manifests.find( + (manifest: CredentialManifest) => manifest.id.toLowerCase() === data.credential_application.manifest_id.toLowerCase(), + ); - if (!manifest) { - Logger.error(`Manifest not found for application with id: ${record.id}`); - continue; - } + if (!manifest) { + Logger.error(`Manifest not found for application with id: ${read.id}`); + continue; + } - const { status } = await this.issuer.processApplicationRecord( - { - record, + const { status } = await this.issuer.processApplicationRecord({ manifest, + record : read, providerId : manifest.issuer.id, - } - ); - Logger.debug(`Processed application with id: ${record.id}`, status); + }); + Logger.debug(`Processed application with id: ${read.id}`, status); + } } + this.sleepServer(milliseconds); + } catch (error) { + Logger.error(error); } - await Time.sleep(20000); } } + private async sleepServer(milliseconds: number): Promise { + Logger.info(`Server sleeping for ${milliseconds}ms ...`); + await Time.sleep(milliseconds); + } + /** * Stops the IssuerServer */ @@ -165,11 +170,10 @@ export class IssuerServer implements IServer { if (!this.issuer.status.initialized) { await this.issuer.initialize(); } - if(!this.issuer.status.setup) { await this.issuer.setup(); } - + await this.sync(); await this.listen(); } catch (error: any) { Logger.error(error);