Skip to content

Commit

Permalink
fix: dapp connection (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanmino authored and greg-schrammel committed Jul 13, 2023
1 parent aa765b0 commit b5cb257
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
46 changes: 26 additions & 20 deletions src/core/providers/RainbowProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EventEmitter } from 'eventemitter3';
import { Messenger } from '../messengers';
import { providerRequestTransport } from '../transports';
import { RPCMethod } from '../types/rpcMethods';
import { getDappHost, isValidUrl } from '../utils/connectedApps';
import { toHex } from '../utils/hex';

export type ChainIdHex = `0x${string}`;
Expand Down Expand Up @@ -55,26 +56,31 @@ export class RainbowProvider extends EventEmitter {

constructor({ messenger }: { messenger?: Messenger } = {}) {
super();
const host = window.location.host;
messenger?.reply(`accountsChanged:${host}`, async (address) => {
this.emit('accountsChanged', [address]);
});
messenger?.reply(`chainChanged:${host}`, async (chainId: number) => {
this.emit('chainChanged', toHex(String(chainId)));
});
messenger?.reply(`disconnect:${host}`, async () => {
this.emit('disconnect');
this.emit('accountsChanged', []);
});
messenger?.reply(`connect:${host}`, async (connectionInfo) => {
this.emit('connect', connectionInfo);
});
messenger?.reply(
'rainbow_setDefaultProvider',
async ({ rainbowAsDefault }: { rainbowAsDefault: boolean }) => {
this.rainbowIsDefaultProvider = rainbowAsDefault;
},
);

// RainbowProvider is also used in popup via RainbowConnector
// here we don't need to listen to anything so we don't need these listeners
if (isValidUrl(window.location.href)) {
const host = getDappHost(window.location.href);
messenger?.reply(`accountsChanged:${host}`, async (address) => {
this.emit('accountsChanged', [address]);
});
messenger?.reply(`chainChanged:${host}`, async (chainId: number) => {
this.emit('chainChanged', toHex(String(chainId)));
});
messenger?.reply(`disconnect:${host}`, async () => {
this.emit('disconnect');
this.emit('accountsChanged', []);
});
messenger?.reply(`connect:${host}`, async (connectionInfo) => {
this.emit('connect', connectionInfo);
});
messenger?.reply(
'rainbow_setDefaultProvider',
async ({ rainbowAsDefault }: { rainbowAsDefault: boolean }) => {
this.rainbowIsDefaultProvider = rainbowAsDefault;
},
);
}
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/core/utils/connectedApps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
export const isValidUrl = (url: string) => {
try {
new URL(url);
return true;
} catch (err) {
return false;
}
};

export const getDappHost = (url: string) => {
const host = new URL(url).host;
if (host.indexOf('www.') === 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/entries/inpage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Ethereum } from '@wagmi/core';
import { initializeMessenger } from '~/core/messengers';
import { RainbowProvider } from '~/core/providers';
import { ChainId } from '~/core/types/chains';
import { getDappHost } from '~/core/utils/connectedApps';

import { injectNotificationIframe } from '../iframe';

Expand Down Expand Up @@ -142,7 +143,7 @@ backgroundMessenger.reply(
extensionUrl: string;
host: string;
}) => {
if (window.location.hostname === host) {
if (getDappHost(window.location.href) === host) {
injectNotificationIframe({ chainId, status, extensionUrl });
}
},
Expand Down

0 comments on commit b5cb257

Please sign in to comment.