• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hyperledger / identus-cloud-agent / 11056380298

26 Sep 2024 04:28PM UTC coverage: 48.66% (+0.05%) from 48.609%
11056380298

Pull #1375

yshyn-iohk
fix: migration to a new eudi.openid4vci library

Signed-off-by: Yurii Shynbuiev <yurii.shynbuiev@iohk.io>
Pull Request #1375: fix: OAS and http clients for Kotliln and TypeScript

34 of 81 new or added lines in 25 files covered. (41.98%)

182 existing lines in 53 files now uncovered.

7860 of 16153 relevant lines covered (48.66%)

0.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

33.9
/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/agent/server/CloudAgentApp.scala
1
package org.hyperledger.identus.agent.server
2

3
import org.hyperledger.identus.agent.notification.WebhookPublisher
4
import org.hyperledger.identus.agent.server.config.AppConfig
5
import org.hyperledger.identus.agent.server.http.{ZHttp4sBlazeServer, ZHttpEndpoints}
6
import org.hyperledger.identus.agent.server.jobs.*
7
import org.hyperledger.identus.agent.walletapi.model.{Entity, Wallet, WalletSeed}
8
import org.hyperledger.identus.agent.walletapi.service.{EntityService, ManagedDIDService, WalletManagementService}
9
import org.hyperledger.identus.agent.walletapi.storage.DIDNonSecretStorage
10
import org.hyperledger.identus.castor.controller.{DIDRegistrarServerEndpoints, DIDServerEndpoints}
11
import org.hyperledger.identus.castor.core.service.DIDService
12
import org.hyperledger.identus.connect.controller.ConnectionServerEndpoints
13
import org.hyperledger.identus.connect.core.service.ConnectionService
14
import org.hyperledger.identus.credentialstatus.controller.CredentialStatusServiceEndpoints
15
import org.hyperledger.identus.event.controller.EventServerEndpoints
16
import org.hyperledger.identus.event.notification.EventNotificationConfig
17
import org.hyperledger.identus.iam.authentication.apikey.ApiKeyAuthenticator
18
import org.hyperledger.identus.iam.entity.http.EntityServerEndpoints
19
import org.hyperledger.identus.iam.wallet.http.WalletManagementServerEndpoints
20
import org.hyperledger.identus.issue.controller.IssueServerEndpoints
21
import org.hyperledger.identus.mercury.{DidOps, HttpClient}
22
import org.hyperledger.identus.oid4vci.CredentialIssuerServerEndpoints
23
import org.hyperledger.identus.pollux.core.service.{CredentialService, PresentationService}
24
import org.hyperledger.identus.pollux.credentialdefinition.CredentialDefinitionRegistryServerEndpoints
25
import org.hyperledger.identus.pollux.credentialschema.{
26
  SchemaRegistryServerEndpoints,
27
  VerificationPolicyServerEndpoints
28
}
29
import org.hyperledger.identus.pollux.prex.PresentationExchangeServerEndpoints
30
import org.hyperledger.identus.pollux.vc.jwt.DidResolver as JwtDidResolver
31
import org.hyperledger.identus.presentproof.controller.PresentProofServerEndpoints
32
import org.hyperledger.identus.resolvers.DIDResolver
33
import org.hyperledger.identus.shared.http.UriResolver
34
import org.hyperledger.identus.shared.models.{HexString, WalletAccessContext, WalletAdministrationContext, WalletId}
35
import org.hyperledger.identus.shared.utils.DurationOps.toMetricsSeconds
36
import org.hyperledger.identus.system.controller.SystemServerEndpoints
37
import org.hyperledger.identus.verification.controller.VcVerificationServerEndpoints
38
import zio.*
39
import zio.metrics.*
40

41
object CloudAgentApp {
42

43
  def run = for {
×
44
    _ <- AgentInitialization.run
×
45
    _ <- issueCredentialDidCommExchangesJob.debug.fork
×
46
    _ <- presentProofExchangeJob.debug.fork
×
47
    _ <- connectDidCommExchangesJob.debug.fork
×
48
    _ <- syncDIDPublicationStateFromDltJob.debug.fork
×
49
    _ <- syncRevocationStatusListsJob.debug.fork
×
50
    _ <- AgentHttpServer.run.tapDefect(e => ZIO.logErrorCause("Agent HTTP Server failure", e)).fork
×
51
    fiber <- DidCommHttpServer.run.tapDefect(e => ZIO.logErrorCause("DIDComm HTTP Server failure", e)).fork
×
52
    _ <- WebhookPublisher.layer.build.map(_.get[WebhookPublisher]).flatMap(_.run.fork)
×
53
    _ <- fiber.join *> ZIO.log(s"Server End")
×
54
    _ <- ZIO.never
×
55
  } yield ()
×
56

57
  private val issueCredentialDidCommExchangesJob: RIO[
58
    AppConfig & DidOps & DIDResolver & JwtDidResolver & HttpClient & CredentialService & DIDNonSecretStorage &
59
      DIDService & ManagedDIDService & PresentationService & WalletManagementService,
60
    Unit
61
  ] =
62
    for {
×
63
      config <- ZIO.service[AppConfig]
×
64
      _ <- (IssueBackgroundJobs.issueCredentialDidCommExchanges @@ Metric
×
65
        .gauge("issuance_flow_did_com_exchange_job_ms_gauge")
×
66
        .trackDurationWith(_.toMetricsSeconds))
×
67
        .repeat(Schedule.spaced(config.pollux.issueBgJobRecurrenceDelay))
×
68
        .unit
69
    } yield ()
×
70

71
  private val presentProofExchangeJob: RIO[
72
    AppConfig & DidOps & UriResolver & DIDResolver & JwtDidResolver & HttpClient & PresentationService &
73
      CredentialService & DIDNonSecretStorage & DIDService & ManagedDIDService,
74
    Unit
75
  ] =
76
    for {
×
77
      config <- ZIO.service[AppConfig]
×
78
      _ <- (PresentBackgroundJobs.presentProofExchanges @@ Metric
×
79
        .gauge("present_proof_flow_did_com_exchange_job_ms_gauge")
×
80
        .trackDurationWith(_.toMetricsSeconds))
×
81
        .repeat(Schedule.spaced(config.pollux.presentationBgJobRecurrenceDelay))
×
82
        .unit
83
    } yield ()
×
84

85
  private val connectDidCommExchangesJob: RIO[
86
    AppConfig & DidOps & DIDResolver & HttpClient & ConnectionService & ManagedDIDService & DIDNonSecretStorage &
87
      WalletManagementService,
88
    Unit
89
  ] =
90
    for {
×
91
      config <- ZIO.service[AppConfig]
×
92
      _ <- (ConnectBackgroundJobs.didCommExchanges @@ Metric
×
93
        .gauge("connection_flow_did_com_exchange_job_ms_gauge")
×
94
        .trackDurationWith(_.toMetricsSeconds))
×
95
        .repeat(Schedule.spaced(config.connect.connectBgJobRecurrenceDelay))
×
96
        .unit
97
    } yield ()
×
98

99
  private val syncRevocationStatusListsJob = {
100
    for {
×
101
      config <- ZIO.service[AppConfig]
×
102
      _ <- (StatusListJobs.syncRevocationStatuses @@ Metric
×
103
        .gauge("revocation_status_list_sync_job_ms_gauge")
×
104
        .trackDurationWith(_.toMetricsSeconds))
×
105
        .repeat(Schedule.spaced(config.pollux.syncRevocationStatusesBgJobRecurrenceDelay))
×
106
    } yield ()
×
107
  }
108

109
  private val syncDIDPublicationStateFromDltJob: URIO[ManagedDIDService & WalletManagementService, Unit] =
110
    ZIO
×
111
      .serviceWithZIO[WalletManagementService](_.listWallets().map(_._1))
×
112
      .flatMap { wallets =>
113
        ZIO.foreach(wallets) { wallet =>
×
114
          DIDStateSyncBackgroundJobs.syncDIDPublicationStateFromDlt
×
115
            .provideSomeLayer(ZLayer.succeed(WalletAccessContext(wallet.id)))
×
116
        }
117
      }
118
      .catchAll(e => ZIO.logError(s"error while syncing DID publication state: $e"))
×
119
      .repeat(Schedule.spaced(10.seconds))
×
120
      .unit
121
      .provideSomeLayer(ZLayer.succeed(WalletAdministrationContext.Admin()))
×
122

123
}
124

125
object AgentHttpServer {
126
  val agentRESTServiceEndpoints = for {
×
127
    allCredentialDefinitionRegistryEndpoints <- CredentialDefinitionRegistryServerEndpoints.all
×
128
    allSchemaRegistryEndpoints <- SchemaRegistryServerEndpoints.all
×
129
    allVerificationPolicyEndpoints <- VerificationPolicyServerEndpoints.all
×
130
    allConnectionEndpoints <- ConnectionServerEndpoints.all
×
131
    allIssueEndpoints <- IssueServerEndpoints.all
×
132
    allStatusListEndpoints <- CredentialStatusServiceEndpoints.all
×
133
    allDIDEndpoints <- DIDServerEndpoints.all
×
134
    allDIDRegistrarEndpoints <- DIDRegistrarServerEndpoints.all
×
135
    allPresentProofEndpoints <- PresentProofServerEndpoints.all
×
136
    allVcVerificationEndpoints <- VcVerificationServerEndpoints.all
×
137
    allSystemEndpoints <- SystemServerEndpoints.all
×
138
    allEntityEndpoints <- EntityServerEndpoints.all
×
139
    allWalletManagementEndpoints <- WalletManagementServerEndpoints.all
×
140
    allEventEndpoints <- EventServerEndpoints.all
×
141
    allOIDCEndpoints <- CredentialIssuerServerEndpoints.all
×
142
    allPresentationExchangeEndpoints <- PresentationExchangeServerEndpoints.all
×
143
  } yield allCredentialDefinitionRegistryEndpoints ++
×
144
    allSchemaRegistryEndpoints ++
145
    allVerificationPolicyEndpoints ++
146
    allConnectionEndpoints ++
147
    allDIDEndpoints ++
148
    allDIDRegistrarEndpoints ++
149
    allIssueEndpoints ++
150
    allStatusListEndpoints ++
151
    allPresentProofEndpoints ++
152
    allVcVerificationEndpoints ++
153
    allPresentationExchangeEndpoints ++
154
    allSystemEndpoints ++
155
    allEntityEndpoints ++
156
    allWalletManagementEndpoints ++
157
    allEventEndpoints ++
158
    allOIDCEndpoints
159

160
  def run =
×
161
    for {
×
162
      allEndpoints <- agentRESTServiceEndpoints
×
163
      allEndpointsWithDocumentation = ZHttpEndpoints.withDocumentations[Task](allEndpoints)
×
164
      server <- ZHttp4sBlazeServer.make("rest_api")
×
165
      appConfig <- ZIO.service[AppConfig]
×
166
      _ <- server.start(allEndpointsWithDocumentation, port = appConfig.agent.httpEndpoint.http.port).debug
×
167
    } yield ()
×
168
}
169

170
object AgentInitialization {
171

172
  private val defaultWalletId = WalletId.default
1✔
173
  private val defaultWallet = Wallet("default", defaultWalletId)
1✔
174
  private val defaultEntity = Entity.Default
175

176
  def run: RIO[AppConfig & WalletManagementService & EntityService & ApiKeyAuthenticator, Unit] =
1✔
177
    for {
1✔
178
      _ <- validateAppConfig
179
      _ <- initializeDefaultWallet
1✔
180
        .provideSomeLayer(ZLayer.succeed(WalletAdministrationContext.Admin()))
1✔
181
    } yield ()
1✔
182

183
  private val validateAppConfig =
184
    ZIO.serviceWithZIO[AppConfig](conf =>
1✔
185
      ZIO
1✔
186
        .fromEither(conf.validate)
1✔
187
        .mapError(msg => RuntimeException(s"Application configuration is invalid. $msg"))
1✔
188
    )
189

190
  private val initializeDefaultWallet =
191
    for {
1✔
192
      _ <- ZIO.logInfo("Initializing default wallet.")
1✔
193
      config <- ZIO.serviceWith[AppConfig](_.agent.defaultWallet)
1✔
194
      walletService <- ZIO.service[WalletManagementService]
1✔
195
      isDefaultWalletEnabled = config.enabled
196
      isDefaultWalletExist <- walletService
1✔
197
        .findWallet(defaultWalletId)
1✔
198
        .map(_.isDefined)
1✔
UNCOV
199
      _ <- ZIO.logInfo(s"Default wallet not enabled.").when(!isDefaultWalletEnabled)
×
200
      _ <- ZIO.logInfo(s"Default wallet already exist.").when(isDefaultWalletExist)
×
201
      _ <- createDefaultWallet.when(isDefaultWalletEnabled && !isDefaultWalletExist)
1✔
202
    } yield ()
1✔
203

204
  private val createDefaultWallet =
205
    for {
1✔
206
      walletService <- ZIO.service[WalletManagementService]
1✔
207
      entityService <- ZIO.service[EntityService]
1✔
208
      apiKeyAuth <- ZIO.service[ApiKeyAuthenticator]
1✔
209
      config <- ZIO.serviceWith[AppConfig](_.agent.defaultWallet)
1✔
210
      seed <- config.seed.fold(ZIO.none) { seedHex =>
1✔
211
        ZIO
1✔
212
          .fromTry(HexString.fromString(seedHex))
1✔
UNCOV
213
          .map(bytes => WalletSeed.fromByteArray(bytes.toByteArray).left.map(Exception(_)))
×
214
          .absolve
215
          .asSome
216
      }
217
      _ <- ZIO.logInfo(s"Default wallet seed is not provided. New seed will be generated.").when(seed.isEmpty)
1✔
218
      _ <- walletService
1✔
219
        .createWallet(defaultWallet, seed)
1✔
220
        .orDieAsUnmanagedFailure
1✔
221
      _ <- entityService.create(defaultEntity).orDieAsUnmanagedFailure
1✔
222
      _ <- apiKeyAuth.add(defaultEntity.id, config.authApiKey)
1✔
223
      _ <- config.webhookUrl.fold(ZIO.unit) { url =>
1✔
224
        val customHeaders = config.webhookApiKey.fold(Map.empty)(apiKey => Map("Authorization" -> s"Bearer $apiKey"))
1✔
225
        walletService
226
          .createWalletNotification(EventNotificationConfig(defaultWalletId, url, customHeaders))
1✔
227
          .orDieAsUnmanagedFailure
1✔
228
          .provide(ZLayer.succeed(WalletAccessContext(defaultWalletId)))
1✔
229
      }
230
    } yield ()
1✔
231

232
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc