Skip to content

Commit

Permalink
feat(anoncreds): store method name in records (#1387)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored Mar 30, 2023
1 parent ef00099 commit 47636b4
Show file tree
Hide file tree
Showing 26 changed files with 139 additions and 20 deletions.
10 changes: 10 additions & 0 deletions packages/anoncreds-rs/src/services/AnonCredsRsHolderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
AnonCredsLinkSecretRepository,
AnonCredsRestrictionWrapper,
legacyIndyCredentialDefinitionIdRegex,
AnonCredsRegistryService,
} from '@aries-framework/anoncreds'
import { AriesFrameworkError, JsonTransformer, TypedArrayEncoder, injectable, utils } from '@aries-framework/core'
import {
Expand Down Expand Up @@ -265,6 +266,10 @@ export class AnonCredsRsHolderService implements AnonCredsHolderService {

const credentialRepository = agentContext.dependencyManager.resolve(AnonCredsCredentialRepository)

const methodName = agentContext.dependencyManager
.resolve(AnonCredsRegistryService)
.getRegistryForIdentifier(agentContext, credential.cred_def_id).methodName

await credentialRepository.save(
agentContext,
new AnonCredsCredentialRecord({
Expand All @@ -276,6 +281,7 @@ export class AnonCredsRsHolderService implements AnonCredsHolderService {
schemaIssuerId: schema.issuerId,
schemaVersion: schema.version,
credentialRevocationId: processedCredential.revocationRegistryIndex?.toString(),
methodName,
})
)

Expand Down Expand Up @@ -305,6 +311,7 @@ export class AnonCredsRsHolderService implements AnonCredsHolderService {
schemaId: credentialRecord.credential.schema_id,
credentialRevocationId: credentialRecord.credentialRevocationId,
revocationRegistryId: credentialRecord.credential.rev_reg_id,
methodName: credentialRecord.methodName,
}
}

Expand All @@ -321,6 +328,7 @@ export class AnonCredsRsHolderService implements AnonCredsHolderService {
schemaName: options.schemaName,
schemaVersion: options.schemaVersion,
schemaIssuerId: options.schemaIssuerId,
methodName: options.methodName,
})

return credentialRecords.map((credentialRecord) => ({
Expand All @@ -332,6 +340,7 @@ export class AnonCredsRsHolderService implements AnonCredsHolderService {
schemaId: credentialRecord.credential.schema_id,
credentialRevocationId: credentialRecord.credentialRevocationId,
revocationRegistryId: credentialRecord.credential.rev_reg_id,
methodName: credentialRecord.methodName,
}))
}

Expand Down Expand Up @@ -397,6 +406,7 @@ export class AnonCredsRsHolderService implements AnonCredsHolderService {
schemaId: credentialRecord.credential.schema_id,
credentialRevocationId: credentialRecord.credentialRevocationId,
revocationRegistryId: credentialRecord.credential.rev_reg_id,
methodName: credentialRecord.methodName,
},
interval: proofRequest.non_revoked,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
import type { JsonObject } from '@hyperledger/anoncreds-nodejs'

import {
AnonCredsModuleConfig,
AnonCredsHolderServiceSymbol,
AnonCredsLinkSecretRecord,
AnonCredsCredentialRecord,
Expand All @@ -21,6 +22,7 @@ import { describeRunInNodeVersion } from '../../../../../tests/runInVersion'
import { AnonCredsCredentialDefinitionRepository } from '../../../../anoncreds/src/repository/AnonCredsCredentialDefinitionRepository'
import { AnonCredsCredentialRepository } from '../../../../anoncreds/src/repository/AnonCredsCredentialRepository'
import { AnonCredsLinkSecretRepository } from '../../../../anoncreds/src/repository/AnonCredsLinkSecretRepository'
import { InMemoryAnonCredsRegistry } from '../../../../anoncreds/tests/InMemoryAnonCredsRegistry'
import { getAgentConfig, getAgentContext, mockFunction } from '../../../../core/tests/helpers'
import { AnonCredsRsHolderService } from '../AnonCredsRsHolderService'

Expand Down Expand Up @@ -53,6 +55,12 @@ const agentContext = getAgentContext({
[AnonCredsLinkSecretRepository, anoncredsLinkSecretRepositoryMock],
[AnonCredsCredentialRepository, anoncredsCredentialRepositoryMock],
[AnonCredsHolderServiceSymbol, anonCredsHolderService],
[
AnonCredsModuleConfig,
new AnonCredsModuleConfig({
registries: [new InMemoryAnonCredsRegistry({})],
}),
],
],
agentConfig,
})
Expand Down Expand Up @@ -219,6 +227,7 @@ describeRunInNodeVersion([18], 'AnonCredsRsHolderService', () => {
schemaIssuerId: 'schemaIssuerDid',
schemaName: 'schemaName',
schemaVersion: 'schemaVersion',
methodName: 'inMemory',
})
)
getByCredentialIdMock.mockResolvedValueOnce(
Expand All @@ -230,6 +239,7 @@ describeRunInNodeVersion([18], 'AnonCredsRsHolderService', () => {
schemaIssuerId: 'schemaIssuerDid',
schemaName: 'schemaName',
schemaVersion: 'schemaVersion',
methodName: 'inMemory',
})
)

Expand Down Expand Up @@ -436,6 +446,7 @@ describeRunInNodeVersion([18], 'AnonCredsRsHolderService', () => {
schemaIssuerId: 'schemaIssuerDid',
schemaName: 'schemaName',
schemaVersion: 'schemaVersion',
methodName: 'inMemory',
})
)

Expand Down Expand Up @@ -466,6 +477,7 @@ describeRunInNodeVersion([18], 'AnonCredsRsHolderService', () => {
schemaIssuerId: 'schemaIssuerDid',
schemaName: 'schemaName',
schemaVersion: 'schemaVersion',
methodName: 'inMemory',
})
)
expect(
Expand Down Expand Up @@ -502,6 +514,7 @@ describeRunInNodeVersion([18], 'AnonCredsRsHolderService', () => {
schemaIssuerId: 'schemaIssuerDid',
schemaName: 'schemaName',
schemaVersion: 'schemaVersion',
methodName: 'inMemory',
}),
])

Expand All @@ -512,6 +525,7 @@ describeRunInNodeVersion([18], 'AnonCredsRsHolderService', () => {
schemaName: 'schemaName',
schemaVersion: 'schemaVersion',
issuerId: 'issuerDid',
methodName: 'inMemory',
})

expect(findByQueryMock).toHaveBeenCalledWith(agentContext, {
Expand All @@ -521,6 +535,7 @@ describeRunInNodeVersion([18], 'AnonCredsRsHolderService', () => {
schemaName: 'schemaName',
schemaVersion: 'schemaVersion',
issuerId: 'issuerDid',
methodName: 'inMemory',
})
expect(credentialInfo).toMatchObject([
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AnonCredsProofRequest } from '@aries-framework/anoncreds'

import {
AnonCredsModuleConfig,
AnonCredsHolderServiceSymbol,
AnonCredsIssuerServiceSymbol,
AnonCredsVerifierServiceSymbol,
Expand Down Expand Up @@ -43,6 +44,12 @@ const agentContext = getAgentContext({
[AnonCredsIssuerServiceSymbol, anonCredsIssuerService],
[AnonCredsHolderServiceSymbol, anonCredsHolderService],
[AnonCredsVerifierServiceSymbol, anonCredsVerifierService],
[
AnonCredsModuleConfig,
new AnonCredsModuleConfig({
registries: [registry],
}),
],
],
agentConfig,
})
Expand Down Expand Up @@ -96,6 +103,7 @@ describeRunInNodeVersion([18], 'AnonCredsRsServices', () => {
new AnonCredsSchemaRecord({
schema: schemaState.schema,
schemaId: schemaState.schemaId,
methodName: 'inMemory',
})
)

Expand All @@ -104,6 +112,7 @@ describeRunInNodeVersion([18], 'AnonCredsRsServices', () => {
new AnonCredsCredentialDefinitionRecord({
credentialDefinition: credentialDefinitionState.credentialDefinition,
credentialDefinitionId: credentialDefinitionState.credentialDefinitionId,
methodName: 'inMemory',
})
)

Expand Down Expand Up @@ -180,6 +189,7 @@ describeRunInNodeVersion([18], 'AnonCredsRsServices', () => {
credentialDefinitionId: credentialDefinitionState.credentialDefinitionId,
revocationRegistryId: null,
credentialRevocationId: undefined, // Should it be null in this case?
methodName: 'inMemory',
})

const proofRequest: AnonCredsProofRequest = {
Expand Down
1 change: 1 addition & 0 deletions packages/anoncreds-rs/src/services/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export function createCredentialForHolder(options: {
credentialDefinitionId,
credentialId,
schemaId,
methodName: 'inMemory',
}
const returnObj = {
credential: credentialObj.toJson() as unknown as AnonCredsCredential,
Expand Down
3 changes: 3 additions & 0 deletions packages/anoncreds-rs/tests/anoncreds-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ describeRunInNodeVersion([18], 'AnonCreds format services using anoncreds-rs', (
new AnonCredsSchemaRecord({
schema: schemaState.schema,
schemaId: schemaState.schemaId,
methodName: 'inMemory',
})
)

Expand All @@ -135,6 +136,7 @@ describeRunInNodeVersion([18], 'AnonCreds format services using anoncreds-rs', (
new AnonCredsCredentialDefinitionRecord({
credentialDefinition: credentialDefinitionState.credentialDefinition,
credentialDefinitionId: credentialDefinitionState.credentialDefinitionId,
methodName: 'inMemory',
})
)

Expand Down Expand Up @@ -268,6 +270,7 @@ describeRunInNodeVersion([18], 'AnonCreds format services using anoncreds-rs', (
credentialDefinitionId: credentialDefinitionState.credentialDefinitionId,
revocationRegistryId: null,
credentialRevocationId: undefined, // FIXME: should be null?
methodName: 'inMemory',
})

expect(holderCredentialRecord.metadata.data).toEqual({
Expand Down
3 changes: 3 additions & 0 deletions packages/anoncreds-rs/tests/indy-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ describeRunInNodeVersion([18], 'Legacy indy format services using anoncreds-rs',
new AnonCredsSchemaRecord({
schema: schemaState.schema,
schemaId: schemaState.schemaId,
methodName: 'inMemory',
})
)

Expand All @@ -136,6 +137,7 @@ describeRunInNodeVersion([18], 'Legacy indy format services using anoncreds-rs',
new AnonCredsCredentialDefinitionRecord({
credentialDefinition: credentialDefinitionState.credentialDefinition,
credentialDefinitionId: credentialDefinitionState.credentialDefinitionId,
methodName: 'inMemory',
})
)

Expand Down Expand Up @@ -268,6 +270,7 @@ describeRunInNodeVersion([18], 'Legacy indy format services using anoncreds-rs',
credentialDefinitionId: credentialDefinitionState.credentialDefinitionId,
revocationRegistryId: null,
credentialRevocationId: undefined, // FIXME: should be null?
methodName: 'inMemory',
})

expect(holderCredentialRecord.metadata.data).toEqual({
Expand Down
19 changes: 16 additions & 3 deletions packages/anoncreds/src/AnonCredsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import type {
RegisterCredentialDefinitionReturn,
RegisterSchemaOptions,
RegisterSchemaReturn,
AnonCredsRegistry,
GetCredentialsOptions,
} from './services'
import type { Extensible } from './services/registry/base'
import type { SimpleQuery } from '@aries-framework/core'

import { AgentContext, inject, injectable } from '@aries-framework/core'

Expand Down Expand Up @@ -163,7 +165,7 @@ export class AnonCredsApi {

try {
const result = await registry.registerSchema(this.agentContext, options)
await this.storeSchemaRecord(result)
await this.storeSchemaRecord(registry, result)

return result
} catch (error) {
Expand All @@ -179,6 +181,10 @@ export class AnonCredsApi {
}
}

public async getCreatedSchemas(query: SimpleQuery<AnonCredsSchemaRecord>) {
return this.anonCredsSchemaRepository.findByQuery(this.agentContext, query)
}

/**
* Retrieve a {@link AnonCredsCredentialDefinition} from the registry associated
* with the {@link credentialDefinitionId}
Expand Down Expand Up @@ -264,7 +270,7 @@ export class AnonCredsApi {
options: options.options,
})

await this.storeCredentialDefinitionRecord(result, credentialDefinitionPrivate, keyCorrectnessProof)
await this.storeCredentialDefinitionRecord(registry, result, credentialDefinitionPrivate, keyCorrectnessProof)

return result
} catch (error) {
Expand All @@ -280,6 +286,10 @@ export class AnonCredsApi {
}
}

public async getCreatedCredentialDefinitions(query: SimpleQuery<AnonCredsCredentialDefinitionRecord>) {
return this.anonCredsCredentialDefinitionRepository.findByQuery(this.agentContext, query)
}

/**
* Retrieve a {@link AnonCredsRevocationRegistryDefinition} from the registry associated
* with the {@link revocationRegistryDefinitionId}
Expand Down Expand Up @@ -357,6 +367,7 @@ export class AnonCredsApi {
}

private async storeCredentialDefinitionRecord(
registry: AnonCredsRegistry,
result: RegisterCredentialDefinitionReturn,
credentialDefinitionPrivate?: Record<string, unknown>,
keyCorrectnessProof?: Record<string, unknown>
Expand All @@ -371,6 +382,7 @@ export class AnonCredsApi {
const credentialDefinitionRecord = new AnonCredsCredentialDefinitionRecord({
credentialDefinitionId: result.credentialDefinitionState.credentialDefinitionId,
credentialDefinition: result.credentialDefinitionState.credentialDefinition,
methodName: registry.methodName,
})

// TODO: do we need to store this metadata? For indy, the registration metadata contains e.g.
Expand Down Expand Up @@ -412,14 +424,15 @@ export class AnonCredsApi {
}
}

private async storeSchemaRecord(result: RegisterSchemaReturn): Promise<void> {
private async storeSchemaRecord(registry: AnonCredsRegistry, result: RegisterSchemaReturn): Promise<void> {
try {
// If we have both the schema and the schemaId we will store a copy of the schema. We may need to handle an
// edge case in the future where we e.g. don't have the id yet, and it is registered through a different channel
if (result.schemaState.schema && result.schemaState.schemaId) {
const schemaRecord = new AnonCredsSchemaRecord({
schemaId: result.schemaState.schemaId,
schema: result.schemaState.schema,
methodName: registry.methodName,
})

await this.anonCredsSchemaRepository.save(this.agentContext, schemaRecord)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ describe('Legacy indy format services', () => {
credentialDefinitionId: legacyCredentialDefinitionId,
revocationRegistryId: null,
credentialRevocationId: null,
methodName: 'indy',
})

expect(holderCredentialRecord.metadata.data).toEqual({
Expand Down
1 change: 1 addition & 0 deletions packages/anoncreds/src/models/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface AnonCredsCredentialInfo {
credentialDefinitionId: string
revocationRegistryId?: string | undefined
credentialRevocationId?: string | undefined
methodName: string
}

export interface AnonCredsRequestedAttributeMatch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ export interface AnonCredsCredentialDefinitionRecordProps {
id?: string
credentialDefinitionId: string
credentialDefinition: AnonCredsCredentialDefinition
methodName: string
}

export type DefaultAnonCredsCredentialDefinitionTags = {
schemaId: string
credentialDefinitionId: string
issuerId: string
tag: string
methodName: string
}

export class AnonCredsCredentialDefinitionRecord extends BaseRecord<
Expand All @@ -28,13 +30,20 @@ export class AnonCredsCredentialDefinitionRecord extends BaseRecord<
public credentialDefinitionId!: string
public credentialDefinition!: AnonCredsCredentialDefinition

/**
* AnonCreds method name. We don't use names explicitly from the registry (there's no identifier for a registry)
* @see https://hyperledger.github.io/anoncreds-methods-registry/
*/
public methodName!: string

public constructor(props: AnonCredsCredentialDefinitionRecordProps) {
super()

if (props) {
this.id = props.id ?? utils.uuid()
this.credentialDefinitionId = props.credentialDefinitionId
this.credentialDefinition = props.credentialDefinition
this.methodName = props.methodName
}
}

Expand All @@ -45,6 +54,7 @@ export class AnonCredsCredentialDefinitionRecord extends BaseRecord<
schemaId: this.credentialDefinition.schemaId,
issuerId: this.credentialDefinition.issuerId,
tag: this.credentialDefinition.tag,
methodName: this.methodName,
}
}
}
Loading

0 comments on commit 47636b4

Please sign in to comment.