Skip to content

Commit

Permalink
fixing manifest fitlering in issuer server; adding trycatch; fixing m…
Browse files Browse the repository at this point in the history
…s sleep; create new method for sleeping
  • Loading branch information
Bnonni committed Sep 6, 2024
1 parent 70f84e8 commit 4724b41
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 68 deletions.
13 changes: 3 additions & 10 deletions packages/issuer/src/issuer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 });
Expand Down
120 changes: 62 additions & 58 deletions packages/server/src/issuer-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
CredentialApplicationVP,
CredentialManifest,
DcxPath,
FileSystem,
Expand Down Expand Up @@ -72,82 +73,86 @@ export class IssuerServer implements IServer {
* Listens for incoming records from the DWN
*/
public async listen(params: SleepTime = { ms: '2m' }): Promise<void> {
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<void> {
Logger.info(`Server sleeping for ${milliseconds}ms ...`);
await Time.sleep(milliseconds);
}

/**
* Stops the IssuerServer
*/
Expand All @@ -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);
Expand Down

0 comments on commit 4724b41

Please sign in to comment.