-
Notifications
You must be signed in to change notification settings - Fork 18
fix: did, schema and cred-def controller #350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
13798a3
fix: did and schema controller
GHkrishna a34d936
fix: naming convention and remove comments
GHkrishna 482bd7d
fix: formatting
GHkrishna 5f8664f
fix: remove unused param
GHkrishna b707c76
fix: credential definition controller
GHkrishna 82c62a6
fix: error when cred-def failure
GHkrishna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
264 changes: 135 additions & 129 deletions
264
src/controllers/anoncreds/cred-def/CredentialDefinitionController.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,142 +1,148 @@ | ||
| // import type { RestAgentModules } from '../../../cliAgent' | ||
| // import type { SchemaId } from '../../examples' | ||
| import type { RestAgentModules } from '../../../cliAgent' | ||
| import type { SchemaId } from '../../examples' | ||
|
|
||
| // import { getUnqualifiedCredentialDefinitionId, parseIndyCredentialDefinitionId } from '@credo-ts/anoncreds' | ||
| // import { Agent } from '@credo-ts/core' | ||
| // import { Request as Req } from 'express' | ||
| // import { Body, Controller, Example, Get, Path, Post, Route, Tags, Security, Response, Request } from 'tsoa' | ||
| // import { injectable } from 'tsyringe' | ||
| import { getUnqualifiedCredentialDefinitionId, parseIndyCredentialDefinitionId } from '@credo-ts/anoncreds' | ||
| import { Agent } from '@credo-ts/core' | ||
| import { Request as Req } from 'express' | ||
| import { Body, Controller, Example, Get, Path, Post, Route, Tags, Security, Response, Request } from 'tsoa' | ||
| import { injectable } from 'tsyringe' | ||
|
|
||
| // import { CredentialEnum, EndorserMode, SchemaError, SCOPES } from '../../../enums' | ||
| // import ErrorHandlingService from '../../../errorHandlingService' | ||
| // import { ENDORSER_DID_NOT_PRESENT } from '../../../errorMessages' | ||
| // import { BadRequestError, InternalServerError, NotFoundError } from '../../../errors/errors' | ||
| // import { CredentialDefinitionExample, CredentialDefinitionId } from '../../examples' | ||
| import { CredentialEnum, EndorserMode, SchemaError, SCOPES } from '../../../enums' | ||
| import ErrorHandlingService from '../../../errorHandlingService' | ||
| import { ENDORSER_DID_NOT_PRESENT } from '../../../errorMessages' | ||
| import { BadRequestError, InternalServerError, NotFoundError } from '../../../errors/errors' | ||
| import { CredentialDefinitionExample, CredentialDefinitionId } from '../../examples' | ||
| import { | ||
| CredentialDefinitionStates, | ||
| GetCredentialDefinitionReturn, | ||
| RegisterCredentialDefinitionReturn, | ||
| } from '../../types' | ||
|
|
||
| // @Tags('Anoncreds - Credential Definitions') | ||
| // @Route('/anoncreds/credential-definitions') | ||
| // @Security('jwt', [SCOPES.TENANT_AGENT, SCOPES.DEDICATED_AGENT]) | ||
| // @injectable() | ||
| // export class CredentialDefinitionController extends Controller { | ||
| // /** | ||
| // * Retrieve credential definition by credential definition id | ||
| // * | ||
| // * @param credentialDefinitionId | ||
| // * @returns CredDef | ||
| // */ | ||
| // @Example(CredentialDefinitionExample) | ||
| // @Get('/:credentialDefinitionId') | ||
| // public async getCredentialDefinitionById( | ||
| // @Request() request: Req, | ||
| // @Path('credentialDefinitionId') credentialDefinitionId: CredentialDefinitionId, | ||
| // ) { | ||
| // try { | ||
| // const credentialDefinitionResult = | ||
| // await request.agent.modules.anoncreds.getCredentialDefinition(credentialDefinitionId) | ||
| @Tags('Anoncreds - Credential Definitions') | ||
| @Route('/anoncreds/credential-definitions') | ||
| @Security('jwt', [SCOPES.TENANT_AGENT, SCOPES.DEDICATED_AGENT]) | ||
| @injectable() | ||
| export class CredentialDefinitionController extends Controller { | ||
| /** | ||
| * Retrieve credential definition by credential definition id | ||
| * | ||
| * @param credentialDefinitionId | ||
| * @returns CredDef | ||
| */ | ||
| @Example(CredentialDefinitionExample) | ||
| @Get('/:credentialDefinitionId') | ||
| public async getCredentialDefinitionById( | ||
| @Request() request: Req, | ||
| @Path('credentialDefinitionId') credentialDefinitionId: CredentialDefinitionId, | ||
| ): Promise<GetCredentialDefinitionReturn> { | ||
| try { | ||
| const credentialDefinitionResult = | ||
| await request.agent.modules.anoncreds.getCredentialDefinition(credentialDefinitionId) | ||
|
|
||
| // if (credentialDefinitionResult.resolutionMetadata?.error === SchemaError.NotFound) { | ||
| // throw new NotFoundError(credentialDefinitionResult.resolutionMetadata.message) | ||
| // } | ||
| // const error = credentialDefinitionResult.resolutionMetadata?.error | ||
| if (credentialDefinitionResult.resolutionMetadata?.error === SchemaError.NotFound) { | ||
| throw new NotFoundError(credentialDefinitionResult.resolutionMetadata.message) | ||
| } | ||
| const error = credentialDefinitionResult.resolutionMetadata?.error | ||
|
|
||
| // if (error === 'invalid' || error === SchemaError.UnSupportedAnonCredsMethod) { | ||
| // throw new BadRequestError(credentialDefinitionResult.resolutionMetadata.message) | ||
| // } | ||
| if (error === 'invalid' || error === SchemaError.UnSupportedAnonCredsMethod) { | ||
| throw new BadRequestError(credentialDefinitionResult.resolutionMetadata.message) | ||
| } | ||
|
|
||
| // if (error !== undefined || credentialDefinitionResult.credentialDefinition === undefined) { | ||
| // throw new InternalServerError(credentialDefinitionResult.resolutionMetadata.message) | ||
| // } | ||
| if (error !== undefined || credentialDefinitionResult.credentialDefinition === undefined) { | ||
| throw new InternalServerError(credentialDefinitionResult.resolutionMetadata.message) | ||
| } | ||
|
|
||
| // return credentialDefinitionResult | ||
| // } catch (error) { | ||
| // throw ErrorHandlingService.handle(error) | ||
| // } | ||
| // } | ||
| return credentialDefinitionResult | ||
| } catch (error) { | ||
| throw ErrorHandlingService.handle(error) | ||
| } | ||
| } | ||
|
|
||
| // /** | ||
| // * Creates a new credential definition. | ||
| // * | ||
| // * @param credentialDefinitionRequest | ||
| // * @returns CredDef | ||
| // */ | ||
| // @Example(CredentialDefinitionExample) | ||
| // @Response(200, 'Action required') | ||
| // @Response(202, 'Wait for action to complete') | ||
| // @Post('/') | ||
| // public async createCredentialDefinition( | ||
| // @Request() request: Req, | ||
| // @Body() | ||
| // credentialDefinitionRequest: { | ||
| // issuerId: string | ||
| // schemaId: SchemaId | ||
| // tag: string | ||
| // endorse?: boolean | ||
| // endorserDid?: string | ||
| // }, | ||
| // ) { | ||
| // try { | ||
| // const { issuerId, schemaId, tag, endorse, endorserDid } = credentialDefinitionRequest | ||
| // credentialDefinitionRequest.endorse = credentialDefinitionRequest.endorse | ||
| // ? credentialDefinitionRequest.endorse | ||
| // : false | ||
| // const credDef = { | ||
| // issuerId, | ||
| // schemaId, | ||
| // tag, | ||
| // type: 'CL', | ||
| // } | ||
| // const credentialDefinitionPayload = { | ||
| // credentialDefinition: credDef, | ||
| // options: { | ||
| // endorserMode: '', | ||
| // endorserDid: '', | ||
| // supportRevocation: false, | ||
| // }, | ||
| // } | ||
| // if (!endorse) { | ||
| // credentialDefinitionPayload.options.endorserMode = EndorserMode.Internal | ||
| // credentialDefinitionPayload.options.endorserDid = issuerId | ||
| // } else { | ||
| // if (!endorserDid) { | ||
| // throw new BadRequestError(ENDORSER_DID_NOT_PRESENT) | ||
| // } | ||
| // credentialDefinitionPayload.options.endorserMode = EndorserMode.External | ||
| // credentialDefinitionPayload.options.endorserDid = endorserDid ? endorserDid : '' | ||
| // } | ||
| /** | ||
| * Creates a new credential definition. | ||
| * | ||
| * @param credentialDefinitionRequest | ||
| * @returns CredDef | ||
| */ | ||
| @Example(CredentialDefinitionExample) | ||
| @Response(200, 'Action required') | ||
| @Response(202, 'Wait for action to complete') | ||
| @Post('/') | ||
| public async createCredentialDefinition( | ||
| @Request() request: Req, | ||
| @Body() | ||
| credentialDefinitionRequest: { | ||
| issuerId: string | ||
| schemaId: SchemaId | ||
| tag: string | ||
| endorse?: boolean | ||
| endorserDid?: string | ||
| }, | ||
| ): Promise<RegisterCredentialDefinitionReturn | CredentialDefinitionStates> { | ||
| try { | ||
| const { issuerId, schemaId, tag, endorse, endorserDid } = credentialDefinitionRequest | ||
| credentialDefinitionRequest.endorse = credentialDefinitionRequest.endorse | ||
| ? credentialDefinitionRequest.endorse | ||
| : false | ||
| const credDef = { | ||
| issuerId, | ||
| schemaId, | ||
| tag, | ||
| type: 'CL', | ||
| } | ||
| const credentialDefinitionPayload = { | ||
| credentialDefinition: credDef, | ||
| options: { | ||
| endorserMode: '', | ||
| endorserDid: '', | ||
| supportRevocation: false, | ||
| }, | ||
| } | ||
| if (!endorse) { | ||
| credentialDefinitionPayload.options.endorserMode = EndorserMode.Internal | ||
| credentialDefinitionPayload.options.endorserDid = issuerId | ||
| } else { | ||
| if (!endorserDid) { | ||
| throw new BadRequestError(ENDORSER_DID_NOT_PRESENT) | ||
| } | ||
| credentialDefinitionPayload.options.endorserMode = EndorserMode.External | ||
| credentialDefinitionPayload.options.endorserDid = endorserDid ? endorserDid : '' | ||
| } | ||
|
|
||
| // const registerCredentialDefinitionResult = | ||
| // await request.agent.modules.anoncreds.registerCredentialDefinition(credentialDefinitionPayload) | ||
| const registerCredentialDefinitionResult = | ||
| await request.agent.modules.anoncreds.registerCredentialDefinition(credentialDefinitionPayload) | ||
|
|
||
| // if (registerCredentialDefinitionResult.credentialDefinitionState.state === CredentialEnum.Failed) { | ||
| // throw new InternalServerError('Falied to register credef on ledger') | ||
| // } | ||
| if (registerCredentialDefinitionResult.credentialDefinitionState.state === CredentialEnum.Failed) { | ||
| const failureReason = registerCredentialDefinitionResult.credentialDefinitionState?.reason ?? 'Failed to register credential definition on ledger' | ||
| throw new InternalServerError(failureReason) | ||
| } | ||
|
|
||
| // if (registerCredentialDefinitionResult.credentialDefinitionState.state === CredentialEnum.Wait) { | ||
| // // The request has been accepted for processing, but the processing has not been completed. | ||
| // this.setStatus(202) | ||
| // return registerCredentialDefinitionResult | ||
| // } | ||
| if (registerCredentialDefinitionResult.credentialDefinitionState.state === CredentialEnum.Wait) { | ||
| // The request has been accepted for processing, but the processing has not been completed. | ||
| this.setStatus(202) | ||
| return registerCredentialDefinitionResult | ||
| } | ||
|
|
||
| // if (registerCredentialDefinitionResult.credentialDefinitionState.state === CredentialEnum.Action) { | ||
| // return registerCredentialDefinitionResult | ||
| // } | ||
| if (registerCredentialDefinitionResult.credentialDefinitionState.state === CredentialEnum.Action) { | ||
| return registerCredentialDefinitionResult | ||
| } | ||
|
|
||
| // // TODO: Return uniform response for both Internally and Externally endorsed Schemas | ||
| // if (!endorse) { | ||
| // const indyCredDefId = parseIndyCredentialDefinitionId( | ||
| // registerCredentialDefinitionResult.credentialDefinitionState.credentialDefinitionId as string, | ||
| // ) | ||
| // const getCredentialDefinitionId = await getUnqualifiedCredentialDefinitionId( | ||
| // indyCredDefId.namespaceIdentifier, | ||
| // indyCredDefId.schemaSeqNo, | ||
| // indyCredDefId.tag, | ||
| // ) | ||
| // registerCredentialDefinitionResult.credentialDefinitionState.credentialDefinitionId = getCredentialDefinitionId | ||
| // return registerCredentialDefinitionResult.credentialDefinitionState | ||
| // } | ||
| // return registerCredentialDefinitionResult | ||
| // } catch (error) { | ||
| // throw ErrorHandlingService.handle(error) | ||
| // } | ||
| // } | ||
| // } | ||
| // TODO: Return uniform response for both Internally and Externally endorsed Schemas | ||
| if (!endorse) { | ||
| const indyCredDefId = parseIndyCredentialDefinitionId( | ||
| registerCredentialDefinitionResult.credentialDefinitionState.credentialDefinitionId as string, | ||
| ) | ||
| const getCredentialDefinitionId = await getUnqualifiedCredentialDefinitionId( | ||
| indyCredDefId.namespaceIdentifier, | ||
| indyCredDefId.schemaSeqNo, | ||
| indyCredDefId.tag, | ||
| ) | ||
| registerCredentialDefinitionResult.credentialDefinitionState.credentialDefinitionId = getCredentialDefinitionId | ||
| return registerCredentialDefinitionResult.credentialDefinitionState | ||
|
GHkrishna marked this conversation as resolved.
|
||
| } | ||
| return registerCredentialDefinitionResult | ||
| } catch (error) { | ||
| throw ErrorHandlingService.handle(error) | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.