Fix confirmation modal

This commit is contained in:
Sosthene 2024-11-25 21:35:26 +01:00
parent da32410c71
commit d923a188e6

View File

@ -4,11 +4,12 @@ import Services from './service';
import { U32_MAX } from './service';
import { navigate } from '../router';
import { addressToEmoji } from '../utils/sp-address.utils';
import { RoleDefinition } from 'dist/pkg/sdk_client';
export default class ModalService {
private static instance: ModalService;
private currentPrd: any;
private currentOutpoint?: string;
private currentOutpoint: string | null = null;
private constructor() {}
private paired_addresses: string[] = [];
@ -51,11 +52,31 @@ export default class ModalService {
}
}
public async openConfirmationModal(pcd: any, outpointCommitment: string) {
let roles = JSON.parse(pcd['roles']); // ['members'][0];
console.log(roles);
let members = roles['owner']['members'];
console.log(members);
public async openConfirmationModal(pcd: any, commitmentTx: 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');
}
let members;
if (map['owner']) {
const owner = map['owner'];
members = owner.members;
} else {
throw new Error('No \"owner\" role');
}
if (members.length != 1) {
throw new Error('Must have exactly 1 member');
}
// We take all the addresses except our own
const service = await Services.getInstance();
const localAddress = await service.getDeviceAddress();
@ -65,6 +86,7 @@ export default class ModalService {
this.paired_addresses.push(address);
}
}
this.currentOutpoint = commitmentTx;
await this.injectModal(members);
const modal = document.getElementById('modal');
if (modal) modal.style.display = 'flex';
@ -96,13 +118,15 @@ export default class ModalService {
const modal = document.getElementById('modal');
// console.log("🚀 ~ Routing ~ confirm ~ prd:", prd)
if (modal) modal.style.display = 'none';
// Just make an empty commitment for now
const emptyTxid = '0'.repeat(64);
const commitmentOutpoint = `${emptyTxid}:${U32_MAX}`;
if (this.currentOutpoint === null || this.paired_addresses.length === 0) {
throw new Error('Missing outpoint and/or paired addresses');
}
// We take the paired device(s) from the contract
await service.pairDevice(commitmentOutpoint, this.paired_addresses);
await service.pairDevice(this.currentOutpoint, this.paired_addresses);
this.paired_addresses = [];
this.currentOutpoint = null;
const newDevice = await service.dumpDevice();
await service.saveDevice(newDevice);
navigate('process');