-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the Trust Ping protocol Reply to Trust Pings in the Prism agent
- Loading branch information
1 parent
2bde732
commit b07da78
Showing
4 changed files
with
166 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...otocol-trust-ping/src/main/scala/io/iohk/atala/mercury/protocol/trustping/TrustPing.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.iohk.atala.mercury.protocol.trustping | ||
|
||
import io.circe._ | ||
import io.circe.syntax._ | ||
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder} | ||
import io.iohk.atala.mercury.model._ | ||
|
||
/** https://identity.foundation/didcomm-messaging/spec/#trust-ping-protocol-20 */ | ||
final case class TrustPing( | ||
`type`: PIURI = TrustPing.`type`, | ||
id: String = java.util.UUID.randomUUID().toString, | ||
from: DidId, | ||
to: DidId, | ||
body: TrustPing.Body, | ||
) { | ||
assert(`type` == TrustPing.`type`) | ||
|
||
def makeMessage: Message = Message( | ||
id = this.id, | ||
`type` = this.`type`, | ||
from = Some(this.from), | ||
to = Seq(this.to), | ||
body = this.body.asJson.asObject.get, | ||
) | ||
|
||
def makeReply = TrustPingResponse( | ||
thid = this.id, | ||
from = to, | ||
to = from, | ||
) | ||
} | ||
|
||
object TrustPing { | ||
def `type`: PIURI = "https://didcomm.org/trust-ping/2.0/ping" | ||
case class Body( | ||
response_requested: Option[Boolean] = Some(true), | ||
) | ||
object Body { | ||
given Encoder[Body] = deriveEncoder[Body] | ||
given Decoder[Body] = deriveDecoder[Body] | ||
} | ||
given Encoder[TrustPing] = deriveEncoder[TrustPing] | ||
given Decoder[TrustPing] = deriveDecoder[TrustPing] | ||
|
||
/** Parse a generecy DIDComm Message into a TrustPing */ | ||
def fromMessage(message: Message): Either[String, TrustPing] = | ||
for { | ||
piuri <- | ||
if (message.`type` == TrustPing.`type`) Right(message.`type`) | ||
else Left(s"Message MUST be of the type '${TrustPing.`type`}' instead of '${message.`type`}'") | ||
body <- message.body.asJson | ||
.as[TrustPing.Body] | ||
.left | ||
.map(ex => "Fail to parse the body of the TrustPing because: " + ex.message) | ||
ret <- message.to match | ||
case Seq(onlyOneRecipient) => | ||
message.from match | ||
case Some(from) => | ||
Right( | ||
TrustPing( | ||
id = message.id, | ||
`type` = piuri, | ||
body = body, | ||
from = from, | ||
to = onlyOneRecipient | ||
) | ||
) | ||
case None => Left("TrustPing needs to define the recipient") | ||
case _ => Left("The recipient is ambiguous. Need to have only 1 recipient") | ||
} yield ret | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...rust-ping/src/main/scala/io/iohk/atala/mercury/protocol/trustping/TrustPingResponse.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.iohk.atala.mercury.protocol.trustping | ||
|
||
import io.circe._ | ||
import io.circe.syntax._ | ||
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder} | ||
import io.iohk.atala.mercury.model._ | ||
|
||
final case class TrustPingResponse( | ||
`type`: PIURI = TrustPingResponse.`type`, | ||
id: String = java.util.UUID.randomUUID().toString, | ||
thid: String, | ||
from: DidId, | ||
to: DidId, | ||
) { | ||
assert(`type` == TrustPingResponse.`type`) | ||
|
||
def makeMessage: Message = Message( | ||
id = this.id, | ||
thid = Some(this.thid), | ||
`type` = this.`type`, | ||
from = Some(this.from), | ||
to = Seq(this.to), | ||
) | ||
} | ||
|
||
object TrustPingResponse { | ||
def `type`: PIURI = "https://didcomm.org/trust-ping/2.0/ping-response" | ||
|
||
/** Parse a generecy DIDComm Message into a TrustPingResponse */ | ||
def fromMessage(message: Message): Either[String, TrustPingResponse] = | ||
for { | ||
piuri <- | ||
if (message.`type` == TrustPingResponse.`type`) Right(message.`type`) | ||
else Left(s"Message MUST be of the type '${TrustPingResponse.`type`}' instead of '${message.`type`}'") | ||
ret <- message.to match | ||
case Seq(onlyOneRecipient) => | ||
message.from match | ||
case Some(from) => | ||
Right( | ||
TrustPingResponse( | ||
id = message.id, | ||
thid = message.thid.getOrElse(???), // TODO | ||
`type` = piuri, | ||
from = from, | ||
to = onlyOneRecipient, | ||
) | ||
) | ||
case None => Left("TrustPingResponse needs to define the recipient") | ||
case _ => Left("The recipient is ambiguous. Need to have only 1 recipient") | ||
} yield ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters