UNPKG

sparrow-providers

Version:

A JavaScript Ubiq provider that connects to the wallet over a stream.

52 lines 2.27 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setGlobalProvider = exports.initializeProvider = void 0; const MetaMaskInpageProvider_1 = __importDefault(require("./MetaMaskInpageProvider")); const shimWeb3_1 = __importDefault(require("./shimWeb3")); /** * Initializes a MetaMaskInpageProvider and (optionally) assigns it as window.ethereum. * * @param options - An options bag. * @param options.connectionStream - A Node.js stream. * @param options.jsonRpcStreamName - The name of the internal JSON-RPC stream. * @param options.maxEventListeners - The maximum number of event listeners. * @param options.shouldSendMetadata - Whether the provider should send page metadata. * @param options.shouldSetOnWindow - Whether the provider should be set as window.ethereum. * @param options.shouldShimWeb3 - Whether a window.web3 shim should be injected. * @returns The initialized provider (whether set or not). */ function initializeProvider({ connectionStream, jsonRpcStreamName, logger = console, maxEventListeners = 100, shouldSendMetadata = true, shouldSetOnWindow = true, shouldShimWeb3 = false, }) { let provider = new MetaMaskInpageProvider_1.default(connectionStream, { jsonRpcStreamName, logger, maxEventListeners, shouldSendMetadata, }); provider = new Proxy(provider, { // some common libraries, e.g. web3@1.x, mess with our API deleteProperty: () => true, }); if (shouldSetOnWindow) { setGlobalProvider(provider); } if (shouldShimWeb3) { shimWeb3_1.default(provider, logger); } return provider; } exports.initializeProvider = initializeProvider; /** * Sets the given provider instance as window.ethereum and dispatches the * 'ethereum#initialized' event on window. * * @param providerInstance - The provider instance. */ function setGlobalProvider(providerInstance) { window.ethereum = providerInstance; window.dispatchEvent(new Event('ethereum#initialized')); } exports.setGlobalProvider = setGlobalProvider; //# sourceMappingURL=initializeInpageProvider.js.map