From ed537f6a3a165f06b1fe3c0b919213ddbaa8e9fb Mon Sep 17 00:00:00 2001 From: Ahmed Moussa Date: Wed, 17 Jan 2024 19:47:45 +0200 Subject: [PATCH] fix(DIDCommWrapper): Crash when body is empty string (#124) --- .../io/iohk/atala/prism/walletsdk/castor/CastorImpl.kt | 7 ++++++- .../atala/prism/walletsdk/castor/shared/CastorShared.kt | 2 +- .../io/iohk/atala/prism/walletsdk/domain/models/Errors.kt | 4 ++-- .../io/iohk/atala/prism/walletsdk/mercury/MercuryImpl.kt | 4 +++- .../prism/walletsdk/mercury/resolvers/DIDCommWrapper.kt | 8 ++++++-- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/castor/CastorImpl.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/castor/CastorImpl.kt index c5e26d5c2..7fb7361fb 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/castor/CastorImpl.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/castor/CastorImpl.kt @@ -28,7 +28,12 @@ import kotlin.jvm.Throws * or a more traditional system requiring secure and private identity management, Castor provides the tools and features * you need to easily create, manage, and resolve DIDs. */ -class CastorImpl(val apollo: Apollo, private val logger: PrismLogger = PrismLoggerImpl(LogComponent.CASTOR)) : Castor { +class CastorImpl +@JvmOverloads +constructor( + val apollo: Apollo, + private val logger: PrismLogger = PrismLoggerImpl(LogComponent.CASTOR) +) : Castor { var resolvers: Array = arrayOf( PeerDIDResolver(), LongFormPrismDIDResolver(this.apollo) diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/castor/shared/CastorShared.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/castor/shared/CastorShared.kt index 62ffc2b01..a042df972 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/castor/shared/CastorShared.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/castor/shared/CastorShared.kt @@ -318,7 +318,7 @@ internal class CastorShared { ) ) ) - throw CastorError.InitialStateOfDIDChanged() + throw CastorError.InitialStateOfDIDChanged(e.message) } val servicesProperty = DIDDocument.Services(services.toTypedArray()) diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/Errors.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/Errors.kt index e08bae11e..c254ad41b 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/Errors.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/Errors.kt @@ -390,8 +390,8 @@ constructor( * * @see CastorError */ - class InitialStateOfDIDChanged : CastorError( - "While trying to resolve Prism DID state changed making it invalid" + class InitialStateOfDIDChanged(message: String? = null) : CastorError( + "While trying to resolve Prism DID state changed making it invalid. $message" ) { override val code: Int get() = 26 diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/mercury/MercuryImpl.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/mercury/MercuryImpl.kt index e51f0c555..9176e2b91 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/mercury/MercuryImpl.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/mercury/MercuryImpl.kt @@ -49,7 +49,9 @@ interface DIDCommProtocol { * decentralized system requiring trusted peer-to-peer connections, Mercury provides the tools and features you need to * establish, manage, and secure your communications easily. */ -class MercuryImpl( +class MercuryImpl +@JvmOverloads +constructor( private val castor: Castor, private val protocol: DIDCommProtocol, private val api: Api, diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/mercury/resolvers/DIDCommWrapper.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/mercury/resolvers/DIDCommWrapper.kt index 746aefda5..d114dbbb8 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/mercury/resolvers/DIDCommWrapper.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/mercury/resolvers/DIDCommWrapper.kt @@ -143,8 +143,12 @@ class DIDCommWrapper(castor: Castor, pluto: Pluto, apollo: Apollo) : DIDCommProt override fun packEncrypted(message: Message): String { val toString = message.to.toString() - val element = Json.parseToJsonElement(message.body) - val map = jsonObjectToMap(element) + val element: JsonElement = if (message.body.isBlank() || message.body.isEmpty()) { + Json.parseToJsonElement("{}") + } else { + Json.parseToJsonElement(message.body) + } + val map: Map = jsonObjectToMap(element) val didCommMsg = org.didcommx.didcomm.message.Message( id = message.id,