2019-08-25 02:39:20 -04:00
|
|
|
"use strict";
|
2020-05-03 17:53:58 -04:00
|
|
|
import { WebSocketProvider } from "./websocket-provider";
|
2019-08-25 02:39:20 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
import { UrlJsonRpcProvider } from "./url-json-rpc-provider";
|
|
|
|
const defaultProjectId = "84842078b09946638c03157f83405213";
|
|
|
|
export class InfuraProvider extends UrlJsonRpcProvider {
|
2020-05-03 17:53:58 -04:00
|
|
|
static getWebSocketProvider(network, apiKey) {
|
|
|
|
const provider = new InfuraProvider(network, apiKey);
|
|
|
|
const connection = provider.connection;
|
|
|
|
if (connection.password) {
|
|
|
|
logger.throwError("INFURA WebSocket project secrets unsupported", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "InfuraProvider.getWebSocketProvider()"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const url = connection.url.replace(/^http/i, "ws").replace("/v3/", "/ws/v3/");
|
|
|
|
return new WebSocketProvider(url, network);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
static getApiKey(apiKey) {
|
2019-11-20 18:57:38 +09:00
|
|
|
const apiKeyObj = {
|
|
|
|
apiKey: defaultProjectId,
|
|
|
|
projectId: defaultProjectId,
|
|
|
|
projectSecret: null
|
|
|
|
};
|
2019-08-25 02:39:20 -04:00
|
|
|
if (apiKey == null) {
|
2019-11-20 18:57:38 +09:00
|
|
|
return apiKeyObj;
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2019-11-20 18:57:38 +09:00
|
|
|
if (typeof (apiKey) === "string") {
|
|
|
|
apiKeyObj.projectId = apiKey;
|
|
|
|
}
|
|
|
|
else if (apiKey.projectSecret != null) {
|
|
|
|
if (typeof (apiKey.projectId) !== "string") {
|
|
|
|
logger.throwArgumentError("projectSecret requires a projectId", "projectId", apiKey.projectId);
|
|
|
|
}
|
|
|
|
if (typeof (apiKey.projectSecret) !== "string") {
|
|
|
|
logger.throwArgumentError("invalid projectSecret", "projectSecret", "[REDACTED]");
|
|
|
|
}
|
|
|
|
apiKeyObj.projectId = apiKey.projectId;
|
|
|
|
apiKeyObj.projectSecret = apiKey.projectSecret;
|
|
|
|
}
|
|
|
|
else if (apiKey.projectId) {
|
|
|
|
apiKeyObj.projectId = apiKey.projectId;
|
|
|
|
}
|
|
|
|
apiKeyObj.apiKey = apiKeyObj.projectId;
|
|
|
|
return apiKeyObj;
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
static getUrl(network, apiKey) {
|
|
|
|
let host = null;
|
|
|
|
switch (network.name) {
|
|
|
|
case "homestead":
|
|
|
|
host = "mainnet.infura.io";
|
|
|
|
break;
|
|
|
|
case "ropsten":
|
|
|
|
host = "ropsten.infura.io";
|
|
|
|
break;
|
|
|
|
case "rinkeby":
|
|
|
|
host = "rinkeby.infura.io";
|
|
|
|
break;
|
|
|
|
case "kovan":
|
|
|
|
host = "kovan.infura.io";
|
|
|
|
break;
|
|
|
|
case "goerli":
|
|
|
|
host = "goerli.infura.io";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logger.throwError("unsupported network", Logger.errors.INVALID_ARGUMENT, {
|
|
|
|
argument: "network",
|
|
|
|
value: network
|
|
|
|
});
|
|
|
|
}
|
2019-11-20 18:57:38 +09:00
|
|
|
const connection = {
|
|
|
|
url: ("https:/" + "/" + host + "/v3/" + apiKey.projectId)
|
|
|
|
};
|
|
|
|
if (apiKey.projectSecret != null) {
|
|
|
|
connection.user = "";
|
|
|
|
connection.password = apiKey.projectSecret;
|
|
|
|
}
|
|
|
|
return connection;
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
}
|