Skip to content

Commit

Permalink
test: add anoncreds restriction test (openwallet-foundation#1294)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored and karimStekelenburg committed Mar 17, 2023
1 parent 33ddf82 commit e1c3d3d
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 12 deletions.
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
4 changes: 3 additions & 1 deletion packages/anoncreds/src/models/AnonCredsRequestedPredicate.ts
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',
},
],
})
})
})

0 comments on commit e1c3d3d

Please sign in to comment.