From 8c1ff4c862b8cecb04c98d71910870e0b73867a0 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Mon, 29 Jun 2020 00:06:31 -0400 Subject: [PATCH] Allow getDefaultProvider to accept a URL as a network. --- packages/providers/src.ts/index.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/providers/src.ts/index.ts b/packages/providers/src.ts/index.ts index 5236a50f..21ba25d4 100644 --- a/packages/providers/src.ts/index.ts +++ b/packages/providers/src.ts/index.ts @@ -42,6 +42,25 @@ const logger = new Logger(version); function getDefaultProvider(network?: Network | string, options?: any): BaseProvider { if (network == null) { network = "homestead"; } + + // If passed a URL, figure out the right type of provider based on the scheme + if (typeof(network) === "string") { + // @TODO: Add support for IpcProvider; maybe if it ends in ".ipc"? + + // Handle http and ws (and their secure variants) + const match = network.match(/^(ws|http)s?:/i); + if (match) { + switch (match[1]) { + case "http": + return new JsonRpcProvider(network); + case "ws": + return new WebSocketProvider(network); + default: + logger.throwArgumentError("unsupported URL scheme", "network", network); + } + } + } + const n = getNetwork(network); if (!n || !n._defaultProvider) { logger.throwError("unsupported getDefaultProvider network", Logger.errors.NETWORK_ERROR, {