Skip to content

Commit

Permalink
feat(data-store): delete verifiable credential (#634)
Browse files Browse the repository at this point in the history
fixes #635
  • Loading branch information
simonas-notcat authored Jul 23, 2021
1 parent 76e0133 commit c7b0131
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
13 changes: 13 additions & 0 deletions __tests__/shared/saveClaims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

})

})
}
22 changes: 22 additions & 0 deletions packages/core/plugin.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/types/IDataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -93,6 +104,14 @@ export interface IDataStore extends IPluginMethodMap {
*/
dataStoreSaveVerifiableCredential(args: IDataStoreSaveVerifiableCredentialArgs): Promise<string>


/**
* Deletes verifiable credential from the data store
* @param args - verifiable credential
* @returns a promise that resolves to a boolean
*/
dataStoreDeleteVerifiableCredential(args: IDataStoreDeleteVerifiableCredentialArgs): Promise<boolean>

/**
* Gets verifiable credential from the data store
* @param args - arguments for getting verifiable credential
Expand Down
23 changes: 23 additions & 0 deletions packages/data-store/src/data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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),
Expand All @@ -53,6 +56,26 @@ export class DataStore implements IAgentPlugin {
}
}

async dataStoreDeleteVerifiableCredential(
args: IDataStoreDeleteVerifiableCredentialArgs,
): Promise<boolean> {
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<string> {
const verifiableCredential = await (await this.dbConnection)
.getRepository(Credential)
Expand Down

0 comments on commit c7b0131

Please sign in to comment.