From 18ef18db71383b68434349360e1c5ef1f2f0f30c Mon Sep 17 00:00:00 2001 From: Sosthene Date: Tue, 24 Jun 2025 15:52:23 +0200 Subject: [PATCH] Add DECODE_PUBLIC_DATA api --- src/router.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/router.ts b/src/router.ts index add7c0f..4dd75c0 100755 --- a/src/router.ts +++ b/src/router.ts @@ -332,6 +332,9 @@ export async function registerAllListeners() { if (state) { // Decrypt all the data we have the key for for (const attribute of Object.keys(state.pcd_commitment)) { + if (attribute === 'roles' || state.public_data[attribute]) { + continue; + } const decryptedAttribute = await services.decryptAttribute(processId, state, attribute); if (decryptedAttribute) { res[attribute] = decryptedAttribute; @@ -634,6 +637,37 @@ export async function registerAllListeners() { } } + const handleDecodePublicData = async (event: MessageEvent) => { + if (event.data.type !== MessageType.DECODE_PUBLIC_DATA) return; + + if (!services.isPaired()) { + const errorMsg = 'Device not paired'; + errorResponse(errorMsg, event.origin); + return; + } + + try { + const { accessToken, encodedData } = event.data; + + if (!accessToken || !tokenService.validateToken(accessToken, event.origin)) { + throw new Error('Invalid or expired session token'); + } + + const decodedData = await services.decodeValue(encodedData); + + window.parent.postMessage( + { + type: MessageType.PUBLIC_DATA_DECODED, + decodedData, + }, + event.origin + ); + } catch (e) { + const errorMsg = `Failed to decode data: ${e}`; + errorResponse(errorMsg, event.origin); + } + } + // Remove before adding to be sure there's no duplicate window.removeEventListener('message', handleUpdateProcess); window.removeEventListener('message', handleRequestLink); @@ -646,6 +680,7 @@ export async function registerAllListeners() { window.removeEventListener('message', handleCreateProcess); window.removeEventListener('message', handleValidateState); window.removeEventListener('message', handleNotifyUpdate); + window.removeEventListener('message', handleDecodePublicData); window.addEventListener('message', handleUpdateProcess); window.addEventListener('message', handleRequestLink); @@ -658,6 +693,7 @@ export async function registerAllListeners() { window.addEventListener('message', handleCreateProcess); window.addEventListener('message', handleValidateState); window.addEventListener('message', handleNotifyUpdate); + window.addEventListener('message', handleDecodePublicData); console.log('Initialized event handlers');