Skip to content

Commit

Permalink
feat(agent): add mediation and ability to send messages
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalo-frade-iohk committed Feb 15, 2023
1 parent 93884d5 commit 6eb95d2
Show file tree
Hide file tree
Showing 88 changed files with 1,135 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ actual class CastorImpl actual constructor(apollo: Apollo) : Castor {
this.apollo = apollo
this.resolvers = arrayOf(
PeerDIDResolver(),
LongFormPrismDIDResolver(this.apollo)
LongFormPrismDIDResolver(this.apollo),
)
}

Expand All @@ -31,18 +31,18 @@ actual class CastorImpl actual constructor(apollo: Apollo) : Castor {

actual override fun createPrismDID(
masterPublicKey: PublicKey,
services: Array<DIDDocument.Service>?
services: Array<DIDDocument.Service>?,
): DID {
TODO("Not yet implemented")
}

actual override fun createPeerDID(
keyPairs: Array<KeyPair>,
services: Array<DIDDocument.Service>
services: Array<DIDDocument.Service>,
): DID {
return CastorShared.createPeerDID(
keyPairs = keyPairs,
services = services
services = services,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ actual class LongFormPrismDIDResolver(
override suspend fun resolve(didString: String): DIDDocument {
return CastorShared.resolveLongFormPrismDID(
apollo = apollo,
didString = didString
didString = didString,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object DIDUrlParser {
did,
listener.path ?: emptyArray(),
listener.query,
listener.fragment
listener.fragment,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.iohk.atala.prism.walletsdk.castor.io.iohk.atala.prism.walletsdk.castor.did.prismdid
package io.iohk.atala.prism.walletsdk.castor.did.prismdid

import io.iohk.atala.prism.walletsdk.castor.did.prismdid.PrismDIDMethodId
import io.iohk.atala.prism.walletsdk.domain.models.CastorError
import io.iohk.atala.prism.walletsdk.domain.models.DID

Expand All @@ -12,7 +11,7 @@ LongFormPrismDID(val did: DID) {

init {
val methodId = PrismDIDMethodId(
did.methodId
did.methodId,
)

if (methodId.sections.size !== 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PrismDIDPublicKey {
CAPABILITY_DELEGATION_KEY("capabilityDelegationKey"),
CAPABILITY_INVOCATION_KEY("capabilityInvocationKey"),
KEY_AGREEMENT_KEY("keyAgreementKey"),
UNKNOWN_KEY("unknownKey")
UNKNOWN_KEY("unknownKey"),
}

private val apollo: Apollo
Expand Down Expand Up @@ -53,8 +53,8 @@ class PrismDIDPublicKey {
id = id,
usage = usage.toProto(),
keyData = io.iohk.atala.prism.protos.PublicKey.KeyData.CompressedEcKeyData(
compressed.toProto()
)
compressed.toProto(),
),
)
}
}
Expand All @@ -76,7 +76,7 @@ fun KeyUsage.fromProto(): PrismDIDPublicKey.Usage {
fun CompressedPublicKey.toProto(): CompressedECKeyData {
return CompressedECKeyData(
curve = uncompressed.curve.curve.value,
data = ByteArr(value)
data = ByteArr(value),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import io.iohk.atala.prism.mercury.didpeer.createPeerDIDNumalgo2
import io.iohk.atala.prism.protos.AtalaOperation
import io.iohk.atala.prism.walletsdk.castor.did.DIDParser
import io.iohk.atala.prism.walletsdk.castor.did.DIDUrlParser
import io.iohk.atala.prism.walletsdk.castor.did.prismdid.LongFormPrismDID
import io.iohk.atala.prism.walletsdk.castor.did.prismdid.PrismDIDPublicKey
import io.iohk.atala.prism.walletsdk.castor.io.iohk.atala.prism.walletsdk.castor.did.prismdid.LongFormPrismDID
import io.iohk.atala.prism.walletsdk.domain.buildingBlocks.Apollo
import io.iohk.atala.prism.walletsdk.domain.models.CastorError
import io.iohk.atala.prism.walletsdk.domain.models.Curve
Expand Down Expand Up @@ -59,16 +59,16 @@ internal class CastorShared {
urls = arrayOf(didString),
verificationMethods = peerDIDDocument.authentication.map {
fromVerificationMethodPeerDID(didString, it)
}.toTypedArray()
)
}.toTypedArray(),
),
)
coreProperties.add(
DIDDocument.KeyAgreement(
urls = arrayOf(didString),
verificationMethods = peerDIDDocument.keyAgreement.map {
fromVerificationMethodPeerDID(didString, it)
}.toTypedArray()
)
}.toTypedArray(),
),
)

val peerDIDServices = peerDIDDocument.service ?: listOf()
Expand All @@ -84,30 +84,30 @@ internal class CastorShared {
serviceEndpoint = DIDDocument.ServiceEndpoint(
uri = service.serviceEndpoint,
accept = service.accept.toTypedArray(),
routingKeys = service.routingKeys.toTypedArray()
)
)
routingKeys = service.routingKeys.toTypedArray(),
),
),
)
}
}
}

coreProperties.add(
DIDDocument.Services(
services.toTypedArray()
)
services.toTypedArray(),
),
)

val did = DIDParser.parse(didString)

return DIDDocument(
id = did,
coreProperties = coreProperties.toTypedArray()
coreProperties = coreProperties.toTypedArray(),
)
}
fun fromVerificationMethodPeerDID(
did: String,
verificationMethod: VerificationMethodPeerDID
verificationMethod: VerificationMethodPeerDID,
): DIDDocument.VerificationMethod {
val didUrl = DIDUrlParser.parse(did)
val controller = DIDParser.parse(verificationMethod.controller)
Expand Down Expand Up @@ -139,7 +139,7 @@ internal class CastorShared {
didUrl,
controller,
type,
Json.decodeFromString<Map<String, String>>(verificationMethod.verMaterial.value as String)
Json.decodeFromString<Map<String, String>>(verificationMethod.verMaterial.value as String),
)
}

Expand All @@ -149,7 +149,7 @@ internal class CastorShared {
controller,
type,
null,
verificationMethod.verMaterial.value as String
verificationMethod.verMaterial.value as String,
)
}

Expand All @@ -159,7 +159,7 @@ internal class CastorShared {
controller,
type,
null,
verificationMethod.verMaterial.value as String
verificationMethod.verMaterial.value as String,
)
}
}
Expand All @@ -174,7 +174,7 @@ internal class CastorShared {
apollo = apollo,
did = did,
stateHash = prismDID.stateHash,
encodedData = prismDID.encodedState.base64UrlDecodedBytes
encodedData = prismDID.encodedState.base64UrlDecodedBytes,
)
} catch (e: Throwable) {
// TODO: Add logger here
Expand All @@ -188,7 +188,7 @@ internal class CastorShared {
val authenticate = verificationMethods.entries.map {
DIDDocument.Authentication(
urls = arrayOf(it.key),
verificationMethods = verificationMethods.values.toTypedArray()
verificationMethods = verificationMethods.values.toTypedArray(),
)
}

Expand All @@ -201,7 +201,7 @@ internal class CastorShared {

return DIDDocument(
id = did,
coreProperties = coreProperties.toTypedArray()
coreProperties = coreProperties.toTypedArray(),
)
}
fun decodeState(
Expand All @@ -210,7 +210,6 @@ internal class CastorShared {
stateHash: String,
encodedData: ByteArray,
): Pair<Map<String, DIDDocument.VerificationMethod>, List<DIDDocument.Service>> {

val sha256 = SHA256()
val verifyEncodedState = sha256.digest(encodedData)
val verifyEncodedStateHex = verifyEncodedState.toHexString()
Expand All @@ -234,22 +233,22 @@ internal class CastorShared {
it.id,
arrayOf(it.type),
DIDDocument.ServiceEndpoint(
uri = it.serviceEndpoint.first()
)
uri = it.serviceEndpoint.first(),
),
)
} ?: listOf()

val verificationMethods =
publicKeys.fold(emptyMap<String, DIDDocument.VerificationMethod>()) { partialResult, publicKey ->
val didUrl = DIDUrl(
did = did,
fragment = publicKey.id
fragment = publicKey.id,
)
val method = DIDDocument.VerificationMethod(
id = didUrl,
controller = did,
type = publicKey.keyData.curve.curve.value,
publicKeyMultibase = publicKey.keyData.value.base64Encoded
publicKeyMultibase = publicKey.keyData.value.base64Encoded,
)
partialResult + (didUrl.string() to method)
}
Expand All @@ -270,15 +269,15 @@ internal class CastorShared {
it.publicKeyMultibase?.let { publicKey ->
PublicKey(
curve = KeyCurve(DIDDocument.VerificationMethod.getCurveByType(it.type)),
value = publicKey.encodeToByteArray()
value = publicKey.encodeToByteArray(),
)
}
}
}

fun createPeerDID(
keyPairs: Array<KeyPair>,
services: Array<DIDDocument.Service>
services: Array<DIDDocument.Service>,
): DID {
val encryptionKeys: MutableList<VerificationMaterialAgreement> = mutableListOf()
val signingKeys: MutableList<VerificationMaterialAuthentication> = mutableListOf()
Expand All @@ -293,8 +292,8 @@ internal class CastorShared {
VerificationMaterialAgreement(
format = VerificationMaterialFormatPeerDID.MULTIBASE,
value = it.publicKey.value.decodeToString(),
type = VerificationMethodTypeAgreement.X25519_KEY_AGREEMENT_KEY_2020
)
type = VerificationMethodTypeAgreement.X25519_KEY_AGREEMENT_KEY_2020,
),
)
}

Expand All @@ -303,8 +302,8 @@ internal class CastorShared {
VerificationMaterialAuthentication(
format = VerificationMaterialFormatPeerDID.MULTIBASE,
value = it.publicKey.value.decodeToString(),
type = VerificationMethodTypeAuthentication.ED25519_VERIFICATION_KEY_2020
)
type = VerificationMethodTypeAuthentication.ED25519_VERIFICATION_KEY_2020,
),
)
}

Expand All @@ -329,10 +328,10 @@ internal class CastorShared {
type = it.type[0],
serviceEndpoint = it.serviceEndpoint.uri,
routingKeys = listOf(),
accept = it.serviceEndpoint.accept?.asList() ?: listOf()
).toDict().toJsonElement()
accept = it.serviceEndpoint.accept?.asList() ?: listOf(),
).toDict().toJsonElement(),
)
}.first()
}.first(),
)

return DIDParser.parse(peerDID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ actual class CastorImpl actual constructor(apollo: Apollo) : Castor {
this.apollo = apollo
this.resolvers = arrayOf(
PeerDIDResolver(),
LongFormPrismDIDResolver(this.apollo)
LongFormPrismDIDResolver(this.apollo),
)
}

Expand All @@ -34,18 +34,18 @@ actual class CastorImpl actual constructor(apollo: Apollo) : Castor {

actual override fun createPrismDID(
masterPublicKey: PublicKey,
services: Array<DIDDocument.Service>?
services: Array<DIDDocument.Service>?,
): DID {
TODO("Not yet implemented")
}

actual override fun createPeerDID(
keyPairs: Array<KeyPair>,
services: Array<DIDDocument.Service>
services: Array<DIDDocument.Service>,
): DID {
return CastorShared.createPeerDID(
keyPairs = keyPairs,
services = services
services = services,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ actual class LongFormPrismDIDResolver(
return GlobalScope.promise {
CastorShared.resolveLongFormPrismDID(
apollo = apollo,
didString = didString
didString = didString,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ actual interface Castor {
actual fun parseDID(did: String): DID
actual fun createPrismDID(
masterPublicKey: PublicKey,
services: Array<DIDDocument.Service>?
services: Array<DIDDocument.Service>?,
): DID

actual fun createPeerDID(
keyPairs: Array<KeyPair>,
services: Array<DIDDocument.Service>
services: Array<DIDDocument.Service>,
): DID

@Throws() // TODO: Add throw classes
Expand All @@ -24,6 +24,6 @@ actual interface Castor {
suspend fun verifySignature(
did: DID,
challenge: ByteArray,
signature: ByteArray
signature: ByteArray,
): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.iohk.atala.prism.walletsdk.domain.buildingBlocks

import io.iohk.atala.prism.walletsdk.domain.models.Message

actual interface Mercury {

actual fun packMessage(message: Message): String

actual fun unpackMessage(message: String): Message

@Throws() // TODO: Add throw classes
suspend fun sendMessage(message: Message): ByteArray?

@Throws() // TODO: Add throw classes
suspend fun sendMessageParseMessage(message: Message): Message?
}
Loading

0 comments on commit 6eb95d2

Please sign in to comment.