-
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(Mercury): Orchestration and tests (#49)
* feat(Mercury): Orchestration and tests * updated for PR comments * Fixing linting issues and request changes from the PR * Fix linting issues * Fix wildcard imports * Add current time to didcomm message in case unpacked message does not has a created time --------- Co-authored-by: Cristian G <[email protected]>
- Loading branch information
1 parent
5ac155b
commit cc1313c
Showing
17 changed files
with
899 additions
and
20 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
42 changes: 42 additions & 0 deletions
42
...y/src/allButJSMain/kotlin/io.iohk.atala.prism.walletsdk.mercury/forward/ForwardMessage.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,42 @@ | ||
package io.iohk.atala.prism.walletsdk.mercury.forward | ||
|
||
import io.iohk.atala.prism.apollo.base64.base64UrlEncoded | ||
import io.iohk.atala.prism.walletsdk.domain.models.AttachmentBase64 | ||
import io.iohk.atala.prism.walletsdk.domain.models.AttachmentDescriptor | ||
import io.iohk.atala.prism.walletsdk.domain.models.DID | ||
import io.iohk.atala.prism.walletsdk.domain.models.Message | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import java.util.UUID | ||
|
||
class ForwardMessage @JvmOverloads constructor( | ||
val body: String, | ||
val from: DID, | ||
val to: DID, | ||
val encryptedMessage: String, | ||
val id: String = UUID.randomUUID().toString() | ||
) { | ||
companion object { | ||
const val type = "https://didcomm.org/routing/2.0/forward" | ||
} | ||
|
||
fun makeMessage(): Message { | ||
val forwardBody = Json.encodeToString(ForwardBody(body)).base64UrlEncoded | ||
val attachmentData = AttachmentBase64(encryptedMessage) | ||
val attachment = AttachmentDescriptor(UUID.randomUUID().toString(), "application/json", attachmentData) | ||
val message = Message( | ||
id = id, | ||
piuri = type, | ||
from = from, | ||
to = to, | ||
body = forwardBody, | ||
attachments = arrayOf(attachment) | ||
) | ||
|
||
return message | ||
} | ||
} | ||
|
||
@Serializable | ||
data class ForwardBody(val next: String) |
82 changes: 82 additions & 0 deletions
82
...allButJSMain/kotlin/io.iohk.atala.prism.walletsdk.mercury/resolvers/DIDCommDIDResolver.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,82 @@ | ||
package io.iohk.atala.prism.walletsdk.mercury.resolvers | ||
|
||
import io.iohk.atala.prism.walletsdk.domain.buildingBlocks.Castor | ||
import io.iohk.atala.prism.walletsdk.domain.models.Curve | ||
import io.iohk.atala.prism.walletsdk.domain.models.DIDDocument | ||
import kotlinx.coroutines.runBlocking | ||
import org.didcommx.didcomm.common.VerificationMaterial | ||
import org.didcommx.didcomm.common.VerificationMaterialFormat | ||
import org.didcommx.didcomm.common.VerificationMethodType | ||
import org.didcommx.didcomm.diddoc.DIDCommService | ||
import org.didcommx.didcomm.diddoc.DIDDoc | ||
import org.didcommx.didcomm.diddoc.DIDDocResolver | ||
import org.didcommx.didcomm.diddoc.VerificationMethod | ||
import java.util.Optional | ||
|
||
class DIDCommDIDResolver(val castor: Castor) : DIDDocResolver { | ||
override fun resolve(did: String): Optional<DIDDoc> { | ||
return runBlocking { | ||
val doc = castor.resolveDID(did) | ||
val authentications = mutableListOf<String>() | ||
val keyAgreements = mutableListOf<String>() | ||
val services = mutableListOf<DIDCommService>() | ||
val verificationMethods = mutableListOf<VerificationMethod>() | ||
|
||
doc.coreProperties.forEach { coreProperty -> | ||
val methods = when (coreProperty) { | ||
is DIDDocument.Authentication -> coreProperty.verificationMethods | ||
is DIDDocument.AssertionMethod -> coreProperty.verificationMethods | ||
is DIDDocument.KeyAgreement -> coreProperty.verificationMethods | ||
is DIDDocument.CapabilityInvocation -> coreProperty.verificationMethods | ||
is DIDDocument.CapabilityDelegation -> coreProperty.verificationMethods | ||
else -> emptyArray() | ||
} | ||
|
||
methods.forEach { method -> | ||
val curve = DIDDocument.VerificationMethod.getCurveByType(method.publicKeyJwk?.get("crv")!!) | ||
|
||
if (curve === Curve.ED25519) { | ||
authentications.add(method.id.string()) | ||
} | ||
|
||
if (curve === Curve.X25519) { | ||
keyAgreements.add(method.id.string()) | ||
} | ||
|
||
verificationMethods.add( | ||
VerificationMethod( | ||
id = method.id.string(), | ||
controller = method.controller.toString(), | ||
type = VerificationMethodType.JSON_WEB_KEY_2020, | ||
verificationMaterial = VerificationMaterial( | ||
VerificationMaterialFormat.JWK, | ||
method.publicKeyJwk.toString() ?: "" | ||
) | ||
) | ||
) | ||
} | ||
|
||
if (coreProperty is DIDDocument.Service && coreProperty.type.contains("DIDCommMessaging")) { | ||
services.add( | ||
DIDCommService( | ||
id = coreProperty.id, | ||
serviceEndpoint = coreProperty.serviceEndpoint.uri, | ||
routingKeys = coreProperty.serviceEndpoint.routingKeys?.toList() ?: emptyList(), | ||
accept = coreProperty.serviceEndpoint.accept?.toList() ?: emptyList() | ||
) | ||
) | ||
} | ||
} | ||
|
||
Optional.of( | ||
DIDDoc( | ||
doc.id.toString(), | ||
keyAgreements, | ||
authentications, | ||
verificationMethods, | ||
services | ||
) | ||
) | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...utJSMain/kotlin/io.iohk.atala.prism.walletsdk.mercury/resolvers/DIDCommSecretsResolver.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,39 @@ | ||
package io.iohk.atala.prism.walletsdk.mercury.resolvers | ||
|
||
import io.iohk.atala.prism.apollo.base64.base64UrlEncoded | ||
import io.iohk.atala.prism.walletsdk.domain.buildingBlocks.Pluto | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import kotlinx.coroutines.runBlocking | ||
import org.didcommx.didcomm.common.VerificationMaterial | ||
import org.didcommx.didcomm.common.VerificationMaterialFormat | ||
import org.didcommx.didcomm.common.VerificationMethodType | ||
import org.didcommx.didcomm.secret.Secret | ||
import org.didcommx.didcomm.secret.SecretResolver | ||
import java.util.Optional | ||
|
||
class DIDCommSecretsResolver(val pluto: Pluto) : SecretResolver { | ||
override fun findKeys(kids: List<String>): Set<String> { | ||
return runBlocking { | ||
kids.filter { pluto.getDIDPrivateKeyByID(it).firstOrNull() != null }.toSet() | ||
} | ||
} | ||
|
||
override fun findKey(kid: String): Optional<Secret> { | ||
return runBlocking { | ||
pluto.getDIDPrivateKeyByID(kid) | ||
.firstOrNull() | ||
?.let { privateKey -> | ||
Optional.of( | ||
Secret( | ||
kid, | ||
VerificationMethodType.JSON_WEB_KEY_2020, | ||
VerificationMaterial( | ||
VerificationMaterialFormat.MULTIBASE, | ||
privateKey.value.base64UrlEncoded | ||
) | ||
) | ||
) | ||
} ?: Optional.empty() | ||
} | ||
} | ||
} |
Oops, something went wrong.