From 9131bc7b53aa32d69ff3369036185d1b4044a537 Mon Sep 17 00:00:00 2001 From: Andres Vallecilla Date: Wed, 18 Dec 2024 22:04:13 -0500 Subject: [PATCH] feat: add apiClient revocation registry --- packages/client/src/ApiClient.ts | 8 ++- .../src/services/CredentialTypeService.ts | 9 --- .../src/services/RevocationRegistryService.ts | 70 +++++++++++++++++++ .../CreateRevocationRegistryDto.ts | 3 +- packages/model/src/types.ts | 2 +- 5 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 packages/client/src/services/RevocationRegistryService.ts diff --git a/packages/client/src/ApiClient.ts b/packages/client/src/ApiClient.ts index 1e8af97..e2148e8 100644 --- a/packages/client/src/ApiClient.ts +++ b/packages/client/src/ApiClient.ts @@ -1,5 +1,6 @@ // src/ApiClient.ts +import { RevocationRegistryService } from './services' import { CredentialTypeService } from './services/CredentialTypeService' import { MessageService } from './services/MessageService' import { ApiVersion } from './types/enums' @@ -23,18 +24,20 @@ import { ApiVersion } from './types/enums' * const apiClient = new ApiClient('http://localhost', ApiVersion.V1) * * // Example to query available credentials - * await apiClient.credentialType.getAllCredentialTypes() + * await apiClient.credentialType.getAll() * * // Example to send a message - * apiClient.message.sendMessage(message: BaseMessage) + * apiClient.message.send(message: BaseMessage) * * The `ApiClient` class provides easy methods for interacting with: * - `message`: Send and manage messages. * - `credentialType`: Query and manage credential types. + * - `revocationRegistry`: Query and manage the revocation registry for credential definitions. */ export class ApiClient { public readonly messages: MessageService public readonly credentialTypes: CredentialTypeService + public readonly revocationRegistry: RevocationRegistryService constructor( private baseURL: string, @@ -42,5 +45,6 @@ export class ApiClient { ) { this.messages = new MessageService(baseURL, version) this.credentialTypes = new CredentialTypeService(baseURL, version) + this.revocationRegistry = new RevocationRegistryService(baseURL, version) } } diff --git a/packages/client/src/services/CredentialTypeService.ts b/packages/client/src/services/CredentialTypeService.ts index ecface2..85c47c7 100644 --- a/packages/client/src/services/CredentialTypeService.ts +++ b/packages/client/src/services/CredentialTypeService.ts @@ -63,13 +63,4 @@ export class CredentialTypeService { return types.map(value => value as CredentialTypeInfo) } - - public async createRevocation(credentialDefinitionId: string): Promise { - const response = await fetch(`${this.url}/revoke`, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ credentialDefinitionId }), - }) - return response.text() - } } diff --git a/packages/client/src/services/RevocationRegistryService.ts b/packages/client/src/services/RevocationRegistryService.ts new file mode 100644 index 0000000..d406193 --- /dev/null +++ b/packages/client/src/services/RevocationRegistryService.ts @@ -0,0 +1,70 @@ +// src/services/RevocationRegistryService.ts + +import { RevocationRegistryInfo } from '@2060.io/service-agent-model' +import { Logger } from 'tslog' + +import { ApiVersion } from '../types/enums' + +const logger = new Logger({ + name: 'RevocationRegistryService', + type: 'pretty', + prettyLogTemplate: '{{logLevelName}} [{{name}}]: ', +}) + +/** + * `RevocationRegistryService` class for managing credential types and interacting with + * the available endpoints related to credential types in the Agent Service. + * + * This class provides methods for querying, creating, and managing revocation registry on credential types. + * For a list of available endpoints and functionality, refer to the methods within this class. + */ +export class RevocationRegistryService { + private url: string + + constructor( + private baseURL: string, + private version: ApiVersion, + ) { + this.url = `${this.baseURL.replace(/\/$/, '')}/${this.version}/credential-types` + } + + public async create(options: RevocationRegistryInfo): Promise { + logger.info(`Creating revocation registry with credentialDefinitionId: ${JSON.stringify(options)}`) + const response = await fetch(`${this.url}/revocationRegistry`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ options }), + }) + return response.text() + } + + public async get(credentialDefinitionId: string): Promise { + logger.info(`Searching revocation registry with credentialDefinitionId: ${credentialDefinitionId}`) + const response = await fetch(`${this.url}/revocationRegistry`, { + method: 'GET', + headers: { accept: 'application/json', 'Content-Type': 'application/json' }, + body: JSON.stringify({ credentialDefinitionId }), + }) + + if (!response.ok) { + throw new Error(`Failed to fetch revocation definitions: ${response.statusText}`) + } + + return (await response.json()) as string[] + } + + public async getAll(): Promise { + logger.info(`Searching all revocation registry`) + const response = await fetch(`${this.url}/revocationRegistry`, { + method: 'GET', + headers: { accept: 'application/json', 'Content-Type': 'application/json' }, + body: JSON.stringify({ credentialDefinitionId: '' }), + }) + + if (!response.ok) { + throw new Error(`Failed to fetch revocation definitions: ${response.statusText}`) + } + + return (await response.json()) as string[] + } +} diff --git a/packages/main/src/controllers/credentials/CreateRevocationRegistryDto.ts b/packages/main/src/controllers/credentials/CreateRevocationRegistryDto.ts index 6e19116..1997a1f 100644 --- a/packages/main/src/controllers/credentials/CreateRevocationRegistryDto.ts +++ b/packages/main/src/controllers/credentials/CreateRevocationRegistryDto.ts @@ -1,5 +1,5 @@ import { ApiProperty } from '@nestjs/swagger' -import { IsString, IsNotEmpty, IsNumber } from 'class-validator' +import { IsString, IsNotEmpty, IsNumber, IsOptional } from 'class-validator' export class CreateRevocationRegistryDto { @ApiProperty({ @@ -18,5 +18,6 @@ export class CreateRevocationRegistryDto { }) @IsNumber() @IsNotEmpty() + @IsOptional() maximumCredentialNumber: number = 1000 } diff --git a/packages/model/src/types.ts b/packages/model/src/types.ts index 5917016..f696ca8 100644 --- a/packages/model/src/types.ts +++ b/packages/model/src/types.ts @@ -47,7 +47,7 @@ export interface CredentialTypeInfo extends CreateCredentialTypeOptions { id: string } -export interface RevocationRegistry { +export interface RevocationRegistryInfo { credentialDefinitionId: string maximumCredentialNumber: number }