Skip to content

Commit

Permalink
Catch failures to redefine window.ethereum
Browse files Browse the repository at this point in the history
  • Loading branch information
steveluscher committed May 3, 2024
1 parent 5a538a4 commit da86870
Showing 1 changed file with 66 additions and 67 deletions.
133 changes: 66 additions & 67 deletions packages/provider-injection/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Object.defineProperty(globalThis, "_backpack_injected_provider", {
value: true,
writable: false
writable: false,
});

import {
MOBILE_APP_TRANSPORT_SENDER_EVENTS,
FromContentScriptTransportSender,
MOBILE_APP_TRANSPORT_SENDER_EVENTS,
ToMobileAppTransportSender,
} from "@coral-xyz/secure-clients";
UserClient } from "@coral-xyz/secure-clients";

// This is a bit of a hack, it's speicifically at the top of this file
// to ensure it's loaded before other code
Expand Down Expand Up @@ -39,6 +39,7 @@ import {
ProviderRootXnftInjection,
ProviderSolanaInjection,
} from "@coral-xyz/provider-core";
import type { TransportSender } from "@coral-xyz/secure-clients/types";
import { initialize } from "@coral-xyz/wallet-standard";
import { v4 as uuidV4 } from "uuid";

Expand All @@ -47,8 +48,6 @@ import type {
EIP6963RequestProviderEvent,
WindowEthereum,
} from "./types";
import { TransportSender } from "@coral-xyz/secure-clients/types";
import { UserClient } from "@coral-xyz/secure-clients";

const logger = getLogger("provider-injection");

Expand Down Expand Up @@ -124,11 +123,11 @@ function initEthereum(secureClientSender: TransportSender) {
...new Set([
...(window.ethereum
? // Coinbase wallet uses a providers array on window.ethereum, so
// include those if already registered
Array.isArray(window.ethereum.providers)
// include those if already registered
Array.isArray(window.ethereum.providers)
? [...window.ethereum.providers, window.ethereum]
: // Else just window.ethereum if it is registered
[window.ethereum]
[window.ethereum]
: []),
backpackEthereum,
]),
Expand Down Expand Up @@ -158,63 +157,6 @@ function initEthereum(secureClientSender: TransportSender) {
// If the cached provider changes, we want to change the cached proxy as well
let cachedCurrentProvider: EIP1193Provider;

Object.defineProperty(window, "ethereum", {
get() {
if (!window.walletRouter)
throw new Error("Expected window.walletRouter to be set");

// Provider cache exists
if (
cachedWindowEthereumProxy &&
cachedCurrentProvider === window.walletRouter.currentProvider
) {
return cachedWindowEthereumProxy;
}

cachedWindowEthereumProxy = new Proxy(
window.walletRouter.currentProvider,
{
get(target, prop, receiver) {
// Sites using web3-react force metamask usage by searching the
// providers array, so remove it for specific sites
// https://github.com/Uniswap/web3-react/blob/f5a54af645a4a2e125ee2f5ead6dd1ecd5d01dda/packages/metamask/src/index.ts#L56-L59
if (
window.walletRouter &&
!(prop in window.walletRouter.currentProvider) &&
prop in window.walletRouter
) {
if (
window.location.href.endsWith(".app.uniswap.org") ||
window.location.href === "app.uniswap.org" ||
(
(
window.location.href === "kwenta.io" ||
window.location.href.endsWith(".kwenta.io")
)
&& prop === "providers"
)
) {
return null;
}
return window.walletRouter[prop];
}

return Reflect.get(target, prop, receiver);
},
}
);

cachedCurrentProvider = window.walletRouter.currentProvider;

return cachedWindowEthereumProxy;
},

set(newProvider) {
window.walletRouter?.addProvider(newProvider);
},
});

// EIP-6963: https://eips.ethereum.org/EIPS/eip-6963
const info = {
uuid: uuidV4(),
name: "Backpack",
Expand All @@ -229,8 +171,65 @@ function initEthereum(secureClientSender: TransportSender) {
})
);
}
window.addEventListener("eip6963:requestProvider", announceProvider);
announceProvider();

try {
Object.defineProperty(window, "ethereum", {
get() {
if (!window.walletRouter)
throw new Error("Expected window.walletRouter to be set");

// Provider cache exists
if (
cachedWindowEthereumProxy &&
cachedCurrentProvider === window.walletRouter.currentProvider
) {
return cachedWindowEthereumProxy;
}

cachedWindowEthereumProxy = new Proxy(
window.walletRouter.currentProvider,
{
get(target, prop, receiver) {
// Sites using web3-react force metamask usage by searching the
// providers array, so remove it for specific sites
// https://github.com/Uniswap/web3-react/blob/f5a54af645a4a2e125ee2f5ead6dd1ecd5d01dda/packages/metamask/src/index.ts#L56-L59
if (
window.walletRouter &&
!(prop in window.walletRouter.currentProvider) &&
prop in window.walletRouter
) {
if (
window.location.href.endsWith(".app.uniswap.org") ||
window.location.href === "app.uniswap.org" ||
((window.location.href === "kwenta.io" ||
window.location.href.endsWith(".kwenta.io")) &&
prop === "providers")
) {
return null;
}
return window.walletRouter[prop];
}

return Reflect.get(target, prop, receiver);
},
}
);

cachedCurrentProvider = window.walletRouter.currentProvider;

return cachedWindowEthereumProxy;
},

set(newProvider) {
window.walletRouter?.addProvider(newProvider);
},
});

window.addEventListener("eip6963:requestProvider", announceProvider);
announceProvider();
} catch {
/* empty */
}
}

main();

0 comments on commit da86870

Please sign in to comment.