diff --git a/prism-agent/src/commonMain/kotlin/io.iohk.atala.prism.walletsdk.prismagent/protocols/proofOfPresentation/Presentation.kt b/prism-agent/src/commonMain/kotlin/io.iohk.atala.prism.walletsdk.prismagent/protocols/proofOfPresentation/Presentation.kt new file mode 100644 index 000000000..fbbd583d0 --- /dev/null +++ b/prism-agent/src/commonMain/kotlin/io.iohk.atala.prism.walletsdk.prismagent/protocols/proofOfPresentation/Presentation.kt @@ -0,0 +1,143 @@ +package io.iohk.atala.prism.walletsdk.prismagent.protocols.proofOfPresentation + +import io.iohk.atala.prism.apollo.uuid.UUID +import io.iohk.atala.prism.walletsdk.domain.models.AttachmentDescriptor +import io.iohk.atala.prism.walletsdk.domain.models.DID +import io.iohk.atala.prism.walletsdk.domain.models.Message +import io.iohk.atala.prism.walletsdk.domain.models.PrismAgentError +import io.iohk.atala.prism.walletsdk.prismagent.protocols.ProtocolType +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json + +@Serializable +data class ProofTypes( + val schema: String, + @SerialName("required_fields") + val requiredFields: Array?, + @SerialName("trust_issuers") + val trustIssuers: Array? +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other == null || this::class != other::class) return false + + other as ProofTypes + + if (schema != other.schema) return false + if (requiredFields != null) { + if (other.requiredFields == null) return false + if (!requiredFields.contentEquals(other.requiredFields)) return false + } else if (other.requiredFields != null) return false + if (trustIssuers != null) { + if (other.trustIssuers == null) return false + if (!trustIssuers.contentEquals(other.trustIssuers)) return false + } else if (other.trustIssuers != null) return false + + return true + } + + override fun hashCode(): Int { + var result = schema.hashCode() + result = 31 * result + (requiredFields?.contentHashCode() ?: 0) + result = 31 * result + (trustIssuers?.contentHashCode() ?: 0) + return result + } +} + +class Presentation { + val type = ProtocolType.DidcommPresentation.value + lateinit var id: String + lateinit var body: Body + lateinit var attachments: Array + var thid: String? = null + lateinit var from: DID + lateinit var to: DID + + constructor( + id: String? = null, + body: Body, + attachments: Array, + thid: String? = null, + from: DID, + to: DID + ) { + this.id = id ?: UUID.randomUUID4().toString() + this.body = body + this.attachments = attachments + this.thid = thid + this.from = from + this.to = to + } + + constructor(fromMessage: Message) { + if ( + fromMessage.piuri == ProtocolType.DidcommPresentation.value && + fromMessage.from != null && + fromMessage.to != null + ) { + val body = Json.decodeFromString(fromMessage.body) + Presentation( + fromMessage.id, + body, + fromMessage.attachments, + fromMessage.thid, + fromMessage.from!!, + fromMessage.to!! + ) + } else { + throw PrismAgentError.invalidMessageError() + } + } + + fun makeMessage() { + Message( + id = id, + piuri = type, + from = from, + to = to, + body = Json.encodeToString(body), + attachments = attachments, + thid = thid + ) + } + + fun makePresentationFromRequest(msg: Message): Presentation { + try { + val requestPresentation = RequestPresentation(msg) + return Presentation( + body = Body( + goalCode = requestPresentation.body.goalCode, + comment = requestPresentation.body.comment + ), + attachments = requestPresentation.attachments, + thid = requestPresentation.id, + from = requestPresentation.to, + to = requestPresentation.from + ) + } catch (e: Exception) { + throw PrismAgentError.invalidRequestPresentationMessageError("Can't form RequestPresentation from Message") + } + } + + override fun equals(other: Any?): Boolean { + if (other == null) return false + if (other::class != this::class) return false + val otherPresentation = other as Presentation + return otherPresentation.type == this.type && + otherPresentation.id == this.id && + otherPresentation.body == this.body && + otherPresentation.attachments.contentEquals(this.attachments) && + otherPresentation.thid == this.thid && + otherPresentation.from == this.from && + otherPresentation.to == this.to + } + + @Serializable + data class Body( + val goalCode: String? = null, + val comment: String? = null + ) +} diff --git a/prism-agent/src/commonMain/kotlin/io.iohk.atala.prism.walletsdk.prismagent/protocols/proofOfPresentation/RequestPresentation.kt b/prism-agent/src/commonMain/kotlin/io.iohk.atala.prism.walletsdk.prismagent/protocols/proofOfPresentation/RequestPresentation.kt new file mode 100644 index 000000000..c5fc557d9 --- /dev/null +++ b/prism-agent/src/commonMain/kotlin/io.iohk.atala.prism.walletsdk.prismagent/protocols/proofOfPresentation/RequestPresentation.kt @@ -0,0 +1,107 @@ +package io.iohk.atala.prism.walletsdk.prismagent.protocols.proofOfPresentation + +import io.iohk.atala.prism.apollo.uuid.UUID +import io.iohk.atala.prism.walletsdk.domain.models.AttachmentDescriptor +import io.iohk.atala.prism.walletsdk.domain.models.DID +import io.iohk.atala.prism.walletsdk.domain.models.Message +import io.iohk.atala.prism.walletsdk.domain.models.PrismAgentError +import io.iohk.atala.prism.walletsdk.prismagent.protocols.ProtocolType +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json + +class RequestPresentation { + + lateinit var id: String + val type = ProtocolType.DidcommRequestPresentation.value + lateinit var body: Body + lateinit var attachments: Array + var thid: String? = null + lateinit var from: DID + lateinit var to: DID + + constructor( + id: String?, + body: Body, + attachments: Array, + thid: String?, + from: DID, + to: DID + ) { + this.id = id ?: UUID.randomUUID4().toString() + this.body = body + this.attachments = attachments + this.thid = thid + this.from = from + this.to = to + } + + constructor(fromMessage: Message) { + if (fromMessage.piuri == ProtocolType.DidcommRequestPresentation.value && + fromMessage.from != null && + fromMessage.to != null + ) { + RequestPresentation( + id = fromMessage.id, + body = Json.decodeFromString(fromMessage.body), + attachments = fromMessage.attachments, + thid = fromMessage.thid, + from = fromMessage.from!!, + to = fromMessage.to!! + ) + } else { + throw PrismAgentError.invalidMessageError() + } + } + + fun makeMessage(): Message { + return Message( + id = this.id, + piuri = this.type, + from = this.from, + to = this.to, + body = Json.encodeToString(this.body), + attachments = this.attachments, + thid = this.thid + ) + } + + fun makeRequestFromProposal(msg: Message): RequestPresentation { + TODO("Do when ProposePresentation is implemented") + } + + @Serializable + data class Body( + @SerialName("goal_code") + val goalCode: String? = null, + val comment: String? = null, + @SerialName("will_confirm") + val willConfirm: Boolean? = false, + @SerialName("proff_types") + val proofTypes: Array + ) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other == null || this::class != other::class) return false + + other as Body + + if (goalCode != other.goalCode) return false + if (comment != other.comment) return false + if (willConfirm != other.willConfirm) return false + if (!proofTypes.contentEquals(other.proofTypes)) return false + + return true + } + + override fun hashCode(): Int { + var result = goalCode?.hashCode() ?: 0 + result = 31 * result + (comment?.hashCode() ?: 0) + result = 31 * result + (willConfirm?.hashCode() ?: 0) + result = 31 * result + proofTypes.contentHashCode() + return result + } + } +}