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

Extension - Add/Remove Chains (Smoldot v.0.3.x) #428

Merged
merged 17 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 packages/connect-extension-protocol/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface ProviderMessageData {
/** The name of the blockchain network the app is talking to **/
chainName: string;
/** What action the `ExtensionMessageRouter` should take **/
action: 'forward' | 'connect' | 'disconnect';
action: 'forward' | 'connect' | 'disconnect' | 'spec';
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
/** The message the `ExtensionMessageRouter` should forward to the background **/
message?: MessageToManager;
}
Expand All @@ -79,7 +79,7 @@ export interface ProviderMessageData {
*/
export interface MessageToManager {
/** Type of the message. Defines how to interpret the {@link payload} */
type: 'rpc';
type: 'rpc' | 'spec';
/** Payload of the message - a JSON encoded RPC request **/
payload: string;
/** whether an RPC message is a subscription or not **/
Expand Down
15 changes: 13 additions & 2 deletions packages/connect/src/ExtensionProvider/ExtensionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,19 @@ export class ExtensionProvider implements ProviderInterface {

// Once connect is sent - send rpc to extension that will contain the chainSpecs
// for the extension to call addChain on smoldot
this.send('spec', [this.#chainSpecs]).catch(console.error);

const someMsg: ProviderMessageData = {
appName: this.#appName,
chainName: this.#chainName,
action: 'spec',
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
origin: EXTENSION_PROVIDER_ORIGIN,
message: {
type: 'spec',
payload: this.#chainSpecs || ''
}
}

provider.send(someMsg);

provider.listen(({data}: ExtensionMessage) => {
if (data.origin && data.origin === CONTENT_SCRIPT_ORIGIN) {
this.#handleMessage(data);
Expand Down
18 changes: 18 additions & 0 deletions projects/extension/src/content/ExtensionMessageRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ export class ExtensionMessageRouter {
window.removeEventListener('message', this.#handleMessage);
}

#sendSpecs = (message: ProviderMessage): void => {
const {data: { chainName }} = message;
const port = this.#ports[chainName];
if (!port) {
// this is probably someone trying to abuse the extension.
console.warn(`App requested to send message to ${chainName} - no port found`);
return;
}
debug(`SENDING SPECS TO ${chainName} PORT`, message);
port.postMessage(message);
}

#establishNewConnection = (message: ProviderMessage): void => {
const data = message.data;
const port = chrome.runtime.connect({ name: `${data.appName}::${data.chainName}` });
Expand Down Expand Up @@ -112,6 +124,12 @@ export class ExtensionMessageRouter {
return this.#establishNewConnection(message);
}

if (data.action === 'spec') {
console.log('data action: ', data.action);
console.log('message', message.data)
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
return this.#sendSpecs(message);
}

if (data.action === 'disconnect') {
return this.#disconnectPort(message);
}
Expand Down