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): support credential attribute value and marker #1369

Merged
merged 19 commits into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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
41 changes: 25 additions & 16 deletions packages/anoncreds-rs/src/services/AnonCredsRsHolderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import {
AnonCredsLinkSecretRepository,
AnonCredsCredentialRepository,
} from '@aries-framework/anoncreds'
import { utils, injectable } from '@aries-framework/core'
import { AnonCredsRestrictionWrapper } from '@aries-framework/anoncreds/src/models/AnonCredsRestrictionWrapper'
Vickysomtee marked this conversation as resolved.
Show resolved Hide resolved
import { JsonTransformer, utils, injectable } from '@aries-framework/core'
import {
anoncreds,
Credential,
Expand Down Expand Up @@ -355,35 +356,43 @@ export class AnonCredsRsHolderService implements AnonCredsHolderService {
private queryFromRestrictions(restrictions: AnonCredsProofRequestRestriction[]) {
const query: Query<AnonCredsCredentialRecord>[] = []

for (const restriction of restrictions) {
const { restrictions: parsedRestrictions } = JsonTransformer.fromJSON({ restrictions }, AnonCredsRestrictionWrapper)

for (const restriction of parsedRestrictions) {
const queryElements: SimpleQuery<AnonCredsCredentialRecord> = {}

if (restriction.cred_def_id) {
queryElements.credentialDefinitionId = restriction.cred_def_id
if (restriction.credentialDefinitionId) {
queryElements.credentialDefinitionId = restriction.credentialDefinitionId
}

if (restriction.issuerId || restriction.issuerDid) {
queryElements.issuerId = restriction.issuerId ?? restriction.issuerDid
}

if (restriction.issuer_id || restriction.issuer_did) {
queryElements.issuerId = restriction.issuer_id ?? restriction.issuer_did
if (restriction.schemaId) {
queryElements.schemaId = restriction.schemaId
}

if (restriction.rev_reg_id) {
queryElements.revocationRegistryId = restriction.rev_reg_id
if (restriction.schemaIssuerId || restriction.schemaIssuerDid) {
queryElements.schemaIssuerId = restriction.schemaIssuerId ?? restriction.issuerDid
}

if (restriction.schema_id) {
queryElements.schemaId = restriction.schema_id
if (restriction.schemaName) {
queryElements.schemaName = restriction.schemaName
}

if (restriction.schema_issuer_id || restriction.schema_issuer_did) {
queryElements.schemaIssuerId = restriction.schema_issuer_id ?? restriction.schema_issuer_did
if (restriction.schemaVersion) {
queryElements.schemaVersion = restriction.schemaVersion
}

if (restriction.schema_name) {
queryElements.schemaName = restriction.schema_name
for (const [attributeName, attributeValue] of Object.entries(restriction.attributeValues)) {
queryElements[`attr::${attributeName}::value`] = attributeValue
}

if (restriction.schema_version) {
queryElements.schemaVersion = restriction.schema_version
for (const [attributeName, isAvailable] of Object.entries(restriction.attributeMarkers)) {
if (isAvailable) {
queryElements[`attr::${attributeName}::marker`] = isAvailable
}
}

query.push(queryElements)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ describe('AnonCredsRsHolderService', () => {
names: ['name', 'height'],
restrictions: [{ cred_def_id: 'crededefid:uri', issuer_id: 'issuerid:uri' }],
},
attr5_referent: {
name: 'name',
restrictions: [{ 'attr::name::value': 'Alice', 'attr::name::marker': '1' }],
},
},
requested_predicates: {
predicate1_referent: { name: 'age', p_type: '>=' as const, p_value: 18 },
Expand Down Expand Up @@ -360,6 +364,32 @@ describe('AnonCredsRsHolderService', () => {
})
})

test('referent with attribute values and marker restriction', async () => {
await anonCredsHolderService.getCredentialsForProofRequest(agentContext, {
proofRequest,
attributeReferent: 'attr5_referent',
})

expect(findByQueryMock).toHaveBeenCalledWith(agentContext, {
attributes: ['name'],
TimoGlastra marked this conversation as resolved.
Show resolved Hide resolved
'attr::name::value': 'Alice',
'attr::name::marker': true,
})
})

test('referent with multiple names and restrictions', async () => {
await anonCredsHolderService.getCredentialsForProofRequest(agentContext, {
proofRequest,
attributeReferent: 'attr4_referent',
})

expect(findByQueryMock).toHaveBeenCalledWith(agentContext, {
attributes: ['name', 'height'],
credentialDefinitionId: 'crededefid:uri',
issuerId: 'issuerid:uri',
})
Vickysomtee marked this conversation as resolved.
Show resolved Hide resolved
})

test('predicate referent', async () => {
await anonCredsHolderService.getCredentialsForProofRequest(agentContext, {
proofRequest,
Expand Down
11 changes: 11 additions & 0 deletions packages/anoncreds/src/models/AnonCredsRestrictionWrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Type } from 'class-transformer'
import { ValidateNested } from 'class-validator'

import { AnonCredsRestrictionTransformer, AnonCredsRestriction } from './AnonCredsRestriction'

export class AnonCredsRestrictionWrapper {
@ValidateNested({ each: true })
@Type(() => AnonCredsRestriction)
@AnonCredsRestrictionTransformer()
public restrictions!: AnonCredsRestriction[]
}
16 changes: 13 additions & 3 deletions packages/anoncreds/src/repository/AnonCredsCredentialRecord.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { AnonCredsCredential } from '../models'
import type { Tags } from '@aries-framework/core'

import { BaseRecord, utils } from '@aries-framework/core'

Expand All @@ -19,9 +20,12 @@ export type DefaultAnonCredsCredentialTags = {
linkSecretId: string
credentialDefinitionId: string
credentialRevocationId?: string
revocationRegistryId?: string
Vickysomtee marked this conversation as resolved.
Show resolved Hide resolved
schemaId: string
attributes: string[]

// the following keys can be used for every `attribute name` in credential.
[key: `attr::${string}::marker`]: true | undefined
[key: `attr::${string}::value`]: string | undefined
}

export type CustomAnonCredsCredentialTags = {
Expand Down Expand Up @@ -62,15 +66,21 @@ export class AnonCredsCredentialRecord extends BaseRecord<
}

public getTags() {
return {
const tags: Tags<DefaultAnonCredsCredentialTags, CustomAnonCredsCredentialTags> = {
...this._tags,
credentialDefinitionId: this.credential.cred_def_id,
schemaId: this.credential.schema_id,
credentialId: this.credentialId,
credentialRevocationId: this.credentialRevocationId,
revocationRegistryId: this.credential.rev_reg_id,
Vickysomtee marked this conversation as resolved.
Show resolved Hide resolved
linkSecretId: this.linkSecretId,
attributes: Object.keys(this.credential.values),
Vickysomtee marked this conversation as resolved.
Show resolved Hide resolved
}

for (const [key, value] of Object.entries(this.credential.values)) {
tags[`attr::${key}::value`] = value.raw
tags[`attr::${key}::marker`] = true
}

return tags
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { AnonCredsCredential } from '@aries-framework/anoncreds'

import { AnonCredsCredentialRecord } from '../AnonCredsCredentialRecord'

describe('AnoncredsCredentialRecords', () => {
test('Returns the correct tags from the getTags methods based on the credential record values', () => {
const anoncredsCredentialRecords = 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 tags = anoncredsCredentialRecords.getTags()

expect(tags).toMatchObject({
issuerId: 'issuerDid',
schemaIssuerId: 'schemaIssuerDid',
schemaName: 'schemaName',
schemaVersion: 'schemaVersion',
credentialDefinitionId: 'credDefId',
schemaId: 'schemaId',
credentialId: 'myCredentialId',
credentialRevocationId: 'credentialRevocationId',
linkSecretId: 'linkSecretId',
attributes: ['attr1', 'attr2'],
'attr::attr1::value': 'value1',
'attr::attr1::marker': true,
'attr::attr2::value': 'value2',
'attr::attr2::marker': true,
})
})
})