1
1
Object . defineProperty ( globalThis , "_backpack_injected_provider" , {
2
2
value : true ,
3
- writable : false
3
+ writable : false ,
4
4
} ) ;
5
5
6
6
import {
7
- MOBILE_APP_TRANSPORT_SENDER_EVENTS ,
8
7
FromContentScriptTransportSender ,
8
+ MOBILE_APP_TRANSPORT_SENDER_EVENTS ,
9
9
ToMobileAppTransportSender ,
10
- } from "@coral-xyz/secure-clients" ;
10
+ UserClient } from "@coral-xyz/secure-clients" ;
11
11
12
12
// This is a bit of a hack, it's speicifically at the top of this file
13
13
// to ensure it's loaded before other code
@@ -39,6 +39,7 @@ import {
39
39
ProviderRootXnftInjection ,
40
40
ProviderSolanaInjection ,
41
41
} from "@coral-xyz/provider-core" ;
42
+ import type { TransportSender } from "@coral-xyz/secure-clients/types" ;
42
43
import { initialize } from "@coral-xyz/wallet-standard" ;
43
44
import { v4 as uuidV4 } from "uuid" ;
44
45
@@ -47,8 +48,6 @@ import type {
47
48
EIP6963RequestProviderEvent ,
48
49
WindowEthereum ,
49
50
} from "./types" ;
50
- import { TransportSender } from "@coral-xyz/secure-clients/types" ;
51
- import { UserClient } from "@coral-xyz/secure-clients" ;
52
51
53
52
const logger = getLogger ( "provider-injection" ) ;
54
53
@@ -124,11 +123,11 @@ function initEthereum(secureClientSender: TransportSender) {
124
123
...new Set ( [
125
124
...( window . ethereum
126
125
? // Coinbase wallet uses a providers array on window.ethereum, so
127
- // include those if already registered
128
- Array . isArray ( window . ethereum . providers )
126
+ // include those if already registered
127
+ Array . isArray ( window . ethereum . providers )
129
128
? [ ...window . ethereum . providers , window . ethereum ]
130
129
: // Else just window.ethereum if it is registered
131
- [ window . ethereum ]
130
+ [ window . ethereum ]
132
131
: [ ] ) ,
133
132
backpackEthereum ,
134
133
] ) ,
@@ -158,63 +157,6 @@ function initEthereum(secureClientSender: TransportSender) {
158
157
// If the cached provider changes, we want to change the cached proxy as well
159
158
let cachedCurrentProvider : EIP1193Provider ;
160
159
161
- Object . defineProperty ( window , "ethereum" , {
162
- get ( ) {
163
- if ( ! window . walletRouter )
164
- throw new Error ( "Expected window.walletRouter to be set" ) ;
165
-
166
- // Provider cache exists
167
- if (
168
- cachedWindowEthereumProxy &&
169
- cachedCurrentProvider === window . walletRouter . currentProvider
170
- ) {
171
- return cachedWindowEthereumProxy ;
172
- }
173
-
174
- cachedWindowEthereumProxy = new Proxy (
175
- window . walletRouter . currentProvider ,
176
- {
177
- get ( target , prop , receiver ) {
178
- // Sites using web3-react force metamask usage by searching the
179
- // providers array, so remove it for specific sites
180
- // https://github.com/Uniswap/web3-react/blob/f5a54af645a4a2e125ee2f5ead6dd1ecd5d01dda/packages/metamask/src/index.ts#L56-L59
181
- if (
182
- window . walletRouter &&
183
- ! ( prop in window . walletRouter . currentProvider ) &&
184
- prop in window . walletRouter
185
- ) {
186
- if (
187
- window . location . href . endsWith ( ".app.uniswap.org" ) ||
188
- window . location . href === "app.uniswap.org" ||
189
- (
190
- (
191
- window . location . href === "kwenta.io" ||
192
- window . location . href . endsWith ( ".kwenta.io" )
193
- )
194
- && prop === "providers"
195
- )
196
- ) {
197
- return null ;
198
- }
199
- return window . walletRouter [ prop ] ;
200
- }
201
-
202
- return Reflect . get ( target , prop , receiver ) ;
203
- } ,
204
- }
205
- ) ;
206
-
207
- cachedCurrentProvider = window . walletRouter . currentProvider ;
208
-
209
- return cachedWindowEthereumProxy ;
210
- } ,
211
-
212
- set ( newProvider ) {
213
- window . walletRouter ?. addProvider ( newProvider ) ;
214
- } ,
215
- } ) ;
216
-
217
- // EIP-6963: https://eips.ethereum.org/EIPS/eip-6963
218
160
const info = {
219
161
uuid : uuidV4 ( ) ,
220
162
name : "Backpack" ,
@@ -229,8 +171,65 @@ function initEthereum(secureClientSender: TransportSender) {
229
171
} )
230
172
) ;
231
173
}
232
- window . addEventListener ( "eip6963:requestProvider" , announceProvider ) ;
233
- announceProvider ( ) ;
174
+
175
+ try {
176
+ Object . defineProperty ( window , "ethereum" , {
177
+ get ( ) {
178
+ if ( ! window . walletRouter )
179
+ throw new Error ( "Expected window.walletRouter to be set" ) ;
180
+
181
+ // Provider cache exists
182
+ if (
183
+ cachedWindowEthereumProxy &&
184
+ cachedCurrentProvider === window . walletRouter . currentProvider
185
+ ) {
186
+ return cachedWindowEthereumProxy ;
187
+ }
188
+
189
+ cachedWindowEthereumProxy = new Proxy (
190
+ window . walletRouter . currentProvider ,
191
+ {
192
+ get ( target , prop , receiver ) {
193
+ // Sites using web3-react force metamask usage by searching the
194
+ // providers array, so remove it for specific sites
195
+ // https://github.com/Uniswap/web3-react/blob/f5a54af645a4a2e125ee2f5ead6dd1ecd5d01dda/packages/metamask/src/index.ts#L56-L59
196
+ if (
197
+ window . walletRouter &&
198
+ ! ( prop in window . walletRouter . currentProvider ) &&
199
+ prop in window . walletRouter
200
+ ) {
201
+ if (
202
+ window . location . href . endsWith ( ".app.uniswap.org" ) ||
203
+ window . location . href === "app.uniswap.org" ||
204
+ ( ( window . location . href === "kwenta.io" ||
205
+ window . location . href . endsWith ( ".kwenta.io" ) ) &&
206
+ prop === "providers" )
207
+ ) {
208
+ return null ;
209
+ }
210
+ return window . walletRouter [ prop ] ;
211
+ }
212
+
213
+ return Reflect . get ( target , prop , receiver ) ;
214
+ } ,
215
+ }
216
+ ) ;
217
+
218
+ cachedCurrentProvider = window . walletRouter . currentProvider ;
219
+
220
+ return cachedWindowEthereumProxy ;
221
+ } ,
222
+
223
+ set ( newProvider ) {
224
+ window . walletRouter ?. addProvider ( newProvider ) ;
225
+ } ,
226
+ } ) ;
227
+
228
+ window . addEventListener ( "eip6963:requestProvider" , announceProvider ) ;
229
+ announceProvider ( ) ;
230
+ } catch {
231
+ /* empty */
232
+ }
234
233
}
235
234
236
235
main ( ) ;
0 commit comments