Skip to content

Commit

Permalink
fix: delete credentials (#766)
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Richardson <mike.richardson@northernblock.io>
  • Loading branch information
NB-MikeRichardson authored May 11, 2022
1 parent 2ad600c commit cbdff28
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
12 changes: 8 additions & 4 deletions packages/core/src/modules/credentials/CredentialsModule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AgentMessage } from '../../agent/AgentMessage'
import type { Logger } from '../../logger'
import type { DeleteCredentialOptions } from './CredentialServiceOptions'
import type {
AcceptOfferOptions,
AcceptProposalOptions,
Expand Down Expand Up @@ -62,7 +63,7 @@ export interface CredentialsModule {
getAll(): Promise<CredentialExchangeRecord[]>
getById(credentialRecordId: string): Promise<CredentialExchangeRecord>
findById(credentialRecordId: string): Promise<CredentialExchangeRecord | null>
deleteById(credentialRecordId: string): Promise<void>
deleteById(credentialRecordId: string, options?: DeleteCredentialOptions): Promise<void>
}

@scoped(Lifecycle.ContainerScoped)
Expand Down Expand Up @@ -501,14 +502,17 @@ export class CredentialsModule implements CredentialsModule {
public findById(credentialRecordId: string): Promise<CredentialExchangeRecord | null> {
return this.credentialRepository.findById(credentialRecordId)
}

/**
* Delete a credential record by id
* Delete a credential record by id, also calls service to delete from wallet
*
* @param credentialId the credential record id
* @param options the delete credential options for the delete operation
*/
public async deleteById(credentialId: string) {
public async deleteById(credentialId: string, options?: DeleteCredentialOptions) {
const credentialRecord = await this.getById(credentialId)
return this.credentialRepository.delete(credentialRecord)
const service = this.getService(credentialRecord.protocolVersion)
return service.deleteById(credentialId, options)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import type {
} from '../../../CredentialsModuleOptions'
import type { CredPropose } from '../../../formats/models/CredPropose'

import { AriesFrameworkError } from '../../../../../../src/error/AriesFrameworkError'
import { DidCommMessageRepository } from '../../../../../../src/storage'
import { setupCredentialTests, waitForCredentialRecord } from '../../../../../../tests/helpers'
import { issueCredential, setupCredentialTests, waitForCredentialRecord } from '../../../../../../tests/helpers'
import testLogger from '../../../../../../tests/logger'
import { AriesFrameworkError } from '../../../../../error/AriesFrameworkError'
import { IndyHolderService } from '../../../../../modules/indy/services/IndyHolderService'
import { DidCommMessageRepository } from '../../../../../storage'
import { JsonTransformer } from '../../../../../utils'
import { CredentialProtocolVersion } from '../../../CredentialProtocolVersion'
import { CredentialState } from '../../../CredentialState'
Expand Down Expand Up @@ -381,6 +382,38 @@ describe('credentials', () => {
}
})

test('Faber Issues Credential which is then deleted from Alice`s wallet', async () => {
const credentialPreview = V2CredentialPreview.fromRecord({
name: 'John',
age: '99',
'x-ray': 'some x-ray',
profile_picture: 'profile picture',
})

const { holderCredential } = await issueCredential({
issuerAgent: faberAgent,
issuerConnectionId: faberConnection.id,
holderAgent: aliceAgent,
credentialTemplate: {
credentialDefinitionId: credDefId,
comment: 'some comment about credential',
preview: credentialPreview,
},
})
// test that delete credential removes from both repository and wallet
// latter is tested by spying on holder service (Indy) to
// see if deleteCredential is called
const holderService = aliceAgent.injectionContainer.resolve(IndyHolderService)

const deleteCredentialSpy = jest.spyOn(holderService, 'deleteCredential')
await aliceAgent.credentials.deleteById(holderCredential.id, { deleteAssociatedCredentials: true })
expect(deleteCredentialSpy).toHaveBeenCalledTimes(1)

return expect(aliceAgent.credentials.getById(holderCredential.id)).rejects.toThrowError(
`CredentialRecord: record with id ${holderCredential.id} not found.`
)
})

test('Alice starts with propose - Faber counter offer - Alice second proposal- Faber sends second offer', async () => {
// proposeCredential -> negotiateProposal -> negotiateOffer -> negotiateProposal -> acceptOffer -> acceptRequest -> DONE (credential issued)
const credentialPreview = V2CredentialPreview.fromRecord({
Expand Down

0 comments on commit cbdff28

Please sign in to comment.