From ad857d4c96ff9a06232ff10c3a9be82024814140 Mon Sep 17 00:00:00 2001
From: Konstantin Tsabolov <konstantin.tsabolov@spherity.com>
Date: Tue, 5 Mar 2024 17:59:01 +0100
Subject: [PATCH] chore(ssi): correctly initialize endorser did

---
 apps/ssi-abstraction/src/agent/dids/dids.service.ts | 13 ++++++++++---
 apps/ssi-abstraction/src/application.ts             | 10 +++-------
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/apps/ssi-abstraction/src/agent/dids/dids.service.ts b/apps/ssi-abstraction/src/agent/dids/dids.service.ts
index 2aad3dd..ef2d8e8 100644
--- a/apps/ssi-abstraction/src/agent/dids/dids.service.ts
+++ b/apps/ssi-abstraction/src/agent/dids/dids.service.ts
@@ -24,7 +24,7 @@ import {
   KeyType,
   TypedArrayEncoder,
 } from '@credo-ts/core';
-import { Injectable } from '@nestjs/common';
+import { Injectable, Logger } from '@nestjs/common';
 import { ConfigService } from '@nestjs/config';
 
 import { LEDGERS } from '../../config/ledger.js';
@@ -34,6 +34,8 @@ import { WithTenantService } from '../withTenantService.js';
 
 @Injectable()
 export class DidsService {
+  private readonly logger = new Logger(this.constructor.name);
+
   public constructor(
     private agentService: AgentService,
     private withTenantService: WithTenantService,
@@ -138,9 +140,14 @@ export class DidsService {
     };
 
     try {
+      this.logger.log('Registering wallet key for endorser dids');
       await this.agentService.agent.wallet.createKey(privKey);
-    } catch {
-      // Key already exists, but we don't care
+    } catch (e) {
+      if (e instanceof Error && e.constructor.name === 'WalletKeyExistsError') {
+        this.logger.log('Wallet key already exists');
+      } else {
+        throw e;
+      }
     }
 
     for (const indyDid of indyDids) {
diff --git a/apps/ssi-abstraction/src/application.ts b/apps/ssi-abstraction/src/application.ts
index 271d8cb..d56a9ad 100644
--- a/apps/ssi-abstraction/src/application.ts
+++ b/apps/ssi-abstraction/src/application.ts
@@ -93,12 +93,8 @@ export class Application implements OnApplicationBootstrap {
   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
-    }
+    await firstValueFrom(
+      this.natsClient.send(EventDidsRegisterEndorserDid.token, {}),
+    );
   }
 }
-- 
GitLab