Skip to content

Commit

Permalink
refactor: message attachment data (#183)
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian G <[email protected]>
  • Loading branch information
cristianIOHK authored Aug 7, 2024
1 parent 06e9a8f commit b90cf9d
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonDecoder
import kotlinx.serialization.json.JsonNames
import kotlinx.serialization.json.jsonObject
import org.hyperledger.identus.apollo.base64.base64UrlDecoded
import org.hyperledger.identus.walletsdk.edgeagent.EdgeAgentError
import java.util.UUID
import kotlin.jvm.JvmOverloads

Expand All @@ -22,6 +24,26 @@ import kotlin.jvm.JvmOverloads
*/
@Serializable
sealed class AttachmentData {

/**
* This method verifies the type of [AttachmentData] and returns the data as a json string.
*/
fun getDataAsJsonString(): String {
return when (this) {
is AttachmentBase64 -> {
this.base64.base64UrlDecoded
}

is AttachmentJsonData -> {
this.data
}

else -> {
throw EdgeAgentError.AttachmentTypeNotSupported()
}
}
}

/**
* The [AttachmentHeader] data class represents the header for a DIDComm attachment.
*/
Expand Down Expand Up @@ -229,12 +251,35 @@ object AttachmentDataSerializer : KSerializer<AttachmentData> {
*/
override fun serialize(encoder: Encoder, value: AttachmentData) {
when (value) {
is AttachmentData.AttachmentHeader -> encoder.encodeSerializableValue(AttachmentData.AttachmentHeader.serializer(), value)
is AttachmentData.AttachmentJws -> encoder.encodeSerializableValue(AttachmentData.AttachmentJws.serializer(), value)
is AttachmentData.AttachmentJwsData -> encoder.encodeSerializableValue(AttachmentData.AttachmentJwsData.serializer(), value)
is AttachmentData.AttachmentBase64 -> encoder.encodeSerializableValue(AttachmentData.AttachmentBase64.serializer(), value)
is AttachmentData.AttachmentLinkData -> encoder.encodeSerializableValue(AttachmentData.AttachmentLinkData.serializer(), value)
is AttachmentData.AttachmentJsonData -> encoder.encodeSerializableValue(AttachmentData.AttachmentJsonData.serializer(), value)
is AttachmentData.AttachmentHeader -> encoder.encodeSerializableValue(
AttachmentData.AttachmentHeader.serializer(),
value
)

is AttachmentData.AttachmentJws -> encoder.encodeSerializableValue(
AttachmentData.AttachmentJws.serializer(),
value
)

is AttachmentData.AttachmentJwsData -> encoder.encodeSerializableValue(
AttachmentData.AttachmentJwsData.serializer(),
value
)

is AttachmentData.AttachmentBase64 -> encoder.encodeSerializableValue(
AttachmentData.AttachmentBase64.serializer(),
value
)

is AttachmentData.AttachmentLinkData -> encoder.encodeSerializableValue(
AttachmentData.AttachmentLinkData.serializer(),
value
)

is AttachmentData.AttachmentJsonData -> encoder.encodeSerializableValue(
AttachmentData.AttachmentJsonData.serializer(),
value
)
}
}

Expand All @@ -255,22 +300,27 @@ object AttachmentDataSerializer : KSerializer<AttachmentData> {
json.containsKey("children") -> {
jsonSerializable.decodeFromJsonElement(AttachmentData.AttachmentHeader.serializer(), json)
}

json.containsKey("protected") && json.containsKey("signature") -> {
jsonSerializable.decodeFromJsonElement(AttachmentData.AttachmentJws.serializer(), json)
}

json.containsKey("base64") -> {
if (json.containsKey("jws")) {
jsonSerializable.decodeFromJsonElement(AttachmentData.AttachmentJwsData.serializer(), json)
} else {
jsonSerializable.decodeFromJsonElement(AttachmentData.AttachmentBase64.serializer(), json)
}
}

json.containsKey("links") && json.containsKey("hash") -> {
jsonSerializable.decodeFromJsonElement(AttachmentData.AttachmentLinkData.serializer(), json)
}

json.containsKey("data") -> {
jsonSerializable.decodeFromJsonElement(AttachmentData.AttachmentJsonData.serializer(), json)
}

else -> throw SerializationException("Unknown AttachmentData type")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import org.hyperledger.identus.walletsdk.domain.buildingblocks.Pollux
import org.hyperledger.identus.walletsdk.domain.models.Api
import org.hyperledger.identus.walletsdk.domain.models.ApiImpl
import org.hyperledger.identus.walletsdk.domain.models.ApolloError
import org.hyperledger.identus.walletsdk.domain.models.AttachmentData
import org.hyperledger.identus.walletsdk.domain.models.AttachmentData.AttachmentBase64
import org.hyperledger.identus.walletsdk.domain.models.AttachmentData.AttachmentJsonData
import org.hyperledger.identus.walletsdk.domain.models.AttachmentDescriptor
Expand Down Expand Up @@ -678,10 +677,7 @@ open class EdgeAgent {
CredentialType.ANONCREDS_OFFER -> {
val linkSecret = getLinkSecret()
val offerDataString = offer.attachments.firstNotNullOf {
when (it.data) {
is AttachmentData.AttachmentBase64 -> it.data.base64
else -> null
}
it.data.getDataAsJsonString()
}
if (offer.thid == null) {
throw EdgeAgentError.MissingOrNullFieldError("thid", "OfferCredential")
Expand Down Expand Up @@ -737,10 +733,9 @@ open class EdgeAgent {
@Throws(UnknownError.SomethingWentWrongError::class)
suspend fun processIssuedCredentialMessage(message: IssueCredential): Credential {
val credentialType = pollux.extractCredentialFormatFromMessage(message.attachments)
val attachment = message.attachments.firstOrNull()?.data as? AttachmentBase64
val attachmentData = message.attachments.firstOrNull()?.data?.getDataAsJsonString()

return attachment?.let {
val credentialData = it.base64.base64UrlDecoded
return attachmentData?.let { credentialData ->
if (message.thid != null) {
val linkSecret = if (credentialType == CredentialType.ANONCREDS_ISSUE) {
getLinkSecret()
Expand Down Expand Up @@ -970,8 +965,6 @@ open class EdgeAgent {
var presentationString: String?
when (attachmentFormat) {
CredentialType.PRESENTATION_EXCHANGE_DEFINITIONS.type -> {
request.attachments.find { it.data::class == AttachmentBase64::class }
?: throw EdgeAgentError.AttachmentTypeNotSupported()
// Presentation Exchange
return handlePresentationDefinitionRequest(request, credential)
}
Expand Down Expand Up @@ -1108,12 +1101,7 @@ open class EdgeAgent {
throw EdgeAgentError.InvalidCredentialError(credential)
}

val msgAttachmentDescriptor =
requestPresentation.attachments.find { it.data::class == AttachmentBase64::class }
?: throw EdgeAgentError.AttachmentTypeNotSupported()

val attachmentBase64 = msgAttachmentDescriptor.data as AttachmentBase64
val presentationDefinitionRequestString = attachmentBase64.base64.base64UrlDecoded
val presentationDefinitionRequestString = requestPresentation.attachments.firstNotNullOf { it.data.getDataAsJsonString() }

val didString =
credential.subject ?: throw Exception("Credential must contain subject")
Expand Down Expand Up @@ -1147,21 +1135,15 @@ open class EdgeAgent {
throw EdgeAgentError.MissingOrNullFieldError("thid", "presentation message")
}
val presentation = Presentation.fromMessage(msg)
val msgAttachmentDescriptor =
presentation.attachments.find { it.data::class == AttachmentBase64::class }
?: throw EdgeAgentError.AttachmentTypeNotSupported()
val attachmentBase64 = msgAttachmentDescriptor.data as AttachmentBase64

val presentationSubmissionString = attachmentBase64.base64.base64UrlDecoded
val presentationSubmissionString = presentation.attachments.firstNotNullOf { it.data.getDataAsJsonString() }

val presentationDefinitionRequest =
pluto.getMessageByThidAndPiuri(msg.thid, ProtocolType.DidcommRequestPresentation.value)
.firstOrNull()
?.let { message ->
val requestPresentation = RequestPresentation.fromMessage(message)
val attachmentDescriptor = requestPresentation.attachments.first()
val base64 = (attachmentDescriptor.data as AttachmentBase64).base64
base64.base64UrlDecoded
requestPresentation.attachments.firstNotNullOf { it.data.getDataAsJsonString() }
} ?: throw EdgeAgentError.MissingOrNullFieldError("thid", "presentation message")
return pollux.verifyPresentationSubmission(
presentationSubmissionString = presentationSubmissionString,
Expand Down
Loading

0 comments on commit b90cf9d

Please sign in to comment.