Skip to content
Snippets Groups Projects
Verified Commit 7f3dbbe2 authored by Berend Sliedrecht's avatar Berend Sliedrecht Committed by Konstantin Tsabolov
Browse files

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


Signed-off-by: default avatarBerend Sliedrecht <berend@animo.id>
parent 53b98bc9
No related branches found
No related tags found
1 merge request!38fix(ssi-abstraction): offer to self uses listener now
import type { TenantAgent } from '../agent.service.js';
import type { CredentialExchangeRecord } from '@credo-ts/core';
import type {
CredentialExchangeRecord,
CredentialStateChangedEvent,
} from '@credo-ts/core';
import type {
EventAnonCredsCredentialOfferGetAll,
EventAnonCredsCredentialOfferGetAllInput,
......@@ -19,17 +22,25 @@ import type {
EventDidcommAnonCredsCredentialsOfferToSelfInput,
} from '@ocm/shared';
import { AutoAcceptCredential, CredentialState } from '@credo-ts/core';
import {
AutoAcceptCredential,
CredentialState,
CredentialEventTypes,
} from '@credo-ts/core';
import { GenericRecord } from '@credo-ts/core/build/modules/generic-records/repository/GenericRecord.js';
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,
......@@ -193,7 +204,50 @@ export class AnonCredsCredentialsService {
const revocationRegistryIndex = await this.getNextRevocationIdx(t);
return t.credentials.offerCredential({
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);
},
);
});
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,
......@@ -206,6 +260,10 @@ export class AnonCredsCredentialsService {
},
},
});
await acceptOfferListener;
return waitUntilDone;
});
}
......
......@@ -145,7 +145,17 @@ export class ConnectionsService {
const outOfBandRecord = await t.oob.createInvitation();
const invitation = outOfBandRecord.outOfBandInvitation;
void t.oob.receiveInvitation(invitation);
const { connectionRecord } = await t.oob.receiveInvitation(invitation);
if (connectionRecord) {
connectionRecord.metadata.set(MetadataTokens.CONNECTION_METADATA_KEY, {
trusted: true,
withSelf: true,
});
const connRepo = t.dependencyManager.resolve(ConnectionRepository);
await connRepo.update(t.context, connectionRecord);
}
return new Promise((resolve) =>
this.agent.events.on<ConnectionStateChangedEvent>(
......
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