diff --git a/__tests__/shared/saveClaims.ts b/__tests__/shared/saveClaims.ts index 0561a8d34..b3fd7c836 100644 --- a/__tests__/shared/saveClaims.ts +++ b/__tests__/shared/saveClaims.ts @@ -101,5 +101,18 @@ export default (testContext: { }) expect(credentials).toHaveLength(2) }) + + it('should be able to delete credential', async () => { + const credentials = await agent.dataStoreORMGetVerifiableCredentials() + expect(credentials).toHaveLength(3) + + const result = await agent.dataStoreDeleteVerifiableCredential({hash: credentials[0].hash}) + expect(result).toEqual(true) + + const credentials2 = await agent.dataStoreORMGetVerifiableCredentials() + expect(credentials2).toHaveLength(2) + + }) + }) } diff --git a/packages/core/plugin.schema.json b/packages/core/plugin.schema.json index a1c1c112f..6f243ca48 100644 --- a/packages/core/plugin.schema.json +++ b/packages/core/plugin.schema.json @@ -1308,6 +1308,19 @@ "IDataStore": { "components": { "schemas": { + "IDataStoreDeleteVerifiableCredentialArgs": { + "type": "object", + "properties": { + "hash": { + "type": "string", + "description": "Required. VerifiableCredential hash" + } + }, + "required": [ + "hash" + ], + "description": "Input arguments for {@link IDataStore.IDataStoreDeleteVerifiableCredentialArgs| IDataStoreDeleteVerifiableCredentialArgs }" + }, "IDataStoreGetMessageArgs": { "type": "object", "properties": { @@ -1631,6 +1644,15 @@ } }, "methods": { + "dataStoreDeleteVerifiableCredential": { + "description": "Deletes verifiable credential from the data store", + "arguments": { + "$ref": "#/components/schemas/IDataStoreDeleteVerifiableCredentialArgs" + }, + "returnType": { + "type": "boolean" + } + }, "dataStoreGetMessage": { "description": "Gets message from the data store", "arguments": { diff --git a/packages/core/src/types/IDataStore.ts b/packages/core/src/types/IDataStore.ts index 1cda791ad..02849869c 100644 --- a/packages/core/src/types/IDataStore.ts +++ b/packages/core/src/types/IDataStore.ts @@ -45,6 +45,17 @@ export interface IDataStoreGetVerifiableCredentialArgs { hash: string } +/** + * Input arguments for {@link IDataStore.IDataStoreDeleteVerifiableCredentialArgs | IDataStoreDeleteVerifiableCredentialArgs} + * @public + */ +export interface IDataStoreDeleteVerifiableCredentialArgs { + /** + * Required. VerifiableCredential hash + */ + hash: string +} + /** * Input arguments for {@link IDataStore.dataStoreSaveVerifiablePresentation | dataStoreSaveVerifiablePresentation} * @public @@ -93,6 +104,14 @@ export interface IDataStore extends IPluginMethodMap { */ dataStoreSaveVerifiableCredential(args: IDataStoreSaveVerifiableCredentialArgs): Promise + + /** + * Deletes verifiable credential from the data store + * @param args - verifiable credential + * @returns a promise that resolves to a boolean + */ + dataStoreDeleteVerifiableCredential(args: IDataStoreDeleteVerifiableCredentialArgs): Promise + /** * Gets verifiable credential from the data store * @param args - arguments for getting verifiable credential diff --git a/packages/data-store/src/data-store.ts b/packages/data-store/src/data-store.ts index dfd856eaf..21f520f7e 100644 --- a/packages/data-store/src/data-store.ts +++ b/packages/data-store/src/data-store.ts @@ -11,9 +11,11 @@ import { VerifiableCredential, VerifiablePresentation, schema, + IDataStoreDeleteVerifiableCredentialArgs, } from '@veramo/core' import { Message, createMessageEntity, createMessage } from './entities/message' import { Credential, createCredentialEntity } from './entities/credential' +import { Claim } from './entities/claim' import { Presentation, createPresentationEntity } from './entities/presentation' import { Connection } from 'typeorm' @@ -28,6 +30,7 @@ export class DataStore implements IAgentPlugin { this.methods = { dataStoreSaveMessage: this.dataStoreSaveMessage.bind(this), dataStoreGetMessage: this.dataStoreGetMessage.bind(this), + dataStoreDeleteVerifiableCredential: this.dataStoreDeleteVerifiableCredential.bind(this), dataStoreSaveVerifiableCredential: this.dataStoreSaveVerifiableCredential.bind(this), dataStoreGetVerifiableCredential: this.dataStoreGetVerifiableCredential.bind(this), dataStoreSaveVerifiablePresentation: this.dataStoreSaveVerifiablePresentation.bind(this), @@ -53,6 +56,26 @@ export class DataStore implements IAgentPlugin { } } + async dataStoreDeleteVerifiableCredential( + args: IDataStoreDeleteVerifiableCredentialArgs, + ): Promise { + const credentialEntity = await (await this.dbConnection) + .getRepository(Credential) + .findOneOrFail(args.hash) + + const claims = await (await this.dbConnection) + .getRepository(Claim) + .find({ where: [{ credential: credentialEntity }] }) + + claims.forEach(async (claim) => { + await claim.remove() + }) + + await (await this.dbConnection).getRepository(Credential).remove(credentialEntity) + + return true + } + async dataStoreSaveVerifiableCredential(args: IDataStoreSaveVerifiableCredentialArgs): Promise { const verifiableCredential = await (await this.dbConnection) .getRepository(Credential)