From 22224c0ab70646a902ff71f9681c8cde510552e9 Mon Sep 17 00:00:00 2001 From: Chralu Date: Mon, 3 Jun 2024 14:20:27 +0200 Subject: [PATCH] Import Archethic Wallet Client in web browser extension implementation. --- web_chrome_extension/src/archethic.ts | 78 ++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/web_chrome_extension/src/archethic.ts b/web_chrome_extension/src/archethic.ts index cee612f35..c80e918e0 100644 --- a/web_chrome_extension/src/archethic.ts +++ b/web_chrome_extension/src/archethic.ts @@ -1,9 +1,85 @@ -import { AWCWebBrowserExtensionStreamChannel, ArchethicWalletClient } from "@archethicjs/sdk"; +import { ArchethicWalletClient, AWCStreamChannel, AWCStreamChannelState } from "@archethicjs/sdk"; var rawParams = document.currentScript?.dataset.params var extensionId = rawParams === undefined ? null : JSON.parse(rawParams).extensionId + +class AWCWebBrowserExtensionStreamChannel implements AWCStreamChannel { + private extensionId: string + private _port: chrome.runtime.Port | null = null + private _state: AWCStreamChannelState = AWCStreamChannelState.CLOSED + + constructor(extensionId: string | undefined) { + if (extensionId === undefined) throw new Error('Archethic Wallet Web extension not available') + this.extensionId = extensionId + } + + async connect(): Promise { + if (this._port !== null) { + console.log(`[AWC] Popup extension already running`) + return + } + + this._state = AWCStreamChannelState.CONNECTING + + console.log(`[AWC] Wait for popup extension ...`) + await chrome.runtime.sendMessage(this.extensionId, 'ensureExtensionPopupOpened') + console.log(`[AWC] ... opened`) + + console.log(`[AWC] Connecting to popup extension ...`) + this._port = chrome.runtime.connect(this.extensionId) + this._port.onDisconnect.addListener(() => { + this._port = null + this._connectionClosed() + }) + this._port.onMessage.addListener((message: string, _) => { + if (message === 'connected') { + this._connectionReady() + return + + } + console.log(`[AWC] Received message ${message}`) + if (this.onReceive !== null) this.onReceive(message) + }) + } + + _connectionClosed() { + console.log(`[AWC] Connection closed`) + this._state = AWCStreamChannelState.CLOSED + if (this.onClose !== null) this.onClose('') + } + + _connectionReady() { + console.log(`[AWC] Connection ready`) + this._state = AWCStreamChannelState.OPEN + if (this.onReady !== null) this.onReady() + } + + async close(): Promise { + this._port?.disconnect() + this._connectionClosed() + } + + async send(data: string): Promise { + if (this._port == null) { + throw "[AWC] Disconnected" + } + await this._port.postMessage(data) + } + + public onReceive: ((data: string) => Promise) | null = null + + public onReady: (() => Promise) | null = null + + public onClose: ((reason: string) => Promise) | null = null + + get state(): AWCStreamChannelState { + return this._state + } +} + + /** * Transport (low level communication) object to communicate with Archethic Wallet browser extension */