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(anoncreds): add getCredential(s) methods #1386

Merged
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
28 changes: 28 additions & 0 deletions packages/anoncreds-rs/src/services/AnonCredsRsHolderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
AnonCredsRequestedPredicateMatch,
AnonCredsCredentialRequest,
AnonCredsCredentialRequestMetadata,
GetCredentialsOptions,
} from '@aries-framework/anoncreds'
import type { AgentContext, Query, SimpleQuery } from '@aries-framework/core'
import type {
Expand Down Expand Up @@ -306,6 +307,33 @@ export class AnonCredsRsHolderService implements AnonCredsHolderService {
}
}

public async getCredentials(
agentContext: AgentContext,
options: GetCredentialsOptions
): Promise<AnonCredsCredentialInfo[]> {
const credentialRecords = await agentContext.dependencyManager
.resolve(AnonCredsCredentialRepository)
.findByQuery(agentContext, {
credentialDefinitionId: options.credentialDefinitionId,
schemaId: options.schemaId,
issuerId: options.issuerId,
schemaName: options.schemaName,
schemaVersion: options.schemaVersion,
schemaIssuerId: options.schemaIssuerId,
})

return credentialRecords.map((credentialRecord) => ({
attributes: Object.fromEntries(
Object.entries(credentialRecord.credential.values).map(([key, value]) => [key, value.raw])
),
credentialDefinitionId: credentialRecord.credential.cred_def_id,
credentialId: credentialRecord.credentialId,
schemaId: credentialRecord.credential.schema_id,
credentialRevocationId: credentialRecord.credentialRevocationId,
revocationRegistryId: credentialRecord.credential.rev_reg_id,
}))
}

public async deleteCredential(agentContext: AgentContext, credentialId: string): Promise<void> {
const credentialRepository = agentContext.dependencyManager.resolve(AnonCredsCredentialRepository)
const credentialRecord = await credentialRepository.getByCredentialId(agentContext, credentialId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const agentContext = getAgentContext({
// FIXME: Re-include in tests when NodeJS wrapper performance is improved
describeRunInNodeVersion([18], 'AnonCredsRsHolderService', () => {
const getByCredentialIdMock = jest.spyOn(anoncredsCredentialRepositoryMock, 'getByCredentialId')
const findByQueryMock = jest.spyOn(anoncredsCredentialRepositoryMock, 'findByQuery')

beforeEach(() => {
getByCredentialIdMock.mockClear()
Expand Down Expand Up @@ -433,6 +434,56 @@ describeRunInNodeVersion([18], 'AnonCredsRsHolderService', () => {
})
})

test('getCredentials', async () => {
findByQueryMock.mockResolvedValueOnce([
new AnonCredsCredentialRecord({
credential: {
cred_def_id: 'credDefId',
schema_id: 'schemaId',
signature: 'signature',
signature_correctness_proof: 'signatureCorrectnessProof',
values: { attr1: { raw: 'value1', encoded: 'encvalue1' }, attr2: { raw: 'value2', encoded: 'encvalue2' } },
rev_reg_id: 'revRegId',
} as AnonCredsCredential,
credentialId: 'myCredentialId',
credentialRevocationId: 'credentialRevocationId',
linkSecretId: 'linkSecretId',
issuerId: 'issuerDid',
schemaIssuerId: 'schemaIssuerDid',
schemaName: 'schemaName',
schemaVersion: 'schemaVersion',
}),
])

const credentialInfo = await anonCredsHolderService.getCredentials(agentContext, {
credentialDefinitionId: 'credDefId',
schemaId: 'schemaId',
schemaIssuerId: 'schemaIssuerDid',
schemaName: 'schemaName',
schemaVersion: 'schemaVersion',
issuerId: 'issuerDid',
})

expect(findByQueryMock).toHaveBeenCalledWith(agentContext, {
credentialDefinitionId: 'credDefId',
schemaId: 'schemaId',
schemaIssuerId: 'schemaIssuerDid',
schemaName: 'schemaName',
schemaVersion: 'schemaVersion',
issuerId: 'issuerDid',
})
expect(credentialInfo).toMatchObject([
{
attributes: { attr1: 'value1', attr2: 'value2' },
credentialDefinitionId: 'credDefId',
credentialId: 'myCredentialId',
revocationRegistryId: 'revRegId',
schemaId: 'schemaId',
credentialRevocationId: 'credentialRevocationId',
},
])
})

test('storeCredential', async () => {
const { credentialDefinition, credentialDefinitionPrivate, keyCorrectnessProof } = createCredentialDefinition({
attributeNames: ['name', 'age', 'sex', 'height'],
Expand Down
9 changes: 9 additions & 0 deletions packages/anoncreds/src/AnonCredsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
RegisterCredentialDefinitionReturn,
RegisterSchemaOptions,
RegisterSchemaReturn,
GetCredentialsOptions,
} from './services'
import type { Extensible } from './services/registry/base'

Expand Down Expand Up @@ -347,6 +348,14 @@ export class AnonCredsApi {
}
}

public async getCredential(credentialId: string) {
return this.anonCredsHolderService.getCredential(this.agentContext, { credentialId })
}

public async getCredentials(options: GetCredentialsOptions) {
return this.anonCredsHolderService.getCredentials(this.agentContext, options)
}

private async storeCredentialDefinitionRecord(
result: RegisterCredentialDefinitionReturn,
credentialDefinitionPrivate?: Record<string, unknown>,
Expand Down
2 changes: 2 additions & 0 deletions packages/anoncreds/src/services/AnonCredsHolderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
GetCredentialsForProofRequestReturn,
CreateLinkSecretReturn,
CreateLinkSecretOptions,
GetCredentialsOptions,
} from './AnonCredsHolderServiceOptions'
import type { AnonCredsCredentialInfo } from '../models'
import type { AnonCredsProof } from '../models/exchange'
Expand All @@ -29,6 +30,7 @@ export interface AnonCredsHolderService {
// We could come up with a hack (as we've received the credential at one point), but for
// now I think it's not that much of an issue
getCredential(agentContext: AgentContext, options: GetCredentialOptions): Promise<AnonCredsCredentialInfo>
getCredentials(agentContext: AgentContext, options: GetCredentialsOptions): Promise<AnonCredsCredentialInfo[]>

createCredentialRequest(
agentContext: AgentContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ export interface GetCredentialOptions {
credentialId: string
}

export interface GetCredentialsOptions {
credentialDefinitionId: string
schemaId: string
schemaIssuerId: string
schemaName: string
schemaVersion: string
issuerId: string
}

// TODO: Maybe we can make this a bit more specific?
export type WalletQuery = Record<string, unknown>
export interface ReferentWalletQuery {
Expand Down
23 changes: 23 additions & 0 deletions packages/indy-sdk/src/anoncreds/services/IndySdkHolderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
AnonCredsCredentialRequestMetadata,
CreateLinkSecretOptions,
CreateLinkSecretReturn,
GetCredentialsOptions,
} from '@aries-framework/anoncreds'
import type { AgentContext } from '@aries-framework/core'
import type {
Expand Down Expand Up @@ -205,6 +206,28 @@ export class IndySdkHolderService implements AnonCredsHolderService {
}
}

public async getCredentials(agentContext: AgentContext, options: GetCredentialsOptions) {
assertIndySdkWallet(agentContext.wallet)

const credentials = await this.indySdk.proverGetCredentials(agentContext.wallet.handle, {
cred_def_id: options.credentialDefinitionId,
schema_id: options.schemaId,
schema_issuer_did: options.schemaIssuerId,
schema_name: options.schemaName,
schema_version: options.schemaVersion,
issuer_did: options.issuerId,
})

return credentials.map((credential) => ({
credentialDefinitionId: credential.cred_def_id,
attributes: credential.attrs,
credentialId: credential.referent,
schemaId: credential.schema_id,
credentialRevocationId: credential.cred_rev_id,
revocationRegistryId: credential.rev_reg_id,
}))
}

public async createCredentialRequest(
agentContext: AgentContext,
options: CreateCredentialRequestOptions
Expand Down