-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement ParticipantContextEventCoordinator (#265)
* renamed event listener impls to *EventPublisher * feat: implement ParticipantContextEventCoordinator * run ci * DEPENDENCIES
- Loading branch information
1 parent
f0a22c7
commit 13e6bde
Showing
12 changed files
with
263 additions
and
26 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
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
73 changes: 73 additions & 0 deletions
73
...va/org/eclipse/edc/identityhub/participantcontext/ParticipantContextEventCoordinator.java
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,73 @@ | ||
/* | ||
* Copyright (c) 2024 Metaform Systems, Inc. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Metaform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.identityhub.participantcontext; | ||
|
||
import org.eclipse.edc.iam.did.spi.document.DidDocument; | ||
import org.eclipse.edc.identithub.did.spi.DidDocumentService; | ||
import org.eclipse.edc.identityhub.spi.KeyPairService; | ||
import org.eclipse.edc.identityhub.spi.events.participant.ParticipantContextCreated; | ||
import org.eclipse.edc.spi.event.Event; | ||
import org.eclipse.edc.spi.event.EventEnvelope; | ||
import org.eclipse.edc.spi.event.EventSubscriber; | ||
import org.eclipse.edc.spi.monitor.Monitor; | ||
|
||
import static org.eclipse.edc.spi.result.ServiceResult.success; | ||
|
||
/** | ||
* Coordinates {@link ParticipantContextCreated} events. More specifically, it coordinates the sequence, in which the following actions are performed: | ||
* <ul> | ||
* <li>Create DID Document</li> | ||
* <li>Optional: publish DID document</li> | ||
* <li>Add a KeyPair</li> | ||
* </ul> | ||
* To that end, the {@link ParticipantContextEventCoordinator} directly collaborates with the {@link KeyPairService} and the {@link DidDocumentService}. | ||
* <p> | ||
* Please note that once this initial sequence is executed, every collaborator service emits their events as per their event contract. | ||
* For example, once a KeyPair is added, the {@link KeyPairService} will emit a {@link org.eclipse.edc.identityhub.spi.events.keypair.KeyPairAdded} event. The {@link DidDocumentService} | ||
* can then react to this event by updating the DID Document. | ||
*/ | ||
public class ParticipantContextEventCoordinator implements EventSubscriber { | ||
private final Monitor monitor; | ||
private final DidDocumentService didDocumentService; | ||
private final KeyPairService keyPairService; | ||
|
||
public ParticipantContextEventCoordinator(Monitor monitor, DidDocumentService didDocumentService, KeyPairService keyPairService) { | ||
this.monitor = monitor; | ||
this.didDocumentService = didDocumentService; | ||
this.keyPairService = keyPairService; | ||
} | ||
|
||
@Override | ||
public <E extends Event> void on(EventEnvelope<E> event) { | ||
var payload = event.getPayload(); | ||
if (payload instanceof ParticipantContextCreated createdEvent) { | ||
var manifest = createdEvent.getManifest(); | ||
var doc = DidDocument.Builder.newInstance() | ||
.id(manifest.getDid()) | ||
.service(manifest.getServiceEndpoints().stream().toList()) | ||
// updating and adding a verification method happens as a result of the KeyPairAddedEvent | ||
.build(); | ||
|
||
didDocumentService.store(doc, manifest.getParticipantId()) | ||
.compose(u -> manifest.isActive() ? didDocumentService.publish(doc.getId()) : success()) | ||
// adding the keypair event will cause the DidDocumentService to update the DID. | ||
.compose(u -> keyPairService.addKeyPair(createdEvent.getParticipantId(), createdEvent.getManifest().getKey(), true)) | ||
.onFailure(f -> monitor.warning("%s".formatted(f.getFailureDetail()))); | ||
|
||
} else { | ||
monitor.warning("Received event with unexpected payload type: %s".formatted(payload.getClass())); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.