Skip to content
Snippets Groups Projects
Commit 6b1697e3 authored by Steffen Schulze's avatar Steffen Schulze
Browse files

Merge branch 'offer-to-self-event' into 'main'

fix(ssi-abstraction): offer to self uses listener now

See merge request eclipse/xfsc/ocm/ocm-engine!38
parents 617a7fa4 ee9e5819
No related branches found
No related tags found
No related merge requests found
Pipeline #40114 failed
......@@ -34,11 +34,15 @@ import { Injectable } from '@nestjs/common';
import { logger } from '@ocm/shared';
import { GenericRecordTokens, MetadataTokens } from '../../common/constants.js';
import { AgentService } from '../agent.service.js';
import { WithTenantService } from '../withTenantService.js';
@Injectable()
export class AnonCredsCredentialsService {
public constructor(private withTenantService: WithTenantService) {}
public constructor(
private withTenantService: WithTenantService,
private agentService: AgentService,
) {}
public async getAll({
tenantId,
......@@ -215,36 +219,50 @@ export class AnonCredsCredentialsService {
const revocationRegistryIndex = await this.getNextRevocationIdx(t);
const acceptOfferListener: Promise<CredentialExchangeRecord> =
new Promise((resolve) =>
t.events.on<CredentialStateChangedEvent>(
CredentialEventTypes.CredentialStateChanged,
async ({ payload: { credentialRecord } }) => {
const connection = connections.find(
(c) => c.id === credentialRecord.connectionId,
);
const withSelf = connection?.metadata.get<{ withSelf: boolean }>(
MetadataTokens.CONNECTION_METADATA_KEY,
);
const isWithSelf = withSelf?.withSelf ?? false;
if (
credentialRecord.state === CredentialState.OfferReceived &&
isWithSelf
) {
resolve(
await t.credentials.acceptOffer({
credentialRecordId: credentialRecord.id,
}),
);
}
},
),
const acceptOfferListener = new Promise((resolve) => {
this.agentService.agent.events.on<CredentialStateChangedEvent>(
CredentialEventTypes.CredentialStateChanged,
async ({ payload: { credentialRecord } }) => {
const { connectionId } = credentialRecord;
if (
!connectionId ||
credentialRecord.state !== CredentialState.OfferReceived
) {
return;
}
const connectionRecord = await t.connections.getById(connectionId);
const metadata = connectionRecord.metadata.get<{
withSelf: boolean;
}>(MetadataTokens.CONNECTION_METADATA_KEY);
if (!metadata || metadata.withSelf === false) return;
await t.credentials.acceptOffer({
credentialRecordId: credentialRecord.id,
autoAcceptCredential: AutoAcceptCredential.Always,
});
resolve(connectionRecord);
},
);
});
await t.credentials.offerCredential({
const waitUntilDone = new Promise<CredentialExchangeRecord>((resolve) =>
this.agentService.agent.events.on<CredentialStateChangedEvent>(
CredentialEventTypes.CredentialStateChanged,
({ payload: { credentialRecord } }) => {
if (
credentialRecord.state === CredentialState.Done ||
credentialRecord.state === CredentialState.CredentialIssued
)
resolve(credentialRecord);
},
),
);
void t.credentials.offerCredential({
protocolVersion: 'v2',
autoAcceptCredential: AutoAcceptCredential.Always,
connectionId: connection.id,
......@@ -258,7 +276,9 @@ export class AnonCredsCredentialsService {
},
});
return acceptOfferListener;
await acceptOfferListener;
return waitUntilDone;
});
}
......
......@@ -152,6 +152,7 @@ export class ConnectionsService {
trusted: true,
withSelf: true,
});
const connRepo = t.dependencyManager.resolve(ConnectionRepository);
await connRepo.update(t.context, connectionRecord);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment