Skip to content

Commit

Permalink
refactor: error handling for endorser controller (#141)
Browse files Browse the repository at this point in the history
* refactor: error handling of endorser controller

Signed-off-by: pranalidhanavade <[email protected]>

* resolved: comments on pull request

Signed-off-by: pranalidhanavade <[email protected]>

---------

Signed-off-by: pranalidhanavade <[email protected]>
  • Loading branch information
pranalidhanavade authored Jul 2, 2024
1 parent c759de7 commit 290566a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 54 deletions.
132 changes: 78 additions & 54 deletions src/controllers/endorser-transaction/EndorserTransactionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand All @@ -48,7 +55,7 @@ export class EndorserTransactionController extends Controller {
const didCreateSubmitResult = await this.agent.dids.create<IndyVdrDidCreateOptions>({
did: didNymTransaction.did,
options: {
endorserMode: 'external',
endorserMode: EndorserMode.External,
endorsedTransaction: {
nymRequest: didNymTransaction.nymRequest,
},
Expand All @@ -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
) {
Expand Down Expand Up @@ -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(
Expand All @@ -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
}
}
2 changes: 2 additions & 0 deletions src/controllers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ export interface CreateInvitationOptions {
invitationDid?: string
}

//todo:Add transaction type
export interface EndorserTransaction {
transaction: string | Record<string, unknown>
endorserDid: string
Expand All @@ -350,6 +351,7 @@ export interface DidNymTransaction {
nymRequest: string
}

//todo:Add endorsedTransaction type
export interface WriteTransaction {
endorsedTransaction: string
endorserDid?: string
Expand Down

0 comments on commit 290566a

Please sign in to comment.