From 5cf50d0a92705105a33c400c62486a9b26b0636d Mon Sep 17 00:00:00 2001
From: Konstantin Tsabolov <konstantin.tsabolov@spherity.com>
Date: Tue, 5 Mar 2024 16:30:22 +0100
Subject: [PATCH] chore(ssi): register endorser DID upon startup

---
 .../src/agent/dids/dids.service.ts            |  7 ++-
 apps/ssi-abstraction/src/application.ts       | 43 +++++++++++++++++--
 apps/ssi-abstraction/src/common/constants.ts  |  2 +
 3 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/apps/ssi-abstraction/src/agent/dids/dids.service.ts b/apps/ssi-abstraction/src/agent/dids/dids.service.ts
index 873054e..2aad3dd 100644
--- a/apps/ssi-abstraction/src/agent/dids/dids.service.ts
+++ b/apps/ssi-abstraction/src/agent/dids/dids.service.ts
@@ -137,12 +137,17 @@ export class DidsService {
       keyType: KeyType.Ed25519,
     };
 
-    await this.agentService.agent.wallet.createKey(privKey);
+    try {
+      await this.agentService.agent.wallet.createKey(privKey);
+    } catch {
+      // Key already exists, but we don't care
+    }
 
     for (const indyDid of indyDids) {
       await this.agentService.agent.dids.import({
         did: indyDid.did,
         privateKeys: [privKey],
+        overwrite: true,
       });
     }
 
diff --git a/apps/ssi-abstraction/src/application.ts b/apps/ssi-abstraction/src/application.ts
index b3b95ea..271d8cb 100644
--- a/apps/ssi-abstraction/src/application.ts
+++ b/apps/ssi-abstraction/src/application.ts
@@ -1,9 +1,12 @@
+import type { OnApplicationBootstrap } from '@nestjs/common';
 import type { ConfigType } from '@nestjs/config';
 
-import { Module } from '@nestjs/common';
+import { Inject, Module } from '@nestjs/common';
 import { ConfigModule } from '@nestjs/config';
 import { RouterModule } from '@nestjs/core';
-import { HealthModule } from '@ocm/shared';
+import { ClientProxy, ClientsModule, Transport } from '@nestjs/microservices';
+import { EventDidsRegisterEndorserDid, HealthModule } from '@ocm/shared';
+import { firstValueFrom } from 'rxjs';
 
 import { AgentModule } from './agent/agent.module.js';
 import { AnonCredsCredentialsModule } from './agent/anoncredsCredentials/anoncredsCredentials.module.js';
@@ -14,6 +17,7 @@ import { DidsModule } from './agent/dids/dids.module.js';
 import { RevocationModule } from './agent/revocation/revocation.module.js';
 import { SchemasModule } from './agent/schemas/schemas.module.js';
 import { TenantsModule } from './agent/tenants/tenants.module.js';
+import { NATS_CLIENT } from './common/constants.js';
 import { agentConfig } from './config/agent.config.js';
 import { httpConfig } from './config/http.config.js';
 import { natsConfig } from './config/nats.config.js';
@@ -35,6 +39,23 @@ import { validationSchema } from './config/validation.js';
       },
     }),
 
+    ClientsModule.registerAsync({
+      clients: [
+        {
+          name: NATS_CLIENT,
+          inject: [natsConfig.KEY],
+          useFactory: (config: ConfigType<typeof natsConfig>) => ({
+            transport: Transport.NATS,
+            options: {
+              servers: [config.url],
+              user: config.user as string,
+              pass: config.password as string,
+            },
+          }),
+        },
+      ],
+    }),
+
     HealthModule.registerAsync({
       inject: [natsConfig.KEY],
       useFactory: (config: ConfigType<typeof natsConfig>) => {
@@ -64,4 +85,20 @@ import { validationSchema } from './config/validation.js';
     RouterModule.register([{ module: HealthModule, path: '/health' }]),
   ],
 })
-export class Application {}
+export class Application implements OnApplicationBootstrap {
+  public constructor(
+    @Inject(NATS_CLIENT) private readonly natsClient: ClientProxy,
+  ) {}
+
+  public async onApplicationBootstrap() {
+    await this.natsClient.connect();
+
+    try {
+      await firstValueFrom(
+        this.natsClient.send(EventDidsRegisterEndorserDid.token, {}),
+      );
+    } catch {
+      // If there was an error, we don't want to crash the application
+    }
+  }
+}
diff --git a/apps/ssi-abstraction/src/common/constants.ts b/apps/ssi-abstraction/src/common/constants.ts
index 1e67c0f..4b69d62 100644
--- a/apps/ssi-abstraction/src/common/constants.ts
+++ b/apps/ssi-abstraction/src/common/constants.ts
@@ -9,3 +9,5 @@ export enum MetadataTokens {
 export enum GenericRecordTokens {
   REVOCATION = 'revocation_generic_record',
 }
+
+export const NATS_CLIENT = Symbol('NATS_CLIENT');
-- 
GitLab