Skip to content

Commit

Permalink
feat: adds verify data rpc method (#133)
Browse files Browse the repository at this point in the history
* feat: add verifyData rpc method

* feat: add testVC generation, fix verifyData tests

* fix: update createTestVCs function

* fix: update tests to use generated testVC

* fix: move test constants for VC creation

* chore: remove some // eslint-disable comments

* fix: remove unnecessary meta field

---------

Co-authored-by: martines3000 <domajnko.martin@gmail.com>
  • Loading branch information
tadejpodrekar and martines3000 authored Mar 14, 2023
1 parent 8cd8a7a commit 48b8a44
Show file tree
Hide file tree
Showing 13 changed files with 680 additions and 167 deletions.
6 changes: 5 additions & 1 deletion packages/snap/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ module.exports = {
files: ['tests/**/*.ts'],
plugins: ['jest'],
extends: ['plugin:jest/recommended'],
rules: { 'jest/prefer-expect-assertions': 'off' },
rules: {
'jest/prefer-expect-assertions': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
env: { jest: true },
},
],
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/blockchain-lab-um/ssi-snap.git"
},
"source": {
"shasum": "pDJPc8SMEbcubigAumQtNEO+0ETsKwsphZiabVJfzpc=",
"shasum": "G298n15J/o5OYCTwpitWR2cSqr8jqgL5OFCCwe5UvlI=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
6 changes: 6 additions & 0 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { createVP } from './rpc/vc/createVP';
import { deleteVC } from './rpc/vc/deleteVC';
import { queryVCs } from './rpc/vc/queryVCs';
import { saveVC } from './rpc/vc/saveVC';
import { verifyData } from './rpc/vc/verifyData';
import { getAvailableVCStores } from './rpc/vcStore/getAvailableVCStores';
import { setVCStore } from './rpc/vcStore/setVCStore';
import { getAddressKeyDeriver } from './utils/keyPair';
Expand All @@ -22,6 +23,7 @@ import {
isValidSaveVCRequest,
isValidSetVCStoreRequest,
isValidSwitchMethodRequest,
isValidVerifyDataRequest,
} from './utils/params';
import { getCurrentAccount, setAccountPublicKey } from './utils/snapUtils';
import {
Expand Down Expand Up @@ -118,6 +120,10 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
isValidResolveDIDRequest(request.params);
res = await resolveDID(request.params.did);
return ResultObject.success(res);
case 'verifyData':
isValidVerifyDataRequest(request.params);
res = await verifyData(apiParams, request.params);
return ResultObject.success(res);
default:
throw new Error('Method not found.');
}
Expand Down
21 changes: 21 additions & 0 deletions packages/snap/src/rpc/vc/verifyData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { VerifyDataRequestParams } from '@blockchain-lab-um/ssi-snap-types';
import { IVerifyResult } from '@veramo/core';

import { ApiParams } from '../../interfaces';
import { veramoVerifyData } from '../../utils/veramoUtils';

export async function verifyData(
params: ApiParams,
args: VerifyDataRequestParams
): Promise<boolean | IVerifyResult> {
const { snap, ethereum } = params;
const verbose = args.verbose || false;

const res = await veramoVerifyData({
snap,
ethereum,
data: args,
});
if (res.error) throw res.error as Error;
return verbose ? res : res.verified;
}
16 changes: 16 additions & 0 deletions packages/snap/src/utils/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SaveVCRequestParams,
SetVCStoreRequestParams,
SwitchMethodRequestParams,
VerifyDataRequestParams,
isAvailableMethods,
isAvailableVCStores,
isSupportedProofFormat,
Expand Down Expand Up @@ -327,3 +328,18 @@ export function isValidResolveDIDRequest(

throw new Error('Invalid ResolveDID request');
}

export function isValidVerifyDataRequest(
params: unknown
): asserts params is VerifyDataRequestParams {
const param = params as VerifyDataRequestParams;
if (
param !== null &&
typeof param === 'object' &&
(('credential' in param && param.credential !== null) ||
('presentation' in param && param.presentation !== null))
)
return;

throw new Error('Invalid VerifyData request');
}
34 changes: 34 additions & 0 deletions packages/snap/src/utils/veramoUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CreateVPRequestParams,
QueryVCsOptions,
QueryVCsRequestResult,
VerifyDataRequestParams,
} from '@blockchain-lab-um/ssi-snap-types';
import {
Filter,
Expand All @@ -13,6 +14,7 @@ import { MetaMaskInpageProvider } from '@metamask/providers';
import { SnapsGlobalObject } from '@metamask/snaps-types';
import {
IIdentifier,
IVerifyResult,
MinimalImportableKey,
VerifiablePresentation,
W3CVerifiableCredential,
Expand Down Expand Up @@ -204,3 +206,35 @@ export async function veramoCreateVP(
}
return null;
}

export async function veramoVerifyData(args: {
snap: SnapsGlobalObject;
ethereum: MetaMaskInpageProvider;
data: VerifyDataRequestParams;
}): Promise<IVerifyResult> {
try {
const { snap, ethereum, data } = args;
const { credential, presentation } = data;

const agent = await getAgent(snap, ethereum);

if (credential) {
const vcResult = (await agent.verifyCredential({
credential,
})) as IVerifyResult;
return vcResult;
}
if (presentation) {
const vpResult = (await agent.verifyPresentation({
presentation,
})) as IVerifyResult;
return vpResult;
}
return {
verified: false,
error: new Error('No valid credential or presentation.'),
} as IVerifyResult;
} catch (error: unknown) {
return { verified: false, error: error as Error } as IVerifyResult;
}
}
Loading

0 comments on commit 48b8a44

Please sign in to comment.