Pairing works

This commit is contained in:
Sosthene 2024-11-30 20:07:27 +01:00
parent 113f036838
commit b822f351fd
3 changed files with 43 additions and 40 deletions

View File

@ -53,23 +53,11 @@ export default class ModalService {
}
// this is kind of too specific for pairing though
public async openConfirmationModal(pcd: any, commitmentTx: string, merkleRoot: string) {
let map: Record<string, RoleDefinition>;
if (pcd['roles']) {
const roles = pcd['roles'];
try {
map = JSON.parse(roles);
} catch (e) {
throw new Error(`Failed to parse roles: ${e}`);
}
} else {
throw new Error('Pcd doesn\'t have a \"roles\" field');
}
public async openPairingConfirmationModal(roleDefinition: Record<string, RoleDefinition>, commitmentTx: string, merkleRoot: string) {
// pairing specifics
let members;
if (map['owner']) {
const owner = map['owner'];
if (roleDefinition['owner']) {
const owner = roleDefinition['owner'];
members = owner.members;
} else {
throw new Error('No \"owner\" role');
@ -83,7 +71,6 @@ export default class ModalService {
// We take all the addresses except our own
const service = await Services.getInstance();
const localAddress = await service.getDeviceAddress();
console.log('🚀 ~ Routing ~ openConfirmationModal ~ pcd:', pcd);
for (const member of members) {
for (const address of member['sp_addresses']) {
if (address !== localAddress) {
@ -127,7 +114,7 @@ export default class ModalService {
// We send the prd update
if (this.currentPcdCommitment) {
try {
const createPrdUpdateReturn = await service.createPrdUpdate(this.currentPcdCommitment);
const createPrdUpdateReturn = service.createPrdUpdate(this.currentPcdCommitment);
await service.handleApiReturn(createPrdUpdateReturn);
} catch (e) {
throw e
@ -138,13 +125,18 @@ export default class ModalService {
// We send confirmation that we validate the change
try {
const approveChangeReturn = await service.approveChange(this.currentPcdCommitment!);
const approveChangeReturn = service.approveChange(this.currentPcdCommitment!);
await service.handleApiReturn(approveChangeReturn);
} catch (e) {
throw e
}
service.pairDevice(this.paired_addresses);
try {
service.pairDevice(this.paired_addresses);
} catch (e) {
throw e;
}
this.paired_addresses = [];
this.currentOutpoint = null;
this.currentPcdCommitment = null;

View File

@ -3,7 +3,7 @@ import { INotification } from '~/models/notification.model';
import { IProcess } from '~/models/process.model';
// import Database from './database';
import { initWebsocket, sendMessage } from '../websockets';
import { ApiReturn, Member, Process } from '../../dist/pkg/sdk_client';
import { ApiReturn, Member, PcdUpdates, Process, RoleDefinition } from '../../dist/pkg/sdk_client';
import ModalService from './modal.service';
import { navigate } from '../router';
import Database from './database.service';
@ -16,7 +16,7 @@ export default class Services {
private static initializing: Promise<Services> | null = null;
private static instance: Services;
private currentProcess: string | null = null;
private pendingUpdates: Record<string, any> = {};
private pendingUpdates: PcdUpdates | null = null;
private currentUpdateMerkleRoot: string | null = null;
private localAddress: string | null = null;
private pairedAddresses: string[] = [];
@ -265,10 +265,10 @@ export default class Services {
public getUpdateProposals(commitmentOutpoint: string) {
try {
const proposals: ApiReturn = this.sdkClient.get_update_proposals(commitmentOutpoint);
const proposals: PcdUpdates = this.sdkClient.get_update_proposals(commitmentOutpoint);
if (proposals.decrypted_pcds && proposals.decrypted_pcds.length != 0) {
this.currentProcess = commitmentOutpoint;
this.pendingUpdates = proposals.decrypted_pcds;
this.pendingUpdates = proposals;
} else {
throw new Error('No pending proposals');
}
@ -315,8 +315,13 @@ export default class Services {
throw e;
}
// do we need to act?
updatedProcess.user_validation_required
if (updatedProcess.modified_state || updatedProcess.new_state) {
this.currentProcess = updatedProcess.commitment_tx;
this.getUpdateProposals(this.currentProcess!);
await this.evaluatePendingUpdates();
}
}
if (apiReturn.commit_to_send) {
@ -343,16 +348,28 @@ export default class Services {
}
private async openConfirmationModal() {
if (this.pendingUpdates.length === 0) {
console.log('No pending updates');
if (!this.pendingUpdates || this.pendingUpdates.modified_values.length === 0) {
console.log('No pending updates to validate');
}
try {
for (const [merkleRoot, actual_proposal] of Object.entries(this.pendingUpdates)) {
await this.routingInstance.openConfirmationModal(actual_proposal, this.currentProcess!, merkleRoot);
for (const value of this.pendingUpdates!.modified_values) {
if (value.notify_user) {
// TODO notification pop up
}
if (!value.need_validation) {
continue;
}
if (value.proof) {
// It seems we already validated that, check the proof and if valid just notify user
continue;
}
const actualProposal: Record<string, RoleDefinition> = JSON.parse(value.new_value);
const merkleRoot: string = value.new_state_merkle_root;
try {
await this.routingInstance.openPairingConfirmationModal(actualProposal, this.currentProcess!, merkleRoot);
} catch (e) {
throw new Error(`${e}`);
}
} catch (e) {
throw new Error(`${e}`);
}
}

View File

@ -161,18 +161,12 @@ async function onOkButtonClick() {
// Create the process
const relayAddress = await service.getRelayAddresses(); // Get one (or more?) relay addresses
const createPairingProcessReturn = await service.createPairingProcess([addressInput], relayAddress[0], 1);
await service.handleApiReturn(createPairingProcessReturn);
if (!createPairingProcessReturn.updated_process) {
throw new Error('createPairingProcess returned an empty new process'); // This should never happen
}
const commitmentOutpoint = createPairingProcessReturn.updated_process.commitment_tx;
// We set the service to the process
service.getUpdateProposals(commitmentOutpoint);
await service.evaluatePendingUpdates();
await service.handleApiReturn(createPairingProcessReturn);
} catch (e) {
console.error(`onOkButtonClick error: ${e}`);
}