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 4 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
39 changes: 24 additions & 15 deletions packages/anoncreds-rs/src/services/AnonCredsRsHolderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {
AnonCredsLinkSecretRepository,
AnonCredsCredentialRepository,
} from '@aries-framework/anoncreds'
import { utils, injectable } from '@aries-framework/core'
import { AnonCredsRestriction } from '@aries-framework/anoncreds/src/models/AnonCredsRestriction'
import { JsonTransformer, utils, injectable } from '@aries-framework/core'
import {
CredentialRequestMetadata,
Credential,
Expand Down Expand Up @@ -340,32 +341,40 @@ export class AnonCredsRsHolderService implements AnonCredsHolderService {
for (const restriction of restrictions) {
const queryElements: SimpleQuery<AnonCredsCredentialRecord> = {}

if (restriction.cred_def_id) {
queryElements.credentialDefinitionId = restriction.cred_def_id
const parsedRestriction = JsonTransformer.fromJSON(restriction, AnonCredsRestriction)

if (parsedRestriction.credentialDefinitionId) {
queryElements.credentialDefinitionId = parsedRestriction.credentialDefinitionId
}

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

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

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

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

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

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

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

query.push(queryElements)
Expand Down
20 changes: 16 additions & 4 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 { TagsBase, Tags } from '@aries-framework/core'

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

Expand All @@ -12,24 +13,28 @@ export interface AnonCredsCredentialRecordProps {
schemaVersion: string
schemaIssuerId: string
issuerId: string

}

export type DefaultAnonCredsCredentialTags = {
credentialId: string
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 = {
schemaName: string
schemaVersion: string
schemaIssuerId: string
issuerId: string
}
} & TagsBase
Vickysomtee marked this conversation as resolved.
Show resolved Hide resolved

export class AnonCredsCredentialRecord extends BaseRecord<
DefaultAnonCredsCredentialTags,
Expand All @@ -47,6 +52,7 @@ export class AnonCredsCredentialRecord extends BaseRecord<
super()

if (props) {

this.id = props.id ?? utils.uuid()
this.credentialId = props.credentialId
this.credential = props.credential
Expand All @@ -62,15 +68,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
}
}