-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPrismAgent.kt
786 lines (733 loc) · 28.5 KB
/
PrismAgent.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
package io.iohk.atala.prism.walletsdk.prismagent
/* ktlint-disable import-ordering */
import io.iohk.atala.prism.apollo.base64.base64UrlDecoded
import io.iohk.atala.prism.apollo.base64.base64UrlEncoded
import io.iohk.atala.prism.walletsdk.domain.buildingblocks.Apollo
import io.iohk.atala.prism.walletsdk.domain.buildingblocks.Castor
import io.iohk.atala.prism.walletsdk.domain.buildingblocks.Mercury
import io.iohk.atala.prism.walletsdk.domain.buildingblocks.Pluto
import io.iohk.atala.prism.walletsdk.domain.buildingblocks.Pollux
import io.iohk.atala.prism.walletsdk.domain.models.Api
import io.iohk.atala.prism.walletsdk.domain.models.ApiImpl
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.AttachmentJsonData
import io.iohk.atala.prism.walletsdk.domain.models.Curve
import io.iohk.atala.prism.walletsdk.domain.models.DID
import io.iohk.atala.prism.walletsdk.domain.models.DIDDocument
import io.iohk.atala.prism.walletsdk.domain.models.DIDPair
import io.iohk.atala.prism.walletsdk.domain.models.KeyCurve
import io.iohk.atala.prism.walletsdk.domain.models.KeyPair
import io.iohk.atala.prism.walletsdk.domain.models.Message
import io.iohk.atala.prism.walletsdk.domain.models.PeerDID
import io.iohk.atala.prism.walletsdk.domain.models.PolluxError
import io.iohk.atala.prism.walletsdk.domain.models.PrismDIDInfo
import io.iohk.atala.prism.walletsdk.domain.models.PrivateKey
import io.iohk.atala.prism.walletsdk.domain.models.Seed
import io.iohk.atala.prism.walletsdk.domain.models.Signature
import io.iohk.atala.prism.walletsdk.domain.models.VerifiableCredential
import io.iohk.atala.prism.walletsdk.domain.models.httpClient
import io.iohk.atala.prism.walletsdk.prismagent.mediation.BasicMediatorHandler
import io.iohk.atala.prism.walletsdk.prismagent.mediation.MediationHandler
import io.iohk.atala.prism.walletsdk.prismagent.protocols.ProtocolType
import io.iohk.atala.prism.walletsdk.prismagent.protocols.connection.DIDCommConnectionRunner
import io.iohk.atala.prism.walletsdk.prismagent.protocols.findProtocolTypeByValue
import io.iohk.atala.prism.walletsdk.prismagent.protocols.issueCredential.IssueCredential
import io.iohk.atala.prism.walletsdk.prismagent.protocols.issueCredential.OfferCredential
import io.iohk.atala.prism.walletsdk.prismagent.protocols.issueCredential.RequestCredential
import io.iohk.atala.prism.walletsdk.prismagent.protocols.outOfBand.DIDCommInvitationRunner
import io.iohk.atala.prism.walletsdk.prismagent.protocols.outOfBand.InvitationType
import io.iohk.atala.prism.walletsdk.prismagent.protocols.outOfBand.OutOfBandInvitation
import io.iohk.atala.prism.walletsdk.prismagent.protocols.outOfBand.PrismOnboardingInvitation
import io.iohk.atala.prism.walletsdk.prismagent.protocols.proofOfPresentation.Presentation
import io.iohk.atala.prism.walletsdk.prismagent.protocols.proofOfPresentation.RequestPresentation
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.http.Url
import io.ktor.http.HttpMethod
import io.ktor.serialization.kotlinx.json.json
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.onSubscription
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationException
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonObject
import java.net.UnknownHostException
import java.time.Duration
/* ktlint-disable import-ordering */
/**
* Check if the passed URL is valid or not.
* @param str string to check its URL validity
* @return [Url] if valid, null if not valid
*/
private fun Url.Companion.parse(str: String): Url? {
try {
return Url(str)
} catch (e: Throwable) {
return null
}
}
/**
* PrismAgent class is responsible for handling the connection to other agents in the network using a provided Mediator
* Service Endpoint and seed data.
*/
class PrismAgent {
var state: State = State.STOPPED
private set(value) {
field = value
prismAgentScope.launch {
flowState.emit(value)
}
}
val seed: Seed
val apollo: Apollo
val castor: Castor
val pluto: Pluto
val mercury: Mercury
val pollux: Pollux
var fetchingMessagesJob: Job? = null
val flowState = MutableSharedFlow<State>()
private val prismAgentScope: CoroutineScope = CoroutineScope(Dispatchers.Default)
private val api: Api
private var connectionManager: ConnectionManager
constructor(
apollo: Apollo,
castor: Castor,
pluto: Pluto,
mercury: Mercury,
pollux: Pollux,
connectionManager: ConnectionManager,
seed: Seed?,
api: Api?
) {
prismAgentScope.launch {
flowState.emit(State.STOPPED)
}
this.apollo = apollo
this.castor = castor
this.pluto = pluto
this.mercury = mercury
this.pollux = pollux
this.connectionManager = connectionManager
this.seed = seed ?: apollo.createRandomSeed().seed
this.api = api ?: ApiImpl(
httpClient {
install(ContentNegotiation) {
json(
Json {
ignoreUnknownKeys = true
prettyPrint = true
isLenient = true
}
)
}
}
)
}
@JvmOverloads
constructor(
apollo: Apollo,
castor: Castor,
pluto: Pluto,
mercury: Mercury,
pollux: Pollux,
seed: Seed? = null,
api: Api? = null,
mediatorHandler: MediationHandler
) {
prismAgentScope.launch {
flowState.emit(State.STOPPED)
}
this.apollo = apollo
this.castor = castor
this.pluto = pluto
this.mercury = mercury
this.pollux = pollux
this.seed = seed ?: apollo.createRandomSeed().seed
this.api = api ?: ApiImpl(
httpClient {
install(ContentNegotiation) {
json(
Json {
ignoreUnknownKeys = true
prettyPrint = true
isLenient = true
}
)
}
}
)
// Pairing will be removed in the future
this.connectionManager =
ConnectionManager(mercury, castor, pluto, mediatorHandler, mutableListOf())
}
init {
flowState.onSubscription {
if (flowState.subscriptionCount.value <= 0) {
state = State.STOPPED
} else {
throw Exception("Agent state only accepts one subscription.")
}
}
}
// Prism agent actions
/**
* Start the [PrismAgent] and Mediator services.
*
* @throws [PrismAgentError.MediationRequestFailedError] failed to connect to mediator.
* @throws [UnknownHostException] if unable to connect to the mediator.
*/
@Throws(PrismAgentError.MediationRequestFailedError::class, UnknownHostException::class)
suspend fun start() {
if (state != State.STOPPED) {
return
}
state = State.STARTING
try {
connectionManager.startMediator()
} catch (error: PrismAgentError.NoMediatorAvailableError) {
try {
val hostDID = createNewPeerDID(updateMediator = false)
connectionManager.registerMediator(hostDID)
} catch (error: UnknownHostException) {
state = State.STOPPED
throw error
}
}
if (connectionManager.mediationHandler.mediator != null) {
state = State.RUNNING
} else {
state = State.STOPPED
throw PrismAgentError.MediationRequestFailedError()
}
}
/**
* Stops the [PrismAgent].
* The function sets the state of [PrismAgent] to [State.STOPPING].
* All ongoing events that was created by the [PrismAgent] are stopped.
* After all the events are stopped the state of the [PrismAgent] is set to [State.STOPPED].
*/
fun stop() {
if (state != State.RUNNING) {
return
}
state = State.STOPPING
fetchingMessagesJob?.cancel()
state = State.STOPPED
}
// DID Higher Functions
/**
* This method create a new Prism DID, that can be used to identify the agent and interact with other agents.
*
* @param keyPathIndex key path index used to identify the DID.
* @param alias An alias that can be used to identify the DID.
* @param services an array of services associated to the DID.
* @return The new created [DID]
*/
@JvmOverloads
suspend fun createNewPrismDID(
keyPathIndex: Int? = null,
alias: String? = null,
services: Array<DIDDocument.Service> = emptyArray()
): DID {
val index = keyPathIndex ?: (pluto.getPrismLastKeyPathIndex().first() + 1)
val keyPair = apollo.createKeyPair(seed = seed, curve = KeyCurve(Curve.SECP256K1, index))
val did = castor.createPrismDID(masterPublicKey = keyPair.publicKey, services = services)
registerPrismDID(did, index, alias, keyPair.privateKey)
return did
}
/**
* This function receives a Prism DID and its information and stores it into the local database.
*
* @param did The DID to be stored
* @param keyPathIndex The index associated with the PrivateKey
* @param alias The alias associated with the DID if any
* @param privateKey The private key used to create the PrismDID
*/
private fun registerPrismDID(
did: DID,
keyPathIndex: Int,
alias: String? = null,
privateKey: PrivateKey
) {
pluto.storePrismDIDAndPrivateKeys(
did = did,
keyPathIndex = keyPathIndex,
alias = alias,
listOf(privateKey)
)
}
/**
* This function creates a new Peer DID, stores it in pluto database and updates the mediator if requested.
*
* @param services The services associated to the new DID.
* @param updateMediator Indicates if the new DID should be added to the mediator's list. It will as well add the
* mediator service.
* @return A new [DID].
*/
@JvmOverloads
suspend fun createNewPeerDID(
services: Array<DIDDocument.Service> = emptyArray(),
updateMediator: Boolean
): DID {
val keyAgreementKeyPair =
apollo.createKeyPair(seed = seed, curve = KeyCurve(Curve.X25519))
val authenticationKeyPair =
apollo.createKeyPair(seed = seed, curve = KeyCurve(Curve.ED25519))
var tmpServices = services
if (updateMediator) {
tmpServices.plus(
DIDDocument.Service(
id = DIDCOMM1,
type = arrayOf(
DIDCOMM_MESSAGING
),
serviceEndpoint = DIDDocument.ServiceEndpoint(
uri = connectionManager.mediationHandler.mediator?.routingDID.toString()
)
)
)
}
val did = castor.createPeerDID(
arrayOf(keyAgreementKeyPair, authenticationKeyPair),
services = tmpServices
)
registerPeerDID(
did,
keyAgreementKeyPair,
authenticationKeyPair,
updateMediator
)
return did
}
/**
* Registers a peer DID with the specified DID and private keys.
*
* @param did The DID to register.
* @param privateKeys The list of private keys associated with the peer DID.
* @param updateMediator Determines whether to update the mediator with the specified DID.
*/
suspend fun registerPeerDID(
did: DID,
keyAgreementKeyPair: KeyPair,
authenticationKeyPair: KeyPair,
updateMediator: Boolean
) {
if (updateMediator) {
updateMediatorWithDID(did)
}
// The next logic is a bit tricky, so it's not forgotten this is a reminder.
// The next few lines are needed because of DIDComm library, the library will need
// to get the secret(private key) that is pair of the public key within the DIDPeer Document
// to this end the library will give you the id of the public key that is `did:{method}:{methodId}#ecnumbasis`.
// So the code below after the did is created, it will retrieve the document and
// and store the private keys with the corresponding `id` of the one created on the document.
// So when the secret resolver asks for the secret we can identify it.
val document = castor.resolveDID(did.toString())
val listOfVerificationMethods: MutableList<DIDDocument.VerificationMethod> =
mutableListOf()
document.coreProperties.forEach {
if (it is DIDDocument.Authentication) {
listOfVerificationMethods.addAll(it.verificationMethods)
}
if (it is DIDDocument.KeyAgreement) {
listOfVerificationMethods.addAll(it.verificationMethods)
}
}
val verificationMethods =
DIDDocument.VerificationMethods(listOfVerificationMethods.toTypedArray())
verificationMethods.values.forEach {
if (it.type.contains("X25519")) {
pluto.storePrivateKeys(keyAgreementKeyPair.privateKey, did, 0, it.id.toString())
} else if (it.type.contains("Ed25519")) {
pluto.storePrivateKeys(
authenticationKeyPair.privateKey,
did,
0,
it.id.toString()
)
}
}
pluto.storePeerDID(
did = did
)
}
/**
* Updates the mediator with the specified DID by updating the key list with the given DID.
*
* @param did The DID to update the mediator with.
*/
suspend fun updateMediatorWithDID(did: DID) {
connectionManager.mediationHandler.updateKeyListWithDIDs(arrayOf(did))
}
fun setupMediatorHandler(mediatorHandler: MediationHandler) {
stop()
this.connectionManager =
ConnectionManager(mercury, castor, pluto, mediatorHandler, mutableListOf())
}
/**
* Sets up a mediator DID for communication with the specified DID.
*
* @param did The DID of the mediator to set up.
*/
suspend fun setupMediatorDID(did: DID) {
val tmpMediatorHandler = BasicMediatorHandler(
mediatorDID = did,
mercury = mercury,
store = BasicMediatorHandler.PlutoMediatorRepositoryImpl(pluto)
)
setupMediatorHandler(tmpMediatorHandler)
}
/**
* This method fetches a DIDInfo from local storage.
*
* @param did The DID to fetch the info for
* @return A PrismDIDInfo if existent, null otherwise
*/
suspend fun getDIDInfo(did: DID): PrismDIDInfo? {
return pluto.getDIDInfoByDID(did)
.first()
}
/**
* This method registers a DID pair into the local database.
*
* @param pair The DIDPair to be stored
*/
fun registerDIDPair(pair: DIDPair) {
pluto.storeDIDPair(pair.host, pair.receiver, pair.name ?: "")
}
/**
* This method returns all DID pairs
*
* @return A list of the store DID pair
*/
suspend fun getAllDIDPairs(): List<DIDPair> {
return pluto.getAllDidPairs().first()
}
/**
* This method returns all registered PeerDIDs.
*
* @return A list of the stored PeerDIDs
*/
suspend fun getAllRegisteredPeerDIDs(): List<PeerDID> {
return pluto.getAllPeerDIDs().first()
}
// Messages related actions
/**
* Sends a DIDComm message through HTTP using mercury and returns a message if this is returned immediately by the REST endpoint.
*
* @param message The message to be sent
* @return The message sent if successful, null otherwise
*/
suspend fun sendMessage(message: Message): Message? {
return connectionManager?.sendMessage(message)
}
// Credentials related actions
/**
* This function will use the provided DID to sign a given message.
*
* @param did The DID which will be used to sign the message.
* @param message The message to be signed.
* @return The signature of the message.
*/
@Throws(PrismAgentError.CannotFindDIDPrivateKey::class)
suspend fun signWith(did: DID, message: ByteArray): Signature {
val privateKey =
pluto.getDIDPrivateKeysByDID(did).first().first()
?: throw PrismAgentError.CannotFindDIDPrivateKey(did.toString())
return apollo.signMessage(privateKey, message)
}
/**
* This function prepares a request credential from an offer given the subject DID.
* @param did Subject DID.
* @param offer Received offer credential.
* @return Created request credential.
* @throws [PolluxError.InvalidPrismDID] if there is a problem creating the request credential.
**/
@Throws(PolluxError.InvalidPrismDID::class)
suspend fun prepareRequestCredentialWithIssuer(
did: DID,
offer: OfferCredential
): RequestCredential {
if (did.method != "prism") {
throw PolluxError.InvalidPrismDID()
}
val privateKeyKeyPath = pluto.getPrismDIDKeyPathIndex(did).first()
val privateKey =
apollo.createKeyPair(seed, KeyCurve(Curve.SECP256K1, privateKeyKeyPath)).privateKey
val offerDataString = offer.attachments.mapNotNull {
when (it.data) {
is AttachmentJsonData -> it.data.data
else -> null
}
}.first()
val offerJsonObject = Json.parseToJsonElement(offerDataString).jsonObject
val jwtString = pollux.createRequestCredentialJWT(did, privateKey, offerJsonObject)
val attachmentDescriptor =
AttachmentDescriptor(
mediaType = "prism/jwt",
data = AttachmentBase64(jwtString.base64UrlEncoded)
)
return RequestCredential(
from = offer.to,
to = offer.from,
thid = offer.thid,
body = RequestCredential.Body(
offer.body.goalCode,
offer.body.comment,
offer.body.formats
),
attachments = arrayOf(attachmentDescriptor)
)
}
/**
* This function parses an issued credential message, stores, and returns the verifiable credential.
* @param message Issue credential Message.
* @return The parsed verifiable credential.
* @throws PrismAgentError if there is a problem parsing the credential.
*/
fun processIssuedCredentialMessage(message: IssueCredential): VerifiableCredential {
val attachment = message.attachments.firstOrNull()?.data as? AttachmentBase64
val jwtString = attachment?.let { it.base64.base64UrlDecoded }
return jwtString?.let {
val credential = pollux.parseVerifiableCredential(it)
pluto.storeCredential(credential)
return credential
} ?: throw UnknownError("Cannot find attachment base64 in message")
}
// Message Events
/**
* Start fetching the messages from the mediator.
*/
@JvmOverloads
fun startFetchingMessages(requestInterval: Int = 5) {
if (fetchingMessagesJob == null) {
fetchingMessagesJob = prismAgentScope.launch {
while (true) {
connectionManager.awaitMessages().collect { array ->
val messagesIds = mutableListOf<String>()
val messages = mutableListOf<Message>()
array.map { pair ->
messagesIds.add(pair.first)
messages.add(pair.second)
}
if (messagesIds.isNotEmpty()) {
connectionManager.mediationHandler.registerMessagesAsRead(
messagesIds.toTypedArray()
)
pluto.storeMessages(messages)
}
}
delay(Duration.ofSeconds(requestInterval.toLong()).toMillis())
}
}
}
fetchingMessagesJob?.let {
if (it.isActive) return
it.start()
}
}
/**
* Stop fetching messages
*/
fun stopFetchingMessages() {
fetchingMessagesJob?.cancel()
}
/**
* Handles the messages events and return a publisher of the messages.
*
* @return [Flow] of [Message].
*/
fun handleMessagesEvents(): Flow<List<Message>> {
return pluto.getAllMessages()
}
/**
* Handles the received messages events and return a publisher of the messages.
*
* @return [Flow] of [Message].
*/
fun handleReceivedMessagesEvents(): Flow<List<Message>> {
return pluto.getAllMessagesReceived()
}
// Invitation functionalities
/**
* Parses the given string as an invitation
* @param str The string to parse
* @return The parsed invitation [InvitationType]
* @throws [PrismAgentError.UnknownInvitationTypeError] if the invitation is not a valid Prism or OOB type
*/
@Throws(PrismAgentError.UnknownInvitationTypeError::class)
suspend fun parseInvitation(str: String): InvitationType {
Url.parse(str)?.let {
return parseOOBInvitation(it)
} ?: run {
try {
val json = Json.decodeFromString<JsonObject>(str)
val typeString: String = if (json.containsKey("type")) {
json["type"].toString().trim('"')
} else {
""
}
val invite: InvitationType = when (findProtocolTypeByValue(typeString)) {
ProtocolType.PrismOnboarding -> parsePrismInvitation(str)
ProtocolType.Didcomminvitation -> parseOOBInvitation(str)
else ->
throw PrismAgentError.UnknownInvitationTypeError()
}
return invite
} catch (e: SerializationException) {
throw PrismAgentError.UnknownInvitationTypeError()
}
}
}
/**
* Parses the given string as a Prism Onboarding invitation
* @param str The string to parse
* @return The parsed Prism Onboarding invitation
* @throws [PrismAgentError.UnknownInvitationTypeError] if the string is not a valid Prism Onboarding invitation
*/
@Throws(PrismAgentError.UnknownInvitationTypeError::class)
private suspend fun parsePrismInvitation(str: String): PrismOnboardingInvitation {
try {
val prismOnboarding = PrismOnboardingInvitation.fromJsonString(str)
val url = prismOnboarding.onboardEndpoint
val did = createNewPeerDID(
arrayOf(
DIDDocument.Service(
id = DIDCOMM1,
type = arrayOf(DIDCOMM_MESSAGING),
serviceEndpoint = DIDDocument.ServiceEndpoint(
uri = url,
accept = arrayOf(DIDCOMM_MESSAGING),
routingKeys = arrayOf()
)
)
),
false
)
prismOnboarding.from = did
return prismOnboarding
} catch (e: Exception) {
throw PrismAgentError.UnknownInvitationTypeError()
}
}
/**
* Parses the given string as an Out-of-Band invitation
* @param str The string to parse
* @returns The parsed Out-of-Band invitation
* @throws [PrismAgentError.UnknownInvitationTypeError] if the string is not a valid URL
*/
@Throws(PrismAgentError.UnknownInvitationTypeError::class)
private suspend fun parseOOBInvitation(str: String): OutOfBandInvitation {
return try {
Json.decodeFromString(str)
} catch (ex: SerializationException) {
throw PrismAgentError.UnknownInvitationTypeError()
}
}
/**
* Parses the given URL as an Out-of-Band invitation
* @param url The URL to parse
* @return The parsed Out-of-Band invitation
* @throws [PrismAgentError.UnknownInvitationTypeError] if the URL is not a valid Out-of-Band invitation
*/
private suspend fun parseOOBInvitation(url: Url): OutOfBandInvitation {
return DIDCommInvitationRunner(url).run()
}
/**
* Accepts an Out-of-Band (DIDComm) invitation and establishes a new connection
* @param invitation The Out-of-Band invitation to accept
* @throws [PrismAgentError.NoMediatorAvailableError] if there is no mediator available or other errors occur during the acceptance process
*/
suspend fun acceptOutOfBandInvitation(invitation: OutOfBandInvitation) {
val ownDID = createNewPeerDID(updateMediator = true)
val pair = DIDCommConnectionRunner(invitation, pluto, ownDID, connectionManager).run()
connectionManager.addConnection(pair)
}
/**
* Accepts a Prism Onboarding invitation and performs the onboarding process
* @param invitation The Prism Onboarding invitation to accept
* @throws [PrismAgentError.FailedToOnboardError] if failed to on board
*/
@Throws(PrismAgentError.FailedToOnboardError::class)
suspend fun acceptInvitation(invitation: PrismOnboardingInvitation) {
@Serializable
data class SendDID(val did: String)
val response = api.request(
HttpMethod.Post.toString(),
invitation.onboardEndpoint,
arrayOf(),
arrayOf(),
SendDID(invitation.from.toString())
)
if (response.status != 200) {
throw PrismAgentError.FailedToOnboardError(response.status, response.jsonString)
}
}
/**
* This method returns a list of all the VerifiableCredentials stored locally.
*/
suspend fun getAllVerifiableCredentials(): List<VerifiableCredential> {
return pluto.getAllCredentials().first()
}
// Proof related actions
/**
* This function creates a Presentation from a request verification.
* @param request Request message received.
* @param credential Verifiable Credential to present.
* @return Presentation message prepared to send.
* @throws PrismAgentError if there is a problem creating the presentation.
**/
@Throws(PolluxError.InvalidPrismDID::class)
suspend fun preparePresentationForRequestProof(
request: RequestPresentation,
credential: VerifiableCredential
): Presentation {
val subjectDID = DID(credential.credentialSubject)
if (subjectDID.method != PRISM) {
throw PolluxError.InvalidPrismDID()
}
val privateKeyKeyPath = pluto.getPrismDIDKeyPathIndex(subjectDID).first()
val privateKey =
apollo.createKeyPair(seed, KeyCurve(Curve.SECP256K1, privateKeyKeyPath)).privateKey
val requestData = request.attachments.mapNotNull {
when (it.data) {
is AttachmentJsonData -> it.data.data
else -> null
}
}.first()
val requestJsonObject = Json.parseToJsonElement(requestData).jsonObject
val jwtString = pollux.createVerifiablePresentationJWT(
subjectDID,
privateKey,
credential,
requestJsonObject
)
val attachmentDescriptor =
AttachmentDescriptor(
mediaType = JWT_MEDIA_TYPE,
data = AttachmentBase64(jwtString.base64UrlEncoded)
)
return Presentation(
from = request.to,
to = request.from,
thid = request.thid,
body = Presentation.Body(request.body.goalCode, request.body.comment),
attachments = arrayOf(attachmentDescriptor)
)
}
/**
* Enumeration representing the current state of the agent.
*/
enum class State {
STOPPED, STARTING, RUNNING, STOPPING
}
}