-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: migrate to regex parsing instead of sdk 1.4
- Loading branch information
Pat Losoponkul
committed
Jul 3, 2023
1 parent
8ba16d5
commit 86a0481
Showing
3 changed files
with
75 additions
and
23 deletions.
There are no files selected for viewing
58 changes: 42 additions & 16 deletions
58
castor/lib/core/src/main/scala/io/iohk/atala/castor/core/model/did/DID.scala
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,37 +1,63 @@ | ||
package io.iohk.atala.castor.core.model.did | ||
|
||
import io.iohk.atala.prism.identity as prismIdentity | ||
import scala.util.matching.Regex | ||
|
||
import scala.util.Try | ||
|
||
final case class DIDMethod private[did] (value: String) { | ||
override def toString: String = value | ||
} | ||
opaque type DIDMethod = String | ||
|
||
object DIDMethod { | ||
def fromString(s: String): Try[DIDMethod] = { | ||
Try(DIDMethod(prismIdentity.DidMethod.Companion.fromString(s).toString)) | ||
|
||
private val DID_METHOD_REGEX: Regex = """^[a-z0-9]+$""".r | ||
|
||
extension (method: DIDMethod) { | ||
def toString: String = method | ||
} | ||
} | ||
|
||
final case class DIDMethodSpecificId private[did] (value: String) { | ||
override def toString: String = value | ||
def fromStringUnsafe(s: String): DIDMethod = s | ||
|
||
def fromString(s: String): Either[String, DIDMethod] = | ||
DID_METHOD_REGEX | ||
.findFirstMatchIn(s) | ||
.toRight(s"The DID method $s does not conform to the syntax") | ||
.map(_ => s) | ||
|
||
} | ||
|
||
opaque type DIDMethodSpecificId = String | ||
|
||
object DIDMethodSpecificId { | ||
def fromString(s: String): Try[DIDMethodSpecificId] = { | ||
Try(DIDMethodSpecificId(prismIdentity.DidMethodSpecificId.Companion.fromString(s).toString)) | ||
|
||
private val DID_METHOD_ID_REGEX: Regex = | ||
"""^(([A-Za-z0-9_\-\.]|(%[0-9A-F]{2}))*:)*([A-Za-z0-9_\-\.]|(%[0-9A-F]{2}))+$""".r | ||
|
||
extension (id: DIDMethodSpecificId) { | ||
def toString: String = id | ||
} | ||
|
||
def fromStringUnsafe(s: String): DIDMethodSpecificId = s | ||
|
||
def fromString(s: String): Either[String, DIDMethodSpecificId] = | ||
DID_METHOD_ID_REGEX | ||
.findFirstMatchIn(s) | ||
.toRight(s"The DID method specific id $s does not conform to the syntax") | ||
.map(_ => s) | ||
|
||
} | ||
|
||
final case class DID( | ||
method: DIDMethod, | ||
methodSpecificId: DIDMethodSpecificId | ||
) { | ||
override def toString: String = s"did:${method.value}:${methodSpecificId.value}" | ||
override def toString: String = s"did:$method:$methodSpecificId" | ||
} | ||
|
||
object DID { | ||
// TODO: implement | ||
def fromString(s: String): Either[String, DID] = ??? | ||
def fromString(s: String): Either[String, DID] = { | ||
for { | ||
_ <- Either.cond(s.startsWith("did:"), (), "DID syntax must start with 'did:' prefix") | ||
methodRaw = s.drop("did:".length).takeWhile(_ != ':') | ||
methodSpecificIdRaw = s.drop("did:".length).dropWhile(_ != ':').drop(1) | ||
method <- DIDMethod.fromString(methodRaw) | ||
methodSpecificId <- DIDMethodSpecificId.fromString(methodSpecificIdRaw) | ||
} yield DID(method, methodSpecificId) | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
castor/lib/core/src/test/scala/io/iohk/atala/castor/core/model/did/DIDSpec.scala
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.iohk.atala.castor.core.model.did | ||
|
||
import zio.* | ||
import zio.test.* | ||
import zio.test.Assertion.* | ||
|
||
object DIDSpec extends ZIOSpecDefault { | ||
|
||
override def spec = suite("DID")(fromStringSpec) | ||
|
||
// TODO: check this https://github.com/w3c/did-test-suite | ||
private val fromStringSpec = suite("DID.fromString")( | ||
test("parse valid PRISM did") { | ||
// val did = "did:prism:c191d4dfe2806d59df4632f78d38f80dfbfbd88187804783717e29116ffb" | ||
// did:prism:c191d4dfe2806d59df4632f78d38f80dfbfbd88187804783717e29116ffb:Cu0CCuoCEjoKBmF1dGgtMRAESi4KCXNlY3AyNTZrMRIhAz3656yBEIzdpZuG3yYn8Npoty3_qQhIbpOC8QmdVnpeEj8KC2Fzc2VydGlvbi0xEAJKLgoJc2VjcDI1NmsxEiECbZrA1SxPTpIEl9VBY4a6hGqaPJbJE7v8U5b236bCbWUSOwoHbWFzdGVyMBABSi4KCXNlY3AyNTZrMRIhA0BNFWJYhLqaVji9EYfIvdi | ||
val did = | ||
"did:prism:c191d4dfe2806d59df4632f78d38f80dfbfbd88187804783717e29116ffb:Cu0CCuoCEjoKBmF1dGgtMRAESi4KCXNlY3AyNTZrMRIhAz3656yBEIzdpZuG3yYn8Npoty3_qQhIbpOC8QmdVnpeEj8KC2Fzc2VydGlvbi0xEAJKLgoJc2VjcDI1NmsxEiECbZrA1SxPTpIEl9VBY4a6hGqaPJbJE7v8U5b236bCbWUSOwoHbWFzdGVyMBABSi4KCXNlY3AyNTZrMRIhA0BNFWJYhLqaVji9EYfIvdi" | ||
val parsed = DID.fromString(did) | ||
println(parsed) | ||
println(parsed.map(_.method)) | ||
println(parsed.map(_.methodSpecificId)) | ||
assert(parsed)(isRight(anything)) | ||
} | ||
) @@ TestAspect.tag("dev") | ||
|
||
} |