Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(data-store): delete verifiable credential #634

Merged
merged 1 commit into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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