UNPKG

@metamask/providers

Version:

A JavaScript Ethereum provider that connects to the wallet over a stream

1 lines 6.45 kB
{"version":3,"file":"initializeInpageProvider.cjs","sourceRoot":"","sources":["../src/initializeInpageProvider.ts"],"names":[],"mappings":";;AA8CA,gDA2CC;AAQD,8CAKC;AAUD,8DAoBC;AAjID,2CAA2C;AAC3C,2CAAwE;AACxE,8GAAoF;AAEpF,yEAAkE;AAClE,6CAAsC;AAyBtC;;;;;;;;;;;;GAYG;AACH,SAAgB,kBAAkB,CAAC,EACjC,gBAAgB,EAChB,MAAM,GAAG,OAAO,EAChB,iBAAiB,GAAG,GAAG,EACvB,YAAY,EACZ,kBAAkB,GAAG,IAAI,EACzB,iBAAiB,GAAG,IAAI,EACxB,cAAc,GAAG,KAAK,GACI;IAC1B,MAAM,QAAQ,GAAG,IAAI,+CAAsB,CAAC,gBAAgB,EAAE;QAC5D,MAAM;QACN,iBAAiB;QACjB,kBAAkB;KACnB,CAAC,CAAC;IAEH,MAAM,eAAe,GAAG,IAAI,KAAK,CAAC,QAAQ,EAAE;QAC1C,0DAA0D;QAC1D,cAAc,EAAE,GAAG,EAAE,CAAC,IAAI;QAC1B,uEAAuE;QACvE,uCAAuC;QACvC,GAAG,CAAC,MAAM,EAAE,QAA0D;YACpE,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC1B,CAAC;KACF,CAAC,CAAC;IAEH,IAAI,YAAY,EAAE,CAAC;QACjB,IAAA,0BAAuB,EAAC;YACtB,IAAI,EAAE,YAAY;YAClB,QAAQ,EAAE,eAAe;SAC1B,CAAC,CAAC;QACH,mCAAmC;QACnC,KAAK,yBAAyB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;IACzD,CAAC;IAED,IAAI,iBAAiB,EAAE,CAAC;QACtB,iBAAiB,CAAC,eAAe,CAAC,CAAC;IACrC,CAAC;IAED,IAAI,cAAc,EAAE,CAAC;QACnB,IAAA,mBAAQ,EAAC,eAAe,EAAE,MAAM,CAAC,CAAC;IACpC,CAAC;IAED,OAAO,eAAe,CAAC;AACzB,CAAC;AAED;;;;;GAKG;AACH,SAAgB,iBAAiB,CAC/B,gBAAwC;IAEvC,MAA8B,CAAC,QAAQ,GAAG,gBAAgB,CAAC;IAC5D,MAAM,CAAC,aAAa,CAAC,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC,CAAC;AAC1D,CAAC;AAED;;;;;;;GAOG;AACI,KAAK,UAAU,yBAAyB,CAC7C,QAAgC,EAChC,YAA+B;IAE/B,MAAM,SAAS,GAAG,IAAA,8CAAY,EAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IAClD,IAAI,SAAS,KAAK,OAAO,EAAE,CAAC;QAC1B,OAAO;IACT,CAAC;IAED,MAAM,aAAa,GAAG,MAAM,QAAQ,CAAC,OAAO,CAA2B;QACrE,MAAM,EAAE,2BAA2B;KACpC,CAAC,CAAC;IACH,MAAM,WAAW,GAAG,aAAa,EAAE,WAAW,CAAC;IAE/C,MAAM,UAAU,GAAG;QACjB,GAAG,YAAY;QACf,WAAW;KACZ,CAAC;IAEF,IAAA,wBAAc,EAAC,UAAU,CAAC,CAAC;AAC7B,CAAC","sourcesContent":["import { type Duplex } from 'readable-stream';\n\nimport type { CAIP294WalletData } from './CAIP294';\nimport { announceWallet } from './CAIP294';\nimport { announceProvider as announceEip6963Provider } from './EIP6963';\nimport { getBuildType } from './extension-provider/createExternalExtensionProvider';\nimport type { MetaMaskInpageProviderOptions } from './MetaMaskInpageProvider';\nimport { MetaMaskInpageProvider } from './MetaMaskInpageProvider';\nimport { shimWeb3 } from './shimWeb3';\nimport type { BaseProviderInfo } from './types';\n\ntype InitializeProviderOptions = {\n /**\n * The stream used to connect to the wallet.\n */\n connectionStream: Duplex;\n\n /**\n * The EIP-6963 provider info / CAIP-294 wallet data that should be announced if set.\n */\n providerInfo?: BaseProviderInfo;\n\n /**\n * Whether the provider should be set as window.ethereum.\n */\n shouldSetOnWindow?: boolean;\n\n /**\n * Whether the window.web3 shim should be set.\n */\n shouldShimWeb3?: boolean;\n} & MetaMaskInpageProviderOptions;\n\n/**\n * Initializes a MetaMaskInpageProvider and (optionally) assigns it as window.ethereum.\n *\n * @param options - An options bag.\n * @param options.connectionStream - A Node.js stream.\n * @param options.maxEventListeners - The maximum number of event listeners.\n * @param options.providerInfo - The EIP-6963 provider info / CAIP-294 wallet data that should be announced if set.\n * @param options.shouldSendMetadata - Whether the provider should send page metadata.\n * @param options.shouldSetOnWindow - Whether the provider should be set as window.ethereum.\n * @param options.shouldShimWeb3 - Whether a window.web3 shim should be injected.\n * @param options.logger - The logging API to use. Default: `console`.\n * @returns The initialized provider (whether set or not).\n */\nexport function initializeProvider({\n connectionStream,\n logger = console,\n maxEventListeners = 100,\n providerInfo,\n shouldSendMetadata = true,\n shouldSetOnWindow = true,\n shouldShimWeb3 = false,\n}: InitializeProviderOptions): MetaMaskInpageProvider {\n const provider = new MetaMaskInpageProvider(connectionStream, {\n logger,\n maxEventListeners,\n shouldSendMetadata,\n });\n\n const proxiedProvider = new Proxy(provider, {\n // some common libraries, e.g. web3@1.x, mess with our API\n deleteProperty: () => true,\n // fix issue with Proxy unable to access private variables from getters\n // https://stackoverflow.com/a/73051482\n get(target, propName: 'chainId' | 'networkVersion' | 'selectedAddress') {\n return target[propName];\n },\n });\n\n if (providerInfo) {\n announceEip6963Provider({\n info: providerInfo,\n provider: proxiedProvider,\n });\n // eslint-disable-next-line no-void\n void announceCaip294WalletData(provider, providerInfo);\n }\n\n if (shouldSetOnWindow) {\n setGlobalProvider(proxiedProvider);\n }\n\n if (shouldShimWeb3) {\n shimWeb3(proxiedProvider, logger);\n }\n\n return proxiedProvider;\n}\n\n/**\n * Sets the given provider instance as window.ethereum and dispatches the\n * 'ethereum#initialized' event on window.\n *\n * @param providerInstance - The provider instance.\n */\nexport function setGlobalProvider(\n providerInstance: MetaMaskInpageProvider,\n): void {\n (window as Record<string, any>).ethereum = providerInstance;\n window.dispatchEvent(new Event('ethereum#initialized'));\n}\n\n/**\n * Announces [CAIP-294](https://github.com/ChainAgnostic/CAIPs/blob/bc4942857a8e04593ed92f7dc66653577a1c4435/CAIPs/caip-294.md) wallet data according to build type and browser.\n * Until released to stable, `extensionId` is only set in the `metamask_getProviderState` result if the build type is `flask`.\n * `extensionId` is included if browser is chromium based because it is only useable by browsers that support [externally_connectable](https://developer.chrome.com/docs/extensions/reference/manifest/externally-connectable).\n *\n * @param provider - The provider {@link MetaMaskInpageProvider} used for retrieving `extensionId`.\n * @param providerInfo - The provider info {@link BaseProviderInfo} that should be announced if set.\n */\nexport async function announceCaip294WalletData(\n provider: MetaMaskInpageProvider,\n providerInfo: CAIP294WalletData,\n): Promise<void> {\n const buildType = getBuildType(providerInfo.rdns);\n if (buildType !== 'flask') {\n return;\n }\n\n const providerState = await provider.request<{ extensionId?: string }>({\n method: 'metamask_getProviderState',\n });\n const extensionId = providerState?.extensionId;\n\n const walletData = {\n ...providerInfo,\n extensionId,\n };\n\n announceWallet(walletData);\n}\n"]}