Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update index.ts #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 70 additions & 38 deletions extension/src/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ export class Server {
async setWallet(wallet: string) {
this.wallet = wallet;
await wasm;
this.client = await new wasm.Client(await wasm.Wallet.create(wallet));
this.client.on_notification((notification: any) => {
console.debug('got notification for', this.subscribers.size, 'subscribers:', notification);

const clientInstance = new wasm.Client(await wasm.Wallet.create(wallet));
this.client = clientInstance;

clientInstance.on_notification((notification: Notification) => {
console.debug('Received notification for', this.subscribers.size, 'subscribers:', notification);
for (const subscriber of this.subscribers.values()) {
subscriber.postMessage(notification);
}
Expand All @@ -27,63 +30,92 @@ export class Server {
return;
}

let target;
if (sender.origin === self.location.origin) {
console.log('Received call from extension', functionName, args);
target = this.client;
} else {
console.log('Received call from content script', functionName, args);
target = this.client.frontend();
}

if (!(functionName in target)) {
console.error('Attempted to call undefined function', functionName);
return;
}
try {
let target;
if (sender.origin === self.location.origin) {
console.log('Received call from extension', functionName, args);
target = this.client;
} else {
console.log('Received call from content script', functionName, args);
target = this.client.frontend();
}

let func = target[functionName as keyof typeof target] as (...args: any) => Promise<any>;
if (typeof target[functionName] !== 'function') {
console.error('Attempted to call undefined function', functionName);
return;
}

console.debug('Calling function', functionName);
const func = target[functionName as keyof typeof target] as (...args: any) => Promise<any>;

const result = await func.apply(target, args);
console.debug('Got result', result);
return result;
console.debug('Calling function', functionName);
const result = await func.apply(target, args);
console.debug('Got result', result);
return result;

} catch (error) {
console.error('Error calling client function:', error);
}
}

async init() {
await (await wasm).default({
await this.loadWasmModule();
this.setupNotificationListener();
this.setupMessageListener();
}

private async loadWasmModule() {
await wasm;
await wasm.default({
module_or_path: (await fetch(wasmModuleUrl)).arrayBuffer(),
});
}

private setupNotificationListener() {
chrome.runtime.onConnect.addListener(port => {
if (port.name !== 'notifications') {
console.warn('Unknown channel type', port.name);
return;
}

this.subscribers.add(port);
port.onDisconnect.addListener(port => this.subscribers.delete(port));
port.onDisconnect.addListener(() => this.removeSubscriber(port));
});
}

private setupMessageListener() {
chrome.runtime.onMessage.addListener((message, sender, respond) => {
if (message.target !== 'wallet')
return false;

if (guard.isCallRequest(message)) {
this.callClientFunction(sender, message.function, ...message.arguments)
.then(respond);
return true;
} else if (guard.isSetWalletRequest(message))
this.setWallet(message.wallet);
else if (guard.isGetWalletRequest(message))
respond(this.wallet);
else
console.warn('Unknown message', message);

return false;
if (message.target !== 'wallet') return false;

switch (true) {
case guard.isCallRequest(message):
this.handleCallRequest(sender, message, respond);
return true;

case guard.isSetWalletRequest(message):
this.setWallet(message.wallet);
return true;

case guard.isGetWalletRequest(message):
respond(this.wallet);
return true;

default:
console.warn('Unknown message', message);
return false;
}
});
}

private async handleCallRequest(sender: chrome.runtime.MessageSender, message: any, respond: Function) {
const result = await this.callClientFunction(sender, message.function, ...message.arguments);
respond(result);
}

private removeSubscriber(port: chrome.runtime.Port) {
this.subscribers.delete(port);
port.onDisconnect.removeListener(() => this.removeSubscriber(port));
}

public static async run() {
new Server().init();
}
Expand Down