-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial presentation controller
- Loading branch information
Showing
4 changed files
with
128 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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
122 changes: 122 additions & 0 deletions
122
src/controllers/presentations/PresentationsController.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { | ||
BadRequestException, | ||
Controller, | ||
Delete, | ||
Get, | ||
InternalServerErrorException, | ||
Logger, | ||
NotFoundException, | ||
Param, | ||
} from '@nestjs/common' | ||
import { ApiTags } from '@nestjs/swagger' | ||
|
||
import { Claim } from '../../model' | ||
import { AgentService } from '../../services/AgentService' | ||
import { CredentialTypeInfo } from '../types' | ||
|
||
@ApiTags('presentations') | ||
@Controller({ | ||
path: 'presentations', | ||
version: '1', | ||
}) | ||
export class PresentationsController { | ||
private readonly logger = new Logger(PresentationsController.name) | ||
|
||
constructor(private readonly agentService: AgentService) {} | ||
|
||
/** | ||
* Get all created credential types | ||
* | ||
* @returns | ||
*/ | ||
@Get('/') | ||
public async getAllCredentialTypes(): Promise<CredentialTypeInfo[]> { | ||
const agent = await this.agentService.getAgent() | ||
|
||
const credentialDefinitions = await agent.modules.anoncreds.getCreatedCredentialDefinitions({}) | ||
|
||
return Promise.all( | ||
credentialDefinitions.map(async record => { | ||
const schemaResult = await agent.modules.anoncreds.getSchema(record.credentialDefinition.schemaId) | ||
|
||
const schema = schemaResult.schema | ||
|
||
return { | ||
id: record.credentialDefinitionId, | ||
name: (record.getTag('name') as string) ?? schema?.name, | ||
version: (record.getTag('version') as string) ?? schema?.version, | ||
attributes: schema?.attrNames || [], | ||
} | ||
}), | ||
) | ||
} | ||
|
||
/** | ||
* Delete a presentation exchange record | ||
* | ||
* @param proofExchangeId Proof Exchange Id | ||
*/ | ||
@Delete('/:proofExchangeId') | ||
public async deleteProofExchangeById(@Param('proofExchangeId') proofExchangeId: string) { | ||
const agent = await this.agentService.getAgent() | ||
await agent.proofs.deleteById(proofExchangeId, { deleteAssociatedDidCommMessages: true }) | ||
} | ||
|
||
/** | ||
* Export a credential type, including its underlying cryptographic data for importing it in another instance | ||
* | ||
* @param credentialTypeId Credential Type Id | ||
* @returns ConnectionRecord | ||
*/ | ||
@Get('/:proofExchangeId') | ||
public async getPresentationById(@Param('proofExchangeId') proofExchangeId: string) { | ||
const agent = await this.agentService.getAgent() | ||
|
||
if (!proofExchangeId) { | ||
throw new BadRequestException({ reason: 'proofExchangeId is required' }) | ||
} | ||
|
||
const record = await agent.proofs.findById(proofExchangeId) | ||
|
||
if (!record) { | ||
throw new NotFoundException({ reason: `proof exchange with id "${proofExchangeId}" not found.` }) | ||
} | ||
|
||
try { | ||
const formatData = await agent.proofs.getFormatData(record.id) | ||
|
||
const revealedAttributes = | ||
formatData.presentation?.anoncreds?.requested_proof.revealed_attrs ?? | ||
formatData.presentation?.indy?.requested_proof.revealed_attrs | ||
|
||
const revealedAttributeGroups = | ||
formatData.presentation?.anoncreds?.requested_proof?.revealed_attr_groups ?? | ||
formatData.presentation?.indy?.requested_proof.revealed_attr_groups | ||
|
||
const claims: Claim[] = [] | ||
if (revealedAttributes) { | ||
for (const [name, value] of Object.entries(revealedAttributes)) { | ||
claims.push(new Claim({ name, value: value.raw })) | ||
} | ||
} | ||
|
||
if (revealedAttributeGroups) { | ||
for (const [, groupAttributes] of Object.entries(revealedAttributeGroups)) { | ||
for (const attrName in groupAttributes.values) { | ||
claims.push(new Claim({ name: attrName, value: groupAttributes.values[attrName].raw })) | ||
} | ||
} | ||
} | ||
return { | ||
claims, | ||
verified: record.isVerified ?? false, | ||
state: record.state, | ||
proofExchangeId: record.id, | ||
threadId: record.threadId, | ||
updatedAt: record.updatedAt, | ||
} | ||
} catch (error) { | ||
throw new InternalServerErrorException(error) | ||
} | ||
} | ||
} |
This file contains 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