diff --git a/src/controllers/endorser-transaction/EndorserTransactionController.ts b/src/controllers/endorser-transaction/EndorserTransactionController.ts index e6d3dd72..6bbdfade 100644 --- a/src/controllers/endorser-transaction/EndorserTransactionController.ts +++ b/src/controllers/endorser-transaction/EndorserTransactionController.ts @@ -10,11 +10,12 @@ import { import { Agent } from '@credo-ts/core' import { injectable } from 'tsyringe' -import { CredentialEnum } from '../../enums/enum' +import { CredentialEnum, EndorserMode } from '../../enums/enum' import ErrorHandlingService from '../../errorHandlingService' +import { BadRequestError } from '../../errors' import { DidNymTransaction, EndorserTransaction, WriteTransaction } from '../types' -import { Body, Controller, Post, Res, Route, Tags, TsoaResponse, Security } from 'tsoa' +import { Body, Controller, Post, Route, Tags, Security } from 'tsoa' @Tags('EndorserTransaction') @Route('/transactions') @@ -31,6 +32,12 @@ export class EndorserTransactionController extends Controller { @Post('/endorse') public async endorserTransaction(@Body() endorserTransaction: EndorserTransaction) { try { + if (!endorserTransaction.transaction) { + throw new BadRequestError('Transaction is required') + } + if (!endorserTransaction.endorserDid) { + throw new BadRequestError('EndorserDid is required') + } const signedTransaction = await this.agent.modules.indyVdr.endorseTransaction( endorserTransaction.transaction, endorserTransaction.endorserDid @@ -48,7 +55,7 @@ export class EndorserTransactionController extends Controller { const didCreateSubmitResult = await this.agent.dids.create({ did: didNymTransaction.did, options: { - endorserMode: 'external', + endorserMode: EndorserMode.External, endorsedTransaction: { nymRequest: didNymTransaction.nymRequest, }, @@ -63,8 +70,6 @@ export class EndorserTransactionController extends Controller { @Post('/write') public async writeSchemaAndCredDefOnLedger( - @Res() forbiddenError: TsoaResponse<400, { reason: string }>, - @Res() internalServerError: TsoaResponse<500, { message: string }>, @Body() writeTransaction: WriteTransaction ) { @@ -98,34 +103,42 @@ export class EndorserTransactionController extends Controller { }, endorsedTransaction?: string ) { - try { - const { issuerId, name, version, attributes } = schema - const { schemaState } = await this.agent.modules.anoncreds.registerSchema({ - options: { - endorserMode: 'external', - endorsedTransaction, - }, - schema: { - attrNames: attributes, - issuerId: issuerId, - name: name, - version: version, - }, - }) + if (!schema.issuerId) { + throw new BadRequestError('IssuerId is required') + } + if (!schema.name) { + throw new BadRequestError('Name is required') + } + if (!schema.version) { + throw new BadRequestError('Version is required') + } + if (!schema.attributes) { + throw new BadRequestError('Attributes is required') + } + const { issuerId, name, version, attributes } = schema + const { schemaState } = await this.agent.modules.anoncreds.registerSchema({ + options: { + endorserMode: EndorserMode.External, + endorsedTransaction, + }, + schema: { + attrNames: attributes, + issuerId: issuerId, + name: name, + version: version, + }, + }) - const indySchemaId = parseIndySchemaId(schemaState.schemaId) - const getSchemaUnqualifiedId = await getUnqualifiedSchemaId( - indySchemaId.namespaceIdentifier, - indySchemaId.schemaName, - indySchemaId.schemaVersion - ) - if (schemaState.state === CredentialEnum.Finished || schemaState.state === CredentialEnum.Action) { - schemaState.schemaId = getSchemaUnqualifiedId - } - return schemaState - } catch (error) { - throw ErrorHandlingService.handle(error) + const indySchemaId = parseIndySchemaId(schemaState.schemaId) + const getSchemaUnqualifiedId = await getUnqualifiedSchemaId( + indySchemaId.namespaceIdentifier, + indySchemaId.schemaName, + indySchemaId.schemaVersion + ) + if (schemaState.state === CredentialEnum.Finished || schemaState.state === CredentialEnum.Action) { + schemaState.schemaId = getSchemaUnqualifiedId } + return schemaState } public async submitCredDefOnLedger( @@ -138,30 +151,41 @@ export class EndorserTransactionController extends Controller { }, endorsedTransaction?: string ) { - try { - const { credentialDefinitionState } = await this.agent.modules.anoncreds.registerCredentialDefinition({ - credentialDefinition, - options: { - endorserMode: 'external', - endorsedTransaction: endorsedTransaction, - }, - }) + if (!credentialDefinition.schemaId) { + throw new BadRequestError('SchemaId is required') + } + if (!credentialDefinition.issuerId) { + throw new BadRequestError('IssuerId is required') + } + if (!credentialDefinition.tag) { + throw new BadRequestError('Tag is required') + } + if (!credentialDefinition.value) { + throw new BadRequestError('Value is required') + } + if (!credentialDefinition.type) { + throw new BadRequestError('Type is required') + } + const { credentialDefinitionState } = await this.agent.modules.anoncreds.registerCredentialDefinition({ + credentialDefinition, + options: { + endorserMode: EndorserMode.External, + endorsedTransaction: endorsedTransaction, + }, + }) - const indyCredDefId = parseIndyCredentialDefinitionId(credentialDefinitionState.credentialDefinitionId) - const getCredentialDefinitionId = await getUnqualifiedCredentialDefinitionId( - indyCredDefId.namespaceIdentifier, - indyCredDefId.schemaSeqNo, - indyCredDefId.tag - ) - if ( - credentialDefinitionState.state === CredentialEnum.Finished || - credentialDefinitionState.state === CredentialEnum.Action - ) { - credentialDefinitionState.credentialDefinitionId = getCredentialDefinitionId - } - return credentialDefinitionState - } catch (error) { - throw ErrorHandlingService.handle(error) + const indyCredDefId = parseIndyCredentialDefinitionId(credentialDefinitionState.credentialDefinitionId) + const getCredentialDefinitionId = await getUnqualifiedCredentialDefinitionId( + indyCredDefId.namespaceIdentifier, + indyCredDefId.schemaSeqNo, + indyCredDefId.tag + ) + if ( + credentialDefinitionState.state === CredentialEnum.Finished || + credentialDefinitionState.state === CredentialEnum.Action + ) { + credentialDefinitionState.credentialDefinitionId = getCredentialDefinitionId } + return credentialDefinitionState } } diff --git a/src/controllers/types.ts b/src/controllers/types.ts index 6c460163..a4650b5e 100644 --- a/src/controllers/types.ts +++ b/src/controllers/types.ts @@ -340,6 +340,7 @@ export interface CreateInvitationOptions { invitationDid?: string } +//todo:Add transaction type export interface EndorserTransaction { transaction: string | Record endorserDid: string @@ -350,6 +351,7 @@ export interface DidNymTransaction { nymRequest: string } +//todo:Add endorsedTransaction type export interface WriteTransaction { endorsedTransaction: string endorserDid?: string