diff --git a/src/services/service.ts b/src/services/service.ts index 73cab33..a2203e4 100755 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -433,6 +433,37 @@ export default class Services { } } + public async tryFetchDiffValue(diffs: UserDiff[]): Promise<[UserDiff[], Record]>{ + if (diffs.length === 0) { + return [[], {}]; + } + + // We check if we have the value in diffs + let retrievedValues: Record = {}; + for (const diff of diffs) { + // Check if `new_value` is missing + if (diff.new_value === null) { + const hash = diff.value_commitment; + if (!hash) { + console.error('No commitment for diff'); + } + try { + const res = await this.fetchValueFromStorage(hash); + if (!res) { + console.error('Failed to fetch value for hash', hash); + } else { + diff.new_value = res['value']; + retrievedValues[hash] = res['value']; + } + } catch (error) { + console.error(`Failed to fetch new_value for diff: ${JSON.stringify(diff)}`, error); + } + } + } + + return [diffs, retrievedValues]; + } + public async handleApiReturn(apiReturn: ApiReturn) { if (apiReturn.new_tx_to_send && apiReturn.new_tx_to_send.transaction.length != 0) { await this.sendNewTxMessage(JSON.stringify(apiReturn.new_tx_to_send)); @@ -481,48 +512,25 @@ export default class Services { } const isPaired = this.isPaired(); - + if (updatedProcess.new_diffs.length != 0) { - // We check if we have the value in diffs - const diffs = updatedProcess.new_diffs; - const stateId = diffs[0].new_state_merkle_root; - let retrievedValues: Record = {}; - for (const diff of diffs) { - // Check if `new_value` is missing - if (diff.new_value === null) { - const hash = diff.value_commitment; - if (!hash) { - console.error('No commitment for diff'); - } - try { - const res = await this.fetchValueFromStorage(hash); - if (!res) { - console.error('Failed to fetch value for hash', hash); - } else { - diff.new_value = res['value']; - retrievedValues[hash] = res['value']; - } - } catch (error) { - console.error(`Failed to fetch new_value for diff: ${JSON.stringify(diff)}`, error); - } - } - } - + const [updatedDiffs, retrievedValues] = await this.tryFetchDiffValue(updatedProcess.new_diffs); if (Object.entries(retrievedValues).length != 0) { + const stateId = updatedDiffs[0].new_state_merkle_root; + const processId = updatedDiffs[0].process_id; // We update the process with the value we retrieved const hashToValues = JSON.stringify(retrievedValues); const apiReturn = this.sdkClient.update_process_state(processId, stateId, hashToValues); await this.handleApiReturn(apiReturn); - } - - if (isPaired) { + } else { try { - await this.saveDiffs(diffs); + await this.saveDiffs(updatedDiffs); } catch (e) { throw e; } - } else { - await this.openPairingConfirmationModal(diffs, updatedProcess.up_to_date_roles); + if (!isPaired) { + await this.openPairingConfirmationModal(updatedDiffs); + } } } @@ -544,7 +552,7 @@ export default class Services { }, 0); } - public async openPairingConfirmationModal(diffs: UserDiff[], roles: Record) { + public async openPairingConfirmationModal(diffs: UserDiff[]) { const rolesDiff = diffs.find((diff) => diff.field === 'roles'); if (!rolesDiff) { throw new Error('Pairing process must have roles'); @@ -552,7 +560,7 @@ export default class Services { const processId = rolesDiff.process_id; const stateId = rolesDiff.new_state_merkle_root; try { - await this.routingInstance.openPairingConfirmationModal(roles, processId, stateId); + await this.routingInstance.openPairingConfirmationModal(rolesDiff.new_value, processId, stateId); } catch (e) { throw new Error(`${e}`); }