diff --git a/src/router.ts b/src/router.ts index 8f6d549..c2f348f 100755 --- a/src/router.ts +++ b/src/router.ts @@ -10,6 +10,7 @@ import { prepareAndSendPairingTx } from './utils/sp-address.utils'; import ModalService from './services/modal.service'; import { MessageType } from './models/process.model'; import { splitPrivateData, isValid32ByteHex } from './utils/service.utils'; +import { MerkleProofResult } from 'pkg/sdk_client'; const routes: { [key: string]: string } = { home: '/src/pages/home/home.html', @@ -722,8 +723,6 @@ export async function registerAllListeners() { const handleGetMerkleProof = async (event: MessageEvent) => { if (event.data.type !== MessageType.GET_MERKLE_PROOF) return; - console.log('handleGetMerkleProof', event.data); - try { const { accessToken, processState, attributeName } = event.data; @@ -747,6 +746,41 @@ export async function registerAllListeners() { } } + const handleValidateMerkleProof = async (event: MessageEvent) => { + if (event.data.type !== MessageType.VALIDATE_MERKLE_PROOF) return; + + try { + const { accessToken, merkleProof, documentHash } = event.data; + + if (!accessToken || !(await tokenService.validateToken(accessToken, event.origin))) { + throw new Error('Invalid or expired session token'); + } + + // Try to parse the proof + // We will validate it's a MerkleProofResult in the wasm + let parsedMerkleProof: MerkleProofResult; + try { + parsedMerkleProof= JSON.parse(merkleProof); + } catch (e) { + throw new Error('Provided merkleProof is not a valid json object'); + } + + const res = services.validateMerkleProof(parsedMerkleProof, documentHash); + + window.parent.postMessage( + { + type: MessageType.MERKLE_PROOF_VALIDATED, + isValid: res, + messageId: event.data.messageId + }, + event.origin + ); + } catch (e) { + const errorMsg = `Failed to get merkle proof: ${e}`; + errorResponse(errorMsg, event.origin, event.data.messageId); + } + } + window.removeEventListener('message', handleMessage); window.addEventListener('message', handleMessage); @@ -795,6 +829,9 @@ export async function registerAllListeners() { case MessageType.GET_MERKLE_PROOF: await handleGetMerkleProof(event); break; + case MessageType.VALIDATE_MERKLE_PROOF: + await handleValidateMerkleProof(event); + break; default: console.warn(`Unhandled message type: ${event.data.type}`); }