Skip to content

Commit

Permalink
feat: [ATL-3103] Implement Request presentation and Presentation logic (
Browse files Browse the repository at this point in the history
#46)

Co-authored-by: Ahmed Moussa <[email protected]>
Signed-off-by: Cristian G <[email protected]>
  • Loading branch information
cristianIOHK and hamada147 committed May 13, 2024
1 parent 4411cdd commit 2acaa79
Show file tree
Hide file tree
Showing 2 changed files with 250 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<String>?,
@SerialName("trust_issuers")
val trustIssuers: Array<String>?
) {
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<AttachmentDescriptor>
var thid: String? = null
lateinit var from: DID
lateinit var to: DID

constructor(
id: String? = null,
body: Body,
attachments: Array<AttachmentDescriptor>,
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<Body>(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
)
}
Original file line number Diff line number Diff line change
@@ -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<AttachmentDescriptor>
var thid: String? = null
lateinit var from: DID
lateinit var to: DID

constructor(
id: String?,
body: Body,
attachments: Array<AttachmentDescriptor>,
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<ProofTypes>
) {
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
}
}
}

0 comments on commit 2acaa79

Please sign in to comment.