-
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.
feat(castor): Resolve LongFormatPrismDIDs in Castor (#23)
* Adding LongFormatPrismDID resolution + tests. Co-authored-by: Ahmed Moussa <[email protected]> Signed-off-by: Javi <[email protected]>
- Loading branch information
1 parent
0fed9ef
commit e07a79f
Showing
24 changed files
with
474 additions
and
22 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 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 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 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
2 changes: 1 addition & 1 deletion
2
castor/src/commonMain/kotlin/io/iohk/atala/prism/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
2 changes: 1 addition & 1 deletion
2
castor/src/commonMain/kotlin/io/iohk/atala/prism/castor/DID/DIDParserListener.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
2 changes: 1 addition & 1 deletion
2
castor/src/commonMain/kotlin/io/iohk/atala/prism/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
2 changes: 1 addition & 1 deletion
2
castor/src/commonMain/kotlin/io/iohk/atala/prism/castor/DID/DIDUrlParserListener.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
2 changes: 1 addition & 1 deletion
2
castor/src/commonMain/kotlin/io/iohk/atala/prism/castor/DID/ErrorStrategy.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
2 changes: 1 addition & 1 deletion
2
castor/src/commonMain/kotlin/io/iohk/atala/prism/castor/DID/InvalidDIDStringError.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
29 changes: 29 additions & 0 deletions
29
castor/src/commonMain/kotlin/io/iohk/atala/prism/castor/DID/PrismDID/LongFormPrismDID.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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.iohk.atala.prism.castor.io.iohk.atala.prism.castor.did.prismdid | ||
|
||
import io.iohk.atala.prism.castor.did.prismdid.PrismDIDMethodId | ||
import io.iohk.atala.prism.domain.models.CastorError | ||
import io.iohk.atala.prism.domain.models.DID | ||
|
||
data class | ||
LongFormPrismDID(val did: DID) { | ||
private val prismMethodId: PrismDIDMethodId | ||
val stateHash: String | ||
val encodedState: String | ||
|
||
init { | ||
val methodId = PrismDIDMethodId( | ||
did.methodId | ||
) | ||
|
||
if (methodId.sections.size !== 2) { | ||
throw CastorError.InvalidLongFormDID() | ||
} | ||
|
||
val stateHash = methodId.sections.first() | ||
val encodedState = methodId.sections.last() | ||
|
||
this.prismMethodId = methodId | ||
this.stateHash = stateHash | ||
this.encodedState = encodedState | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
castor/src/commonMain/kotlin/io/iohk/atala/prism/castor/DID/PrismDID/PrismDIDMethodId.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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.iohk.atala.prism.castor.did.prismdid | ||
|
||
import io.iohk.atala.prism.domain.models.CastorError | ||
|
||
data class PrismDIDMethodId(private val value: String) { | ||
val sections: List<String> | ||
get() = value.split(":").map { it } | ||
|
||
override fun toString(): String { | ||
return value | ||
} | ||
|
||
constructor(sections: List<String>) : this(sections.joinToString(":")) { | ||
val sectionRegex = Regex("^[A-Za-z0-9_-]+$") | ||
if (!sections.all { sectionRegex.matches(it) }) { | ||
throw CastorError.MethodIdIsDoesNotSatisfyRegex() | ||
} | ||
val methodSpecificIdRegex = Regex("^([A-Za-z0-9_-]*:)*[A-Za-z0-9_-]+$") | ||
if (!methodSpecificIdRegex.matches(value)) { | ||
throw CastorError.MethodIdIsDoesNotSatisfyRegex() | ||
} | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
castor/src/commonMain/kotlin/io/iohk/atala/prism/castor/DID/PrismDID/PrismDIDPublicKey.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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package io.iohk.atala.prism.castor.did.prismdid | ||
|
||
import io.iohk.atala.prism.domain.buildingBlocks.Apollo | ||
import io.iohk.atala.prism.domain.models.CastorError | ||
import io.iohk.atala.prism.domain.models.CompressedPublicKey | ||
import io.iohk.atala.prism.domain.models.PublicKey | ||
import io.iohk.atala.prism.protos.CompressedECKeyData | ||
import io.iohk.atala.prism.protos.KeyUsage | ||
import pbandk.ByteArr | ||
|
||
class PrismDIDPublicKey { | ||
enum class Usage(val value: String) { | ||
MASTER_KEY("masterKey"), | ||
ISSUING_KEY("issuingKey"), | ||
AUTHENTICATION_KEY("authenticationKey"), | ||
REVOCATION_KEY("revocationKey"), | ||
CAPABILITY_DELEGATION_KEY("capabilityDelegationKey"), | ||
CAPABILITY_INVOCATION_KEY("capabilityInvocationKey"), | ||
KEY_AGREEMENT_KEY("keyAgreementKey"), | ||
UNKNOWN_KEY("unknownKey") | ||
} | ||
|
||
private val apollo: Apollo | ||
val id: String | ||
val usage: Usage | ||
val keyData: PublicKey | ||
|
||
constructor(apollo: Apollo, id: String, usage: Usage, keyData: PublicKey) { | ||
this.apollo = apollo | ||
this.id = id | ||
this.usage = usage | ||
this.keyData = keyData | ||
} | ||
|
||
constructor(apollo: Apollo, proto: io.iohk.atala.prism.protos.PublicKey) { | ||
this.apollo = apollo | ||
this.id = proto.id | ||
this.usage = proto.usage.fromProto() | ||
this.keyData = when (proto.keyData) { | ||
is io.iohk.atala.prism.protos.PublicKey.KeyData.CompressedEcKeyData -> { | ||
apollo.compressedPublicKey(compressedData = proto.keyData.value.data.array).uncompressed | ||
} | ||
else -> { | ||
throw CastorError.InvalidPublicKeyEncoding() | ||
} | ||
} | ||
} | ||
|
||
fun toProto(): io.iohk.atala.prism.protos.PublicKey { | ||
val compressed = apollo.compressedPublicKey(keyData) | ||
return io.iohk.atala.prism.protos.PublicKey( | ||
id = id, | ||
usage = usage.toProto(), | ||
keyData = io.iohk.atala.prism.protos.PublicKey.KeyData.CompressedEcKeyData( | ||
compressed.toProto() | ||
) | ||
) | ||
} | ||
} | ||
|
||
fun KeyUsage.fromProto(): PrismDIDPublicKey.Usage { | ||
return when (this) { | ||
is KeyUsage.MASTER_KEY -> PrismDIDPublicKey.Usage.MASTER_KEY | ||
is KeyUsage.ISSUING_KEY -> PrismDIDPublicKey.Usage.ISSUING_KEY | ||
is KeyUsage.AUTHENTICATION_KEY -> PrismDIDPublicKey.Usage.AUTHENTICATION_KEY | ||
is KeyUsage.REVOCATION_KEY -> PrismDIDPublicKey.Usage.REVOCATION_KEY | ||
is KeyUsage.CAPABILITY_DELEGATION_KEY -> PrismDIDPublicKey.Usage.CAPABILITY_DELEGATION_KEY | ||
is KeyUsage.CAPABILITY_INVOCATION_KEY -> PrismDIDPublicKey.Usage.CAPABILITY_INVOCATION_KEY | ||
is KeyUsage.KEY_AGREEMENT_KEY -> PrismDIDPublicKey.Usage.KEY_AGREEMENT_KEY | ||
is KeyUsage.UNKNOWN_KEY -> PrismDIDPublicKey.Usage.UNKNOWN_KEY | ||
is KeyUsage.UNRECOGNIZED -> PrismDIDPublicKey.Usage.UNKNOWN_KEY | ||
} | ||
} | ||
|
||
fun CompressedPublicKey.toProto(): CompressedECKeyData { | ||
return CompressedECKeyData( | ||
curve = uncompressed.curve.curve.value, | ||
data = ByteArr(value) | ||
) | ||
} | ||
|
||
fun PrismDIDPublicKey.Usage.id(index: Int): String { | ||
return when (this) { | ||
PrismDIDPublicKey.Usage.MASTER_KEY -> "master$index" | ||
PrismDIDPublicKey.Usage.ISSUING_KEY -> "issuing$index" | ||
PrismDIDPublicKey.Usage.AUTHENTICATION_KEY -> "authentication$index" | ||
PrismDIDPublicKey.Usage.REVOCATION_KEY -> "revocation$index" | ||
PrismDIDPublicKey.Usage.CAPABILITY_DELEGATION_KEY -> "capabilityDelegation$index" | ||
PrismDIDPublicKey.Usage.CAPABILITY_INVOCATION_KEY -> "capabilityInvocation$index" | ||
PrismDIDPublicKey.Usage.KEY_AGREEMENT_KEY -> "keyAgreement$index" | ||
PrismDIDPublicKey.Usage.UNKNOWN_KEY -> "unknown$index" | ||
} | ||
} | ||
|
||
fun PrismDIDPublicKey.Usage.toProto(): KeyUsage { | ||
return when (this) { | ||
PrismDIDPublicKey.Usage.MASTER_KEY -> KeyUsage.MASTER_KEY | ||
PrismDIDPublicKey.Usage.ISSUING_KEY -> KeyUsage.ISSUING_KEY | ||
PrismDIDPublicKey.Usage.AUTHENTICATION_KEY -> KeyUsage.AUTHENTICATION_KEY | ||
PrismDIDPublicKey.Usage.REVOCATION_KEY -> KeyUsage.REVOCATION_KEY | ||
PrismDIDPublicKey.Usage.CAPABILITY_DELEGATION_KEY -> KeyUsage.CAPABILITY_DELEGATION_KEY | ||
PrismDIDPublicKey.Usage.CAPABILITY_INVOCATION_KEY -> KeyUsage.CAPABILITY_INVOCATION_KEY | ||
PrismDIDPublicKey.Usage.KEY_AGREEMENT_KEY -> KeyUsage.KEY_AGREEMENT_KEY | ||
PrismDIDPublicKey.Usage.UNKNOWN_KEY -> KeyUsage.UNKNOWN_KEY | ||
} | ||
} |
Oops, something went wrong.