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

fix: delete isMetaMask property and share same EIP-1193 instance with EIP-6963 #1614

Closed
wants to merge 3 commits into from
Closed
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
39 changes: 21 additions & 18 deletions src/entries/inpage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ declare global {
lodash: unknown;
rainbow: RainbowProvider;
providers: RainbowProvider[];
rnbwWalletRouter: {
walletRouter: {
rainbowProvider: RainbowProvider;
lastInjectedProvider?: RainbowProvider;
currentProvider: RainbowProvider;
Expand Down Expand Up @@ -62,21 +62,21 @@ const rainbowProvider = new RainbowProvider({
},
});

// Delete the `isMetaMask` property to avoid infinite loops
// that'll cause memory leak with Coinbase Wallet and other wallets.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete rainbowProvider.isMetaMask;

if (shouldInjectProvider()) {
// eslint-disable-next-line prefer-object-spread
const providerCopy = Object.create(
Object.getPrototypeOf(rainbowProvider),
Object.getOwnPropertyDescriptors(rainbowProvider),
);
providerCopy.isMetaMask = false;
announceProvider({
info: {
icon: RAINBOW_ICON_RAW_SVG,
name: 'Rainbow',
rdns: 'me.rainbow',
uuid: uuid4(),
},
provider: providerCopy as RainbowProvider as EIP1193Provider,
provider: rainbowProvider as EIP1193Provider,
});

backgroundMessenger.reply(
Expand Down Expand Up @@ -112,14 +112,14 @@ if (shouldInjectProvider()) {
},
ethereum: {
get() {
return window.rnbwWalletRouter.currentProvider;
return window.walletRouter.currentProvider;
},
set(newProvider) {
window.rnbwWalletRouter?.addProvider(newProvider);
window.walletRouter.addProvider(newProvider);
},
configurable: false,
},
rnbwWalletRouter: {
walletRouter: {
value: {
rainbowProvider,
lastInjectedProvider: window.ethereum,
Expand All @@ -130,19 +130,19 @@ if (shouldInjectProvider()) {
],
setDefaultProvider(rainbowAsDefault: boolean) {
if (rainbowAsDefault) {
window.rnbwWalletRouter.currentProvider = window.rainbow;
window.walletRouter.currentProvider = window.rainbow;
} else {
const nonDefaultProvider =
window.rnbwWalletRouter?.lastInjectedProvider ?? window.ethereum;
window.rnbwWalletRouter.currentProvider = nonDefaultProvider;
window.walletRouter.lastInjectedProvider ?? window.ethereum;
window.walletRouter.currentProvider = nonDefaultProvider;
}
},
addProvider(provider: RainbowProvider) {
if (!window.rnbwWalletRouter?.providers?.includes(provider)) {
window.rnbwWalletRouter?.providers?.push(provider);
if (!window.walletRouter.providers.includes(provider)) {
window.walletRouter.providers.push(provider);
}
if (rainbowProvider !== provider) {
window.rnbwWalletRouter.lastInjectedProvider = provider;
window.walletRouter.lastInjectedProvider = provider;
}
},
},
Expand All @@ -151,12 +151,15 @@ if (shouldInjectProvider()) {
},
});

// defining `providers` on rainbowProvider, since it's undefined on the object itself
window.rainbow.providers = window.walletRouter.providers;

window.dispatchEvent(new Event('ethereum#initialized'));

backgroundMessenger.reply(
'rainbow_setDefaultProvider',
async ({ rainbowAsDefault }: { rainbowAsDefault: boolean }) => {
window.rnbwWalletRouter?.setDefaultProvider(rainbowAsDefault);
window.walletRouter.setDefaultProvider(rainbowAsDefault);
},
);
}
Expand Down
Loading