From 36b9d466d400a0f87f6272bc428965601023581a Mon Sep 17 00:00:00 2001 From: Annelein <48122190+Annelein@users.noreply.github.com> Date: Mon, 4 Apr 2022 20:44:32 +0200 Subject: [PATCH] feat: regex for schemaVersion, issuerDid, credDefId, schemaId, schemaIssuerDid (#679) Signed-off-by: annelein --- .../messages/ProposeCredentialMessage.ts | 10 ++++++- .../credentials/models/IndyCredentialInfo.ts | 5 +++- .../ledger/__tests__/IndyPoolService.test.ts | 2 +- .../proofs/__tests__/ProofRequest.test.ts | 8 ++--- .../proofs/messages/PresentationPreview.ts | 4 +++ .../modules/proofs/models/AttributeFilter.ts | 11 ++++++- .../modules/proofs/models/ProofIdentifier.ts | 5 +++- .../core/src/utils/__tests__/regex.test.ts | 29 +++++++++++++++++++ packages/core/src/utils/index.ts | 1 + packages/core/src/utils/regex.ts | 4 +++ 10 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 packages/core/src/utils/__tests__/regex.test.ts create mode 100644 packages/core/src/utils/regex.ts diff --git a/packages/core/src/modules/credentials/messages/ProposeCredentialMessage.ts b/packages/core/src/modules/credentials/messages/ProposeCredentialMessage.ts index ab7043b971..2ecacf5851 100644 --- a/packages/core/src/modules/credentials/messages/ProposeCredentialMessage.ts +++ b/packages/core/src/modules/credentials/messages/ProposeCredentialMessage.ts @@ -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' @@ -71,6 +72,7 @@ export class ProposeCredentialMessage extends AgentMessage { @Expose({ name: 'schema_issuer_did' }) @IsString() @IsOptional() + @Matches(indyDidRegex) public schemaIssuerDid?: string /** @@ -79,6 +81,7 @@ export class ProposeCredentialMessage extends AgentMessage { @Expose({ name: 'schema_id' }) @IsString() @IsOptional() + @Matches(schemaIdRegex) public schemaId?: string /** @@ -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 /** @@ -103,6 +109,7 @@ export class ProposeCredentialMessage extends AgentMessage { @Expose({ name: 'cred_def_id' }) @IsString() @IsOptional() + @Matches(credDefIdRegex) public credentialDefinitionId?: string /** @@ -111,5 +118,6 @@ export class ProposeCredentialMessage extends AgentMessage { @Expose({ name: 'issuer_did' }) @IsString() @IsOptional() + @Matches(indyDidRegex) public issuerDid?: string } diff --git a/packages/core/src/modules/credentials/models/IndyCredentialInfo.ts b/packages/core/src/modules/credentials/models/IndyCredentialInfo.ts index e1c2d21d61..72ee614879 100644 --- a/packages/core/src/modules/credentials/models/IndyCredentialInfo.ts +++ b/packages/core/src/modules/credentials/models/IndyCredentialInfo.ts @@ -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 { @@ -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' }) diff --git a/packages/core/src/modules/ledger/__tests__/IndyPoolService.test.ts b/packages/core/src/modules/ledger/__tests__/IndyPoolService.test.ts index 20f093def7..7bf8dcffd4 100644 --- a/packages/core/src/modules/ledger/__tests__/IndyPoolService.test.ts +++ b/packages/core/src/modules/ledger/__tests__/IndyPoolService.test.ts @@ -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', diff --git a/packages/core/src/modules/proofs/__tests__/ProofRequest.test.ts b/packages/core/src/modules/proofs/__tests__/ProofRequest.test.ts index cee8eda160..0d0b74cde8 100644 --- a/packages/core/src/modules/proofs/__tests__/ProofRequest.test.ts +++ b/packages/core/src/modules/proofs/__tests__/ProofRequest.test.ts @@ -14,7 +14,7 @@ describe('ProofRequest', () => { name: 'Timo', restrictions: [ { - schema_id: 'string', + schema_id: 'q7ATwTYbQDgiigVijUAej:2:test:1.0', }, ], }, @@ -26,7 +26,7 @@ describe('ProofRequest', () => { p_value: 10, restrictions: [ { - schema_id: 'string', + schema_id: 'q7ATwTYbQDgiigVijUAej:2:test:1.0', }, ], }, @@ -49,7 +49,7 @@ describe('ProofRequest', () => { names: [], restrictions: [ { - schema_id: 'string', + schema_id: 'q7ATwTYbQDgiigVijUAej:2:test:1.0', }, ], }, @@ -61,7 +61,7 @@ describe('ProofRequest', () => { p_value: 10, restrictions: [ { - schema_id: 'string', + schema_id: 'q7ATwTYbQDgiigVijUAej:2:test:1.0', }, ], }, diff --git a/packages/core/src/modules/proofs/messages/PresentationPreview.ts b/packages/core/src/modules/proofs/messages/PresentationPreview.ts index 655656704f..ca7c657757 100644 --- a/packages/core/src/modules/proofs/messages/PresentationPreview.ts +++ b/packages/core/src/modules/proofs/messages/PresentationPreview.ts @@ -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' @@ -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' }) @@ -81,6 +84,7 @@ export class PresentationPreviewPredicate { @Expose({ name: 'cred_def_id' }) @IsString() + @Matches(credDefIdRegex) public credentialDefinitionId!: string @IsEnum(PredicateType) diff --git a/packages/core/src/modules/proofs/models/AttributeFilter.ts b/packages/core/src/modules/proofs/models/AttributeFilter.ts index 4dbaab8bca..90b628799e 100644 --- a/packages/core/src/modules/proofs/models/AttributeFilter.ts +++ b/packages/core/src/modules/proofs/models/AttributeFilter.ts @@ -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) { @@ -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' }) @@ -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() diff --git a/packages/core/src/modules/proofs/models/ProofIdentifier.ts b/packages/core/src/modules/proofs/models/ProofIdentifier.ts index d12a896359..66f337e8b2 100644 --- a/packages/core/src/modules/proofs/models/ProofIdentifier.ts +++ b/packages/core/src/modules/proofs/models/ProofIdentifier.ts @@ -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) { @@ -17,6 +19,7 @@ export class ProofIdentifier { @Expose({ name: 'cred_def_id' }) @IsString() + @Matches(credDefIdRegex) public credentialDefinitionId!: string @Expose({ name: 'rev_reg_id' }) diff --git a/packages/core/src/utils/__tests__/regex.test.ts b/packages/core/src/utils/__tests__/regex.test.ts new file mode 100644 index 0000000000..93cbaa7ae8 --- /dev/null +++ b/packages/core/src/utils/__tests__/regex.test.ts @@ -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 + }) +}) diff --git a/packages/core/src/utils/index.ts b/packages/core/src/utils/index.ts index ef3e921a00..e9352470ff 100644 --- a/packages/core/src/utils/index.ts +++ b/packages/core/src/utils/index.ts @@ -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' diff --git a/packages/core/src/utils/regex.ts b/packages/core/src/utils/regex.ts new file mode 100644 index 0000000000..629be026df --- /dev/null +++ b/packages/core/src/utils/regex.ts @@ -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}$/