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

input-output-hk / atala-prism-building-blocks / 5544147843

13 Jul 2023 02:11PM UTC coverage: 28.655% (+0.3%) from 28.368%
5544147843

push

web-flow
feat: ATL-4888 Anoncred schema type (#590)

* feat: ATL-4888 Anoncred Schema

* feat: ATL-4888 Added test

* feat: ATL-4888 Include version in type

2799 of 9768 relevant lines covered (28.65%)

0.29 hits per line

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

0.0
/mercury/mercury-library/agent-didcommx/src/main/scala/io/iohk/atala/mercury/MessagingService.scala
1
package io.iohk.atala.mercury
2

3
import scala.jdk.CollectionConverters.*
4
import zio._
5

6
import io.circe._
7
import io.circe.JsonObject
8
import io.iohk.atala.mercury.model._
9
import io.iohk.atala.mercury.model.error._
10
import io.iohk.atala.mercury.protocol.routing._
11
import io.iohk.atala.resolvers.DIDResolver
12

13
type HttpOrDID = String //TODO
14
case class ServiceEndpoint(uri: HttpOrDID, accept: Option[Seq[String]], routingKeys: Option[Seq[String]])
15
case class MessageAndAddress(msg: Message, url: String)
16

17
object MessagingService {
18

19
  /** Encrypted payload (message) and make the Forward Message */
×
20
  def makeForwardMessage(message: Message, mediator: DidId): URIO[DidOps & DidAgent, ForwardMessage] =
×
21
    for {
×
22
      didCommService <- ZIO.service[DidOps]
×
23
      encrypted <- didCommService.packEncrypted(message, to = message.to.head) // TODO head
×
24
      msg = ForwardMessage(
25
        to = mediator,
26
        expires_time = None,
×
27
        body = ForwardBody(next = message.to.head), // TODO check msg head
×
28
        attachments = Seq(AttachmentDescriptor.buildJsonAttachment(payload = encrypted.asJson)),
29
      )
30
    } yield (msg)
31

32
  /** Create a Message and any Forward Message as needed */
×
33
  def makeMessage(msg: Message): ZIO[
34
    DidOps & DidAgent & DIDResolver & HttpClient,
35
    SendMessageError,
36
    MessageAndAddress
37
  ] =
×
38
    for {
×
39
      didCommService <- ZIO.service[DidOps]
×
40
      resolver <- ZIO.service[DIDResolver]
×
41
      sendToDID <- msg.to match {
×
42
        case Seq() => // TODO support for anonymous message
×
43
          ZIO.fail(
×
44
            SendMessageError(
×
45
              new RuntimeException("Missing the destination DID - TODO support for anonymous message")
46
            )
47
          )
×
48
        case Seq(value) =>
×
49
          ZIO.succeed(value)
×
50
        case head +: tail => // TODO support for multiple destinations
×
51
          ZIO.fail(
×
52
            SendMessageError(
×
53
              new RuntimeException("TODO multiple destinations")
54
            )
55
          )
56
      }
57

×
58
      serviceEndpoint <- resolver
×
59
        .didCommServices(sendToDID) /* Seq[DIDCommService] */
×
60
        .catchAll { case ex => ZIO.fail(SendMessageError(ex)) }
61
        .flatMap {
×
62
          case Seq() =>
×
63
            ZIO.fail(
×
64
              SendMessageError(
×
65
                new RuntimeException("To send a Message you need a destination") // TODO ERROR
66
              )
67
            )
×
68
          case Seq(v) =>
×
69
            ZIO.succeed(
70
              ServiceEndpoint(
×
71
                uri = v.getServiceEndpoint(),
×
72
                accept = Option(v.getAccept()).map(_.asScala.toSeq),
×
73
                routingKeys = Option(v.getRoutingKeys()).map(_.asScala.toSeq)
74
              )
75
            )
×
76
          case headDID +: tail =>
×
77
            ZIO.logError("TODO multiple destinations") *>
×
78
              ZIO.succeed(
79
                ServiceEndpoint(
×
80
                  uri = headDID.getServiceEndpoint(),
×
81
                  accept = Option(headDID.getAccept()).map(_.asScala.toSeq),
×
82
                  routingKeys = Option(headDID.getRoutingKeys()).map(_.asScala.toSeq)
83
                )
84
              )
85
        }
86

×
87
      msgToSend <- serviceEndpoint match {
×
88
        case ServiceEndpoint(url, _, None) if url.startsWith("http") =>
×
89
          ZIO.log(s"No Forward Message needed. (send to $url)") *>
×
90
            ZIO.succeed(MessageAndAddress(msg, url))
×
91
        case ServiceEndpoint(url, _, Some(Seq())) if url.startsWith("http") =>
×
92
          ZIO.log(s"No Forward Message needed. (send to $url)") *>
×
93
            ZIO.succeed(MessageAndAddress(msg, url))
×
94
        case ServiceEndpoint(did, _, _) if did.startsWith("did:") =>
×
95
          for {
×
96
            _ <- ZIO.log(s"Make Forward Message for Mediator '$did'")
97
            mediator = DidId(did)
×
98
            forwardMessage <- makeForwardMessage(message = msg, mediator = mediator)
×
99
            finalMessage <- makeMessage(forwardMessage.asMessage) // Maybe it needs a double warping
100
          } yield finalMessage
×
101
        case ServiceEndpoint(uri, _, Some(routingKeys)) =>
×
102
          ZIO.log(s"RoutingDID: $routingKeys") *> ??? // ZIO.fail(???) // FIXME no support for routingKeys
×
103
        case s @ ServiceEndpoint(_, _, None) =>
×
104
          ZIO.logError(s"Unxpected ServiceEndpoint $s") *> ZIO.fail(
×
105
            SendMessageError(new RuntimeException(s"Unxpected ServiceEndpoint $s"))
106
          )
107
      }
108
    } yield (msgToSend)
109

110
    /** Encrypt and send a Message via HTTP
111
      *
112
      * TODO Move this method to another model
113
      */
×
114
  def send(msg: Message): ZIO[DidOps & DidAgent & DIDResolver & HttpClient, SendMessageError, HttpResponse] =
×
115
    ZIO.logAnnotate("msgId", msg.id) {
×
116
      for {
×
117
        auxFinalMessage <- makeMessage(msg)
×
118
        MessageAndAddress(finalMessage, serviceEndpoint) = auxFinalMessage
×
119
        didCommService <- ZIO.service[DidOps]
×
120
        encryptedMessage <-
×
121
          if (finalMessage.`type` == ForwardMessage.PIURI)
×
122
            didCommService.packEncryptedAnon(msg = finalMessage, to = finalMessage.to.head) // TODO Head
123
          else
×
124
            didCommService.packEncrypted(msg = finalMessage, to = finalMessage.to.head) // TODO Head
125

×
126
        _ <- ZIO.log(s"Sending to Message to '$serviceEndpoint'")
×
127
        resp <- HttpClient
×
128
          .postDIDComm(url = serviceEndpoint, data = encryptedMessage.string)
×
129
          .catchAll { case ex => ZIO.fail(SendMessageError(ex, Some(encryptedMessage.string))) }
×
130
        _ <- ZIO.when(resp.status >= 300)(
×
131
          ZIO.logWarning(
×
132
            s"Message to '$serviceEndpoint' return [${resp.status}] '${resp.bodyAsString}' for the request '${encryptedMessage.string}'"
133
          )
134
        )
135

136
      } yield (resp)
137
    }
138

139
}
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