From a4204ef2db769de53d12f0d881d2c4422545c390 Mon Sep 17 00:00:00 2001 From: Timo Glastra Date: Sat, 11 Mar 2023 16:31:09 +0100 Subject: [PATCH] fix: remove named capture groups (#1378) named capture groups are only supported in more recent versions of hermes Signed-off-by: Timo Glastra --- .../utils/__tests__/identifiers.test.ts | 2 +- .../src/anoncreds/utils/identifiers.ts | 128 +++++++++++++----- .../utils/_tests_/identifiers.test.ts | 2 +- .../src/anoncreds/utils/identifiers.ts | 128 +++++++++++++----- 4 files changed, 186 insertions(+), 74 deletions(-) diff --git a/packages/indy-sdk/src/anoncreds/utils/__tests__/identifiers.test.ts b/packages/indy-sdk/src/anoncreds/utils/__tests__/identifiers.test.ts index 74488d4108..f60ebe04a1 100644 --- a/packages/indy-sdk/src/anoncreds/utils/__tests__/identifiers.test.ts +++ b/packages/indy-sdk/src/anoncreds/utils/__tests__/identifiers.test.ts @@ -13,7 +13,7 @@ import { describe('identifiers', () => { describe('indySdkAnonCredsRegistryIdentifierRegex', () => { - test('matches against a legacy indy did, schema id, credential definition id and revocation registry id', () => { + test('matches against a legacy schema id, credential definition id and revocation registry id', () => { const did = '7Tqg6BwSSWapxgUDm9KKgg' const schemaId = 'BQ42WeE24jFHeyGg8x9XAz:2:Medical Bill:1.0' const credentialDefinitionId = 'N7baRMcyvPwWc8v85CtZ6e:3:CL:100669:SCH Employee ID' diff --git a/packages/indy-sdk/src/anoncreds/utils/identifiers.ts b/packages/indy-sdk/src/anoncreds/utils/identifiers.ts index 19f1df864c..decd85f10b 100644 --- a/packages/indy-sdk/src/anoncreds/utils/identifiers.ts +++ b/packages/indy-sdk/src/anoncreds/utils/identifiers.ts @@ -6,34 +6,28 @@ import { DID_INDY_REGEX } from '../../utils/did' const didIndyAnonCredsBase = - /(?did:indy:(?((?:[a-z][_a-z0-9-]*)(?::[a-z][_a-z0-9-]*)?)):(?([1-9A-HJ-NP-Za-km-z]{21,22})))\/anoncreds\/v0/ + /(did:indy:((?:[a-z][_a-z0-9-]*)(?::[a-z][_a-z0-9-]*)?):([1-9A-HJ-NP-Za-km-z]{21,22}))\/anoncreds\/v0/ // did:indy::/anoncreds/v0/SCHEMA// -const didIndySchemaIdRegex = new RegExp( - `^${didIndyAnonCredsBase.source}/SCHEMA/(?.+)/(?[0-9.]+)$` -) +const didIndySchemaIdRegex = new RegExp(`^${didIndyAnonCredsBase.source}/SCHEMA/(.+)/([0-9.]+)$`) // :2:: -const legacyIndySchemaIdRegex = - /^(?(?[a-zA-Z0-9]{21,22})):2:(?.+):(?[0-9.]+)$/ +const legacyIndySchemaIdRegex = /^([a-zA-Z0-9]{21,22}):2:(.+):([0-9.]+)$/ // did:indy::/anoncreds/v0/CLAIM_DEF// -const didIndyCredentialDefinitionIdRegex = new RegExp( - `^${didIndyAnonCredsBase.source}/CLAIM_DEF/(?[1-9][0-9]*)/(?.+)$` -) +const didIndyCredentialDefinitionIdRegex = new RegExp(`^${didIndyAnonCredsBase.source}/CLAIM_DEF/([1-9][0-9]*)/(.+)$`) // :3:CL:: -const legacyIndyCredentialDefinitionIdRegex = - /^(?(?[a-zA-Z0-9]{21,22})):3:CL:(?[1-9][0-9]*):(?.+)$/ +const legacyIndyCredentialDefinitionIdRegex = /^([a-zA-Z0-9]{21,22}):3:CL:([1-9][0-9]*):(.+)$/ // did:indy::/anoncreds/v0/REV_REG_DEF/// const didIndyRevocationRegistryIdRegex = new RegExp( - `^${didIndyAnonCredsBase.source}/REV_REG_DEF/(?[1-9][0-9]*)/(?.+)/(?.+)$` + `^${didIndyAnonCredsBase.source}/REV_REG_DEF/([1-9][0-9]*)/(.+)/(.+)$` ) // :4::3:CL::CL_ACCUM: const legacyIndyRevocationRegistryIdRegex = - /^(?(?[a-zA-Z0-9]{21,22})):4:[a-zA-Z0-9]{21,22}:3:CL:(?[1-9][0-9]*):(?.+):CL_ACCUM:(?.+)$/ + /^([a-zA-Z0-9]{21,22}):4:[a-zA-Z0-9]{21,22}:3:CL:([1-9][0-9]*):(.+):CL_ACCUM:(.+)$/ // combines both legacy and did:indy anoncreds identifiers and also the issuer id const indySdkAnonCredsRegexes = [ @@ -58,7 +52,7 @@ const indySdkAnonCredsRegexes = [ ] export const indySdkAnonCredsRegistryIdentifierRegex = new RegExp( - indySdkAnonCredsRegexes.map((r) => r.source.replace(/(\?<[a-zA-Z]+>)?/g, '')).join('|') + indySdkAnonCredsRegexes.map((r) => r.source).join('|') ) export function getDidIndySchemaId(namespace: string, unqualifiedDid: string, name: string, version: string) { @@ -110,12 +104,33 @@ interface ParsedSchemaId { namespace?: string } -export function parseSchemaId(schemaId: string) { - const match = schemaId.match(didIndySchemaIdRegex) ?? schemaId.match(legacyIndySchemaIdRegex) - - if (!match) throw new Error(`Invalid schema id: ${schemaId}`) - - return match.groups as unknown as ParsedSchemaId +export function parseSchemaId(schemaId: string): ParsedSchemaId { + const didIndyMatch = schemaId.match(didIndySchemaIdRegex) + if (didIndyMatch) { + const [, did, namespace, namespaceIdentifier, schemaName, schemaVersion] = didIndyMatch + + return { + did, + namespaceIdentifier, + schemaName, + schemaVersion, + namespace, + } + } + + const legacyMatch = schemaId.match(legacyIndySchemaIdRegex) + if (legacyMatch) { + const [, did, schemaName, schemaVersion] = legacyMatch + + return { + did, + namespaceIdentifier: did, + schemaName, + schemaVersion, + } + } + + throw new Error(`Invalid schema id: ${schemaId}`) } interface ParsedCredentialDefinitionId { @@ -126,14 +141,33 @@ interface ParsedCredentialDefinitionId { namespace?: string } -export function parseCredentialDefinitionId(credentialDefinitionId: string) { - const match = - credentialDefinitionId.match(didIndyCredentialDefinitionIdRegex) ?? - credentialDefinitionId.match(legacyIndyCredentialDefinitionIdRegex) - - if (!match) throw new Error(`Invalid credential definition id: ${credentialDefinitionId}`) - - return match.groups as unknown as ParsedCredentialDefinitionId +export function parseCredentialDefinitionId(credentialDefinitionId: string): ParsedCredentialDefinitionId { + const didIndyMatch = credentialDefinitionId.match(didIndyCredentialDefinitionIdRegex) + if (didIndyMatch) { + const [, did, namespace, namespaceIdentifier, schemaSeqNo, tag] = didIndyMatch + + return { + did, + namespaceIdentifier, + schemaSeqNo, + tag, + namespace, + } + } + + const legacyMatch = credentialDefinitionId.match(legacyIndyCredentialDefinitionIdRegex) + if (legacyMatch) { + const [, did, schemaSeqNo, tag] = legacyMatch + + return { + did, + namespaceIdentifier: did, + schemaSeqNo, + tag, + } + } + + throw new Error(`Invalid credential definition id: ${credentialDefinitionId}`) } interface ParsedRevocationRegistryId { @@ -145,12 +179,34 @@ interface ParsedRevocationRegistryId { namespace?: string } -export function parseRevocationRegistryId(revocationRegistryId: string) { - const match = - revocationRegistryId.match(didIndyRevocationRegistryIdRegex) ?? - revocationRegistryId.match(legacyIndyRevocationRegistryIdRegex) - - if (!match) throw new Error(`Invalid revocation registry id: ${revocationRegistryId}`) - - return match.groups as unknown as ParsedRevocationRegistryId +export function parseRevocationRegistryId(revocationRegistryId: string): ParsedRevocationRegistryId { + const didIndyMatch = revocationRegistryId.match(didIndyRevocationRegistryIdRegex) + if (didIndyMatch) { + const [, did, namespace, namespaceIdentifier, schemaSeqNo, credentialDefinitionTag, revocationRegistryTag] = + didIndyMatch + + return { + did, + namespaceIdentifier, + schemaSeqNo, + credentialDefinitionTag, + revocationRegistryTag, + namespace, + } + } + + const legacyMatch = revocationRegistryId.match(legacyIndyRevocationRegistryIdRegex) + if (legacyMatch) { + const [, did, schemaSeqNo, credentialDefinitionTag, revocationRegistryTag] = legacyMatch + + return { + did, + namespaceIdentifier: did, + schemaSeqNo, + credentialDefinitionTag, + revocationRegistryTag, + } + } + + throw new Error(`Invalid revocation registry id: ${revocationRegistryId}`) } diff --git a/packages/indy-vdr/src/anoncreds/utils/_tests_/identifiers.test.ts b/packages/indy-vdr/src/anoncreds/utils/_tests_/identifiers.test.ts index 555605a1d9..1f01c18209 100644 --- a/packages/indy-vdr/src/anoncreds/utils/_tests_/identifiers.test.ts +++ b/packages/indy-vdr/src/anoncreds/utils/_tests_/identifiers.test.ts @@ -13,7 +13,7 @@ import { describe('identifiers', () => { describe('indyVdrAnonCredsRegistryIdentifierRegex', () => { - test('matches against a legacy indy did, schema id, credential definition id and revocation registry id', () => { + test('matches against a legacy schema id, credential definition id and revocation registry id', () => { const did = '7Tqg6BwSSWapxgUDm9KKgg' const schemaId = 'BQ42WeE24jFHeyGg8x9XAz:2:Medical Bill:1.0' const credentialDefinitionId = 'N7baRMcyvPwWc8v85CtZ6e:3:CL:100669:SCH Employee ID' diff --git a/packages/indy-vdr/src/anoncreds/utils/identifiers.ts b/packages/indy-vdr/src/anoncreds/utils/identifiers.ts index e7e1a2bd49..cc05d2b3bb 100644 --- a/packages/indy-vdr/src/anoncreds/utils/identifiers.ts +++ b/packages/indy-vdr/src/anoncreds/utils/identifiers.ts @@ -6,34 +6,28 @@ import { DID_INDY_REGEX } from '../../utils/did' const didIndyAnonCredsBase = - /(?did:indy:(?((?:[a-z][_a-z0-9-]*)(?::[a-z][_a-z0-9-]*)?)):(?([1-9A-HJ-NP-Za-km-z]{21,22})))\/anoncreds\/v0/ + /(did:indy:((?:[a-z][_a-z0-9-]*)(?::[a-z][_a-z0-9-]*)?):([1-9A-HJ-NP-Za-km-z]{21,22}))\/anoncreds\/v0/ // did:indy::/anoncreds/v0/SCHEMA// -const didIndySchemaIdRegex = new RegExp( - `^${didIndyAnonCredsBase.source}/SCHEMA/(?.+)/(?[0-9.]+)$` -) +const didIndySchemaIdRegex = new RegExp(`^${didIndyAnonCredsBase.source}/SCHEMA/(.+)/([0-9.]+)$`) // :2:: -const legacyIndySchemaIdRegex = - /^(?(?[a-zA-Z0-9]{21,22})):2:(?.+):(?[0-9.]+)$/ +const legacyIndySchemaIdRegex = /^([a-zA-Z0-9]{21,22}):2:(.+):([0-9.]+)$/ // did:indy::/anoncreds/v0/CLAIM_DEF// -const didIndyCredentialDefinitionIdRegex = new RegExp( - `^${didIndyAnonCredsBase.source}/CLAIM_DEF/(?[1-9][0-9]*)/(?.+)$` -) +const didIndyCredentialDefinitionIdRegex = new RegExp(`^${didIndyAnonCredsBase.source}/CLAIM_DEF/([1-9][0-9]*)/(.+)$`) // :3:CL:: -const legacyIndyCredentialDefinitionIdRegex = - /^(?(?[a-zA-Z0-9]{21,22})):3:CL:(?[1-9][0-9]*):(?.+)$/ +const legacyIndyCredentialDefinitionIdRegex = /^([a-zA-Z0-9]{21,22}):3:CL:([1-9][0-9]*):(.+)$/ // did:indy::/anoncreds/v0/REV_REG_DEF/// const didIndyRevocationRegistryIdRegex = new RegExp( - `^${didIndyAnonCredsBase.source}/REV_REG_DEF/(?[1-9][0-9]*)/(?.+)/(?.+)$` + `^${didIndyAnonCredsBase.source}/REV_REG_DEF/([1-9][0-9]*)/(.+)/(.+)$` ) // :4::3:CL::CL_ACCUM: const legacyIndyRevocationRegistryIdRegex = - /^(?(?[a-zA-Z0-9]{21,22})):4:[a-zA-Z0-9]{21,22}:3:CL:(?[1-9][0-9]*):(?.+):CL_ACCUM:(?.+)$/ + /^([a-zA-Z0-9]{21,22}):4:[a-zA-Z0-9]{21,22}:3:CL:([1-9][0-9]*):(.+):CL_ACCUM:(.+)$/ // combines both legacy and did:indy anoncreds identifiers and also the issuer id const indyVdrAnonCredsRegexes = [ @@ -58,7 +52,7 @@ const indyVdrAnonCredsRegexes = [ ] export const indyVdrAnonCredsRegistryIdentifierRegex = new RegExp( - indyVdrAnonCredsRegexes.map((r) => r.source.replace(/(\?<[a-zA-Z]+>)?/g, '')).join('|') + indyVdrAnonCredsRegexes.map((r) => r.source).join('|') ) export function getDidIndySchemaId(namespace: string, unqualifiedDid: string, name: string, version: string) { @@ -110,12 +104,33 @@ interface ParsedSchemaId { namespace?: string } -export function parseSchemaId(schemaId: string) { - const match = schemaId.match(didIndySchemaIdRegex) ?? schemaId.match(legacyIndySchemaIdRegex) - - if (!match) throw new Error(`Invalid schema id: ${schemaId}`) - - return match.groups as unknown as ParsedSchemaId +export function parseSchemaId(schemaId: string): ParsedSchemaId { + const didIndyMatch = schemaId.match(didIndySchemaIdRegex) + if (didIndyMatch) { + const [, did, namespace, namespaceIdentifier, schemaName, schemaVersion] = didIndyMatch + + return { + did, + namespaceIdentifier, + schemaName, + schemaVersion, + namespace, + } + } + + const legacyMatch = schemaId.match(legacyIndySchemaIdRegex) + if (legacyMatch) { + const [, did, schemaName, schemaVersion] = legacyMatch + + return { + did, + namespaceIdentifier: did, + schemaName, + schemaVersion, + } + } + + throw new Error(`Invalid schema id: ${schemaId}`) } interface ParsedCredentialDefinitionId { @@ -126,14 +141,33 @@ interface ParsedCredentialDefinitionId { namespace?: string } -export function parseCredentialDefinitionId(credentialDefinitionId: string) { - const match = - credentialDefinitionId.match(didIndyCredentialDefinitionIdRegex) ?? - credentialDefinitionId.match(legacyIndyCredentialDefinitionIdRegex) - - if (!match) throw new Error(`Invalid credential definition id: ${credentialDefinitionId}`) - - return match.groups as unknown as ParsedCredentialDefinitionId +export function parseCredentialDefinitionId(credentialDefinitionId: string): ParsedCredentialDefinitionId { + const didIndyMatch = credentialDefinitionId.match(didIndyCredentialDefinitionIdRegex) + if (didIndyMatch) { + const [, did, namespace, namespaceIdentifier, schemaSeqNo, tag] = didIndyMatch + + return { + did, + namespaceIdentifier, + schemaSeqNo, + tag, + namespace, + } + } + + const legacyMatch = credentialDefinitionId.match(legacyIndyCredentialDefinitionIdRegex) + if (legacyMatch) { + const [, did, schemaSeqNo, tag] = legacyMatch + + return { + did, + namespaceIdentifier: did, + schemaSeqNo, + tag, + } + } + + throw new Error(`Invalid credential definition id: ${credentialDefinitionId}`) } interface ParsedRevocationRegistryId { @@ -145,12 +179,34 @@ interface ParsedRevocationRegistryId { namespace?: string } -export function parseRevocationRegistryId(revocationRegistryId: string) { - const match = - revocationRegistryId.match(didIndyRevocationRegistryIdRegex) ?? - revocationRegistryId.match(legacyIndyRevocationRegistryIdRegex) - - if (!match) throw new Error(`Invalid revocation registry id: ${revocationRegistryId}`) - - return match.groups as unknown as ParsedRevocationRegistryId +export function parseRevocationRegistryId(revocationRegistryId: string): ParsedRevocationRegistryId { + const didIndyMatch = revocationRegistryId.match(didIndyRevocationRegistryIdRegex) + if (didIndyMatch) { + const [, did, namespace, namespaceIdentifier, schemaSeqNo, credentialDefinitionTag, revocationRegistryTag] = + didIndyMatch + + return { + did, + namespaceIdentifier, + schemaSeqNo, + credentialDefinitionTag, + revocationRegistryTag, + namespace, + } + } + + const legacyMatch = revocationRegistryId.match(legacyIndyRevocationRegistryIdRegex) + if (legacyMatch) { + const [, did, schemaSeqNo, credentialDefinitionTag, revocationRegistryTag] = legacyMatch + + return { + did, + namespaceIdentifier: did, + schemaSeqNo, + credentialDefinitionTag, + revocationRegistryTag, + } + } + + throw new Error(`Invalid revocation registry id: ${revocationRegistryId}`) }