diff --git a/waltid-did/src/commonMain/kotlin/id/walt/did/dids/registrar/local/web/DidWebRegistrar.kt b/waltid-did/src/commonMain/kotlin/id/walt/did/dids/registrar/local/web/DidWebRegistrar.kt index 9c9046ab9..d72ae2fe9 100644 --- a/waltid-did/src/commonMain/kotlin/id/walt/did/dids/registrar/local/web/DidWebRegistrar.kt +++ b/waltid-did/src/commonMain/kotlin/id/walt/did/dids/registrar/local/web/DidWebRegistrar.kt @@ -9,6 +9,7 @@ import id.walt.did.dids.registrar.DidResult import id.walt.did.dids.registrar.dids.DidCreateOptions import id.walt.did.dids.registrar.local.LocalRegistrarMethod import id.walt.did.utils.EncodingUtils.urlEncode +import id.walt.did.utils.ExtensionMethods.ensurePrefix class DidWebRegistrar : LocalRegistrarMethod("web") { override suspend fun register(options: DidCreateOptions): DidResult = options.get("keyType")?.let { @@ -21,7 +22,7 @@ class DidWebRegistrar : LocalRegistrarMethod("web") { }?.let { val domain = urlEncode(it) val path = options.get("path")?.takeIf { it.isNotEmpty() }?.let { - it.split("/").joinToString(":") { part -> urlEncode(part) } + it.ensurePrefix("/").split("/").joinToString(":") { part -> urlEncode(part) } } ?: "" DidResult( "did:web:$domain$path", DidDocument( diff --git a/waltid-did/src/jvmTest/kotlin/registrars/DidWebRegistrarTest.kt b/waltid-did/src/jvmTest/kotlin/registrars/DidWebRegistrarTest.kt index fcec88fc4..4701a95c3 100644 --- a/waltid-did/src/jvmTest/kotlin/registrars/DidWebRegistrarTest.kt +++ b/waltid-did/src/jvmTest/kotlin/registrars/DidWebRegistrarTest.kt @@ -8,6 +8,7 @@ import id.walt.did.dids.registrar.dids.DidCreateOptions import id.walt.did.dids.registrar.dids.DidWebCreateOptions import id.walt.did.dids.registrar.local.web.DidWebRegistrar import id.walt.did.utils.EncodingUtils +import id.walt.did.utils.ExtensionMethods.ensurePrefix import kotlinx.coroutines.runBlocking import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.Arguments @@ -75,11 +76,7 @@ class DidWebRegistrarTest : DidRegistrarTestBase(DidWebRegistrar()) { val domain = options.get("domain")!! val path = options.get("path") defaultDidAssertions(result, options) - // assert [did identifier] and [domain + path] are identical - assert( - EncodingUtils.urlDecode(DidUtils.identifierFromDid(did)!!) == - domain.plus(path?.replace("/", ":")) - ) + identifierAssertions(did, domain, path) } private val webKeyAssertions: registrarKeyAssertion = { result, options, key -> @@ -87,10 +84,16 @@ class DidWebRegistrarTest : DidRegistrarTestBase(DidWebRegistrar()) { val domain = options.get("domain")!! val path = options.get("path") defaultKeyAssertions(result, options, key) + identifierAssertions(did, domain, path) + } + + private fun identifierAssertions(did: String, domain: String, path: String?) { // assert [did identifier] and [domain + path] are identical assert( - EncodingUtils.urlDecode(DidUtils.identifierFromDid(did)!!) == - domain.plus(path?.replace("/", ":")) + //TODO: avoid computations in result comparison + EncodingUtils.urlDecode(DidUtils.identifierFromDid(did)!!) == domain.plus( + path.takeIf { !it.isNullOrEmpty() }?.ensurePrefix("/")?.replace("/", ":") ?: "" + ) ) } }