Refactor initialisation of pairing process

This commit is contained in:
Sosthene 2024-10-12 11:59:59 +02:00
parent b8a1a0bc6b
commit edef1428c3
2 changed files with 49 additions and 61 deletions

View File

@ -7,20 +7,17 @@ document.addEventListener('DOMContentLoaded', async () => {
try { try {
const services = await Services.getInstance(); const services = await Services.getInstance();
let user = await services.getWallet() let device = await services.getDevice()
setTimeout(async () => {
if(!user) {
const sp_adress = await services.createNewDevice();
user = await services.getWallet()
if(!device) {
device = await services.createNewDevice();
} else { } else {
const address = await services.getAdresses() await services.restoreDevice(device)
const device = await services.getDevice()
if(device) await services.restoreDevice(device)
} }
await services.addWebsocketConnection(wsurl); await services.addWebsocketConnection(wsurl);
await services.recoverInjectHtml() await services.recoverInjectHtml();
await services.restoreProcesses();
await services.restoreMessages();
const queryString = window.location.search; const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString) const urlParams = new URLSearchParams(queryString)
@ -28,7 +25,6 @@ document.addEventListener('DOMContentLoaded', async () => {
if(pairingAddress) { if(pairingAddress) {
await services.sendPairingTx(pairingAddress) await services.sendPairingTx(pairingAddress)
} }
})
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }

View File

@ -16,12 +16,13 @@ type ProcessesCache = {
[key: string]: any; [key: string]: any;
}; };
const U32_MAX = 4294967295;
export default class Services { export default class Services {
private static instance: Services; private static instance: Services;
private current_process: string | null = null; private current_process: string | null = null;
private sdkClient: any; private sdkClient: any;
private websocketConnection: WebSocketClient | null = null; private websocketConnection: WebSocketClient | null = null;
private sp_address: string | null = null;
private processes: IProcess[] | null = null; private processes: IProcess[] | null = null;
private notifications: INotification[] | null = null; private notifications: INotification[] | null = null;
private subscriptions: {element: Element; event: string; eventHandler: string;}[] = [] ; private subscriptions: {element: Element; event: string; eventHandler: string;}[] = [] ;
@ -52,6 +53,7 @@ export default class Services {
services.websocketConnection = newClient; services.websocketConnection = newClient;
} }
} }
public async recoverInjectHtml(): Promise<void> { public async recoverInjectHtml(): Promise<void> {
const container = document.getElementById('containerId'); const container = document.getElementById('containerId');
@ -71,9 +73,12 @@ export default class Services {
if(btn) { if(btn) {
this.addSubscription(btn, 'click', 'injectProcessListPage') this.addSubscription(btn, 'click', 'injectProcessListPage')
} }
const url = location.href // const url = location.href
this.generateQRCode(this.sp_address || '') const services = await Services.getInstance();
const localAddress = await this.getDeviceAddress();
this.generateQRCode(localAddress || '')
} }
private generateQRCode = async (text: string) => { private generateQRCode = async (text: string) => {
@ -90,77 +95,64 @@ export default class Services {
} }
async prepareProcessTx(myAddress: string, recipientAddress: string) { async prepareProcessTx(myAddress: string, recipientAddress: string) {
const initial_session_privkey = new Uint8Array(32);
const txid = '0'.repeat(64) const initial_session_pubkey = new Uint8Array(32);
var vout = Number.MAX_SAFE_INTEGER;
const paringTemplate = { const paringTemplate = {
"html": "",
"style": "",
"script": "",
"description": "AliceBob", "description": "AliceBob",
"roles": { "roles": {
"owner": { "owner": {
"members": [{sp_addresses: [myAddress]}, {sp_addresses:[recipientAddress]}], "members":
[{sp_addresses: [myAddress]}, {sp_addresses: [recipientAddress]}],
"validation_rules": "validation_rules":
[ [
{ {
"quorum": 1.0, "quorum": 1.0,
"fields": [ "fields": [
"description",
"roles", "roles",
"pairing_tx" "session_privkey",
"session_pubkey",
"key_parity"
], ],
"min_sig_member": 1.0 "min_sig_member": 1.0
} }
] ]
} }
}, },
"pairing_tx": `${txid}:4294967295`, "session_privkey": initial_session_privkey,
"session_pubkey": initial_session_pubkey,
"key_parity": true,
} }
const service = await Services.getInstance(); const service = await Services.getInstance();
const process = await service.sdkClient.create_update_transaction(undefined, JSON.stringify(paringTemplate), 1) const apiReturn = await service.sdkClient.create_update_transaction(undefined, JSON.stringify(paringTemplate), 1)
return process return apiReturn
} }
async sendPairingTx(sp_address: string): Promise<void> { public async sendPairingTx(spAddress: string): Promise<void> {
const services = await Services.getInstance(); const services = await Services.getInstance();
const amount = await this.getAmount() as any const amount = await this.getAmount() as any
console.log("🚀 ~ Services ~ sendPairingTx ~ amount:", amount) console.log("🚀 ~ Services ~ sendPairingTx ~ amount:", amount)
// if(amount === 0n) { if(amount === 0n) {
// const faucetMessage = await services.createFaucetMessage() const faucetMessage = await services.createFaucetMessage()
// console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ faucetMessage:", faucetMessage) console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ faucetMessage:", faucetMessage)
// services.websocketConnection?.sendNormalMessage(faucetMessage) services.websocketConnection?.sendNormalMessage(faucetMessage)
// }
const spAddress = await this.getDeviceAddress() as any
let txid = '0'.repeat(64)
setTimeout(async () => {
let pairing = await services.sdkClient.pair_device(`${txid}:4294967295`, [sp_address])
const process = await this.prepareProcessTx(spAddress, sp_address)
const tx = process.new_tx_to_send
const parsedTx = JSON.parse(tx)
const transaction = parsedTx.transaction
txid = await services.sdkClient.get_txid(transaction)
const root_commitment = process.updated_process[0];
const init_process = process.updated_process[1];
console.log("🚀 ~ Services ~ setTimeout ~ init_process:", init_process, init_process.payload)
console.log("🚀 ~ Services ~ setTimeout ~ txToSend:", tx)
const prd = JSON.stringify(init_process.impending_requests[0]);
services.websocketConnection?.sendMessage('NewTx', tx)
pairing = await services.sdkClient.pair_device(`${txid}:0`, [sp_address])
const dump = await services.sdkClient.dump_device()
const prd_response = await services.sdkClient.response_prd(root_commitment, prd, true)
console.log("🚀 ~ Services ~ setTimeout ~ prd_response:", prd_response, prd, root_commitment)
if(process?.ciphers_to_send && process.ciphers_to_send.length) {
await this.sendCipherMessages(process.ciphers_to_send)
} }
const router = await Routing.getInstance();
router.openLoginModal(spAddress, sp_address) const localAddress = await this.getDeviceAddress() as any
}, 2000) const emptyTxid = '0'.repeat(64)
try {
await services.sdkClient.pair_device(`${emptyTxid}:${U32_MAX}`, [spAddress])
} catch (e) {
console.error("Services ~ Error:", e);
return
}
const apiReturn = await this.prepareProcessTx(localAddress, spAddress)
this.handleApiReturn(apiReturn);
return
} }
async resetDevice() { async resetDevice() {