MetaMask / MetaMask/metamask-sdk

Global type is modified importing into node.js projects

Open
#491 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
338
Forks
260
PR merge metrics
No merged PRs in 30d

Description

In HyperPlay, which is an electron app, we import metamask sdk (v0.12.0) in the main (node.js) process.
This imports the following from `node_modules\@metamask\sdk\dist\node\es\src\index.d.ts`
```
import { CommunicationLayerPreference, ConnectionStatus, DEFAULT_SERVER_URL, EventType, ServiceStatus, MessageType, PlatformType } from '@metamask/sdk-communication-layer';
import WebView from 'react-native-webview';
import { SDKProvider } from './provider/SDKProvider';
import { MetaMaskSDK, MetaMaskSDKOptions } from './sdk';
import { RPC_URLS_MAP } from './services/MetaMaskSDK/InitializerManager/setupReadOnlyRPCProviders';
import { PROVIDER_UPDATE_TYPE } from './types/ProviderUpdateType';
import type { SDKLoggingOptions } from './types/SDKLoggingOptions';
declare global {
interface Window {
ReactNativeWebView?: WebView;
sdkProvider: SDKProvider;
ethereum?: SDKProvider;
extension: unknown;
MSStream: unknown;
}
}
export { CommunicationLayerPreference, ConnectionStatus, DEFAULT_SERVER_URL, EventType, MessageType, RPC_URLS_MAP, MetaMaskSDK, PROVIDER_UPDATE_TYPE, PlatformType, SDKProvider, };
export type { MetaMaskSDKOptions, SDKLoggingOptions, ServiceStatus };
export default MetaMaskSDK;
//# sourceMappingURL=index.d.ts.map
```

This type modifies the global type which conflicts with the frontend definition for window.ethereum of
```
declare global {
interface Window {
ethereum: {
/*eslint-disable-next-line @typescript-eslint/no-explicit-any */
request: (args: any) => any
/*eslint-disable-next-line @typescript-eslint/no-explicit-any */
send: (...args: any) => any
/*eslint-disable-next-line @typescript-eslint/no-explicit-any */
sendAsync: (...args: any) => any
/*eslint-disable-next-line @typescript-eslint/no-explicit-any */
on: (topic: string, handler: (...args: any) => void) => void
isConnected: () => boolean
}
}
}
```

I don't see the purpose of modifying the global window.etheruem object for nodejs projects since the provider will be obtained by following the steps defined here https://docs.metamask.io/wallet/how-to/connect/set-up-sdk/javascript/nodejs/ and the sdk object will be called directly.

Would it be possible to remove the following from `node_modules\@metamask\sdk\dist\node\es\src\index.d.ts`?
```
declare global {
interface Window {
ReactNativeWebView?: WebView;
sdkProvider: SDKProvider;
ethereum?: SDKProvider;
extension: unknown;
MSStream: unknown;
}
}
```

Contributor guide

Open the contributing guide

Research direction

Inspect node_modules/@metamask/sdk/dist/node/es/src/index.d.ts and the Node.js SDK setup documentation first. Verify how the declared global Window fields affect the Electron main process and frontend ethereum definition; done means the Node.js import no longer causes the reported global type conflict.

Written by the indexing model from the issue text.

Assessment

Tech stack
electron, node.js, react-native, typescript
Domain
backend, developer-experience
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.