Skip to content

Commit

Permalink
feat: regex for schemaVersion, issuerDid, credDefId, schemaId, schema…
Browse files Browse the repository at this point in the history
…IssuerDid (#679)

Signed-off-by: annelein <[email protected]>
  • Loading branch information
Annelein authored Apr 4, 2022
1 parent 1b01bce commit 36b9d46
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Attachment } from '../../../decorators/attachment/Attachment'

import { Expose, Type } from 'class-transformer'
import { Equals, IsInstance, IsOptional, IsString, ValidateNested } from 'class-validator'
import { Equals, IsInstance, IsOptional, IsString, Matches, ValidateNested } from 'class-validator'

import { AgentMessage } from '../../../agent/AgentMessage'
import { credDefIdRegex, indyDidRegex, schemaIdRegex, schemaVersionRegex } from '../../../utils'

import { CredentialPreview } from './CredentialPreview'

Expand Down Expand Up @@ -71,6 +72,7 @@ export class ProposeCredentialMessage extends AgentMessage {
@Expose({ name: 'schema_issuer_did' })
@IsString()
@IsOptional()
@Matches(indyDidRegex)
public schemaIssuerDid?: string

/**
Expand All @@ -79,6 +81,7 @@ export class ProposeCredentialMessage extends AgentMessage {
@Expose({ name: 'schema_id' })
@IsString()
@IsOptional()
@Matches(schemaIdRegex)
public schemaId?: string

/**
Expand All @@ -95,6 +98,9 @@ export class ProposeCredentialMessage extends AgentMessage {
@Expose({ name: 'schema_version' })
@IsString()
@IsOptional()
@Matches(schemaVersionRegex, {
message: 'Version must be X.X or X.X.X',
})
public schemaVersion?: string

/**
Expand All @@ -103,6 +109,7 @@ export class ProposeCredentialMessage extends AgentMessage {
@Expose({ name: 'cred_def_id' })
@IsString()
@IsOptional()
@Matches(credDefIdRegex)
public credentialDefinitionId?: string

/**
Expand All @@ -111,5 +118,6 @@ export class ProposeCredentialMessage extends AgentMessage {
@Expose({ name: 'issuer_did' })
@IsString()
@IsOptional()
@Matches(indyDidRegex)
public issuerDid?: string
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { IndyCredentialInfo as IndySDKCredentialInfo } from 'indy-sdk'

import { Expose } from 'class-transformer'
import { IsOptional, IsString } from 'class-validator'
import { IsOptional, IsString, Matches } from 'class-validator'

import { credDefIdRegex, schemaIdRegex } from '../../../utils'
import { JsonTransformer } from '../../../utils/JsonTransformer'

export class IndyCredentialInfo {
Expand All @@ -29,10 +30,12 @@ export class IndyCredentialInfo {

@Expose({ name: 'schema_id' })
@IsString()
@Matches(schemaIdRegex)
public schemaId!: string

@Expose({ name: 'cred_def_id' })
@IsString()
@Matches(credDefIdRegex)
public credentialDefinitionId!: string

@Expose({ name: 'rev_reg_id' })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('IndyLedgerService', () => {
})

it('should return the first pool with a self certifying DID if at least one did is self certifying ', async () => {
const did = 'V6ty6ttM3EjuCtosH6sGtW'
const did = 'did:sov:q7ATwTYbQDgiigVijUAej'
// Found on one production and one non production ledger
const responses = getDidResponsesForDid(did, pools, {
indicioMain: '~43X4NhAFqREffK7eWdKgFH',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('ProofRequest', () => {
name: 'Timo',
restrictions: [
{
schema_id: 'string',
schema_id: 'q7ATwTYbQDgiigVijUAej:2:test:1.0',
},
],
},
Expand All @@ -26,7 +26,7 @@ describe('ProofRequest', () => {
p_value: 10,
restrictions: [
{
schema_id: 'string',
schema_id: 'q7ATwTYbQDgiigVijUAej:2:test:1.0',
},
],
},
Expand All @@ -49,7 +49,7 @@ describe('ProofRequest', () => {
names: [],
restrictions: [
{
schema_id: 'string',
schema_id: 'q7ATwTYbQDgiigVijUAej:2:test:1.0',
},
],
},
Expand All @@ -61,7 +61,7 @@ describe('ProofRequest', () => {
p_value: 10,
restrictions: [
{
schema_id: 'string',
schema_id: 'q7ATwTYbQDgiigVijUAej:2:test:1.0',
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import {
IsMimeType,
IsOptional,
IsString,
Matches,
ValidateIf,
ValidateNested,
} from 'class-validator'

import { credDefIdRegex } from '../../../utils'
import { JsonTransformer } from '../../../utils/JsonTransformer'
import { replaceLegacyDidSovPrefix } from '../../../utils/messageType'
import { PredicateType } from '../models/PredicateType'
Expand Down Expand Up @@ -39,6 +41,7 @@ export class PresentationPreviewAttribute {
@Expose({ name: 'cred_def_id' })
@IsString()
@ValidateIf((o: PresentationPreviewAttribute) => o.referent !== undefined)
@Matches(credDefIdRegex)
public credentialDefinitionId?: string

@Expose({ name: 'mime-type' })
Expand Down Expand Up @@ -81,6 +84,7 @@ export class PresentationPreviewPredicate {

@Expose({ name: 'cred_def_id' })
@IsString()
@Matches(credDefIdRegex)
public credentialDefinitionId!: string

@IsEnum(PredicateType)
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/modules/proofs/models/AttributeFilter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Expose, Transform, TransformationType, Type } from 'class-transformer'
import { IsInstance, IsOptional, IsString, ValidateNested } from 'class-validator'
import { IsInstance, IsOptional, IsString, Matches, ValidateNested } from 'class-validator'

import { credDefIdRegex, indyDidRegex, schemaIdRegex, schemaVersionRegex } from '../../../utils'

export class AttributeValue {
public constructor(options: AttributeValue) {
Expand Down Expand Up @@ -32,11 +34,13 @@ export class AttributeFilter {
@Expose({ name: 'schema_id' })
@IsOptional()
@IsString()
@Matches(schemaIdRegex)
public schemaId?: string

@Expose({ name: 'schema_issuer_did' })
@IsOptional()
@IsString()
@Matches(indyDidRegex)
public schemaIssuerDid?: string

@Expose({ name: 'schema_name' })
Expand All @@ -47,16 +51,21 @@ export class AttributeFilter {
@Expose({ name: 'schema_version' })
@IsOptional()
@IsString()
@Matches(schemaVersionRegex, {
message: 'Version must be X.X or X.X.X',
})
public schemaVersion?: string

@Expose({ name: 'issuer_did' })
@IsOptional()
@IsString()
@Matches(indyDidRegex)
public issuerDid?: string

@Expose({ name: 'cred_def_id' })
@IsOptional()
@IsString()
@Matches(credDefIdRegex)
public credentialDefinitionId?: string

@IsOptional()
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/modules/proofs/models/ProofIdentifier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Expose } from 'class-transformer'
import { IsNumber, IsOptional, IsString } from 'class-validator'
import { IsNumber, IsOptional, IsString, Matches } from 'class-validator'

import { credDefIdRegex } from '../../../utils'

export class ProofIdentifier {
public constructor(options: ProofIdentifier) {
Expand All @@ -17,6 +19,7 @@ export class ProofIdentifier {

@Expose({ name: 'cred_def_id' })
@IsString()
@Matches(credDefIdRegex)
public credentialDefinitionId!: string

@Expose({ name: 'rev_reg_id' })
Expand Down
29 changes: 29 additions & 0 deletions packages/core/src/utils/__tests__/regex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { credDefIdRegex, indyDidRegex, schemaIdRegex, schemaVersionRegex } from '../regex'

describe('Valid Regular Expression', () => {
const invalidTest = 'test'

test('test for credDefIdRegex', async () => {
const test = 'q7ATwTYbQDgiigVijUAej:3:CL:160971:1.0.0'
expect(test).toMatch(credDefIdRegex)
expect(credDefIdRegex.test(invalidTest)).toBeFalsy()
})

test('test for indyDidRegex', async () => {
const test = 'did:sov:q7ATwTYbQDgiigVijUAej'
expect(test).toMatch(indyDidRegex)
expect(indyDidRegex.test(invalidTest)).toBeFalsy
})

test('test for schemaIdRegex', async () => {
const test = 'q7ATwTYbQDgiigVijUAej:2:test:1.0'
expect(test).toMatch(schemaIdRegex)
expect(schemaIdRegex.test(invalidTest)).toBeFalsy
})

test('test for schemaVersionRegex', async () => {
const test = '1.0.0'
expect(test).toMatch(schemaVersionRegex)
expect(schemaVersionRegex.test(invalidTest)).toBeFalsy
})
})
1 change: 1 addition & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './MultiBaseEncoder'
export * from './buffer'
export * from './MultiHashEncoder'
export * from './JWE'
export * from './regex'
export * from './indyProofRequest'
export * from './VarintEncoder'
export * from './Hasher'
4 changes: 4 additions & 0 deletions packages/core/src/utils/regex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const schemaIdRegex = /^[a-zA-Z0-9]{21,22}:2:.+:[0-9.]+$/
export const schemaVersionRegex = /^(\d+\.)?(\d+\.)?(\*|\d+)$/
export const credDefIdRegex = /^([a-zA-Z0-9]{21,22}):3:CL:(([1-9][0-9]*)|([a-zA-Z0-9]{21,22}:2:.+:[0-9.]+)):(.+)?$/
export const indyDidRegex = /^(did:sov:)?[a-zA-Z0-9]{21,22}$/

0 comments on commit 36b9d46

Please sign in to comment.