Skip to content
Snippets Groups Projects
helpers.ts 2.76 KiB
Newer Older
  • Learn to ignore specific revisions
  • import {
      CredentialExchangeRecord,
    } from '@aries-framework/core';
    
    export function parseSchema(schemaId?: string): {
      name: string;
      version: string;
    } {
      let name = 'Credential';
      let version = '';
      if (schemaId) {
        const schemaIdRegex = /(.*?):([0-9]):([a-zA-Z .\-_0-9]+):([a-z0-9._-]+)$/;
        const schemaIdParts = schemaId.match(schemaIdRegex);
        if (schemaIdParts?.length === 5) {
          name = `${schemaIdParts?.[3].replace(/_|-/g, ' ')}`
            .split(' ')
            .map(
              schemaIdPart =>
                schemaIdPart.charAt(0).toUpperCase() + schemaIdPart.substring(1),
            )
            .join(' ');
          version = schemaIdParts?.[4];
        }
      }
      return { name, version };
    }
    
    export function parseCredDef(credentialDefinitionId?: string): {
      credName: string;
    } {
      let credName = '';
      if (credentialDefinitionId) {
        const credDefIdRegex =
          /^([a-zA-Z0-9]{21,22}):3:CL:(([1-9][0-9]*)|([a-zA-Z0-9]{21,22}:2:.+:[0-9.]+)):(.+)?$/;
        const credDefParts = credentialDefinitionId.match(credDefIdRegex);
    
        if (credDefParts?.length === 6) {
          credName = `${credDefParts?.[5].replace(/_|-/g, ' ')}`
            .split(' ')
            .map(
              credDefIdPart =>
                credDefIdPart.charAt(0).toUpperCase() + credDefIdPart.substring(1),
            )
            .join(' ');
        }
      }
      return { credName };
    }
    
    export function credentialSchema(
      credential: CredentialExchangeRecord,
    ): string | undefined {
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      console.log('Credential SCHEMA: ', credential.metadata);
      return undefined;
      // return credential.metadata.get(CredentialMetadataKeys.IndyCredential)
      //   ?.schemaId;
    
    }
    
    export function credentialDefinition(
      credential: CredentialExchangeRecord,
    ): string | undefined {
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      return undefined;
      // return credential.metadata.get(CredentialMetadataKeys.IndyCredential)
      //   ?.credentialDefinitionId;
    
    }
    
    export function parsedSchema(credential: CredentialExchangeRecord): {
      name: string;
      version: string;
    } {
      return parseSchema(credentialSchema(credential));
    }
    
    export function parsedCredentialDefinition(
      credential: CredentialExchangeRecord,
    ): { credName: string; } {
      return parseCredDef(credentialDefinition(credential));
    }
    
    export function hashCode(s: string): number {
    
      if (!s) {
        return 0;
      }
    
      return s
        .split('')
        .reduce((hash, char) => char.charCodeAt(0) + ((hash << 5) - hash), 0);
    }
    
    export function hashToRGBA(i: number) {
      const colour = (i & 0x00ffffff).toString(16).toUpperCase();
      return `#${'00000'.substring(0, 6 - colour.length)}${colour}`;
    }
    
    export const getCredDefName = (credentialDefinitionId: string) => {
      const data = credentialDefinitionId.split(':');
      return data[data.length - 1];
    };
    
    export const getSchemaNameFromSchemaId = (schemaId: string) => {
      const data = schemaId.split(':');
      return data[data.length - 1];
    };