Skip to content

Commit

Permalink
feat: implement proving a presentation and handle for the non-impleme…
Browse files Browse the repository at this point in the history
…nted methods

Signed-off-by: Nam Hoang <hoangxuannam160493@gmail.com>
  • Loading branch information
namhoang1604 committed Jun 23, 2023
1 parent 9163b72 commit 91475b7
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
104 changes: 104 additions & 0 deletions packages/vc-api/src/controllers/holder-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import {
IAgent,
UniqueVerifiableCredential,
UniqueVerifiablePresentation,
UnsignedPresentation,
VerifiableCredential,
VerifiablePresentation,
} from '@vckit/core-types';
import { Response } from 'express';
import { errorHandler } from '../error-handler.js';
import { RequestWithAgent } from '../types/request-type.js';
import { configuration } from '../config/index.js';

/**
* Retrieves a specific verifiable credential based on its ID.
Expand Down Expand Up @@ -81,6 +84,19 @@ export const deleteCredential = async (
res.status(error.code).json({ error: error.message });
}
};
/**
* Derives a verifiable credential is not implemented.
* @param {RequestWithAgent} req - The request object.
* @param {Response} res - The response object.
* @throws {Error} If the agent is not available.
*/
export const deriveCredential = async (
req: RequestWithAgent,
res: Response
) => {
if (!req.agent) throw Error('Agent not available');
res.status(501).json({ error: 'Not implemented' });
};

/**
* Retrieves a specific verifiable presentation based on its ID.
Expand Down Expand Up @@ -132,6 +148,77 @@ export const getPresentations = async (
}
};

/**
* Proves a verifiable presentation.
* @public
* @param {RequestWithAgent} req - The request object.
* @param {Response} res - The response object.
* @throws {Error} If the agent is not available.
*/
export const provePresentation = async (
req: RequestWithAgent,
res: Response
) => {
if (!req.agent) throw Error('Agent not available');
try {
const result = await proveVerifiablefPresentation(req.agent, req.body);
res.status(201).json(result);
} catch (e) {
const error = errorHandler(e);
res.status(error.code).json({ error: error.message });
}
};

/**
* delete a verifiable presentation is not implemented.
* @param {RequestWithAgent} req - The request object.
* @param {Response} res - The response object.
* @throws {Error} If the agent is not available.
*/
export const deletePresentation = async (
req: RequestWithAgent,
res: Response
) => {
if (!req.agent) throw Error('Agent not available');
res.status(501).json({ error: 'Not implemented' });
};

/**
* get exchanges is not implemented.
* @param {RequestWithAgent} req - The request object.
* @param {Response} res - The response object.
* @throws {Error} If the agent is not available.
*/
export const getExchanges = async (req: RequestWithAgent, res: Response) => {
if (!req.agent) throw Error('Agent not available');
res.status(501).json({ error: 'Not implemented' });
};

/**
* initiate an exchange is not implemented.
* @param {RequestWithAgent} req - The request object.
* @param {Response} res - The response object.
* @throws {Error} If the agent is not available.
*/
export const initiateExchange = async (
req: RequestWithAgent,
res: Response
) => {
if (!req.agent) throw Error('Agent not available');
res.status(501).json({ error: 'Not implemented' });
};

/**
* receive the exchange is not implemented.
* @param {RequestWithAgent} req - The request object.
* @param {Response} res - The response object.
* @throws {Error} If the agent is not available.
*/
export const receiveExchange = async (req: RequestWithAgent, res: Response) => {
if (!req.agent) throw Error('Agent not available');
res.status(501).json({ error: 'Not implemented' });
};

const getUniqueVerifiableCredential = async (
agent: IAgent,
id: string
Expand Down Expand Up @@ -197,3 +284,20 @@ const getVerifiablePresentations = async (agent: IAgent) => {
presentation.verifiablePresentation
);
};

const proveVerifiablefPresentation = async (
agent: IAgent,
data: { presentation: UnsignedPresentation; options: any }
): Promise<VerifiablePresentation> => {
const params = {
presentation: data.presentation,
...configuration,
...data.options,
};

const result: VerifiablePresentation = await agent.execute(
'createVerifiablePresentation',
params
);
return result;
};
2 changes: 1 addition & 1 deletion packages/vc-api/src/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface VCApiError {
export const errorHandler = (error: Error): VCApiError => {
let vcApiError: VCApiError;
const errorType = error.message.split(':')[0];
const errorMessage = error.message.split(':')[1];
const errorMessage = error.message.split(':')[1]?.trim();
switch (errorType) {
case 'not_found':
return (vcApiError = {
Expand Down
18 changes: 18 additions & 0 deletions packages/vc-api/src/holder-router.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Router, json } from 'express';
import {
deleteCredential,
deletePresentation,
deriveCredential,
getCredential,
getCredentials,
getExchanges,
getPresentation,
getPresentations,
initiateExchange,
provePresentation,
receiveExchange,
} from './controllers/holder-controller.js';

/**
Expand All @@ -23,9 +29,21 @@ export const HolderRouter = (): Router => {

router.delete('/credentials/:id', deleteCredential);

router.post('/credentials/derive', deriveCredential);

router.get('/presentations/:id', getPresentation);

router.get('/presentations', getPresentations);

router.post('/presentations/prove', provePresentation);

router.delete('/presentations/:id', deletePresentation);

router.get('/exchanges', getExchanges);

router.post('/exchanges/:exchangeId', initiateExchange);

router.post('/exchanges/:exchangeId/:transactionId', receiveExchange);

return router;
};

0 comments on commit 91475b7

Please sign in to comment.