diff --git a/src/services/service.ts b/src/services/service.ts index 151506d..f4f5887 100755 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -302,25 +302,31 @@ export default class Services { } const myAddress: string = this.sdkClient.get_address(); pairWith.push(myAddress); + const roles: Record = { + pairing: { + members: [{ sp_addresses: pairWith }], + validation_rules: [ + { + quorum: 1.0, + fields: ['description', 'counter'], + min_sig_member: 1.0, + }, + ], + storages: [storageUrl] + }, + }; const pairingTemplate = { description: 'pairing', counter: 0, - roles: { - pairing: { - members: [{ sp_addresses: pairWith }], - validation_rules: [ - { - quorum: 1.0, - fields: ['description', 'roles', 'counter'], - min_sig_member: 1.0, - }, - ], - storages: [storageUrl] - }, - }, }; try { - return this.sdkClient.create_new_process(JSON.stringify(pairingTemplate), null, relayAddress, feeRate); + return this.sdkClient.create_new_process( + JSON.stringify(pairingTemplate), + JSON.stringify(roles), + null, + relayAddress, + feeRate + ); } catch (e) { throw new Error(`Creating process failed:, ${e}`); } @@ -639,71 +645,51 @@ export default class Services { } } - setTimeout(async () => { - if (apiReturn.updated_process) { - const updatedProcess = apiReturn.updated_process; + if (apiReturn.updated_process) { + const updatedProcess = apiReturn.updated_process; - const processId: string = updatedProcess.process_id; + const processId: string = updatedProcess.process_id; - // Save process to db + // Save process to db + try { + await this.saveProcessToDb(processId, updatedProcess.current_process); + } catch (e) { + throw e; + } + + const isPaired = this.isPaired(); + + if (updatedProcess.diffs && updatedProcess.diffs.length != 0) { try { - await this.saveProcessToDb(processId, updatedProcess.current_process); + await this.saveDiffsToDb(updatedProcess.diffs); } catch (e) { - throw e; - } - - const isPaired = this.isPaired(); - - if (updatedProcess.diffs && updatedProcess.diffs.length != 0) { - const [updatedDiffs, retrievedValues] = await this.tryFetchDiffValue(updatedProcess.diffs); - if (Object.entries(retrievedValues).length != 0) { - const stateId = updatedDiffs[0].state_id; - 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); - } else { - try { - await this.saveDiffsToDb(updatedDiffs); - } catch (e) { - throw e; - } - if (!isPaired) { - await this.openPairingConfirmationModal(updatedDiffs); - } - } - } - - - if (updatedProcess.validated_state) { - const responsePrdReturn = this.sdkClient.create_response_prd(processId, updatedProcess.validated_state); - await this.handleApiReturn(responsePrdReturn); + console.error('Failed to save diffs to db:', e); } } - - if (apiReturn.commit_to_send) { - const commit = apiReturn.commit_to_send; - await this.sendCommitMessage(JSON.stringify(commit)); + if (!isPaired) { + console.log(updatedProcess); + await this.openPairingConfirmationModal(updatedProcess.current_process.states[0]); } + } - if (apiReturn.ciphers_to_send && apiReturn.ciphers_to_send.length != 0) { - await this.sendCipherMessages(apiReturn.ciphers_to_send); - } - }, 0); + if (apiReturn.commit_to_send) { + const commit = apiReturn.commit_to_send; + await this.sendCommitMessage(JSON.stringify(commit)); + } + + if (apiReturn.ciphers_to_send && apiReturn.ciphers_to_send.length != 0) { + await this.sendCipherMessages(apiReturn.ciphers_to_send); + } } - public async openPairingConfirmationModal(diffs: UserDiff[]) { - const rolesDiff = diffs.find((diff) => diff.field === 'roles'); - if (!rolesDiff) { - throw new Error('Pairing process must have roles'); - } - const processId = rolesDiff.process_id; - const stateId = rolesDiff.state_id; + public async openPairingConfirmationModal(firstState: ProcessState) { + const roles = firstState.roles; + const processId = firstState.commited_in; + const stateId = firstState.state_id; try { - await this.routingInstance.openPairingConfirmationModal(rolesDiff.new_value, processId, stateId); + await this.routingInstance.openPairingConfirmationModal(roles, processId, stateId); } catch (e) { - throw new Error(`${e}`); + console.error(e); } } @@ -804,7 +790,6 @@ export default class Services { try { this.sdkClient.roles_contains_us(JSON.stringify(roles)); } catch (e) { - console.error(e); return false; } @@ -815,7 +800,6 @@ export default class Services { try { this.sdkClient.roles_contains_member(JSON.stringify(roles), member); } catch (e) { - console.error(e); return false; }