decryptAttribute request missing data

This commit is contained in:
NicolasCantu 2025-03-04 14:54:56 +01:00
parent 3d09a20512
commit 3785285e4d

View File

@ -917,21 +917,27 @@ export default class Services {
}
}
async decryptAttribute(state: ProcessState, attribute: string): Promise<string | null> {
let hash;
let key;
try {
hash = state.pcd_commitment[attribute];
} catch (e) {
console.error(`Failed to find hash for attribute ${attribute}`);
return null;
}
try {
key = state.keys[attribute];
} catch (e) {
console.error(`Failed to find key for attribute ${attribute}`);
return null;
async decryptAttribute(processId: string, state: ProcessState, attribute: string): Promise<string | null> {
let hash = state.pcd_commitment[attribute];
let key = state.keys[attribute];
// If hash or key is missing, request an update and then retry
if (!hash || !key) {
await this.requestDataFromPeers(processId, [state.state_id], [state.roles]);
const maxRetries = 5;
const retryDelay = 500; // delay in milliseconds
let retries = 0;
while ((!hash || !key) && retries < maxRetries) {
await new Promise(resolve => setTimeout(resolve, retryDelay));
// Re-read hash and key after waiting
hash = state.pcd_commitment[attribute];
key = state.keys[attribute];
retries++;
}
}
if (hash && key) {
const blob = await this.getBlobFromDb(hash);
if (blob) {
@ -943,12 +949,12 @@ export default class Services {
const clear = this.sdkClient.decrypt_data(new Uint8Array(keyBuf), cipher);
if (clear) {
// This is stringified json, we parse it back
// Parse the stringified JSON
return JSON.parse(clear);
}
}
}
return null;
}