fix pairing to adapt to latest version - still mocking ending
This commit is contained in:
parent
033b92eed8
commit
cbdeb4345a
@ -20,6 +20,10 @@ body {
|
|||||||
background-blend-mode :soft-light;
|
background-blend-mode :soft-light;
|
||||||
height: 100vh;
|
height: 100vh;
|
||||||
}
|
}
|
||||||
|
.message {
|
||||||
|
font-size: 14px;
|
||||||
|
overflow-wrap: anywhere;
|
||||||
|
}
|
||||||
|
|
||||||
/** Modal Css */
|
/** Modal Css */
|
||||||
.modal {
|
.modal {
|
||||||
@ -35,7 +39,7 @@ body {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.modal-content {
|
.modal-content {
|
||||||
width: 40%;
|
width: 55%;
|
||||||
height: 30%;
|
height: 30%;
|
||||||
background-color: white;
|
background-color: white;
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
@ -55,14 +59,14 @@ body {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.confirmation-box {
|
.confirmation-box {
|
||||||
margin-top: 20px;
|
/* margin-top: 20px; */
|
||||||
align-content: center;
|
align-content: center;
|
||||||
width: 70%;
|
width: 70%;
|
||||||
height: 20%;
|
height: 20%;
|
||||||
padding: 20px;
|
/* padding: 20px; */
|
||||||
font-size: 1.6em;
|
font-size: 1.5em;
|
||||||
color: #333333;
|
color: #333333;
|
||||||
top: 20%;
|
top: 5%;
|
||||||
position: relative;
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
15
src/html/confirmation-modal.html
Normal file
15
src/html/confirmation-modal.html
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<div id="modal" class="modal">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-title">Login</div>
|
||||||
|
<div class="message">
|
||||||
|
Attempting to pair device with address
|
||||||
|
<strong>{{device1}}</strong>
|
||||||
|
with device with address
|
||||||
|
<strong>{{device2}}</strong>
|
||||||
|
</div>
|
||||||
|
<div class="confirmation-box">
|
||||||
|
<a class="btn confirmation-btn" onclick="confirm()">Confirm</a>
|
||||||
|
<a class="btn refusal-btn" onclick="closeConfirmationModal()">Refuse</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
13
src/html/confirmation-modal.js
Normal file
13
src/html/confirmation-modal.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import Routing from "/src/services/routing.service.ts";
|
||||||
|
|
||||||
|
const router = await Routing.getInstance();
|
||||||
|
export async function confirm() {
|
||||||
|
router.confirm()
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function closeConfirmationModal() {
|
||||||
|
router.closeConirmationModal()
|
||||||
|
}
|
||||||
|
|
||||||
|
window.confirm = confirm;
|
||||||
|
window.closeConfirmationModal = closeConfirmationModal;
|
@ -25,9 +25,9 @@ document.querySelectorAll('.tab').forEach(tab => {
|
|||||||
|
|
||||||
|
|
||||||
//// Modal
|
//// Modal
|
||||||
export async function openModal(prd) {
|
export async function openModal(myAddress, receiverAddress) {
|
||||||
const router = await Routing.getInstance();
|
const router = await Routing.getInstance();
|
||||||
router.openLoginModal(prd)
|
router.openLoginModal(myAddress, receiverAddress)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -73,12 +73,12 @@ docReady(function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function scanDevice() {
|
function scanDevice() {
|
||||||
service.sendPairingTx('decodedText')
|
// service.sendPairingTx('decodedText')
|
||||||
|
|
||||||
// const scannerImg = document.querySelector('#scanner')
|
const scannerImg = document.querySelector('#scanner')
|
||||||
// if(scannerImg) scannerImg.style.display = 'none'
|
if(scannerImg) scannerImg.style.display = 'none'
|
||||||
// const scannerQrCode = document.querySelector('.qr-code-scanner')
|
const scannerQrCode = document.querySelector('.qr-code-scanner')
|
||||||
// if(scannerQrCode) scannerQrCode.style.display = 'block'
|
if(scannerQrCode) scannerQrCode.style.display = 'block'
|
||||||
}
|
}
|
||||||
|
|
||||||
window.scanDevice = scanDevice
|
window.scanDevice = scanDevice
|
@ -1,9 +1,16 @@
|
|||||||
<div id="modal" class="modal">
|
<div id="login-modal" class="modal">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
<div class="modal-title">Login</div>
|
<div class="modal-title">Login</div>
|
||||||
<div class="confirmation-box">
|
<div class="confirmation-box">
|
||||||
<a class="btn confirmation-btn" onclick="confirmLogin()">Confirm</a>
|
<div class="message">
|
||||||
<a class="btn refusal-btn" onclick="closeLoginModal()">Refuse</a>
|
Attempting to pair device with address
|
||||||
|
<strong>{{device1}}</strong>
|
||||||
|
with device with address
|
||||||
|
<strong>{{device2}}</strong>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
Awaiting pairing validation...
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
@ -1,7 +1,7 @@
|
|||||||
import Services from './services/service';
|
import Services from './services/service';
|
||||||
// import { WebSocketClient } from './websockets';
|
// import { WebSocketClient } from './websockets';
|
||||||
|
|
||||||
const wsurl = `ws://74.234.68.175:8090`;
|
const wsurl = `ws://74.234.68.175:8091`;
|
||||||
document.addEventListener('DOMContentLoaded', async () => {
|
document.addEventListener('DOMContentLoaded', async () => {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
@ -20,6 +20,13 @@ document.addEventListener('DOMContentLoaded', async () => {
|
|||||||
}
|
}
|
||||||
await services.addWebsocketConnection(wsurl);
|
await services.addWebsocketConnection(wsurl);
|
||||||
await services.recoverInjectHtml()
|
await services.recoverInjectHtml()
|
||||||
|
|
||||||
|
const queryString = window.location.search;
|
||||||
|
const urlParams = new URLSearchParams(queryString)
|
||||||
|
const pairingAddress = urlParams.get('address')
|
||||||
|
if(pairingAddress) {
|
||||||
|
await services.sendPairingTx(pairingAddress)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
@ -37,7 +37,7 @@ class Database {
|
|||||||
},
|
},
|
||||||
AnkMessages: {
|
AnkMessages: {
|
||||||
name: "messages",
|
name: "messages",
|
||||||
options: {'keyPath': 'transaction'},
|
options: {'keyPath': 'id'},
|
||||||
indices: []
|
indices: []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import Database from './database';
|
import Database from './database';
|
||||||
import modalHtml from '../html/login-modal.html?raw';
|
import modalHtml from '../html/login-modal.html?raw';
|
||||||
|
import confirmationModalHtml from '../html/confirmation-modal.html?raw';
|
||||||
import modalScript from '../html/login-modal.js?raw';
|
import modalScript from '../html/login-modal.js?raw';
|
||||||
|
import confirmationModalScript from '../html/confirmation-modal.js?raw';
|
||||||
import Services from './service';
|
import Services from './service';
|
||||||
|
|
||||||
|
|
||||||
@ -8,7 +10,8 @@ export default class Routing {
|
|||||||
private static instance: Routing;
|
private static instance: Routing;
|
||||||
private database: any;
|
private database: any;
|
||||||
private sdkClient: any;
|
private sdkClient: any;
|
||||||
private prd: any;
|
private currentPrd: any;
|
||||||
|
private currentOutpoint?: string;
|
||||||
private constructor() {}
|
private constructor() {}
|
||||||
|
|
||||||
// Method to access the singleton instance of Services
|
// Method to access the singleton instance of Services
|
||||||
@ -25,20 +28,46 @@ export default class Routing {
|
|||||||
this.database = Database.getInstance()
|
this.database = Database.getInstance()
|
||||||
}
|
}
|
||||||
|
|
||||||
public openLoginModal(prd?: any) {
|
public openLoginModal(myAddress: string, receiverAddress: string) {
|
||||||
const container = document.querySelector('.page-container');
|
const container = document.querySelector('.page-container');
|
||||||
if (container) container.innerHTML += modalHtml;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public openConfirmationModal(prd?: any, outpointCommitment?: string) {
|
||||||
|
this.currentPrd = prd;
|
||||||
|
const sender = JSON.parse(prd.sender)?.sp_addresses
|
||||||
|
let html = confirmationModalHtml
|
||||||
|
html = html.replace('{{device1}}', sender[0])
|
||||||
|
html = html.replace('{{device2}}', sender[1])
|
||||||
|
this.currentOutpoint = outpointCommitment as string;
|
||||||
|
const container = document.querySelector('.page-container');
|
||||||
|
|
||||||
|
if (container) container.innerHTML += html;
|
||||||
const modal = document.getElementById('modal')
|
const modal = document.getElementById('modal')
|
||||||
if (modal) modal.style.display = 'flex';
|
if (modal) modal.style.display = 'flex';
|
||||||
const newScript = document.createElement('script');
|
const newScript = document.createElement('script');
|
||||||
newScript.setAttribute('type', 'module')
|
newScript.setAttribute('type', 'module')
|
||||||
newScript.textContent = modalScript;
|
newScript.textContent = confirmationModalScript;
|
||||||
document.head.appendChild(newScript).parentNode?.removeChild(newScript);
|
document.head.appendChild(newScript).parentNode?.removeChild(newScript);
|
||||||
|
|
||||||
|
// Add correct text
|
||||||
|
|
||||||
// Close modal when clicking outside of it
|
// Close modal when clicking outside of it
|
||||||
window.onclick = (event) => {
|
window.onclick = (event) => {
|
||||||
const modal = document.getElementById('modal');
|
const modal = document.getElementById('modal');
|
||||||
if (event.target === modal) {
|
if (event.target === modal) {
|
||||||
this.closeLoginModal(prd);
|
this.confirm(this.currentPrd || prd, outpointCommitment || this.currentOutpoint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -48,10 +77,20 @@ export default class Routing {
|
|||||||
console.log("🚀 ~ Routing ~ confirmLogin ~ loginTx:", loginTx)
|
console.log("🚀 ~ Routing ~ confirmLogin ~ loginTx:", loginTx)
|
||||||
this.sdkClient.login('LOGIN', loginTx)
|
this.sdkClient.login('LOGIN', loginTx)
|
||||||
}
|
}
|
||||||
async closeLoginModal(prd?: any) {
|
async closeLoginModal() {
|
||||||
|
const modal = document.getElementById('login-modal')
|
||||||
|
if (modal) modal.style.display = 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
async confirm(prd?: any, outpointCommitment?: string) {
|
||||||
const service = await Services.getInstance()
|
const service = await Services.getInstance()
|
||||||
|
const modal = document.getElementById('modal')
|
||||||
|
console.log("🚀 ~ Routing ~ confirm ~ prd:", this.currentPrd || prd, outpointCommitment)
|
||||||
|
if (modal) modal.style.display = 'none';
|
||||||
|
if(this.currentPrd || prd) service.pairDevice(this.currentPrd || prd, outpointCommitment || this.currentOutpoint as string)
|
||||||
|
}
|
||||||
|
async closeConfirmationModal() {
|
||||||
const modal = document.getElementById('modal')
|
const modal = document.getElementById('modal')
|
||||||
if (modal) modal.style.display = 'none';
|
if (modal) modal.style.display = 'none';
|
||||||
if(prd) service.pairDevice(prd)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,7 +9,7 @@ import Database from './database';
|
|||||||
import { WebSocketClient } from '../websockets';
|
import { WebSocketClient } from '../websockets';
|
||||||
import QRCode from 'qrcode'
|
import QRCode from 'qrcode'
|
||||||
import { servicesVersion } from 'typescript';
|
import { servicesVersion } from 'typescript';
|
||||||
import { CachedMessage } from '../../dist/pkg/sdk_client';
|
import { ApiReturn, CachedMessage, Member } from '../../dist/pkg/sdk_client';
|
||||||
import Routing from './routing.service';
|
import Routing from './routing.service';
|
||||||
|
|
||||||
export default class Services {
|
export default class Services {
|
||||||
@ -28,7 +28,6 @@ export default class Services {
|
|||||||
// Method to access the singleton instance of Services
|
// Method to access the singleton instance of Services
|
||||||
public static async getInstance(): Promise<Services> {
|
public static async getInstance(): Promise<Services> {
|
||||||
if (!Services.instance) {
|
if (!Services.instance) {
|
||||||
console.log("🚀 ~ Services ~ getInstance ~ Services.instance:", Services.instance)
|
|
||||||
Services.instance = new Services();
|
Services.instance = new Services();
|
||||||
await Services.instance.init();
|
await Services.instance.init();
|
||||||
}
|
}
|
||||||
@ -68,17 +67,18 @@ export default class Services {
|
|||||||
if(btn) {
|
if(btn) {
|
||||||
this.addSubscription(btn, 'click', 'injectProcessListPage')
|
this.addSubscription(btn, 'click', 'injectProcessListPage')
|
||||||
}
|
}
|
||||||
|
const url = location.href
|
||||||
|
|
||||||
this.generateQRCode(this.sp_address || '')
|
this.generateQRCode(this.sp_address || '')
|
||||||
}
|
}
|
||||||
|
|
||||||
private generateQRCode = async (text: string) => {
|
private generateQRCode = async (text: string) => {
|
||||||
|
console.log("🚀 ~ Services ~ generateQRCode= ~ text:", text)
|
||||||
try {
|
try {
|
||||||
const container = document.getElementById('containerId');
|
const container = document.getElementById('containerId');
|
||||||
const url = await QRCode.toDataURL(text);
|
const url = await QRCode.toDataURL(text);
|
||||||
const qrCode = container?.querySelector('.qr-code img');
|
const qrCode = container?.querySelector('.qr-code img');
|
||||||
qrCode?.setAttribute('src', url)
|
qrCode?.setAttribute('src', url)
|
||||||
console.log(url);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
@ -89,78 +89,84 @@ export default class Services {
|
|||||||
const txid = '0'.repeat(64)
|
const txid = '0'.repeat(64)
|
||||||
var vout = Number.MAX_SAFE_INTEGER;
|
var vout = Number.MAX_SAFE_INTEGER;
|
||||||
const paringTemplate = {
|
const paringTemplate = {
|
||||||
"uuid": "",
|
|
||||||
"html": "",
|
"html": "",
|
||||||
"script": "",
|
|
||||||
"style": "",
|
"style": "",
|
||||||
"init_state": {
|
"script": "",
|
||||||
|
"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": 0.0,
|
"quorum": 1.0,
|
||||||
"fields": [
|
"fields": [
|
||||||
"roles",
|
"roles",
|
||||||
"pairing_tx"
|
"pairing_tx"
|
||||||
],
|
],
|
||||||
"min_sig_member": 0.0
|
"min_sig_member": 1.0
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"pairing_tx": "",
|
"pairing_tx": `${txid}:4294967295`,
|
||||||
},
|
|
||||||
"commited_in": `${txid}:4294967295`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const service = await Services.getInstance();
|
const service = await Services.getInstance();
|
||||||
const process = await service.sdkClient.create_process_from_template(JSON.stringify(paringTemplate))
|
const process = await service.sdkClient.create_update_transaction(undefined, JSON.stringify(paringTemplate), 1)
|
||||||
console.log("🚀 ~ Services ~ prepareProcessTx ~ process:", process)
|
|
||||||
return process
|
return process
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendPairingTx(sp_address: string): Promise<void> {
|
async sendPairingTx(sp_address: 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:", typeof amount)
|
|
||||||
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
|
const spAddress = await this.getDeviceAddress() as any
|
||||||
const process = await this.prepareProcessTx(spAddress, 'sprt1qqdxya9n4pzc9vetzug7fch2xag4x5vxxrjc6rnh2rh3xhlvalmlrsqmy5t6vfa0nuf92dudf9uvaye3afw6uu86kw34q2x3k9qmmf8qp0vtfm9xa')
|
let txid = '0'.repeat(64)
|
||||||
if(process) await services.sdkClient.pair_device(process.uuid, ['sprt1qqdxya9n4pzc9vetzug7fch2xag4x5vxxrjc6rnh2rh3xhlvalmlrsqmy5t6vfa0nuf92dudf9uvaye3afw6uu86kw34q2x3k9qmmf8qp0vtfm9xa'])
|
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
const tx = await services.sdkClient.create_process_init_transaction(process, 1)
|
let pairing = await services.sdkClient.pair_device(`${txid}:4294967295`, [sp_address])
|
||||||
console.log("🚀 ~ Services ~ sendPairingTx ~ tx:", tx)
|
const process = await this.prepareProcessTx(spAddress, sp_address)
|
||||||
services.websocketConnection?.sendMessage('NewTx', JSON.stringify(tx))
|
const tx = process.new_tx_to_send
|
||||||
if(tx?.new_messages && tx.new_messages.length) {
|
const parsedTx = JSON.parse(tx)
|
||||||
await this.sendCipherMessages(tx.new_messages)
|
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)
|
||||||
}, 2000)
|
}, 2000)
|
||||||
// setTimeout(() => {
|
|
||||||
// const paringin = services.sdkClient.create_pairing_transaction('sprt1qqf7jes989hkmfh0w9526pqy0rd5hlflclzez5dzateeza7hhl0vqzqhnmhe0kkez9m5vx2rastph5patqr5h3tr9pcsukzea0lz0dp6tcqaz2u8j', 1)
|
|
||||||
// console.log("🚀 ~ Services ~ setTimeout ~ paringin:", paringin)
|
|
||||||
// services.websocketConnection?.sendMessage('NewTx', JSON.stringify(paringin))
|
|
||||||
// if(paringin?.new_network_msg?.ciphertext) {
|
|
||||||
// services.websocketConnection?.sendMessage('Cipher', paringin?.new_network_msg?.ciphertext)
|
|
||||||
// let get_outputs_result = services.sdkClient.get_outputs()
|
|
||||||
// console.log("🚀 ~ Services ~ setTimeout ~ get_outputs_result:", get_outputs_result)
|
|
||||||
// }
|
|
||||||
// }, 5000)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendCipherMessages(messages: CachedMessage[]) {
|
async resetDevice() {
|
||||||
|
const service = await Services.getInstance()
|
||||||
|
await service.sdkClient.reset_device()
|
||||||
|
}
|
||||||
|
|
||||||
|
async sendCipherMessages(messages: string[]) {
|
||||||
const service = await Services.getInstance();
|
const service = await Services.getInstance();
|
||||||
messages.forEach((message) => {
|
messages.forEach((message) => {
|
||||||
message.cipher?.forEach((cipher => {
|
service.websocketConnection?.sendMessage('Cipher', message)
|
||||||
service.websocketConnection?.sendMessage('Cipher', cipher)
|
|
||||||
}))
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,10 +180,18 @@ export default class Services {
|
|||||||
async parseCipher(message: string) {
|
async parseCipher(message: string) {
|
||||||
try {
|
try {
|
||||||
const services = await Services.getInstance();
|
const services = await Services.getInstance();
|
||||||
|
try {
|
||||||
|
JSON.parse(message)
|
||||||
|
const router = await Routing.getInstance();
|
||||||
|
router.closeLoginModal()
|
||||||
|
this.injectProcessListPage()
|
||||||
|
} catch {
|
||||||
|
console.log('Not proper format for cipher')
|
||||||
|
}
|
||||||
const parsedTx = await services.sdkClient.parse_cipher(message, 0.00001)
|
const parsedTx = await services.sdkClient.parse_cipher(message, 0.00001)
|
||||||
console.log("🚀 ~ Services ~ parseCipher ~ parsedTx:", parsedTx)
|
console.log("🚀 ~ Services ~ parseCipher ~ parsedTx:", parsedTx)
|
||||||
await this.processTx(parsedTx)
|
await this.processTx(parsedTx)
|
||||||
await this.saveCipherTxToDb(parsedTx)
|
// await this.saveCipherTxToDb(parsedTx)
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log(e)
|
console.log(e)
|
||||||
}
|
}
|
||||||
@ -191,60 +205,112 @@ export default class Services {
|
|||||||
console.log("🚀 ~ Services ~ parseNewTx ~ parsedTx:", parsedTx)
|
console.log("🚀 ~ Services ~ parseNewTx ~ parsedTx:", parsedTx)
|
||||||
if(parsedTx) {
|
if(parsedTx) {
|
||||||
await this.processTx(parsedTx)
|
await this.processTx(parsedTx)
|
||||||
await this.saveTxToDb(parsedTx)
|
// await this.saveTxToDb(parsedTx)
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log(e)
|
console.log(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async processTx(tx: CachedMessage) {
|
async processTx(tx: ApiReturn) {
|
||||||
if(tx.status) {
|
const service = await Services.getInstance()
|
||||||
switch(tx.status) {
|
if(tx.ciphers_to_send && tx.ciphers_to_send.length) {
|
||||||
case 'Opened':
|
await this.sendCipherMessages(tx.ciphers_to_send)
|
||||||
if(tx.prd) {
|
}
|
||||||
const parsedPrd = JSON.parse(tx.prd)
|
|
||||||
console.log("🚀 ~ Services ~ processTx ~ Opened:", parsedPrd)
|
if(tx.new_tx_to_send) {
|
||||||
|
service.websocketConnection?.sendMessage('NewTx', tx.new_tx_to_send)
|
||||||
|
}
|
||||||
|
if(tx.updated_process && tx.updated_process.length) {
|
||||||
|
// if(tx.ciphers_to_send?.length != 1 || tx.updated_process?.length != 2) return
|
||||||
|
const impendingRequest = tx.updated_process[1].impending_requests[0]
|
||||||
|
const pcdCommitment = impendingRequest.payload
|
||||||
|
const outpointCommitment = tx.updated_process[0]
|
||||||
|
console.log("🚀 ~ Services ~ processTx ~ pcdCommitment:", pcdCommitment)
|
||||||
|
if(impendingRequest.prd_type === 'Update' && tx.ciphers_to_send.length === 0) {
|
||||||
const router = await Routing.getInstance();
|
const router = await Routing.getInstance();
|
||||||
|
|
||||||
// Mocker le fait que c'est une transaction de connection
|
// Mocker le fait que c'est une transaction de connection
|
||||||
if(parsedPrd && parsedPrd.prd_type === 'Init') {
|
const services = await Services.getInstance();
|
||||||
router.openLoginModal(parsedPrd)
|
// const faucetMessage = await services.createFaucetMessage()
|
||||||
} else if(parsedPrd && parsedPrd.prd_type === 'Signed') {
|
// console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ faucetMessage:", faucetMessage)
|
||||||
router.closeLoginModal()
|
// services.websocketConnection?.sendNormalMessage(faucetMessage)
|
||||||
}
|
router.openConfirmationModal(impendingRequest, outpointCommitment)
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'TxWaitingPrd':
|
|
||||||
console.log("🚀 ~ Services ~ processTx ~ TxWaitingPrd:", tx)
|
|
||||||
break;
|
|
||||||
case 'CipherWaitingTx':
|
|
||||||
console.log("🚀 ~ Services ~ processTx ~ CipherWaitingTx:", tx)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async pairDevice(prd: any) {
|
if(tx.updated_cached_msg && tx.updated_cached_msg.length) {
|
||||||
|
tx.updated_cached_msg.forEach(message => this.saveTxToDb(message))
|
||||||
|
}
|
||||||
|
|
||||||
|
// if(tx.prd) {
|
||||||
|
// const parsedPrd = JSON.parse(tx.prd)
|
||||||
|
// console.log("🚀 ~ Services ~ processTx ~ Opened:", parsedPrd)
|
||||||
|
// const router = await Routing.getInstance();
|
||||||
|
|
||||||
|
// // Mocker le fait que c'est une transaction de connection
|
||||||
|
// if(parsedPrd && parsedPrd.prd_type === 'Init') {
|
||||||
|
// const services = await Services.getInstance();
|
||||||
|
// const faucetMessage = await services.createFaucetMessage()
|
||||||
|
// console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ faucetMessage:", faucetMessage)
|
||||||
|
// services.websocketConnection?.sendNormalMessage(faucetMessage)
|
||||||
|
// router.openConfirmationModal(parsedPrd)
|
||||||
|
// } else if(parsedPrd && parsedPrd.prd_type === 'Signed') {
|
||||||
|
// router.closeLoginModal()
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
async pairDevice(prd: any, outpointCommitment: string) {
|
||||||
|
console.log("🚀 ~ Services ~ pairDevice ~ prd:", prd)
|
||||||
const service = await Services.getInstance();
|
const service = await Services.getInstance();
|
||||||
await service.sdkClient.pair_device(prd.process_uuid, ['sprt1qqwp87lrahm2ye9kje45dqgsncvkg94fr7amrnaaflxkx6kn62za7uq4kpegfp3ks475e9jp7y0h0xzsqydkf35lg7g0kukr6zkynde552gk6z92n'])
|
const spAddress = await this.getDeviceAddress() as any;
|
||||||
const spAddress = await this.getDeviceAddress() as any
|
const sender = JSON.parse(prd?.sender)
|
||||||
const process = await this.prepareProcessTx(spAddress, 'sprt1qqwp87lrahm2ye9kje45dqgsncvkg94fr7amrnaaflxkx6kn62za7uq4kpegfp3ks475e9jp7y0h0xzsqydkf35lg7g0kukr6zkynde552gk6z92n')
|
const senderAddress = sender?.sp_addresses?.find((address: string) => address !== spAddress)
|
||||||
const tx = await service.sdkClient.create_process_init_transaction(process, 1)
|
console.log("🚀 ~ Services ~ pairDevice ~ senderAddress:", senderAddress)
|
||||||
for(const msg of tx.new_messages) {
|
if(senderAddress) {
|
||||||
msg.prd?.replace('Init', 'Signed')
|
const proposal = service.sdkClient.get_update_proposals(outpointCommitment);
|
||||||
}
|
console.log("🚀 ~ Services ~ pairDevice ~ proposal:", proposal)
|
||||||
|
// const pairingTx = proposal.pairing_tx.replace(/^\"+|\"+$/g, '')
|
||||||
|
const parsedProposal = JSON.parse(proposal[0])
|
||||||
|
|
||||||
|
console.log("🚀 ~ Services ~ pairDevice ~ parsedProposal:", parsedProposal)
|
||||||
|
const roles = JSON.parse(parsedProposal.roles)
|
||||||
|
console.log("🚀 ~ Services ~ pairDevice ~ roles:", roles, Array.isArray(roles), !roles.owner)
|
||||||
|
if(Array.isArray(roles) || !roles.owner) return
|
||||||
|
const proposalMembers = roles?.owner?.members
|
||||||
|
const isFirstDevice = proposalMembers.some((member: Member) => member.sp_addresses.some(address => address === spAddress))
|
||||||
|
const isSecondDevice = proposalMembers.some((member: Member) => member.sp_addresses.some(address => address === senderAddress))
|
||||||
|
console.log("🚀 ~ Services ~ pairDevice ~ proposalMembers:", proposalMembers)
|
||||||
|
if(proposalMembers?.length !== 2 || !isFirstDevice || !isSecondDevice) return
|
||||||
|
const pairingTx = parsedProposal?.pairing_tx?.replace(/^\"+|\"+$/g, '')
|
||||||
|
|
||||||
|
let txid = '0'.repeat(64)
|
||||||
|
console.log("🚀 ~ Services ~ pairDevice ~ pairingTx:", pairingTx, `${txid}:4294967295`)
|
||||||
|
|
||||||
|
const pairing = await service.sdkClient.pair_device(`${txid}:4294967295`, [senderAddress])
|
||||||
|
const device = this.dumpDevice()
|
||||||
|
console.log("🚀 ~ Services ~ pairDevice ~ device:", device)
|
||||||
|
this.saveDevice(device)
|
||||||
|
// await service.sdkClient.pair_device(pairingTx, [senderAddress])
|
||||||
|
console.log("🚀 ~ Services ~ pairDevice ~ pairing:", pairing)
|
||||||
|
// const process = await this.prepareProcessTx(spAddress, senderAddress)
|
||||||
|
console.log("🚀 ~ Services ~ pairDevice ~ process:", outpointCommitment, prd, prd.payload)
|
||||||
|
const prdString = JSON.stringify(prd).trim()
|
||||||
|
console.log("🚀 ~ Services ~ pairDevice ~ prdString:", prdString)
|
||||||
|
let tx = await service.sdkClient.response_prd(outpointCommitment, prdString, true)
|
||||||
console.log("🚀 ~ Services ~ pairDevice ~ tx:", tx)
|
console.log("🚀 ~ Services ~ pairDevice ~ tx:", tx)
|
||||||
service.websocketConnection?.sendMessage('NewTx', JSON.stringify(tx))
|
if(tx.ciphers_to_send) {
|
||||||
|
tx.ciphers_to_send.forEach((cipher: string) => service.websocketConnection?.sendMessage('Cipher', cipher))
|
||||||
|
}
|
||||||
|
this.injectProcessListPage()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async saveTxToDb(tx: string) {
|
async saveTxToDb(tx: CachedMessage) {
|
||||||
const database = await Database.getInstance();
|
const database = await Database.getInstance();
|
||||||
const indexedDb = await database.getDb();
|
const indexedDb = await database.getDb();
|
||||||
|
await database.writeObject(indexedDb, 'messages', tx, null);
|
||||||
if(tx) {
|
|
||||||
await database.writeObject(indexedDb, database.getStoreList().AnkMessages, tx, null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async saveCipherTxToDb(tx: string) {
|
async saveCipherTxToDb(tx: string) {
|
||||||
@ -315,9 +381,8 @@ export default class Services {
|
|||||||
|
|
||||||
async createNewDevice() {
|
async createNewDevice() {
|
||||||
const service = await Services.getInstance();
|
const service = await Services.getInstance();
|
||||||
const sp_address = await service.sdkClient.create_new_device(1994, 'regtest')
|
const sp_address = await service.sdkClient.create_new_device(0, 'regtest')
|
||||||
if(sp_address) {
|
if(sp_address) {
|
||||||
console.log("🚀 ~ Services ~ createNewDevice ~ device:", {sp_adress: sp_address})
|
|
||||||
const database = await Database.getInstance();
|
const database = await Database.getInstance();
|
||||||
const indexedDb = await database.getDb();
|
const indexedDb = await database.getDb();
|
||||||
await database.writeObject(indexedDb, database.getStoreList().AnkSpAddress, {sp_address: sp_address}, null);
|
await database.writeObject(indexedDb, database.getStoreList().AnkSpAddress, {sp_address: sp_address}, null);
|
||||||
@ -326,7 +391,6 @@ export default class Services {
|
|||||||
await service.saveDevice(device)
|
await service.saveDevice(device)
|
||||||
}
|
}
|
||||||
this.sp_address = sp_address;
|
this.sp_address = sp_address;
|
||||||
console.log("🚀 ~ Services ~ createNewDevice ~ device:", sp_address)
|
|
||||||
return sp_address;
|
return sp_address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,6 +15,13 @@ class WebSocketClient {
|
|||||||
console.log('WebSocket connection established');
|
console.log('WebSocket connection established');
|
||||||
// Once the connection is open, send all messages in the queue
|
// Once the connection is open, send all messages in the queue
|
||||||
// this.sendNormalMessage(faucetMessage)
|
// this.sendNormalMessage(faucetMessage)
|
||||||
|
const service = await Services.getInstance();
|
||||||
|
const amount = await service.getAmount() as bigint
|
||||||
|
|
||||||
|
if(amount === 0n) {
|
||||||
|
const faucetMessage = await services.createFaucetMessage()
|
||||||
|
this.sendNormalMessage(faucetMessage)
|
||||||
|
}
|
||||||
|
|
||||||
while (this.messageQueue.length > 0) {
|
while (this.messageQueue.length > 0) {
|
||||||
const message = this.messageQueue.shift();
|
const message = this.messageQueue.shift();
|
||||||
|
@ -2,6 +2,8 @@ import { defineConfig } from 'vite';
|
|||||||
import vue from '@vitejs/plugin-vue'; // or react from '@vitejs/plugin-react' if using React
|
import vue from '@vitejs/plugin-vue'; // or react from '@vitejs/plugin-react' if using React
|
||||||
import wasm from 'vite-plugin-wasm';
|
import wasm from 'vite-plugin-wasm';
|
||||||
import {createHtmlPlugin} from 'vite-plugin-html';
|
import {createHtmlPlugin} from 'vite-plugin-html';
|
||||||
|
import fs from 'fs'
|
||||||
|
import path from 'path'
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
optimizeDeps: {
|
optimizeDeps: {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user