Skip to content
Snippets Groups Projects

chore: abstract dtos to encompass a wider range of use cases and fix for out of band proof requests

Closed Boyan Tsolov requested to merge core-dtos into main
All threads resolved!
Compare and Show latest version
8 files
+ 141
126
Compare changes
  • Side-by-side
  • Inline
Files
8
import { Injectable } from "@nestjs/common";
import { AskarService } from "./askar.service";
import {
AcceptInvitationResponseDto,
AcceptProofRequestDto,
ConnectionRecordDto,
ProofReqDto,
ConnectionNotFoundError,
CreateCredentialDefinitionRequsetDto,
CreateCredentialDefinitionResponseDto,
CreddefRecordDto,
CreateInvitationResponseDto,
CreateSchemaRequestDto,
CreateSchemaResponseDto,
SchemaRecordDto,
CredentialNotCreatedError,
IssueCredentialRequestDto,
IssueCredentialResponseDto,
CredentialRecordDto,
IssueProofRequestDto,
IssueProofResponseDto,
GetProofByIdResponseDto,
ProofRecordDto,
MakeBasicMessageRequestDto,
MakeBasicMessageResponseDto,
MessageRecordDto,
SchemaNotCreatedError,
DeclineProofResponseDto,
SchemaNotFoundError,
} from "@ocm-engine/dtos";
import {
AutoAcceptProof,
CredentialState,
JsonEncoder,
ProofState,
} from "@aries-framework/core";
import { CredentialState, ProofState } from "@aries-framework/core";
import { AnonCredsRequestedAttribute } from "@aries-framework/anoncreds";
import { uuid } from "@aries-framework/core/build/utils/uuid";
import { waitForProofExchangeRecordSubject } from "../agent.utils";
@@ -54,9 +48,10 @@ export class AgentService {
throw new ConnectionNotFoundError();
}
const r = new AcceptInvitationResponseDto();
const r = new ConnectionRecordDto();
r.connectionName = connectionRecord.theirLabel;
r.connectionId = connectionRecord.id;
r.state = connectionRecord.state;
r.id = connectionRecord.id;
r.did = connectionRecord.did;
r.invitationDid = connectionRecord.invitationDid;
r.outOfBandId = connectionRecord.outOfBandId;
@@ -65,20 +60,90 @@ export class AgentService {
return r;
};
fetchConnections() {
return this.askar.agent.connections.getAll();
async fetchConnections() {
const agentResponse = await this.askar.agent.connections.getAll();
const connectionArray = agentResponse.map((singleConnectionRes) => {
const connectionResponse = new ConnectionRecordDto();
connectionResponse.id = singleConnectionRes.id;
connectionResponse.state = singleConnectionRes.state;
connectionResponse.connectionName = singleConnectionRes.theirLabel;
connectionResponse.alias = singleConnectionRes.alias;
connectionResponse.did = singleConnectionRes.did;
connectionResponse.invitationDid = singleConnectionRes.invitationDid;
connectionResponse.outOfBandId = singleConnectionRes.outOfBandId;
connectionResponse.createdAt = singleConnectionRes.createdAt;
return connectionResponse;
});
if (connectionArray.length < 1) {
throw new ConnectionNotFoundError();
}
return connectionArray;
}
getConnectionById = (id: string) => {
return this.askar.agent.connections.findById(id);
getConnectionById = async (id: string) => {
const agentResponse = await this.askar.agent.connections.findById(id);
if (!agentResponse) {
throw new ConnectionNotFoundError();
}
const connectionResponse = new ConnectionRecordDto();
connectionResponse.id = agentResponse.id;
connectionResponse.state = agentResponse.state;
connectionResponse.connectionName = agentResponse.theirLabel;
connectionResponse.alias = agentResponse.alias;
connectionResponse.did = agentResponse.did;
connectionResponse.invitationDid = agentResponse.invitationDid;
connectionResponse.outOfBandId = agentResponse.outOfBandId;
connectionResponse.createdAt = agentResponse.createdAt;
return connectionResponse;
};
fetchSchemas = () => {
return this.askar.agent.modules.anoncreds.getCreatedSchemas({});
fetchSchemas = async () => {
const agentResponse =
await this.askar.agent.modules.anoncreds.getCreatedSchemas({});
const schemaResponse = agentResponse.map((singleSchemaRes) => {
const schemaResponse = new SchemaRecordDto();
schemaResponse.id = singleSchemaRes.schemaId;
schemaResponse.name = singleSchemaRes.schema.name;
schemaResponse.attributes = singleSchemaRes.schema.attrNames;
schemaResponse.version = singleSchemaRes.schema.version;
schemaResponse.issuerId = singleSchemaRes.schema.issuerId;
schemaResponse.methodName = singleSchemaRes.methodName;
return schemaResponse;
});
if (schemaResponse.length < 1) {
throw new SchemaNotFoundError();
}
return schemaResponse;
};
getSchemaById = (schemaId: string) => {
return this.askar.agent.modules.anoncreds.getSchema(schemaId);
getSchemaById = async (schemaId: string) => {
const agentResponse = await this.askar.agent.modules.anoncreds.getSchema(
schemaId,
);
if (!agentResponse || !agentResponse.schema) {
throw new SchemaNotFoundError();
}
const schemaResponse = new SchemaRecordDto();
schemaResponse.id = agentResponse.schemaId;
schemaResponse.name = agentResponse.schema.name;
schemaResponse.attributes = agentResponse.schema.attrNames;
schemaResponse.version = agentResponse.schema.version;
schemaResponse.issuerId = agentResponse.schema.issuerId;
return schemaResponse;
};
createSchema = async (schema: CreateSchemaRequestDto) => {
@@ -99,10 +164,10 @@ export class AgentService {
throw new SchemaNotCreatedError();
}
const response = new CreateSchemaResponseDto();
const response = new SchemaRecordDto();
response.name = schemaResult.schemaState.schema.name;
response.schemaId = schemaResult.schemaState.schemaId;
response.id = schemaResult.schemaState.schemaId;
response.issuerId = schemaResult.schemaState.schema.issuerId;
response.version = schemaResult.schemaState.schema.version;
response.attributes = schemaResult.schemaState.schema.attrNames;
@@ -129,12 +194,11 @@ export class AgentService {
throw new CredentialNotCreatedError();
}
const response = new CreateCredentialDefinitionResponseDto();
response.credentialDefinitionId =
credDef.credentialDefinitionState.credentialDefinitionId;
const response = new CreddefRecordDto();
response.id = credDef.credentialDefinitionState.credentialDefinitionId;
response.schemaId =
credDef.credentialDefinitionState.credentialDefinition.schemaId;
response.isserId =
response.issuerId =
credDef.credentialDefinitionState.credentialDefinition.issuerId;
response.tag = credDef.credentialDefinitionState.credentialDefinition.tag;
@@ -160,8 +224,8 @@ export class AgentService {
});
console.log(JSON.stringify(credentialExchangeRecord, null, 2));
const response = new IssueCredentialResponseDto();
response.credentialId = credentialExchangeRecord.id;
const response = new CredentialRecordDto();
response.id = credentialExchangeRecord.id;
response.connectionId = credentialExchangeRecord.connectionId;
response.attributes = credentialExchangeRecord.credentialAttributes;
response.createdAt = credentialExchangeRecord.createdAt;
@@ -188,8 +252,8 @@ export class AgentService {
credentialRecordId,
});
const response = new IssueCredentialResponseDto();
response.credentialId = credentialExchangeRecord.id;
const response = new CredentialRecordDto();
response.id = credentialExchangeRecord.id;
response.connectionId = credentialExchangeRecord.connectionId;
response.attributes = credentialExchangeRecord.credentialAttributes;
response.createdAt = credentialExchangeRecord.createdAt;
@@ -201,11 +265,14 @@ export class AgentService {
const credentials = await this.askar.agent.credentials.findAllByQuery({
state: CredentialState.Done,
});
// REVIEW: get All credentials, or make the filter optional?
// const credentials = await this.askar.agent.credentials.getAll();
const response: Array<IssueCredentialResponseDto> = [];
const response: Array<CredentialRecordDto> = [];
for (const offer of credentials) {
const t = new IssueCredentialResponseDto();
t.credentialId = offer.id;
const t = new CredentialRecordDto();
t.id = offer.id;
t.state = offer.state;
t.connectionId = offer.connectionId;
t.createdAt = offer.createdAt;
t.attributes = offer.credentialAttributes;
@@ -220,10 +287,10 @@ export class AgentService {
state: CredentialState.OfferReceived,
});
const response: Array<IssueCredentialResponseDto> = [];
const response: Array<CredentialRecordDto> = [];
for (const offer of offers) {
const t = new IssueCredentialResponseDto();
t.credentialId = offer.id;
const t = new CredentialRecordDto();
t.id = offer.id;
t.connectionId = offer.connectionId;
t.createdAt = offer.createdAt;
t.attributes = offer.credentialAttributes;
@@ -262,7 +329,6 @@ export class AgentService {
requested_attributes: requestedAttributes,
},
},
autoAcceptProof: AutoAcceptProof.ContentApproved,
});
console.log({ proofRecord });
@@ -273,7 +339,9 @@ export class AgentService {
domain: this.askar.agentConfig.agentPeerAddress,
});
return { proofUrl: invitationUrl };
return {
proofUrl: invitationUrl,
};
}
console.log(`${issueProofDto.connectionId} detected, issuing proof`);
@@ -290,8 +358,8 @@ export class AgentService {
},
});
const response = new IssueProofResponseDto();
response.proofId = exchangeRecord.id;
const response = new ProofRecordDto();
response.id = exchangeRecord.id;
response.connectionId = exchangeRecord.connectionId;
response.state = exchangeRecord.state;
response.updatedAt = exchangeRecord.updatedAt;
@@ -305,10 +373,10 @@ export class AgentService {
state: ProofState.RequestReceived,
});
const response: Array<IssueProofResponseDto> = [];
const response: Array<ProofRecordDto> = [];
for (const proof of proofs) {
const t = new IssueProofResponseDto();
t.proofId = proof.id;
const t = new ProofRecordDto();
t.id = proof.id;
t.connectionId = proof.connectionId;
t.state = proof.state;
t.updatedAt = proof.updatedAt;
@@ -327,9 +395,9 @@ export class AgentService {
return proofRecord;
}
const proofResponse = new GetProofByIdResponseDto();
const proofResponse = new ProofRecordDto();
proofResponse.proofId = proofRecord.id;
proofResponse.id = proofRecord.id;
proofResponse.connectionId = proofRecord.connectionId;
proofResponse.state = proofRecord.state;
proofResponse.updatedAt = proofRecord.updatedAt;
@@ -338,7 +406,7 @@ export class AgentService {
return proofResponse;
};
acceptProof = async (acceptProofDto: AcceptProofRequestDto) => {
acceptProof = async (acceptProofDto: ProofReqDto) => {
if (acceptProofDto.proofUrl) {
return this.acceptOobProof(acceptProofDto.proofUrl);
}
@@ -346,11 +414,11 @@ export class AgentService {
};
acceptOobProof = async (url: string) => {
const param = url.split("d_m=")[1];
const t = JsonEncoder.fromBase64(param);
await this.askar.agent.receiveMessage(t);
await this.askar.agent.oob.receiveInvitationFromUrl(url, {
autoAcceptConnection: false,
autoAcceptInvitation: true,
// reuseConnection: true,
});
const record = await waitForProofExchangeRecordSubject(this.askar.agentR, {
state: ProofState.RequestReceived,
@@ -366,14 +434,14 @@ export class AgentService {
proofFormats: requestedCredentials.proofFormats,
});
const response = new IssueProofResponseDto();
const response = new ProofRecordDto();
response.proofId = acceptedRecord.id;
response.id = acceptedRecord.id;
response.state = acceptedRecord.state;
response.updatedAt = acceptedRecord.updatedAt;
response.createdAt = acceptedRecord.createdAt;
return acceptedRecord;
return response;
};
acceptConnectionProof = async (proofRecordId: string) => {
@@ -392,8 +460,8 @@ export class AgentService {
console.log(JSON.stringify(proof, null, 2));
const response = new IssueProofResponseDto();
response.proofId = proof.id;
const response = new ProofRecordDto();
response.id = proof.id;
response.connectionId = proof.connectionId;
response.state = proof.state;
response.updatedAt = proof.updatedAt;
@@ -408,8 +476,8 @@ export class AgentService {
// sendProblemReport: false, // REVIEW: do we have a use case for this key?
});
const declineResponse = new DeclineProofResponseDto();
declineResponse.proofId = resultFromDecline.id;
const declineResponse = new ProofRecordDto();
declineResponse.id = resultFromDecline.id;
declineResponse.connectionId = resultFromDecline.connectionId;
declineResponse.state = resultFromDecline.state;
declineResponse.updatedAt = resultFromDecline.updatedAt;
@@ -423,7 +491,7 @@ export class AgentService {
};
sendMessage = async (message: MakeBasicMessageRequestDto) => {
const response = new MakeBasicMessageResponseDto();
const response = new MessageRecordDto();
const m = await this.askar.agent.basicMessages.sendMessage(
message.connectionId,
@@ -433,7 +501,7 @@ export class AgentService {
const connectionInfo = await this.getConnectionById(message.connectionId);
response.connectionId = m.connectionId;
response.to = connectionInfo?.theirLabel || "";
response.to = connectionInfo?.connectionName || "";
response.id = m.id;
response.message = m.content;
Loading