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

[BRO-39] Migrate away from injecting API (Draft example of chrome API) #477

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions examples/voting/src/Wallet.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* eslint-disable import/no-unresolved */
/* eslint-disable no-plusplus */
import React from 'react';
import { detectConcordiumProvider } from '@concordium/browser-wallet-api-helpers';
import { concordiumWalletApiProxy } from '@concordium/browser-wallet-api-helpers';
import { Alert, Button } from 'react-bootstrap';
import {
AccountTransactionType,
Expand All @@ -19,7 +19,7 @@ import moment from 'moment';
import { RAW_SCHEMA_BASE64, TESTNET_GENESIS_BLOCK_HASH } from './config';

export async function init(setConnectedAccount) {
const client = await detectConcordiumProvider();
const client = concordiumWalletApiProxy;
// Listen for relevant events from the wallet.
client.on('accountChanged', (account) => {
console.debug('browserwallet event: accountChange', { account });
Expand Down
23 changes: 23 additions & 0 deletions packages/browser-wallet-api-helpers/src/detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,26 @@ export async function detectConcordiumProvider(timeout = 5000): Promise<WalletAp
}
});
}

function sendMsgToExtension(target: string, payload: object) {
const editorExtensionId = 'meacpflndgfiiioniiafpcnpehchnaab';
return new Promise((resolve) => {
chrome.runtime.sendMessage(editorExtensionId, { target, payload }, (response) => {
resolve(response);
});
});
}

// intercept all interactions with object
// in this way we can remotely interact with class instance inside BrowserWallet
// by forwarding 'property' name and payload
const proxyHandler = {
get(target, prop) {
return function (...args) {
return sendMsgToExtension(prop, args);
};
},
};

// {} as WalletApi - also enables TS annotations
export const concordiumWalletApiProxy = new Proxy({} as WalletApi, proxyHandler);
2 changes: 1 addition & 1 deletion packages/browser-wallet-api-helpers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './wallet-api-types';
export * from './util';
export { detectConcordiumProvider } from './detector';
export { detectConcordiumProvider, concordiumWalletApiProxy } from './detector';
27 changes: 27 additions & 0 deletions packages/browser-wallet/src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,3 +638,30 @@ forwardToPopup(
withPromptEnd,
(msg) => createMessageTypeFilter(MessageType.Web3IdProof)(msg) && isAgeProof(msg.payload)
);

// import { walletApi } from '@concordium/browser-wallet-api';
// We cannot do a simple import in this case
// It creates circular dependencies that cause errors during build
// walletApi is build to work in page context, not inside BW

// Alternatively we can create new implementation of class WalletApi
// For example implementation of connect()
// Inside can be used bgMessageHandler.sendInternalMessage
// But BW page with connect prompt, will try to receive 'window.location' of the page from which request was made
// So now value 'location' should be provided in payload, and corresponding changes made in React component
// All this leads to more changes across BW, not just only of creation of new WalletApi class
class WalletApi {
public async connect(): Promise<string | undefined> {
return Promise.resolve(bgMessageHandler.sendInternalMessage(InternalMessageType.Connect));
}
}

const walletApi = new WalletApi();

chrome.runtime.onMessageExternal.addListener(function (request, sender, sendResponse) {
const { payload, target } = request;
walletApi[target](...payload).then((response) => {
sendResponse(response);
});
return true;
});
Loading