diff --git a/src/services.ts b/src/services.ts index e5fd51b..cc8ab92 100644 --- a/src/services.ts +++ b/src/services.ts @@ -1,5 +1,6 @@ import { createUserReturn, User, Process } from '../dist/pkg/sdk_client'; import IndexedDB from './database' +import { WebSocketClient } from './websockets'; class Services { private static instance: Services; @@ -162,6 +163,18 @@ class Services { return imageBytes; } + public async parseBitcoinMessage(raw: Blob): Promise { + try { + const buffer = await raw.arrayBuffer(); + const uint8Array = new Uint8Array(buffer); + const msg: string = this.sdkClient.parse_bitcoin_network_msg(uint8Array); + return msg; + } catch (error) { + console.error("Error processing the blob:", error); + return null; + } + } + public async displayRevoke(): Promise { const services = await Services.getInstance(); services.injectHtml('REVOKE'); @@ -200,6 +213,11 @@ class Services { services.attachSubmitListener("form4nk", services.updateAnId); } + public async parse4nkMessage(raw: string): Promise { + const msg: string = this.sdkClient.parse_4nk_msg(raw); + return msg; + } + public injectUpdateAnIdHtml(bodyToInject: string, styleToInject: string, scriptToInject: string) { console.log("JS html : "+bodyToInject); const body = document.getElementsByTagName('body')[0]; @@ -263,6 +281,16 @@ class Services { } } + public async checkTransaction(tx: string): Promise { + const services = await Services.getInstance(); + try { + return services.sdkClient.check_network_transaction(tx); + } catch (error) { + console.error(error); + return null; + } + } + public async getAllProcessForUser(pre_id: string): Promise { const services = await Services.getInstance(); let user: User; @@ -401,6 +429,16 @@ class Services { } return success; } + + public obtainTokenWithFaucet(wsclient: WebSocketClient, spaddress: string): string | null { + try { + wsclient.sendMessage(spaddress); + } catch (error) { + console.error("Failed to obtain tokens with relay ", wsclient.getUrl()); + return null; + } + return null; + } } export default Services; diff --git a/src/websockets.ts b/src/websockets.ts new file mode 100644 index 0000000..72d8c80 --- /dev/null +++ b/src/websockets.ts @@ -0,0 +1,85 @@ +import Services from "./services"; +// import * as mempool from "./mempool"; + +class WebSocketClient { + private ws: WebSocket; + private messageQueue: string[] = []; + + constructor(url: string, private services: Services) { + this.ws = new WebSocket(url); + + this.ws.addEventListener('open', (event) => { + console.log('WebSocket connection established'); + // Once the connection is open, send all messages in the queue + while (this.messageQueue.length > 0) { + const message = this.messageQueue.shift(); + if (message) { + this.ws.send(message); + } + } + }); + + // Listen for messages + this.ws.addEventListener('message', (event) => { + const msgData = event.data; + console.log(msgData); + + (async () => { + if (msgData instanceof Blob) { + // bitcoin network msg is just bytes + let res = await services.parseBitcoinMessage(msgData); + if (res) { + let ours = await services.checkTransaction(res); + if (ours) { + console.log("Found our utxo in "+res); + } else { + console.log("No utxo found in tx "+res); + } + } else { + console.error("Faile to parse a bitcoin network msg"); + } + } else if (typeof(msgData) === 'string') { + // json strings are 4nk message + console.log("Received text message: "+msgData); + let res = await services.parse4nkMessage(msgData); + if (res) { + console.debug(res); + } + } else { + console.error("Received an unknown message"); + } + })(); + }); + + // Listen for possible errors + this.ws.addEventListener('error', (event) => { + console.error('WebSocket error:', event); + }); + + // Listen for when the connection is closed + this.ws.addEventListener('close', (event) => { + console.log('WebSocket is closed now.'); + }); + } + + // Method to send messages + public sendMessage(message: string): void { + if (this.ws.readyState === WebSocket.OPEN) { + this.ws.send(message); + } else { + console.error('WebSocket is not open. ReadyState:', this.ws.readyState); + this.messageQueue.push(message); + } + } + + public getUrl(): string { + return this.ws.url; + } + + // Method to close the WebSocket connection + public close(): void { + this.ws.close(); + } +} + +export { WebSocketClient };