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 { TenantAgent } from '../agent.service.js';
import type { CredentialExchangeRecord } from '@credo-ts/core'; import type {
CredentialExchangeRecord,
CredentialStateChangedEvent,
} from '@credo-ts/core';
import type { import type {
EventAnonCredsCredentialOfferGetAll, EventAnonCredsCredentialOfferGetAll,
EventAnonCredsCredentialOfferGetAllInput, EventAnonCredsCredentialOfferGetAllInput,
...@@ -19,17 +22,25 @@ import type { ...@@ -19,17 +22,25 @@ import type {
EventDidcommAnonCredsCredentialsOfferToSelfInput, EventDidcommAnonCredsCredentialsOfferToSelfInput,
} from '@ocm/shared'; } 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 { GenericRecord } from '@credo-ts/core/build/modules/generic-records/repository/GenericRecord.js';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { logger } from '@ocm/shared'; import { logger } from '@ocm/shared';
import { GenericRecordTokens, MetadataTokens } from '../../common/constants.js'; import { GenericRecordTokens, MetadataTokens } from '../../common/constants.js';
import { AgentService } from '../agent.service.js';
import { WithTenantService } from '../withTenantService.js'; import { WithTenantService } from '../withTenantService.js';
@Injectable() @Injectable()
export class AnonCredsCredentialsService { export class AnonCredsCredentialsService {
public constructor(private withTenantService: WithTenantService) {} public constructor(
private withTenantService: WithTenantService,
private agentService: AgentService,
) {}
public async getAll({ public async getAll({
tenantId, tenantId,
...@@ -193,7 +204,50 @@ export class AnonCredsCredentialsService { ...@@ -193,7 +204,50 @@ export class AnonCredsCredentialsService {
const revocationRegistryIndex = await this.getNextRevocationIdx(t); 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', protocolVersion: 'v2',
autoAcceptCredential: AutoAcceptCredential.Always, autoAcceptCredential: AutoAcceptCredential.Always,
connectionId: connection.id, connectionId: connection.id,
...@@ -206,6 +260,10 @@ export class AnonCredsCredentialsService { ...@@ -206,6 +260,10 @@ export class AnonCredsCredentialsService {
}, },
}, },
}); });
await acceptOfferListener;
return waitUntilDone;
}); });
} }
......
...@@ -145,7 +145,17 @@ export class ConnectionsService { ...@@ -145,7 +145,17 @@ export class ConnectionsService {
const outOfBandRecord = await t.oob.createInvitation(); const outOfBandRecord = await t.oob.createInvitation();
const invitation = outOfBandRecord.outOfBandInvitation; 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) => return new Promise((resolve) =>
this.agent.events.on<ConnectionStateChangedEvent>( 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