From 9890615e8495a99cea899def8ad74a6f80b9f8b3 Mon Sep 17 00:00:00 2001 From: Andraz <69682837+andyv09@users.noreply.github.com> Date: Tue, 20 Jun 2023 10:55:25 +0200 Subject: [PATCH] feat: createVP rpc method accepts VCs instead of VC IDs (#247) * feat: change parameter to actual vcs * fix: datastore combobox * fix: linting problems * fix: params tests * fix: tests * fix: linting errors * fix: linting error * chore: fix lint issues --------- Co-authored-by: Martin Domajnko --- .../CreatePresentationDisplay/index.tsx | 27 ++--------- .../VCTable/DataStoreCombobox/index.tsx | 6 +-- packages/docs/docs/tutorial/implementation.md | 11 +---- packages/docs/docs/tutorial/rpc-methods.md | 4 +- packages/snap/snap.manifest.json | 2 +- packages/snap/src/utils/params.ts | 33 ------------- packages/snap/src/utils/veramoUtils.ts | 19 +------- packages/snap/tests/rpc/onRpcRequest.spec.ts | 48 ++----------------- packages/snap/tests/utils/params.spec.ts | 35 -------------- packages/snap/tests/utils/veramoUtils.spec.ts | 36 ++------------ packages/types/src/params.ts | 2 +- 11 files changed, 24 insertions(+), 199 deletions(-) diff --git a/packages/dapp/src/components/CreatePresentationDisplay/index.tsx b/packages/dapp/src/components/CreatePresentationDisplay/index.tsx index 46ca65823..df7a2f09a 100644 --- a/packages/dapp/src/components/CreatePresentationDisplay/index.tsx +++ b/packages/dapp/src/components/CreatePresentationDisplay/index.tsx @@ -3,14 +3,15 @@ import { useState } from 'react'; import Link from 'next/link'; import { - AvailableVCStores, QueryVCsRequestResult, SupportedProofFormats, - VCRequest, } from '@blockchain-lab-um/masca-types'; import { isError } from '@blockchain-lab-um/utils'; import { ArrowLeftIcon } from '@heroicons/react/20/solid'; -import { W3CVerifiablePresentation } from '@veramo/core'; +import { + W3CVerifiableCredential, + W3CVerifiablePresentation, +} from '@veramo/core'; import { useTranslations } from 'next-intl'; import { shallow } from 'zustand/shallow'; @@ -59,25 +60,7 @@ const CreatePresentationDisplay = () => { const handleCreateVP = async () => { if (!api) return; setLoading(true); - const vcs: VCRequest[] = selectedVCs.map((vc) => { - if (vc.metadata.store) { - if (typeof vc.metadata.store === 'string') - return { - id: vc.metadata.id, - metadata: { - store: vc.metadata.store as AvailableVCStores, - }, - }; - if (Array.isArray(vc.metadata.store)) - return { - id: vc.metadata.id, - metadata: { - store: vc.metadata.store[0] as AvailableVCStores, - }, - }; - } - return { id: vc.metadata.id }; - }); + const vcs: W3CVerifiableCredential[] = selectedVCs.map((vc) => vc.data); const proofOptions = { type: '', domain, challenge }; diff --git a/packages/dapp/src/components/VCTable/DataStoreCombobox/index.tsx b/packages/dapp/src/components/VCTable/DataStoreCombobox/index.tsx index f75e16f2a..f919e10dd 100644 --- a/packages/dapp/src/components/VCTable/DataStoreCombobox/index.tsx +++ b/packages/dapp/src/components/VCTable/DataStoreCombobox/index.tsx @@ -73,9 +73,9 @@ const DataStoreCombobox = ({ vcs, isConnected }: DataStoreComboboxProps) => { > {({ open }) => (
-
+
{ placeholder="None" onChange={(event) => setQuery(event.target.value)} /> - + <> { - if ( - vc !== null && - typeof vc === 'object' && - 'id' in vc && - typeof vc.id === 'string' - ) { - if ( - 'metadata' in vc && - vc.metadata !== null && - typeof vc.metadata === 'object' && - 'store' in vc.metadata && - vc.metadata.store !== null && - typeof vc.metadata.store === 'string' && - !isAvailableVCStores(vc.metadata.store) - ) { - throw new Error(`Store ${vc.metadata.store} is not supported!`); - } - if ( - 'metadata' in vc && - vc.metadata !== null && - typeof vc.metadata === 'object' && - 'store' in vc.metadata && - vc.metadata.store !== null && - typeof vc.metadata.store === 'string' && - !isEnabledVCStore(account, state, vc.metadata?.store) - ) { - throw new Error(`Store ${vc.metadata.store} is not enabled!`); - } - } else throw new Error('VC is invalid format'); - }); return; } diff --git a/packages/snap/src/utils/veramoUtils.ts b/packages/snap/src/utils/veramoUtils.ts index 51480b4ab..d44cf8801 100644 --- a/packages/snap/src/utils/veramoUtils.ts +++ b/packages/snap/src/utils/veramoUtils.ts @@ -192,7 +192,7 @@ export async function veramoCreateVP( }, createVPParams: CreateVPRequestParams ): Promise { - const vcsMetadata = createVPParams.vcs; + const { vcs } = createVPParams; const domain = createVPParams.proofOptions?.domain; const challenge = createVPParams.proofOptions?.challenge; const proofFormat = createVPParams.proofFormat @@ -214,23 +214,6 @@ export async function veramoCreateVP( agent ); - const vcs: W3CVerifiableCredential[] = []; - - for (const vcMetadata of vcsMetadata) { - // eslint-disable-next-line no-await-in-loop - const vcObj = (await agent.query({ - filter: { - type: 'id', - filter: vcMetadata.id, - }, - options: { store: vcMetadata.metadata?.store }, - })) as QueryVCsRequestResult[]; - if (vcObj.length > 0) { - const vc: W3CVerifiableCredential = vcObj[0].data; - vcs.push(vc); - } - } - if (vcs.length === 0) { throw new Error('VC does not exist'); } diff --git a/packages/snap/tests/rpc/onRpcRequest.spec.ts b/packages/snap/tests/rpc/onRpcRequest.spec.ts index 54e54bfce..3862534b8 100644 --- a/packages/snap/tests/rpc/onRpcRequest.spec.ts +++ b/packages/snap/tests/rpc/onRpcRequest.spec.ts @@ -26,6 +26,7 @@ import { exampleDIDKey, exampleTestKey, exampleTestVCPayload, + exampleVC, getDefaultSnapState, jsonPath, } from '../testUtils/constants'; @@ -947,7 +948,7 @@ describe('onRpcRequest', () => { jsonrpc: '2.0', method: 'createVP', params: { - vcs: [{ id: saveRes.data[0].id, metadata: { store: 'snap' } }], + vcs: [exampleVC], }, }, })) as Result; @@ -1005,7 +1006,7 @@ describe('onRpcRequest', () => { jsonrpc: '2.0', method: 'createVP', params: { - vcs: [{ id: saveRes.data[0].id }], + vcs: [exampleVC], }, }, })) as Result; @@ -1026,47 +1027,6 @@ describe('onRpcRequest', () => { expect.assertions(1); }); - - it('should fail creating VP - VC does not exist', async () => { - snapMock.rpcMocks.snap_dialog.mockReturnValue(false); - - let res = (await onRpcRequest({ - origin: 'localhost', - request: { - id: 'test-id', - jsonrpc: '2.0', - method: 'saveVC', - params: { - verifiableCredential: exampleVeramoVCJWT, - options: { store: 'snap' }, - }, - }, - })) as Result; - - if (isSuccess(res)) { - throw new Error('Should return error'); - } - - res = (await onRpcRequest({ - origin: 'localhost', - request: { - id: 'test-id', - jsonrpc: '2.0', - method: 'createVP', - params: { - vcs: [{ id: 'test-id' }], - }, - }, - })) as Result; - - if (isSuccess(res)) { - throw new Error('Should return error'); - } - - expect(res.error).toBe('Error: VC does not exist'); - - expect.assertions(1); - }); }); describe('togglePopups', () => { @@ -1677,7 +1637,7 @@ describe('onRpcRequest', () => { jsonrpc: '2.0', method: 'createVP', params: { - vcs: [{ id: saveRes.data[0].id, metadata: { store: 'snap' } }], + vcs: [exampleVC], }, }, })) as Result; diff --git a/packages/snap/tests/utils/params.spec.ts b/packages/snap/tests/utils/params.spec.ts index c33c873b8..445737808 100644 --- a/packages/snap/tests/utils/params.spec.ts +++ b/packages/snap/tests/utils/params.spec.ts @@ -186,17 +186,6 @@ describe('Utils [params]', () => { isValidCreateVPRequest */ describe('isValidCreateVPRequest', () => { - it('should fail for not enabled store', () => { - const state = getDefaultSnapState(); - state.accountState[address].accountConfig.ssi.vcStore.ceramic = false; - expect(() => - isValidCreateVPRequest( - { vcs: [{ id: '123', metadata: { store: 'ceramic' } }] }, - address, - state - ) - ).toThrow('Store ceramic is not enabled!'); - }); it('should succeed if vcId is a string', () => { expect(() => isValidCreateVPRequest( @@ -256,18 +245,6 @@ describe('Utils [params]', () => { ) ).toThrow(Error); }); - - it('should fail if vcId is a number', () => { - expect(() => - isValidCreateVPRequest( - { - vcs: [{ id: 123 }], - }, - address, - getDefaultSnapState() - ) - ).toThrow('VC is invalid format'); - }); it('should fail if vcs is null', () => { expect(() => isValidCreateVPRequest( @@ -315,18 +292,6 @@ describe('Utils [params]', () => { ) ).not.toThrow('err'); }); - it('should throw request with wrong store', () => { - expect(() => - isValidCreateVPRequest( - { - vcs: [{ id: 'test-id', metadata: { store: 'snapp' } }], - proofFormat: 'jwt', - }, - address, - getDefaultSnapState() - ) - ).toThrow('Store snapp is not supported!'); - }); it('should not throw request with proofFormat and proofOptions', () => { expect(() => diff --git a/packages/snap/tests/utils/veramoUtils.spec.ts b/packages/snap/tests/utils/veramoUtils.spec.ts index a1af43375..cee6eacb9 100644 --- a/packages/snap/tests/utils/veramoUtils.spec.ts +++ b/packages/snap/tests/utils/veramoUtils.spec.ts @@ -957,7 +957,7 @@ describe('Utils [veramo]', () => { account: address, bip44CoinTypeNode: bip44Entropy as BIP44CoinTypeNode, }, - { proofFormat: 'jwt', vcs: [{ id: res[0].id }] } + { proofFormat: 'jwt', vcs: [exampleVC] } ); expect(createdVP).not.toBeNull(); @@ -1029,7 +1029,7 @@ describe('Utils [veramo]', () => { account: address, bip44CoinTypeNode: bip44Entropy as BIP44CoinTypeNode, }, - { proofFormat: 'EthereumEip712Signature2021', vcs: [{ id: res[0].id }] } + { proofFormat: 'EthereumEip712Signature2021', vcs: [exampleVC] } ); expect(createdVP).not.toBeNull(); @@ -1061,7 +1061,7 @@ describe('Utils [veramo]', () => { account: address, bip44CoinTypeNode: bip44Entropy as BIP44CoinTypeNode, }, - { proofFormat: 'jwt', vcs: [{ id: res[0].id }, { id: 'wrong_id' }] } + { proofFormat: 'jwt', vcs: [exampleVC] } ); expect(createdVP).not.toBeNull(); @@ -1093,7 +1093,7 @@ describe('Utils [veramo]', () => { account: address, bip44CoinTypeNode: bip44Entropy as BIP44CoinTypeNode, }, - { proofFormat: 'jwt', vcs: [{ id: res[0].id }, { id: res[0].id }] } + { proofFormat: 'jwt', vcs: [exampleVC, exampleVC] } ); expect(createdVP).not.toBeNull(); @@ -1151,12 +1151,7 @@ describe('Utils [veramo]', () => { }, { proofFormat: 'jwt', - vcs: [ - { id: res[0].id, metadata: { store: 'snap' } }, - { id: resjwt[0].id, metadata: { store: 'snap' } }, - { id: res2[0].id, metadata: { store: 'snap' } }, - { id: res3[0].id, metadata: { store: 'snap' } }, - ], + vcs: [exampleVC, exampleVC, exampleVCJSONLD, exampleVCEIP712], } ); expect(createdVP).not.toBeNull(); @@ -1177,27 +1172,6 @@ describe('Utils [veramo]', () => { expect.assertions(3); }); - it('should fail creating a VP and throw VC does not exist', async () => { - const initialState = getDefaultSnapState(); - - snapMock.rpcMocks.snap_dialog.mockResolvedValue(true); - - await expect( - veramoCreateVP( - { - snap: snapMock, - ethereum: ethereumMock, - state: initialState, - account: address, - bip44CoinTypeNode: bip44Entropy as BIP44CoinTypeNode, - }, - { proofFormat: 'jwt', vcs: [{ id: 'test-id' }] } - ) - ).rejects.toThrow('VC does not exist'); - - expect.assertions(1); - }); - it('should fail creating a VP and throw user rejected error', async () => { snapMock.rpcMocks.snap_dialog.mockResolvedValue(false); diff --git a/packages/types/src/params.ts b/packages/types/src/params.ts index a66965369..905cf03bd 100644 --- a/packages/types/src/params.ts +++ b/packages/types/src/params.ts @@ -55,7 +55,7 @@ export type SetCurrentAccountRequestParams = { }; export type CreateVPRequestParams = { - vcs: VCRequest[]; + vcs: W3CVerifiableCredential[]; proofFormat?: SupportedProofFormats; proofOptions?: ProofOptions; };