Skip to content
Snippets Groups Projects
Unverified Commit e4a56fba authored by Zdravko Iliev's avatar Zdravko Iliev
Browse files

wip: handling connection and proof requests events. Keep track of connections.


Signed-off-by: default avatarZdravko Iliev <zdravko.iliev@vereign.com>
parent 59e4340d
No related branches found
No related tags found
1 merge request!38feat: rest agent support for connection and proof change state events
Pipeline #65369 failed with stage
in 1 minute and 9 seconds
......@@ -27,6 +27,7 @@ export class AgentEventListenerServce implements OnModuleInit {
constructor(
private readonly gatewayClient: GatewayClient,
//TODO: can I replace with agent service ??
private readonly askar: AskarService,
private readonly configService: ConfigService,
) {}
......@@ -36,53 +37,57 @@ export class AgentEventListenerServce implements OnModuleInit {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.agentConfig = this.configService.get<IConfAgent>("agent")!;
console.log(this.agentConfig.agentIsSVDX);
console.log(this.agentConfig.agentIsRest);
if (this.agentConfig.agentIsSVDX && this.agentConfig.agentIsRest) {
//listen to when someone accepts a connection only when HIN webhook is set up and agent is in rest mode
//Extract to separate methods
this.askar.agent.events.on(
ConnectionEventTypes.ConnectionStateChanged,
async (ev: ConnectionStateChangedEvent) => {
console.log("role", ev.payload.connectionRecord.role);
console.log("state", ev.payload.connectionRecord.state);
if (
ev.payload.connectionRecord.role === DidExchangeRole.Responder &&
ev.payload.connectionRecord.state !== "completed"
) {
console.log("izlizam");
return;
}
console.log("connection accepted", JSON.stringify(ev, null, 2));
//I should have early return and only send proof request once the connection is accepted
await this.askar.agent.connections.addConnectionType(
ev.payload.connectionRecord.id,
ev.payload.connectionRecord.theirLabel || "svdx",
);
const connectionDid = ev.payload.connectionRecord.theirDid; //?
console.log("connection accepted", JSON.stringify(ev, null, 2));
const connections = await this.askar.agent.connections.findAllByQuery(
{
did: connectionDid,
},
);
const connections =
await this.askar.agent.connections.findAllByConnectionTypes([
ev.payload.connectionRecord.theirLabel || "svdx",
]);
console.log(JSON.stringify(connections, null, 2));
//delete everything without the current connection
if (connections.length < 2) {
return;
}
//I should delete every connection besides the one that issued the proof ?
connections.sort(
(a, b) =>
new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime(),
);
while (connections.length > 1) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const con = connections.pop()!;
//can this happen with oob proof request
console.log(`deleting ${con.id}`);
await this.askar.agent.connections.deleteById(con.id);
}
// when the request is accepted get the theirDid and search in connections ??
try {
//TODO: replace with method from agent service
await this.askar.agent.proofs.requestProof({
protocolVersion: "v2",
connectionId: ev.payload.connectionRecord.id,
connectionId: connections[0].id,
proofFormats: {
//TODO: replace with formatting method
anoncreds: {
name: "proof-request",
version: "1.0",
......@@ -91,10 +96,8 @@ export class AgentEventListenerServce implements OnModuleInit {
name: "email",
restrictions: [
{
schema_id:
"did:indy:bcovrin:test:JpJEjf2LfretHgfQjMZnsi/anoncreds/v0/SCHEMA/hin-mail-demo/0.0.1",
cred_def_id:
"did:indy:bcovrin:test:uyuegPSRmfqy6RUdgwJ5y/anoncreds/v0/CLAIM_DEF/50002/hin-demo-cred-def",
schema_id: this.agentConfig.agentSVDXSchemaId,
cred_def_id: this.agentConfig.agentSVDXCredDefId,
},
],
},
......@@ -113,14 +116,12 @@ export class AgentEventListenerServce implements OnModuleInit {
ProofEventTypes.ProofStateChanged,
async (ev: ProofStateChangedEvent) => {
console.log(
"proof event accepted",
"proof event received",
JSON.stringify(ev.payload.proofRecord, null, 2),
);
//is this the correct state ??
if (
ProofState.Done !== ev.payload.proofRecord.state
) {
if (ProofState.Done !== ev.payload.proofRecord.state) {
return;
}
......@@ -128,8 +129,6 @@ export class AgentEventListenerServce implements OnModuleInit {
ev.payload.proofRecord.id,
);
console.log(JSON.stringify(result));
if (!result) return;
const t = await result.formats[0].attachmentId;
......@@ -138,6 +137,8 @@ export class AgentEventListenerServce implements OnModuleInit {
if (!d) return;
//TODO: send to svdx
console.log(ev.payload.proofRecord.connectionId);
console.log(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
......
......@@ -19,5 +19,8 @@ export const agentConfig = registerAs(
agentConsumerRateLimit: parseInt(process.env["AGENT_RETE_LIMIT"]!),
agentPort: parseInt(process.env["AGENT_PORT"]!),
agentIsSVDX: process.env["AGENT_IS_SVDX"] === "true",
agentSVDXSchemaId: process.env["AGENT_SVDX_SCHEMA_ID"]!,
agentSVDXCredDefId: process.env["AGENT_SVDX_CRED_DEF_ID"]!,
agentSVDXWebHook: process.env["AGENT_SVDX_WEBHOOK_URL"]!,
}),
);
......@@ -13,4 +13,7 @@ export interface IConfAgent {
agentConsumerRateLimit: number;
agentPort: number;
agentIsSVDX: boolean;
agentSVDXSchemaId: string;
agentSVDXCredDefId: string;
agentSVDXWebHook: string;
}
......@@ -14,4 +14,7 @@ export const agentSchema = Joi.object({
AGENT_RETE_LIMIT: Joi.string().required(),
AGENT_PORT: Joi.string().required(),
AGENT_IS_SVDX: Joi.string().optional(),
AGENT_SVDX_SCHEMA_ID: Joi.string().optional(),
AGENT_SVDX_CRED_DEF_ID: Joi.string().optional(),
AGENT_SVDX_WEBHOOK_URL: Joi.string().optional(),
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment