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
2 changes: 1 addition & 1 deletion 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' | 'spec';
action: 'forward' | 'connect' | 'disconnect';
/** The message the `ExtensionMessageRouter` should forward to the background **/
message?: MessageToManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export class ExtensionProvider implements ProviderInterface {
const someMsg: ProviderMessageData = {
appName: this.#appName,
chainName: this.#chainName,
action: 'spec',
action: 'forward',
origin: EXTENSION_PROVIDER_ORIGIN,
message: {
type: 'spec',
Expand Down
6 changes: 3 additions & 3 deletions projects/extension/src/background/AppMediator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export class AppMediator extends (EventEmitter as { new(): StateEmitter }) {
}

#handleRpcRequest = (msg: MessageToManager): void => {
if (msg.type !== 'rpc') {
if (msg.type !== 'rpc' && msg.type !== 'spec') {
console.warn(`Unrecognised message type ${msg.type} received from content script`);
return;
}
Expand All @@ -261,14 +261,14 @@ export class AppMediator extends (EventEmitter as { new(): StateEmitter }) {
}
const chainName = this.#chainName as string;

if (parsed.method === 'spec' && chainName) {
if (msg.type === 'spec' && chainName) {
// When params[0] (chainSpecs in the current case is empty) then this is a
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
// known relay chain and specs should be retrieved from inside the extension
let chainSpec: string;
if (Object.keys(relayChains).includes(chainName)) {
chainSpec = relayChains[chainName];
} else {
chainSpec = parsed.params[0] as string;
chainSpec = msg.payload;
}
this.#addChain(chainName, chainSpec).catch(console.error);
} else {
Expand Down
20 changes: 1 addition & 19 deletions projects/extension/src/content/ExtensionMessageRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,6 @@ 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 @@ -124,12 +112,6 @@ export class ExtensionMessageRouter {
return this.#establishNewConnection(message);
}

if (data.action === 'spec') {
console.log('data action: ', data.action);
console.log('message', message.data)
return this.#sendSpecs(message);
}

if (data.action === 'disconnect') {
return this.#disconnectPort(message);
}
Expand All @@ -142,7 +124,7 @@ export class ExtensionMessageRouter {
return;
}

if (innerMessage.type === 'rpc') {
if (innerMessage.type === 'rpc' || innerMessage.type === 'spec') {
return this.#forwardRpcMessage(message);
}

Expand Down