diff --git a/src/services/service.ts b/src/services/service.ts index 7db94eb..fee6094 100755 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -917,21 +917,27 @@ export default class Services { } } - async decryptAttribute(state: ProcessState, attribute: string): Promise { - 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 { + 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; }