Refactor initialisation of pairing process
This commit is contained in:
parent
b8a1a0bc6b
commit
edef1428c3
20
src/index.ts
20
src/index.ts
@ -4,23 +4,20 @@ import Services from './services/service';
|
||||
// const wsurl = `ws://74.234.68.175:8091`;
|
||||
const wsurl = `wss://localhost/ws/`;
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
try {
|
||||
try {
|
||||
|
||||
const services = await Services.getInstance();
|
||||
let user = await services.getWallet()
|
||||
|
||||
setTimeout(async () => {
|
||||
if(!user) {
|
||||
const sp_adress = await services.createNewDevice();
|
||||
user = await services.getWallet()
|
||||
let device = await services.getDevice()
|
||||
|
||||
if(!device) {
|
||||
device = await services.createNewDevice();
|
||||
} else {
|
||||
const address = await services.getAdresses()
|
||||
const device = await services.getDevice()
|
||||
if(device) await services.restoreDevice(device)
|
||||
await services.restoreDevice(device)
|
||||
}
|
||||
await services.addWebsocketConnection(wsurl);
|
||||
await services.recoverInjectHtml()
|
||||
await services.recoverInjectHtml();
|
||||
await services.restoreProcesses();
|
||||
await services.restoreMessages();
|
||||
|
||||
const queryString = window.location.search;
|
||||
const urlParams = new URLSearchParams(queryString)
|
||||
@ -28,7 +25,6 @@ document.addEventListener('DOMContentLoaded', async () => {
|
||||
if(pairingAddress) {
|
||||
await services.sendPairingTx(pairingAddress)
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
@ -16,12 +16,13 @@ type ProcessesCache = {
|
||||
[key: string]: any;
|
||||
};
|
||||
|
||||
const U32_MAX = 4294967295;
|
||||
|
||||
export default class Services {
|
||||
private static instance: Services;
|
||||
private current_process: string | null = null;
|
||||
private sdkClient: any;
|
||||
private websocketConnection: WebSocketClient | null = null;
|
||||
private sp_address: string | null = null;
|
||||
private processes: IProcess[] | null = null;
|
||||
private notifications: INotification[] | null = null;
|
||||
private subscriptions: {element: Element; event: string; eventHandler: string;}[] = [] ;
|
||||
@ -52,6 +53,7 @@ export default class Services {
|
||||
services.websocketConnection = newClient;
|
||||
}
|
||||
}
|
||||
|
||||
public async recoverInjectHtml(): Promise<void> {
|
||||
const container = document.getElementById('containerId');
|
||||
|
||||
@ -71,9 +73,12 @@ export default class Services {
|
||||
if(btn) {
|
||||
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) => {
|
||||
@ -90,77 +95,64 @@ export default class Services {
|
||||
}
|
||||
|
||||
async prepareProcessTx(myAddress: string, recipientAddress: string) {
|
||||
|
||||
const txid = '0'.repeat(64)
|
||||
var vout = Number.MAX_SAFE_INTEGER;
|
||||
const initial_session_privkey = new Uint8Array(32);
|
||||
const initial_session_pubkey = new Uint8Array(32);
|
||||
const paringTemplate = {
|
||||
"html": "",
|
||||
"style": "",
|
||||
"script": "",
|
||||
"description": "AliceBob",
|
||||
"roles": {
|
||||
"owner": {
|
||||
"members": [{sp_addresses: [myAddress]}, {sp_addresses:[recipientAddress]}],
|
||||
"members":
|
||||
[{sp_addresses: [myAddress]}, {sp_addresses: [recipientAddress]}],
|
||||
"validation_rules":
|
||||
[
|
||||
{
|
||||
"quorum": 1.0,
|
||||
"fields": [
|
||||
"description",
|
||||
"roles",
|
||||
"pairing_tx"
|
||||
"session_privkey",
|
||||
"session_pubkey",
|
||||
"key_parity"
|
||||
],
|
||||
"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 process = await service.sdkClient.create_update_transaction(undefined, JSON.stringify(paringTemplate), 1)
|
||||
return process
|
||||
const apiReturn = await service.sdkClient.create_update_transaction(undefined, JSON.stringify(paringTemplate), 1)
|
||||
return apiReturn
|
||||
}
|
||||
|
||||
async sendPairingTx(sp_address: string): Promise<void> {
|
||||
public async sendPairingTx(spAddress: string): Promise<void> {
|
||||
const services = await Services.getInstance();
|
||||
const amount = await this.getAmount() as any
|
||||
console.log("🚀 ~ Services ~ sendPairingTx ~ amount:", amount)
|
||||
// if(amount === 0n) {
|
||||
// const faucetMessage = await services.createFaucetMessage()
|
||||
// console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ faucetMessage:", 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)
|
||||
if(amount === 0n) {
|
||||
const faucetMessage = await services.createFaucetMessage()
|
||||
console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ faucetMessage:", faucetMessage)
|
||||
services.websocketConnection?.sendNormalMessage(faucetMessage)
|
||||
}
|
||||
const router = await Routing.getInstance();
|
||||
router.openLoginModal(spAddress, sp_address)
|
||||
}, 2000)
|
||||
|
||||
const localAddress = await this.getDeviceAddress() as any
|
||||
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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user