-
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(agent): add mediation and ability to send messages
- Loading branch information
1 parent
7cc738f
commit e1944ac
Showing
16 changed files
with
417 additions
and
14 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
2 changes: 1 addition & 1 deletion
2
domain/src/commonMain/kotlin/io.iohk.atala.prism.domain/models/MediatorDID.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
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
66 changes: 66 additions & 0 deletions
66
...agent/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/prismagent/ConnectionManager.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,66 @@ | ||
package io.iohk.atala.prism.walletsdk.prismagent | ||
|
||
import io.iohk.atala.prism.domain.buildingBlocks.Castor | ||
import io.iohk.atala.prism.domain.buildingBlocks.Mercury | ||
import io.iohk.atala.prism.domain.buildingBlocks.Pluto | ||
import io.iohk.atala.prism.domain.models.DID | ||
import io.iohk.atala.prism.domain.models.Message | ||
import io.iohk.atala.prism.domain.models.PrismAgentError | ||
import io.iohk.atala.prism.walletsdk.prismagent.mediation.MediatorHandler | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.map | ||
|
||
class ConnectionManager { | ||
private val mercury: Mercury | ||
private val castor: Castor | ||
private val pluto: Pluto | ||
val mediator: MediatorHandler | ||
|
||
constructor( | ||
mercury: Mercury, | ||
castor: Castor, | ||
pluto: Pluto, | ||
mediatorHandler: MediatorHandler | ||
) { | ||
this.mercury = mercury | ||
this.castor = castor | ||
this.pluto = pluto | ||
this.mediator = mediatorHandler | ||
} | ||
|
||
@Throws() | ||
suspend fun startMediator() { | ||
mediator.bootRegisteredMediator() | ||
.first() | ||
} | ||
|
||
@Throws() | ||
suspend fun registerMediator(host: DID) { | ||
mediator.achieveMediation(host) | ||
.first() | ||
} | ||
|
||
@Throws() | ||
suspend fun sendMessage(message: Message): Message? { | ||
if(mediator.mediator == null) { | ||
throw PrismAgentError.noMediatorAvailableError() | ||
} | ||
pluto.storeMessage(message) | ||
return mercury.sendMessageParseMessage(message) | ||
} | ||
|
||
@Throws() | ||
fun awaitMessages(): Flow<Array<Message>> { | ||
return mediator.pickupUnreadMessages(10) | ||
.map { | ||
val messagesIds = it.map { it.first }.toTypedArray() | ||
mediator.registerMessagesAsRead(messagesIds) | ||
it.map { it.second }.toTypedArray() | ||
} | ||
.map { | ||
pluto.storeMessages(it) | ||
it | ||
} | ||
} | ||
} |
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
129 changes: 129 additions & 0 deletions
129
...onMain/kotlin/io/iohk/atala/prism/walletsdk/prismagent/mediation/BasicMediationHandler.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,129 @@ | ||
package io.iohk.atala.prism.walletsdk.prismagent.mediation | ||
|
||
import io.iohk.atala.prism.apollo.uuid.UUID | ||
import io.iohk.atala.prism.domain.buildingBlocks.Mercury | ||
import io.iohk.atala.prism.domain.buildingBlocks.Pluto | ||
import io.iohk.atala.prism.domain.models.AttachmentBase64 | ||
import io.iohk.atala.prism.domain.models.AttachmentJsonData | ||
import io.iohk.atala.prism.domain.models.DID | ||
import io.iohk.atala.prism.domain.models.Mediator | ||
import io.iohk.atala.prism.domain.models.Message | ||
import io.iohk.atala.prism.domain.models.PrismAgentError | ||
import io.iohk.atala.prism.walletsdk.prismagent.protocols.mediation.MediationGrant | ||
import io.iohk.atala.prism.walletsdk.prismagent.protocols.mediation.MediationKeysUpdateList | ||
import io.iohk.atala.prism.walletsdk.prismagent.protocols.mediation.MediationRequest | ||
import io.iohk.atala.prism.walletsdk.prismagent.protocols.pickup.PickupDelivery | ||
import io.iohk.atala.prism.walletsdk.prismagent.protocols.pickup.PickupReceived | ||
import io.iohk.atala.prism.walletsdk.prismagent.protocols.pickup.PickupRequest | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.map | ||
|
||
final class BasicMediationHandler: MediatorHandler { | ||
final class PlutoMediatorStoreImpl: MediatorStore { | ||
private val pluto: Pluto | ||
|
||
constructor(pluto: Pluto) { | ||
this.pluto = pluto | ||
} | ||
|
||
override fun getAllMediators(): Flow<Array<Mediator>> { | ||
return pluto.getAllMediators() | ||
} | ||
|
||
override fun storeMediator(mediator: Mediator) { | ||
pluto.storeMediator(mediator.mediatorDID, mediator.hostDID, mediator.routingDID) | ||
} | ||
} | ||
|
||
override val mediatorDID: DID | ||
override var mediator: Mediator? | ||
private set | ||
|
||
private val mercury: Mercury | ||
private val store: MediatorStore | ||
|
||
constructor(mediatorDID: DID, mercury: Mercury, store: MediatorStore) { | ||
this.mediatorDID = mediatorDID | ||
this.mediator = null | ||
this.mercury = mercury | ||
this.store = store | ||
} | ||
|
||
override fun bootRegisteredMediator(): Flow<Mediator> { | ||
return mediator?.let {flow { emit(it) } } | ||
?: store.getAllMediators().map { it.first() } | ||
} | ||
|
||
override fun achieveMediation(host: DID): Flow<Mediator> { | ||
val requestMessage = MediationRequest(from = host, to = mediatorDID).makeMessage() | ||
return flow { | ||
emit(mercury.sendMessageParseMessage(message = requestMessage)) | ||
}.map { | ||
if(it != null) { | ||
val grantedMessage = it.let { MediationGrant(it) } | ||
val routingDID = DID(grantedMessage.body.routingDid) | ||
Mediator( | ||
id = UUID.randomUUID4().toString(), | ||
mediatorDID = mediatorDID, | ||
hostDID = host, | ||
routingDID = routingDID | ||
) | ||
} else { | ||
throw PrismAgentError.mediationRequestFailedError() | ||
} | ||
} | ||
} | ||
|
||
override suspend fun updateKeyListWithDIDs(dids: Array<DID>) { | ||
val keyListUpdateMessage = mediator?.let{ | ||
MediationKeysUpdateList( | ||
from = it.hostDID, | ||
to = it.mediatorDID, | ||
recipientDids = dids | ||
).makeMessage() | ||
} ?: run { | ||
throw PrismAgentError.noMediatorAvailableError() | ||
} | ||
keyListUpdateMessage.let { message -> mercury.sendMessage(message) } | ||
} | ||
|
||
override fun pickupUnreadMessages(limit: Int): Flow<Array<Pair<String, Message>>> { | ||
val requestMessage = mediator?.let{ | ||
PickupRequest( | ||
from = it.hostDID, | ||
to = it.mediatorDID, | ||
body = PickupRequest.Body(null, limit.toString()) | ||
).makeMessage() | ||
} ?: throw PrismAgentError.noMediatorAvailableError() | ||
|
||
return flow { | ||
emit(mercury.sendMessageParseMessage(requestMessage)) | ||
}.map { | ||
val receivedMessage = it?.let { PickupDelivery(it) } | ||
receivedMessage?.let { | ||
it.attachments.map { | ||
val data = it.data | ||
when(data) { | ||
is AttachmentBase64 -> Pair(it.id, data.base64) | ||
is AttachmentJsonData ->Pair(it.id, data.data) | ||
else -> Pair("", "") // TODO: This needs to be changed | ||
} | ||
}.map { | ||
Pair(it.first, mercury.unpackMessage(it.second)) | ||
}.toTypedArray() | ||
} ?: emptyArray() | ||
} | ||
} | ||
|
||
override suspend fun registerMessagesAsRead(ids: Array<String>) { | ||
val requestMessage = mediator?.let{ | ||
PickupReceived( | ||
from = it.hostDID, | ||
to = it.mediatorDID, | ||
body = PickupReceived.Body(messageIdList = ids) | ||
).makeMessage() | ||
} ?: throw PrismAgentError.noMediatorAvailableError() | ||
mercury.sendMessage(requestMessage) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...c/commonMain/kotlin/io/iohk/atala/prism/walletsdk/prismagent/mediation/MediatorHandler.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,26 @@ | ||
package io.iohk.atala.prism.walletsdk.prismagent.mediation | ||
|
||
import io.iohk.atala.prism.domain.models.DID | ||
import io.iohk.atala.prism.domain.models.Mediator | ||
import io.iohk.atala.prism.domain.models.Message | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface MediatorHandler { | ||
val mediator: Mediator? | ||
val mediatorDID: DID | ||
|
||
@Throws() | ||
fun bootRegisteredMediator(): Flow<Mediator?> | ||
|
||
@Throws() | ||
fun achieveMediation(host: DID): Flow<Mediator> | ||
|
||
@Throws() | ||
suspend fun updateKeyListWithDIDs(dids: Array<DID>) | ||
|
||
@Throws() | ||
fun pickupUnreadMessages(limit: Int): Flow<Array<Pair<String, Message>>> | ||
|
||
@Throws() | ||
suspend fun registerMessagesAsRead(ids: Array<String>) | ||
} |
9 changes: 9 additions & 0 deletions
9
...src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/prismagent/mediation/MediatorStore.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,9 @@ | ||
package io.iohk.atala.prism.walletsdk.prismagent.mediation | ||
|
||
import io.iohk.atala.prism.domain.models.Mediator | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface MediatorStore { | ||
fun storeMediator(mediator: Mediator) | ||
fun getAllMediators(): Flow<Array<Mediator>> | ||
} |
3 changes: 2 additions & 1 deletion
3
...dk/prismagent/protocols/MediationGrant.kt → ...ent/protocols/mediation/MediationGrant.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
Oops, something went wrong.