ethers.js/packages/providers/lib/infura-provider.js
2020-05-03 17:53:58 -04:00

102 lines
4.0 KiB
JavaScript

"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var websocket_provider_1 = require("./websocket-provider");
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
var url_json_rpc_provider_1 = require("./url-json-rpc-provider");
var defaultProjectId = "84842078b09946638c03157f83405213";
var InfuraProvider = /** @class */ (function (_super) {
__extends(InfuraProvider, _super);
function InfuraProvider() {
return _super !== null && _super.apply(this, arguments) || this;
}
InfuraProvider.getWebSocketProvider = function (network, apiKey) {
var provider = new InfuraProvider(network, apiKey);
var connection = provider.connection;
if (connection.password) {
logger.throwError("INFURA WebSocket project secrets unsupported", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
operation: "InfuraProvider.getWebSocketProvider()"
});
}
var url = connection.url.replace(/^http/i, "ws").replace("/v3/", "/ws/v3/");
return new websocket_provider_1.WebSocketProvider(url, network);
};
InfuraProvider.getApiKey = function (apiKey) {
var apiKeyObj = {
apiKey: defaultProjectId,
projectId: defaultProjectId,
projectSecret: null
};
if (apiKey == null) {
return apiKeyObj;
}
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;
};
InfuraProvider.getUrl = function (network, apiKey) {
var 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_1.Logger.errors.INVALID_ARGUMENT, {
argument: "network",
value: network
});
}
var connection = {
url: ("https:/" + "/" + host + "/v3/" + apiKey.projectId)
};
if (apiKey.projectSecret != null) {
connection.user = "";
connection.password = apiKey.projectSecret;
}
return connection;
};
return InfuraProvider;
}(url_json_rpc_provider_1.UrlJsonRpcProvider));
exports.InfuraProvider = InfuraProvider;