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: delete credential from wallet #691

Merged
merged 12 commits into from
Apr 4, 2022
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
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@multiformats/base-x": "^4.0.1",
"@stablelib/ed25519": "^1.0.2",
"@stablelib/sha256": "^1.0.1",
"@types/indy-sdk": "^1.16.15",
"@types/indy-sdk": "^1.16.16",
"@types/node-fetch": "^2.5.10",
"@types/ws": "^7.4.4",
"abort-controller": "^3.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/modules/credentials/CredentialsModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ export class CredentialsModule {
*
* @param credentialId the credential record id
*/
public async deleteById(credentialId: string) {
return this.credentialService.deleteById(credentialId)
public async deleteById(credentialId: string, options?: { deleteAssociatedCredential: boolean }) {
return this.credentialService.deleteById(credentialId, options)
}

private registerHandlers(dispatcher: Dispatcher) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const mockCredentialRecord = ({
metadata,
threadId,
connectionId,
credentialId,
tags,
id,
credentialAttributes,
Expand All @@ -106,6 +107,7 @@ const mockCredentialRecord = ({
tags?: CustomCredentialTags
threadId?: string
connectionId?: string
credentialId?: string
id?: string
credentialAttributes?: CredentialPreviewAttribute[]
} = {}) => {
Expand All @@ -123,6 +125,7 @@ const mockCredentialRecord = ({
state: state || CredentialState.OfferSent,
threadId: threadId ?? offerMessage.id,
connectionId: connectionId ?? '123',
credentialId: credentialId ?? '123',
tags,
})

Expand Down Expand Up @@ -1087,6 +1090,29 @@ describe('CredentialService', () => {
})
})

describe('deleteCredential', () => {
it('should call delete from repository', async () => {
const credential = mockCredentialRecord()
mockFunction(credentialRepository.getById).mockReturnValue(Promise.resolve(credential))

const repositoryDeleteSpy = jest.spyOn(credentialRepository, 'delete')
await credentialService.deleteById(credential.id)
expect(repositoryDeleteSpy).toHaveBeenNthCalledWith(1, credential)
})

it('deleteAssociatedCredential parameter should call deleteCredential in indyHolderService with credentialId', async () => {
const storeCredentialMock = indyHolderService.deleteCredential as jest.Mock<Promise<void>, [string]>

const credential = mockCredentialRecord()
mockFunction(credentialRepository.getById).mockReturnValue(Promise.resolve(credential))

await credentialService.deleteById(credential.id, {
deleteAssociatedCredential: true,
})
expect(storeCredentialMock).toHaveBeenNthCalledWith(1, credential.credentialId)
})
})

describe('declineOffer', () => {
const threadId = 'fd9c5ddb-ec11-4acd-bc32-540736249754'
let credential: CredentialRecord
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,9 +804,14 @@ export class CredentialService {
*
* @param credentialId the credential record id
*/
public async deleteById(credentialId: string) {
public async deleteById(credentialId: string, options?: DeleteCredentialOptions): Promise<void> {
const credentialRecord = await this.getById(credentialId)
return this.credentialRepository.delete(credentialRecord)

await this.credentialRepository.delete(credentialRecord)

if (options?.deleteAssociatedCredential && credentialRecord.credentialId) {
await this.indyHolderService.deleteCredential(credentialRecord.credentialId)
}
}

/**
Expand Down Expand Up @@ -852,6 +857,10 @@ export class CredentialService {
}
}

export interface DeleteCredentialOptions {
deleteAssociatedCredential: boolean
}

export interface CredentialProtocolMsgReturnType<MessageType extends AgentMessage> {
message: MessageType
credentialRecord: CredentialRecord
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/modules/indy/services/IndyHolderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,24 @@ export class IndyHolderService {
}
}

/**
* Delete a credential stored in the wallet by id.
*
* @param credentialId the id (referent) of the credential
*
*/
public async deleteCredential(credentialId: Indy.CredentialId): Promise<void> {
try {
return await this.indy.proverDeleteCredential(this.wallet.handle, credentialId)
} catch (error) {
this.logger.error(`Error deleting Indy Credential from Wallet`, {
error,
})

throw isIndyError(error) ? new IndySdkError(error) : error
}
}

private async fetchCredentialsForReferent(searchHandle: number, referent: string, limit?: number) {
try {
let credentials: Indy.IndyCredential[] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const IndyHolderService = jest.fn(() => ({
storeCredential: jest.fn(({ credentialId }: StoreCredentialOptions) =>
Promise.resolve(credentialId ?? 'some-random-uuid')
),

deleteCredential: jest.fn(() => Promise.resolve()),
createCredentialRequest: jest.fn(({ holderDid, credentialDefinition }: CreateCredentialRequestOptions) =>
Promise.resolve([
{
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"events": "^3.3.0"
},
"devDependencies": {
"@types/indy-sdk-react-native": "npm:@types/indy-sdk@^1.16.15",
"@types/indy-sdk-react-native": "npm:@types/indy-sdk@^1.16.16",
"@types/react-native": "^0.64.10",
"indy-sdk-react-native": "^0.1.16",
"react": "17.0.1",
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2242,7 +2242,7 @@
dependencies:
"@types/node" "*"

"@types/indy-sdk-react-native@npm:@types/indy-sdk@^1.16.15", "@types/indy-sdk@^1.16.15":
"@types/indy-sdk-react-native@npm:@types/indy-sdk@^1.16.16", "@types/indy-sdk@^1.16.16":
version "1.16.16"
resolved "https://registry.yarnpkg.com/@types/indy-sdk/-/indy-sdk-1.16.16.tgz#dc9e51c61c266eff616991fa7df88309975d9f1e"
integrity sha512-bSWG56bAFvjTTLHJoGEXylbbN533g9XEtOTPS9G1EvIv5AsVcy/OIZOkXXElbzvYdZ1Q+qCxZDD5T0OqMYNmjg==
Expand Down