import modalHtml from '../components/login-modal/login-modal.html?raw'; import modalScript from '../components/login-modal/login-modal.js?raw'; 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 | null = null; private constructor() {} private paired_addresses: string[] = []; // Method to access the singleton instance of Services public static async getInstance(): Promise { if (!ModalService.instance) { ModalService.instance = new ModalService(); } return ModalService.instance; } public openLoginModal(myAddress: string, receiverAddress: string) { const container = document.querySelector('.page-container'); let html = modalHtml; html = html.replace('{{device1}}', myAddress); html = html.replace('{{device2}}', receiverAddress); if (container) container.innerHTML += html; const modal = document.getElementById('login-modal'); if (modal) modal.style.display = 'flex'; const newScript = document.createElement('script'); newScript.setAttribute('type', 'module'); newScript.textContent = modalScript; document.head.appendChild(newScript).parentNode?.removeChild(newScript); } async injectModal(members: any[]) { const container = document.querySelector('.page-container'); if (container) { let html = await fetch('/src/components/modal/confirmation-modal.html').then((res) => res.text()); html = html.replace('{{device1}}', await addressToEmoji(members[0]['sp_addresses'][0])); html = html.replace('{{device2}}', await addressToEmoji(members[0]['sp_addresses'][1])); container.innerHTML += html; // Dynamically load the header JS const script = document.createElement('script'); script.src = '/src/components/modal/confirmation-modal.ts'; script.type = 'module'; document.head.appendChild(script); } } public async openConfirmationModal(pcd: any, commitmentTx: string) { let map: Record; 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(); console.log('🚀 ~ Routing ~ openConfirmationModal ~ pcd:', pcd); for (const address of members[0]['sp_addresses']) { if (address !== localAddress) { this.paired_addresses.push(address); } } this.currentOutpoint = commitmentTx; await this.injectModal(members); const modal = document.getElementById('modal'); if (modal) modal.style.display = 'flex'; // const newScript = document.createElement('script'); // newScript.setAttribute('type', 'module'); // newScript.textContent = confirmationModalScript; // document.head.appendChild(newScript).parentNode?.removeChild(newScript); // Add correct text // Close modal when clicking outside of it window.onclick = (event) => { const modal = document.getElementById('modal'); if (event.target === modal) { this.closeConfirmationModal(); } }; } confirmLogin() { console.log('=============> Confirm Login'); } async closeLoginModal() { const modal = document.getElementById('login-modal'); if (modal) modal.style.display = 'none'; } async confirmPairing() { const service = await Services.getInstance(); const modal = document.getElementById('modal'); // console.log("🚀 ~ Routing ~ confirm ~ prd:", prd) if (modal) modal.style.display = 'none'; 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(this.currentOutpoint, this.paired_addresses); this.paired_addresses = []; this.currentOutpoint = null; const newDevice = await service.dumpDevice(); await service.saveDevice(newDevice); navigate('process'); } async closeConfirmationModal() { const service = await Services.getInstance(); await service.unpairDevice(); const modal = document.getElementById('modal'); if (modal) modal.style.display = 'none'; } }