-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: replace antlr with regex to solve did creation issues (#114)
- Loading branch information
1 parent
cecbbb1
commit 854fc70
Showing
14 changed files
with
70 additions
and
305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
33 changes: 11 additions & 22 deletions
33
atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/castor/did/DIDParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,23 @@ | ||
package io.iohk.atala.prism.walletsdk.castor.did | ||
|
||
import io.iohk.atala.prism.walletsdk.castor.antlrgrammar.DIDAbnfLexer | ||
import io.iohk.atala.prism.walletsdk.castor.antlrgrammar.DIDAbnfParser | ||
import io.iohk.atala.prism.walletsdk.domain.models.CastorError | ||
import io.iohk.atala.prism.walletsdk.domain.models.DID | ||
import org.antlr.v4.kotlinruntime.CharStreams | ||
import org.antlr.v4.kotlinruntime.CommonTokenStream | ||
import org.antlr.v4.kotlinruntime.tree.ParseTree | ||
import org.antlr.v4.kotlinruntime.tree.ParseTreeWalker | ||
import kotlin.jvm.Throws | ||
|
||
object DIDParser { | ||
|
||
@Throws(CastorError.InvalidDIDString::class) | ||
fun parse(didString: String): DID { | ||
val inputStream = CharStreams.fromString(didString) | ||
val lexer = DIDAbnfLexer(inputStream) | ||
val tokenStream = CommonTokenStream(lexer) | ||
val parser = DIDAbnfParser(tokenStream) | ||
|
||
parser.errorHandler = ErrorStrategy() | ||
|
||
val context = parser.did() | ||
val listener = DIDParserListener() | ||
ParseTreeWalker().walk(listener, context as ParseTree) | ||
|
||
val scheme = listener.scheme ?: throw CastorError.InvalidDIDString("Invalid DID string, missing scheme") | ||
val methodName = listener.methodName ?: throw CastorError.InvalidDIDString("Invalid DID string, missing method name") | ||
val methodId = listener.methodId ?: throw CastorError.InvalidDIDString("Invalid DID string, missing method ID") | ||
|
||
return DID(scheme, methodName, methodId) | ||
val regex = | ||
"""^did:(?<method>[a-z0-9]+):(?<idstring>[a-z0-9.\-_%]+:*[a-z0-9.\-_%]+[^#?:]+)$""".toRegex(RegexOption.IGNORE_CASE) | ||
val matchResult = regex.find(didString) | ||
matchResult?.let { | ||
val scheme = "did" | ||
val methodName = it.groups["method"]?.value | ||
?: throw CastorError.InvalidDIDString("Invalid DID string, missing method name") | ||
val methodId = it.groups["idstring"]?.value | ||
?: throw CastorError.InvalidDIDString("Invalid DID string, missing method ID") | ||
return DID(scheme, methodName, methodId) | ||
} ?: throw CastorError.InvalidDIDString("DID string does not match the expected structure.") | ||
} | ||
} |
25 changes: 0 additions & 25 deletions
25
...m-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/castor/did/DIDParserListener.kt
This file was deleted.
Oops, something went wrong.
57 changes: 28 additions & 29 deletions
57
...-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/castor/did/DIDUrlParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,40 @@ | ||
package io.iohk.atala.prism.walletsdk.castor.did | ||
|
||
import io.iohk.atala.prism.walletsdk.castor.antlrgrammar.DIDUrlAbnfLexer | ||
import io.iohk.atala.prism.walletsdk.castor.antlrgrammar.DIDUrlAbnfParser | ||
import io.iohk.atala.prism.walletsdk.domain.models.CastorError | ||
import io.iohk.atala.prism.walletsdk.domain.models.DID | ||
import io.iohk.atala.prism.walletsdk.domain.models.DIDUrl | ||
import org.antlr.v4.kotlinruntime.CharStreams | ||
import org.antlr.v4.kotlinruntime.CommonTokenStream | ||
import org.antlr.v4.kotlinruntime.tree.ParseTree | ||
import org.antlr.v4.kotlinruntime.tree.ParseTreeWalker | ||
import kotlin.jvm.Throws | ||
|
||
object DIDUrlParser { | ||
@Throws(CastorError.InvalidDIDString::class) | ||
fun parse(didUrlString: String): DIDUrl { | ||
val inputStream = CharStreams.fromString(didUrlString) | ||
val lexer = DIDUrlAbnfLexer(inputStream) | ||
val tokenStream = CommonTokenStream(lexer) | ||
val parser = DIDUrlAbnfParser(tokenStream) | ||
val regex = | ||
"""^did:(?<method>[a-z0-9]+)(?::(?<idstring>[^#?/]*))?(?<path>[^#?]*)?(?<query>\?[^#]*)?(?<fragment>#.*)?$""".toRegex( | ||
RegexOption.IGNORE_CASE | ||
) | ||
val matchResult = regex.find(didUrlString) | ||
|
||
parser.errorHandler = ErrorStrategy() | ||
|
||
val context = parser.did_url() | ||
val listener = DIDUrlParserListener() | ||
ParseTreeWalker().walk(listener, context as ParseTree) | ||
|
||
val scheme = listener.scheme ?: throw CastorError.InvalidDIDString("Invalid DID string, missing scheme") | ||
val methodName = listener.methodName ?: throw CastorError.InvalidDIDString("Invalid DID string, missing method name") | ||
val methodId = listener.methodId ?: throw CastorError.InvalidDIDString("Invalid DID string, missing method ID") | ||
|
||
val did = DID(scheme, methodName, methodId) | ||
|
||
return DIDUrl( | ||
did, | ||
listener.path ?: emptyArray(), | ||
listener.query, | ||
listener.fragment | ||
) | ||
matchResult?.let { it -> | ||
val method = it.groups["method"]?.value | ||
?: throw CastorError.InvalidDIDString("Invalid DID string, missing method name") | ||
val idString = it.groups["idstring"]?.value | ||
?: throw CastorError.InvalidDIDString("Invalid DID string, missing method ID") | ||
val path = | ||
it.groups["path"]?.value ?: throw CastorError.InvalidDIDString("Invalid DID string, missing path") | ||
val query = it.groups["query"]?.value ?: "" | ||
val fragment = it.groups["fragment"]?.value ?: "" | ||
val attributes = if (query.isNotEmpty()) { | ||
query.removePrefix("?").split("&") | ||
.associate { | ||
val (key, value) = it.split("=") | ||
key to (value ?: "") | ||
} | ||
} else { | ||
mapOf() | ||
} | ||
val paths = path.split("/").filter { it.isNotEmpty() }.toTypedArray() | ||
val did = DID("did", method, idString) | ||
val fragmentValue = if (fragment.isNotEmpty()) fragment.removePrefix("#") else null | ||
return DIDUrl(did, paths, attributes, fragmentValue) | ||
} ?: throw CastorError.InvalidDIDString("DID string does not match the expected structure.") | ||
} | ||
} |
50 changes: 0 additions & 50 deletions
50
...dk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/castor/did/DIDUrlParserListener.kt
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
...prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/castor/did/ErrorStrategy.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.