Refactor pairing logic

Account for the fact that we need the pairing id earlier now
This commit is contained in:
Sosthene 2025-09-05 07:57:17 +02:00
parent bfca596e8b
commit e0e186f4f4
2 changed files with 23 additions and 67 deletions

View File

@ -251,15 +251,20 @@ export async function registerAllListeners() {
}
console.log('🚀 Starting pairing process');
await prepareAndSendPairingTx();
const myAddress = services.getDeviceAddress();
const createPairingProcessReturn = await services.createPairingProcess('', [myAddress]);
const pairingId = createPairingProcessReturn.updated_process?.process_id;
const stateId = createPairingProcessReturn.updated_process?.current_process?.states[0]?.state_id as string;
services.pairDevice(pairingId, [myAddress]);
await services.handleApiReturn(createPairingProcessReturn);
const createPrdUpdateReturn = await services.createPrdUpdate(pairingId, stateId);
await services.handleApiReturn(createPrdUpdateReturn);
const approveChangeReturn = await services.approveChange(pairingId, stateId);
await services.handleApiReturn(approveChangeReturn);
await services.confirmPairing();
const pairingId = services.getPairingProcessId();
if (!pairingId) {
throw new Error('Failed to get pairing process id');
}
// Send success response
const successMsg = {
type: MessageType.PAIRING_CREATED,

View File

@ -702,33 +702,16 @@ export default class Services {
}
public async confirmPairing() {
if (!this.processId || !this.stateId) {
console.error('Missing process and/or state ID');
try {
// Is the wasm paired?
const pairingId = this.getPairingProcessId();
// TODO confirm that the pairing process id is known, commited
const newDevice = this.dumpDeviceFromMemory();
await this.saveDeviceInDatabase(newDevice);
} catch (e) {
console.error('Failed to confirm pairing');
return;
}
let createPrdUpdateReturn;
try {
createPrdUpdateReturn = await this.createPrdUpdate(this.processId, this.stateId);
} catch (e) {
throw new Error(`createPrdUpdate failed: ${e}`);
}
await this.handleApiReturn(createPrdUpdateReturn);
let approveChangeReturn;
try {
approveChangeReturn = await this.approveChange(this.processId, this.stateId);
} catch (e) {
throw new Error(`approveChange failed: ${e}`);
}
await this.handleApiReturn(approveChangeReturn);
await this.pairDevice();
this.processId = null;
this.stateId = null;
const newDevice = this.dumpDeviceFromMemory();
await this.saveDeviceInDatabase(newDevice);
await navigate('account');
}
public async updateDevice(): Promise<void> {
@ -766,41 +749,9 @@ export default class Services {
}
}
public async pairDevice() {
if (!this.processId) {
console.error('No processId set');
return;
}
const process = await this.getProcess(this.processId);
if (!process) {
console.error('Unknown process');
return;
}
let spAddressList: string[] = [];
public pairDevice(processId: string, spAddressList: string[]): void {
try {
let encodedSpAddressList: number[] = [];
if (this.stateId) {
const state = process.states.find(state => state.state_id === this.stateId);
if (state) {
encodedSpAddressList = state.public_data['pairedAddresses'];
}
} else {
// We assume it's the last commited state
const lastCommitedState = this.getLastCommitedState(process);
if (lastCommitedState) {
encodedSpAddressList = lastCommitedState.public_data['pairedAddresses'];
}
}
spAddressList = this.sdkClient.decode_value(encodedSpAddressList);
if (!spAddressList || spAddressList.length == 0) {
throw new Error('Empty pairedAddresses');
}
} catch (e) {
throw new Error(`Failed to get pairedAddresses from process: ${e}`);
}
try {
this.sdkClient.pair_device(this.processId, spAddressList);
this.sdkClient.pair_device(processId, spAddressList);
} catch (e) {
throw new Error(`Failed to pair device: ${e}`);
}