Skip to content

Commit

Permalink
Import Archethic Wallet Client in web browser extension implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chralu committed Jun 3, 2024
1 parent b72a31a commit 22224c0
Showing 1 changed file with 77 additions and 1 deletion.
78 changes: 77 additions & 1 deletion web_chrome_extension/src/archethic.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
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<void> {
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<void> {
this._port?.disconnect()
this._connectionClosed()
}

async send(data: string): Promise<void> {
if (this._port == null) {
throw "[AWC] Disconnected"
}
await this._port.postMessage(data)
}

public onReceive: ((data: string) => Promise<void>) | null = null

public onReady: (() => Promise<void>) | null = null

public onClose: ((reason: string) => Promise<void>) | null = null

get state(): AWCStreamChannelState {
return this._state
}
}


/**
* Transport (low level communication) object to communicate with Archethic Wallet browser extension
*/
Expand Down

0 comments on commit 22224c0

Please sign in to comment.