-
Notifications
You must be signed in to change notification settings - Fork 15
/
MessagingService.scala
194 lines (174 loc) · 7.54 KB
/
MessagingService.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package io.iohk.atala.mercury
import scala.jdk.CollectionConverters.*
import zio._
import io.circe._
import io.circe.Json._
import io.circe.parser._
import io.circe.JsonObject
import io.iohk.atala.mercury.model._
import io.iohk.atala.mercury.model.error._
import io.iohk.atala.mercury.protocol.routing._
import io.iohk.atala.resolvers.DIDResolver
import org.didcommx.didcomm.common.VerificationMethodType
import org.didcommx.didcomm.common.VerificationMaterialFormat
type HttpOrDID = String //TODO
case class ServiceEndpoint(uri: HttpOrDID, accept: Option[Seq[String]], routingKeys: Option[Seq[String]])
object MessagingService {
def isForwardMessage[Service <: DidComm, Resolver <: DIDResolver](
didCommService: Service,
resolver: Resolver,
didCommServiceEndpoint: ServiceEndpoint,
message: Message,
encrypted: EncryptedMessage): ZIO[Any, Throwable, EncryptedMessage] = {
if (didCommServiceEndpoint.uri.startsWith("did:")) {
for {
_ <- Console.printLine("RoutingDID:" + DidId(didCommServiceEndpoint.uri))
forwardMessage <- didCommService.packEncrypted(
ForwardMessage(
from = message.from.get,
to = DidId(didCommServiceEndpoint.uri),
expires_time = None,
body = ForwardBody(next = message.to.head), // TODO check msg header
attachments = Seq(AttachmentDescriptor.buildJsonAttachment(payload = encrypted.asJson)),
).asMessage,
to = DidId(didCommServiceEndpoint.uri)
)
} yield forwardMesage
} else {
ZIO.succeed(encrypted)
}
}
/** Encrypt and send a Message via HTTP
*
* TODO Move this method to another model
*/
def send(msg: Message): ZIO[DidComm & DIDResolver & HttpClient, SendMessageError, HttpResponseBody] = {
for {
didCommService <- ZIO.service[DidComm]
resolver <- ZIO.service[DIDResolver]
sendToDID <- msg.to match {
case Seq() => // TODO support for anonymous message
ZIO.fail(new RuntimeException("Missing the destination DID - TODO support for anonymous message"))
case Seq(value) =>
ZIO.succeed(value)
case head +: tail => // TODO support for multiple destinations
ZIO.fail(new RuntimeException("TODO multiple destinations"))
}
_ <- Console.printLine("Encrypted Message")
encryptedMessage <- didCommService.packEncrypted(msg, to = msg.to.head) // TODO head
didCommServiceUrl <- resolver
.didCommServices(sendToDID) /* Seq[DIDCommService] */
.flatMap {
case Seq() =>
ZIO.fail(new RuntimeException("To send a Message you need a destination")) // TODO ERROR
case Seq(v) =>
ZIO.succeed(
ServiceEndpoint(
uri = v.getServiceEndpoint(),
accept = Option(v.getAccept()).map(_.asScala.toSeq),
routingKeys = Option(v.getRoutingKeys()).map(_.asScala.toSeq)
)
)
case headDID +: tail =>
ZIO.logError("TODO multiple destinations") *>
ZIO.succeed(
ServiceEndpoint(
uri = headDID.getServiceEndpoint(),
accept = Option(headDID.getAccept()).map(_.asScala.toSeq),
routingKeys = Option(headDID.getRoutingKeys()).map(_.asScala.toSeq)
)
)
}
_ <- Console.printLine("Forward message")
sendMsg = isForwardMessage(didCommService, resolver, didCommServiceUrl, msg, encryptedMessage)
jsonString <- sendMsg.map(_.string)
serviceEndpoint <- if (didCommServiceUrl.uri.startsWith("did:"))
resolver
.didCommServices(DidId(didCommServiceUrl.uri))
.map(_.toSeq.head.getServiceEndpoint()) // TODO this is not safe and also need to be recursive
else ZIO.succeed(didCommServiceUrl.uri)
_ <- Console.printLine("Sending to" + serviceEndpoint)
res <- HttpClient.postDIDComm(serviceEndpoint, jsonString)
} yield (res)
}.catchAll { case ex =>
ZIO.fail(SendMessageError(ex))
}
// WIP do not delete!
// /** Encrypt and send a Message via HTTP
// *
// * TODO move to another module (agent module)
// */
// def sendMessage(msg: Message): ZIO[DidComm & DIDResolver, Throwable, HttpResponseBody] = { // TODO Throwable
// for {
// didCommService <- ZIO.service[DidComm]
// resolver <- ZIO.service[DIDResolver]
// sendToDID <- msg.to match {
// case Seq() => // TODO support for anonymous message
// ZIO.fail(new RuntimeException("Missing the destination DID - TODO support for anonymous message"))
// case Seq(value) =>
// ZIO.succeed(value)
// case head +: tail => // TODO support for multiple destinations
// ZIO.fail(new RuntimeException("TODO multiple destinations"))
// }
// didCommService <- resolver
// .didCommServices(sendToDID) /* Seq[DIDCommService] */
// .flatMap {
// case Seq() =>
// ZIO.fail(new RuntimeException("To send a Message you need a destination")) // TODO ERROR
// case Seq(v) =>
// ZIO.succeed(
// ServiceEndpoint(
// uri = v.getServiceEndpoint(),
// accept = Option(v.getAccept()).map(_.asScala.toSeq),
// routingKeys = Option(v.getRoutingKeys()).map(_.asScala.toSeq)
// )
// )
// case headDID +: tail =>
// ZIO.logError("TODO multiple destinations") *>
// ZIO.succeed(
// ServiceEndpoint(
// uri = headDID.getServiceEndpoint(),
// accept = Option(headDID.getAccept()).map(_.asScala.toSeq),
// routingKeys = Option(headDID.getRoutingKeys()).map(_.asScala.toSeq)
// )
// )
// }
// sendToHttp <- didCommService match { // TODO
// case ServiceEndpoint(uri: HttpOrDID, _, _) if uri.startsWith("http") => ZIO.succeed(uri)
// case ServiceEndpoint(uri: HttpOrDID, _, _) if uri.startsWith("did") => ZIO.fail(???) // FIXME
// }
// keys <- didCommService match { // TODO
// case ServiceEndpoint(_, _, None | Some(Seq())) => ZIO.succeed(Seq())
// case ServiceEndpoint(_, _, Some(routingKeys)) =>
// ZIO.forall(routingKeys) { key =>
// val did = DidId(key.split("#", 1)(1))
// val allKeys = resolver
// .resolveDID(did)
// .map(e =>
// e.getVerificationMethods()
// .asScala
// .toSeq
// .find(_.getId() == key)
// .map { vm =>
// vm.getVerificationMaterial().getFormat() match
// case VerificationMaterialFormat.JWK =>
// vm.getVerificationMaterial().getValue()
// case VerificationMaterialFormat.BASE58 => ZIO.fail(???) // FIXME
// case VerificationMaterialFormat.MULTIBASE => ZIO.fail(???) // FIXME
// case VerificationMaterialFormat.OTHER => ZIO.fail(???) // FIXME
// }
// )
// ZIO.succeed(Seq()) // FIXME
// }
// }
// serviceEndpoint = {
// // didCommService.getRoutingKeys().to
// ???
// }
// encryptedForwardMessage <- didCommService.packEncrypted(msg, to = sendToDID)
// jsonString = encryptedForwardMessage.string
// _ <- Console.printLine("Sending to" + serviceEndpoint)
// res <- postDIDComm(serviceEndpoint)
// } yield (res)
// }
}