Skip to content
Snippets Groups Projects
Commit 06a96f2c authored by Berend Sliedrecht's avatar Berend Sliedrecht
Browse files

feat(ssi): block connection by id or did


Signed-off-by: default avatarBerend Sliedrecht <berend@animo.id>
parent fdcce1c0
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@ import {
} from '@aries-framework/core';
import {
EventDidcommConnectionsBlock,
EventDidcommConnectionsCreateWithSelf,
EventDidcommConnectionsGetAll,
EventDidcommConnectionsGetById,
......@@ -49,4 +50,21 @@ describe('Connection Events', () => {
state: DidExchangeState.Completed,
});
});
it('should create a new connections block event', () => {
const event = new EventDidcommConnectionsBlock(
new ConnectionRecord({
role: DidExchangeRole.Requester,
state: DidExchangeState.Completed,
}),
);
expect(typeof event.id).toStrictEqual('string');
expect(event.type).toStrictEqual('EventDidcommConnectionsBlock');
expect(event.timestamp).toBeInstanceOf(Date);
expect(event.instance).toMatchObject({
role: DidExchangeRole.Requester,
state: DidExchangeState.Completed,
});
});
});
......@@ -55,9 +55,7 @@ export class EventDidcommConnectionsCreateWithSelf extends BaseEvent<ConnectionR
public static token = 'didcomm.connections.createWithSelf';
public get instance() {
return JsonTransformer.fromJSON(this.data, ConnectionRecord, {
validate: true,
});
return JsonTransformer.fromJSON(this.data, ConnectionRecord);
}
public static fromEvent(e: EventDidcommConnectionsCreateWithSelf) {
......@@ -69,3 +67,17 @@ export class EventDidcommConnectionsCreateWithSelf extends BaseEvent<ConnectionR
);
}
}
export class EventDidcommConnectionsBlock extends BaseEvent<ConnectionRecord | null> {
public static token = 'didcomm.connections.block';
public get instance() {
return this.data
? JsonTransformer.fromJSON(this.data, ConnectionRecord)
: null;
}
public static fromEvent(e: EventDidcommConnectionsBlock) {
return new EventDidcommConnectionsBlock(e.data, e.id, e.type, e.timestamp);
}
}
......@@ -203,6 +203,11 @@ export class AgentService implements OnApplicationShutdown {
public async onApplicationShutdown() {
if (!this.agent.isInitialized) return;
await this.agent.shutdown();
// If we cannot shutdown the wallet on application shutdown, no error will occur
// This is done because the Askar shutdown procedure is a bit buggy
try {
await this.agent.shutdown();
// eslint-disable-next-line no-empty
} catch {}
}
}
......@@ -4,6 +4,7 @@ import {
EventDidcommConnectionsGetById,
EventDidcommConnectionsGetAll,
EventDidcommConnectionsCreateWithSelf,
EventDidcommConnectionsBlock,
} from '@ocm/shared';
import { ConnectionsService } from './connections.service.js';
......@@ -36,4 +37,15 @@ export class ConnectionsController {
await this.connectionsService.createConnectionWithSelf(),
);
}
@MessagePattern(EventDidcommConnectionsBlock.token)
public async blockConnection({
idOrDid,
}: {
idOrDid: string;
}): Promise<EventDidcommConnectionsBlock> {
return new EventDidcommConnectionsBlock(
await this.connectionsService.blockByIdOrDid(idOrDid),
);
}
}
......@@ -9,6 +9,7 @@ import {
ConnectionRepository,
DidExchangeState,
} from '@aries-framework/core';
import { isDid } from '@aries-framework/core/build/utils/did.js';
import { Injectable } from '@nestjs/common';
import { MetadataTokens } from '../../common/constants.js';
......@@ -30,6 +31,37 @@ export class ConnectionsService {
return await this.agent.connections.findById(id);
}
public async blockByIdOrDid(
idOrDid: string,
): Promise<ConnectionRecord | null> {
if (isDid(idOrDid)) {
const records = await this.agent.connections.findAllByQuery({
theirDid: idOrDid,
});
if (records.length === 0) {
return null;
}
if (records.length > 1) {
throw new Error(
'Found multiple records with the same DID. This should not be possible',
);
}
await this.agent.connections.deleteById(records[0].id);
return records[0];
}
const record = await this.agent.connections.findById(idOrDid);
if (!record) return null;
await this.agent.connections.deleteById(record.id);
return record;
}
public async createConnectionWithSelf(): Promise<ConnectionRecord> {
const outOfBandRecord = await this.agent.oob.createInvitation();
const invitation = outOfBandRecord.outOfBandInvitation;
......
......@@ -8,6 +8,7 @@ import {
EventDidcommConnectionsGetById,
EventDidcommConnectionsGetAll,
EventDidcommConnectionsCreateWithSelf,
EventDidcommConnectionsBlock,
} from '@ocm/shared';
import { firstValueFrom } from 'rxjs';
......@@ -83,4 +84,16 @@ describe('Connections', () => {
);
expect(metadata).toMatchObject({ trusted: true });
});
it(EventDidcommConnectionsBlock.token, async () => {
const response$: Observable<EventDidcommConnectionsBlock> = client.send(
EventDidcommConnectionsBlock.token,
{ idOrDid: 'some-id' },
);
const response = await firstValueFrom(response$);
const eventInstance = EventDidcommConnectionsBlock.fromEvent(response);
expect(eventInstance.instance).toBeNull();
});
});
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