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

test: add anoncreds restriction test #1294

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
6 changes: 3 additions & 3 deletions packages/anoncreds/src/formats/AnonCredsProofFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export interface AnonCredsProposeProofFormat {
export interface AnonCredsRequestProofFormat {
name: string
version: string
nonRevoked?: AnonCredsNonRevokedInterval
requestedAttributes?: Record<string, AnonCredsRequestedAttribute>
requestedPredicates?: Record<string, AnonCredsRequestedPredicate>
non_revoked?: AnonCredsNonRevokedInterval
requested_attributes?: Record<string, AnonCredsRequestedAttribute>
requested_predicates?: Record<string, AnonCredsRequestedPredicate>
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ export class LegacyIndyProofFormatService implements ProofFormatService<LegacyIn
name: indyFormat.name,
version: indyFormat.version,
nonce: await agentContext.wallet.generateNonce(),
requested_attributes: indyFormat.requestedAttributes ?? {},
requested_predicates: indyFormat.requestedPredicates ?? {},
non_revoked: indyFormat.nonRevoked,
requested_attributes: indyFormat.requested_attributes ?? {},
requested_predicates: indyFormat.requested_predicates ?? {},
non_revoked: indyFormat.non_revoked,
} satisfies AnonCredsProofRequest

// Validate to make sure user provided correct input
Expand Down
3 changes: 2 additions & 1 deletion packages/anoncreds/src/models/AnonCredsProofRequest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AnonCredsRequestedAttributeOptions } from './AnonCredsRequestedAttribute'
import type { AnonCredsRequestedPredicateOptions } from './AnonCredsRequestedPredicate'

import { IndyRevocationInterval } from '@aries-framework/core'
Expand All @@ -16,7 +17,7 @@ export interface AnonCredsProofRequestOptions {
nonce: string
nonRevoked?: AnonCredsRevocationInterval
ver?: '1.0' | '2.0'
requestedAttributes?: Record<string, AnonCredsRequestedAttribute>
requestedAttributes?: Record<string, AnonCredsRequestedAttributeOptions>
requestedPredicates?: Record<string, AnonCredsRequestedPredicateOptions>
}

Expand Down
11 changes: 10 additions & 1 deletion packages/anoncreds/src/models/AnonCredsRequestedAttribute.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import type { AnonCredsRestrictionOptions } from './AnonCredsRestriction'

import { Expose, Type } from 'class-transformer'
import { ArrayNotEmpty, IsArray, IsInstance, IsOptional, IsString, ValidateIf, ValidateNested } from 'class-validator'

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

export interface AnonCredsRequestedAttributeOptions {
name?: string
names?: string[]
nonRevoked?: AnonCredsRevocationInterval
restrictions?: AnonCredsRestrictionOptions[]
}

export class AnonCredsRequestedAttribute {
public constructor(options: AnonCredsRequestedAttribute) {
public constructor(options: AnonCredsRequestedAttributeOptions) {
if (options) {
this.name = options.name
this.names = options.names
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { AnonCredsRestrictionOptions } from './AnonCredsRestriction'

import { Expose, Type } from 'class-transformer'
import { IsArray, IsIn, IsInstance, IsInt, IsOptional, IsString, ValidateNested } from 'class-validator'

Expand All @@ -12,7 +14,7 @@ export interface AnonCredsRequestedPredicateOptions {
predicateType: AnonCredsPredicateType
predicateValue: number
nonRevoked?: AnonCredsRevocationInterval
restrictions?: AnonCredsRestriction[]
restrictions?: AnonCredsRestrictionOptions[]
}

export class AnonCredsRequestedPredicate {
Expand Down
19 changes: 16 additions & 3 deletions packages/anoncreds/src/models/AnonCredsRestriction.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { Exclude, Expose, Transform, TransformationType } from 'class-transformer'
import { IsOptional, IsString } from 'class-validator'

export interface AnonCredsRestrictionOptions {
schemaId?: string
schemaIssuerDid?: string
schemaIssuerId?: string
schemaName?: string
schemaVersion?: string
issuerDid?: string
issuerId?: string
credentialDefinitionId?: string
attributeMarkers?: Record<string, true>
attributeValues?: Record<string, string>
}

export class AnonCredsRestriction {
public constructor(options: AnonCredsRestriction) {
public constructor(options: AnonCredsRestrictionOptions) {
if (options) {
this.schemaId = options.schemaId
this.schemaIssuerDid = options.schemaIssuerDid
Expand All @@ -12,8 +25,8 @@ export class AnonCredsRestriction {
this.issuerDid = options.issuerDid
this.issuerId = options.issuerId
this.credentialDefinitionId = options.credentialDefinitionId
this.attributeMarkers = options.attributeMarkers
this.attributeValues = options.attributeValues
this.attributeMarkers = options.attributeMarkers ?? {}
this.attributeValues = options.attributeValues ?? {}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,69 @@ describe('AnonCredsRestriction', () => {
],
})
})

test('transforms properties from and to json with correct casing', () => {
const restrictions = new Wrapper({
restrictions: [
new AnonCredsRestriction({
credentialDefinitionId: 'credentialDefinitionId',
issuerDid: 'issuerDid',
issuerId: 'issuerId',
schemaName: 'schemaName',
schemaVersion: 'schemaVersion',
schemaId: 'schemaId',
schemaIssuerDid: 'schemaIssuerDid',
schemaIssuerId: 'schemaIssuerId',
}),
],
})

expect(JsonTransformer.toJSON(restrictions)).toMatchObject({
restrictions: [
{
cred_def_id: 'credentialDefinitionId',
issuer_did: 'issuerDid',
issuer_id: 'issuerId',
schema_name: 'schemaName',
schema_version: 'schemaVersion',
schema_id: 'schemaId',
schema_issuer_did: 'schemaIssuerDid',
schema_issuer_id: 'schemaIssuerId',
},
],
})

expect(
JsonTransformer.fromJSON(
{
restrictions: [
{
cred_def_id: 'credentialDefinitionId',
issuer_did: 'issuerDid',
issuer_id: 'issuerId',
schema_name: 'schemaName',
schema_version: 'schemaVersion',
schema_id: 'schemaId',
schema_issuer_did: 'schemaIssuerDid',
schema_issuer_id: 'schemaIssuerId',
},
],
},
Wrapper
)
).toMatchObject({
restrictions: [
{
credentialDefinitionId: 'credentialDefinitionId',
issuerDid: 'issuerDid',
issuerId: 'issuerId',
schemaName: 'schemaName',
schemaVersion: 'schemaVersion',
schemaId: 'schemaId',
schemaIssuerDid: 'schemaIssuerDid',
schemaIssuerId: 'schemaIssuerId',
},
],
})
})
})