Add DECODE_PUBLIC_DATA api

This commit is contained in:
Sosthene 2025-06-24 15:52:23 +02:00
parent 50a92995d7
commit 18ef18db71

View File

@ -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');