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

bugfix: MM is not injected in FF #1167

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
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
38 changes: 36 additions & 2 deletions lib/web3/useProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { WindowPostMessageStream } from '@metamask/post-message-stream';
import { initializeProvider } from '@metamask/providers';
import React from 'react';
import type { WindowProvider } from 'wagmi';

Expand All @@ -14,7 +16,34 @@ export default function useProvider() {
const [ wallet, setWallet ] = React.useState<WalletType>();

React.useEffect(() => {
if (!('ethereum' in window && window.ethereum) || !feature.isEnabled) {
if (!feature.isEnabled) {
return;
}

if (!('ethereum' in window && window.ethereum)) {
if (feature.wallets.includes('metamask') && window.navigator.userAgent.includes('Firefox')) {
// workaround for MetaMask in Firefox
// Firefox blocks MetaMask injection script because of our CSP for 'script-src'
// so we have to inject it manually while the issue is not fixed
// https://github.com/MetaMask/metamask-extension/issues/3133#issuecomment-1025641185
const metamaskStream = new WindowPostMessageStream({
name: 'metamask-inpage',
target: 'metamask-contentscript',
});

// this will initialize the provider and set it as window.ethereum
initializeProvider({
connectionStream: metamaskStream as never,
shouldShimWeb3: true,
});
} else {
return;
}
}

// have to check again in case provider was not set as window.ethereum in the previous step for MM in FF
// and also it makes typescript happy
if (!('ethereum' in window && window.ethereum)) {
return;
}

Expand All @@ -25,8 +54,13 @@ export default function useProvider() {
for (const wallet of feature.wallets) {
const provider = providers.find((provider) => {
return (
// some wallets (e.g TokenPocket, Liquality, etc) try to look like MetaMask but they are not (not even close)
// so we have to check in addition the presence of the provider._events property
// found this hack in wagmi repo
// https://github.com/wagmi-dev/wagmi/blob/399b9eab8cfd698b49bfaa8456598dad9b597e0e/packages/connectors/src/types.ts#L65
// for now it's the only way to distinguish them
(wallet === 'metamask' && provider.isMetaMask && Boolean(provider._events)) ||
(wallet === 'coinbase' && provider.isCoinbaseWallet) ||
(wallet === 'metamask' && provider.isMetaMask) ||
(wallet === 'token_pocket' && provider.isTokenPocket)
);
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@chakra-ui/theme-tools": "^2.0.18",
"@emotion/react": "^11.10.4",
"@emotion/styled": "^11.10.4",
"@metamask/post-message-stream": "^7.0.0",
"@metamask/providers": "^10.2.1",
"@monaco-editor/react": "^4.4.6",
"@sentry/nextjs": "^7.12.1",
Expand Down
10 changes: 10 additions & 0 deletions playwright-ct.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ const config: PlaywrightTestConfig = defineConfig({
sourcemap: false,
minify: false,
},
resolve: {
alias: {
// There is an issue with building these package using vite that I cannot resolve
// The solution described here - https://github.com/vitejs/vite/issues/9703#issuecomment-1216662109
// doesn't seam to work well with our setup
// so for now we just mock these modules in tests
'@metamask/post-message-stream': './playwright/mocks/modules/@metamask/post-message-stream.js',
'@metamask/providers': './playwright/mocks/modules/@metamask/providers.js',
},
},
define: {
'process.env': 'process.env', // Port over window.process envs
},
Expand Down
9 changes: 9 additions & 0 deletions playwright/mocks/modules/@metamask/post-message-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class WindowPostMessageStream {
constructor() {
return null;
}
}

export {
WindowPostMessageStream,
};
5 changes: 5 additions & 0 deletions playwright/mocks/modules/@metamask/providers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function initializeProvider() {}

export {
initializeProvider,
};
2 changes: 1 addition & 1 deletion ui/address/AddressDetails.pw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ test('token', async({ mount, page }) => {

await page.evaluate(() => {
window.ethereum = {
providers: [ { isMetaMask: true } ],
providers: [ { isMetaMask: true, _events: {} } ],
}as WindowProvider;
});

Expand Down
4 changes: 3 additions & 1 deletion ui/snippets/footer/Footer.pw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ base.describe('with custom links, 4 cols', () => {
await page.evaluate(() => {
window.ethereum = {
isMetaMask: true,
_events: {},
} as WindowProvider;
});

Expand Down Expand Up @@ -90,6 +91,7 @@ base.describe('without custom links', () => {
await page.evaluate(() => {
window.ethereum = {
isMetaMask: true,
_events: {},
} as WindowProvider;
});
await page.route(BACKEND_VERSION_API_URL, (route) => {
Expand All @@ -112,7 +114,7 @@ base.describe('without custom links', () => {
base('with indexing alert +@dark-mode +@mobile', async({ mount, page }) => {
await page.evaluate(() => {
window.ethereum = {
providers: [ { isMetaMask: true } ],
providers: [ { isMetaMask: true, _events: {} } ],
} as WindowProvider;
});

Expand Down
123 changes: 114 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2397,6 +2397,38 @@
minimatch "^3.1.2"
strip-json-comments "^3.1.1"

"@ethereumjs/common@^3.2.0":
version "3.2.0"
resolved "https://registry.yarnpkg.com/@ethereumjs/common/-/common-3.2.0.tgz#b71df25845caf5456449163012074a55f048e0a0"
integrity sha512-pksvzI0VyLgmuEF2FA/JR/4/y6hcPq8OUail3/AvycBaW1d5VSauOZzqGvJ3RTmR4MU35lWE8KseKOsEhrFRBA==
dependencies:
"@ethereumjs/util" "^8.1.0"
crc-32 "^1.2.0"

"@ethereumjs/rlp@^4.0.1":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@ethereumjs/rlp/-/rlp-4.0.1.tgz#626fabfd9081baab3d0a3074b0c7ecaf674aaa41"
integrity sha512-tqsQiBQDQdmPWE1xkkBq4rlSW5QZpLOUJ5RJh2/9fug+q9tnUhuZoVLk7s0scUIKTOzEtR72DFBXI4WiZcMpvw==

"@ethereumjs/tx@^4.1.2":
version "4.2.0"
resolved "https://registry.yarnpkg.com/@ethereumjs/tx/-/tx-4.2.0.tgz#5988ae15daf5a3b3c815493bc6b495e76009e853"
integrity sha512-1nc6VO4jtFd172BbSnTnDQVr9IYBFl1y4xPzZdtkrkKIncBCkdbgfdRV+MiTkJYAtTxvV12GRZLqBFT1PNK6Yw==
dependencies:
"@ethereumjs/common" "^3.2.0"
"@ethereumjs/rlp" "^4.0.1"
"@ethereumjs/util" "^8.1.0"
ethereum-cryptography "^2.0.0"

"@ethereumjs/util@^8.1.0":
version "8.1.0"
resolved "https://registry.yarnpkg.com/@ethereumjs/util/-/util-8.1.0.tgz#299df97fb6b034e0577ce9f94c7d9d1004409ed4"
integrity sha512-zQ0IqbdX8FZ9aw11vP+dZkKDkS+kgIvQPHnSAXzP9pLu+Rfu3D3XEeLbicvoXJTYnhZiPmsZUxgdzXwNKxRPbA==
dependencies:
"@ethereumjs/rlp" "^4.0.1"
ethereum-cryptography "^2.0.0"
micro-ftch "^0.3.1"

"@floating-ui/core@^1.0.5":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@floating-ui/core/-/core-1.1.0.tgz#0a1dee4bbce87ff71602625d33f711cafd8afc08"
Expand Down Expand Up @@ -2750,6 +2782,14 @@
once "^1.4.0"
readable-stream "^2.3.3"

"@metamask/post-message-stream@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@metamask/post-message-stream/-/post-message-stream-7.0.0.tgz#b7698909ec54923b6a6990b8d46cc15c2810d840"
integrity sha512-+dTUNtuNY9o4ooPiXEOqSkqQWQ9APkVx3dAkBvAsJLetAGXvmoBeIMN1DL2QU+9BTbQ9JyJ6NycQnhvwRRc1HQ==
dependencies:
"@metamask/utils" "^5.0.0"
readable-stream "3.6.2"

"@metamask/providers@^10.2.1":
version "10.2.1"
resolved "https://registry.yarnpkg.com/@metamask/providers/-/providers-10.2.1.tgz#61304940adeccc7421dcda30ffd1d834273cc77b"
Expand Down Expand Up @@ -2783,6 +2823,17 @@
semver "^7.3.8"
superstruct "^1.0.3"

"@metamask/utils@^5.0.0":
version "5.0.2"
resolved "https://registry.yarnpkg.com/@metamask/utils/-/utils-5.0.2.tgz#140ba5061d90d9dac0280c19cab101bc18c8857c"
integrity sha512-yfmE79bRQtnMzarnKfX7AEJBwFTxvTyw3nBQlu/5rmGXrjAeAMltoGxO62TFurxrQAFMNa/fEjIHNvungZp0+g==
dependencies:
"@ethereumjs/tx" "^4.1.2"
"@types/debug" "^4.1.7"
debug "^4.3.4"
semver "^7.3.8"
superstruct "^1.0.3"

"@monaco-editor/loader@^1.3.2":
version "1.3.2"
resolved "https://registry.yarnpkg.com/@monaco-editor/loader/-/loader-1.3.2.tgz#04effbb87052d19cd7d3c9d81c0635490f9bb6d8"
Expand Down Expand Up @@ -2989,6 +3040,13 @@
dependencies:
"@noble/hashes" "1.3.0"

"@noble/[email protected]", "@noble/curves@~1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.1.0.tgz#f13fc667c89184bc04cccb9b11e8e7bae27d8c3d"
integrity sha512-091oBExgENk/kGj3AZmtBDMpxQPDtxQABR2B9lb1JbVTs6ytdzZNwvhxQ4MWasRNEzlbEH8jCWFCwhF/Obj5AA==
dependencies:
"@noble/hashes" "1.3.1"

"@noble/ed25519@^1.7.0":
version "1.7.1"
resolved "https://registry.yarnpkg.com/@noble/ed25519/-/ed25519-1.7.1.tgz#6899660f6fbb97798a6fbd227227c4589a454724"
Expand All @@ -2999,11 +3057,21 @@
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.0.tgz#085fd70f6d7d9d109671090ccae1d3bec62554a1"
integrity sha512-ilHEACi9DwqJB0pw7kv+Apvh50jiiSyR/cQ3y4W7lOR5mhvn/50FLUfsnfJz0BDZtl/RR16kXvptiv6q1msYZg==

"@noble/[email protected]":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.1.tgz#8831ef002114670c603c458ab8b11328406953a9"
integrity sha512-EbqwksQwz9xDRGfDST86whPBgM65E0OH/pCgqW0GBVzO22bNE+NuIbeTb714+IfSjU3aRk47EUvXIb5bTsenKA==

"@noble/hashes@^1.1.2":
version "1.1.5"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.1.5.tgz#1a0377f3b9020efe2fae03290bd2a12140c95c11"
integrity sha512-LTMZiiLc+V4v1Yi16TD6aX2gmtKszNye0pQgbaLqkvhIqP7nVsSaJsWloGQjJfJ8offaoP5GtX3yY5swbcJxxQ==

"@noble/hashes@~1.3.1":
version "1.3.2"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.2.tgz#6f26dbc8fbc7205873ce3cee2f690eba0d421b39"
integrity sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==

"@noble/secp256k1@^1.6.3":
version "1.7.1"
resolved "https://registry.yarnpkg.com/@noble/secp256k1/-/secp256k1-1.7.1.tgz#b251c70f824ce3ca7f8dc3df08d58f005cc0507c"
Expand Down Expand Up @@ -3292,6 +3360,15 @@
"@noble/hashes" "~1.3.0"
"@scure/base" "~1.1.0"

"@scure/[email protected]":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@scure/bip32/-/bip32-1.3.1.tgz#7248aea723667f98160f593d621c47e208ccbb10"
integrity sha512-osvveYtyzdEVbt3OfwwXFr4P2iVBL5u1Q3q4ONBfDY/UpOuXmOlbgwc1xECEboY8wIays8Yt6onaWMUdUbfl0A==
dependencies:
"@noble/curves" "~1.1.0"
"@noble/hashes" "~1.3.1"
"@scure/base" "~1.1.0"

"@scure/[email protected]":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@scure/bip39/-/bip39-1.2.0.tgz#a207e2ef96de354de7d0002292ba1503538fc77b"
Expand All @@ -3300,6 +3377,14 @@
"@noble/hashes" "~1.3.0"
"@scure/base" "~1.1.0"

"@scure/[email protected]":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@scure/bip39/-/bip39-1.2.1.tgz#5cee8978656b272a917b7871c981e0541ad6ac2a"
integrity sha512-Z3/Fsz1yr904dduJD0NpiyRHhRYHdcnyh73FZWiV+/qhWi83wNJ3NWolYqCEN+ZWsUz2TWwajJggcRE9r1zUYg==
dependencies:
"@noble/hashes" "~1.3.0"
"@scure/base" "~1.1.0"

"@sentry/[email protected]":
version "7.15.0"
resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-7.15.0.tgz#1723dc8efcea9239d26072126755f61f6fb9448d"
Expand Down Expand Up @@ -6398,6 +6483,11 @@ cosmiconfig@^7.0.0, cosmiconfig@^7.0.1:
path-type "^4.0.0"
yaml "^1.10.0"

crc-32@^1.2.0:
version "1.2.2"
resolved "https://registry.yarnpkg.com/crc-32/-/crc-32-1.2.2.tgz#3cad35a934b8bf71f25ca524b6da51fb7eace2ff"
integrity sha512-ROmzCKrTnOwybPcJApAA6WBWij23HVfGVNKqqrZpuyZOHqK2CwHSvpGuyt/UNNvaIjEd8X5IFGp4Mh+Ie1IHJQ==

create-require@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
Expand Down Expand Up @@ -7672,6 +7762,16 @@ eth-rpc-errors@^4.0.2:
dependencies:
fast-safe-stringify "^2.0.6"

ethereum-cryptography@^2.0.0:
version "2.1.2"
resolved "https://registry.yarnpkg.com/ethereum-cryptography/-/ethereum-cryptography-2.1.2.tgz#18fa7108622e56481157a5cb7c01c0c6a672eb67"
integrity sha512-Z5Ba0T0ImZ8fqXrJbpHcbpAvIswRte2wGNR/KePnu8GbbvgJ47lMxT/ZZPG6i9Jaht4azPDop4HaM00J0J59ug==
dependencies:
"@noble/curves" "1.1.0"
"@noble/hashes" "1.3.1"
"@scure/bip32" "1.3.1"
"@scure/bip39" "1.2.1"

event-target-shim@^5.0.0:
version "5.0.1"
resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789"
Expand Down Expand Up @@ -9776,6 +9876,11 @@ mersenne-twister@^1.1.0:
resolved "https://registry.yarnpkg.com/mersenne-twister/-/mersenne-twister-1.1.0.tgz#f916618ee43d7179efcf641bec4531eb9670978a"
integrity sha512-mUYWsMKNrm4lfygPkL3OfGzOPTR2DBlTkBNHM//F6hGp8cLThY897crAlk3/Jo17LEOOjQUrNAx6DvgO77QJkA==

micro-ftch@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/micro-ftch/-/micro-ftch-0.3.1.tgz#6cb83388de4c1f279a034fb0cf96dfc050853c5f"
integrity sha512-/0LLxhzP0tfiR5hcQebtudP56gUurs2CLkGarnCiB/OqEyUFQ6U3paQi/tgLv0hBJYt2rnr9MNpxz4fiiugstg==

micromatch@^4.0.2, micromatch@^4.0.4, micromatch@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-4.0.5.tgz#bc8999a7cbbf77cdc89f132f6e467051b49090c6"
Expand Down Expand Up @@ -11139,6 +11244,15 @@ [email protected]:
dependencies:
loose-envify "^1.1.0"

[email protected], readable-stream@^3.4.0:
version "3.6.2"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.6.2.tgz#56a9b36ea965c00c5a93ef31eb111a0f11056967"
integrity sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==
dependencies:
inherits "^2.0.3"
string_decoder "^1.1.1"
util-deprecate "^1.0.1"

readable-stream@^2.0.6, readable-stream@^2.3.3:
version "2.3.7"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.7.tgz#1eca1cf711aef814c04f62252a36a62f6cb23b57"
Expand All @@ -11161,15 +11275,6 @@ readable-stream@^3.1.1, readable-stream@^3.5.0, readable-stream@^3.6.0:
string_decoder "^1.1.1"
util-deprecate "^1.0.1"

readable-stream@^3.4.0:
version "3.6.2"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.6.2.tgz#56a9b36ea965c00c5a93ef31eb111a0f11056967"
integrity sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==
dependencies:
inherits "^2.0.3"
string_decoder "^1.1.1"
util-deprecate "^1.0.1"

readable-stream@^4.0.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-4.2.0.tgz#a7ef523d3b39e4962b0db1a1af22777b10eeca46"
Expand Down