ethers.js/packages/providers/lib/infura-provider.js

102 lines
4.0 KiB
JavaScript
Raw Permalink Normal View History

2019-05-14 18:48:48 -04:00
"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 });
2020-05-03 17:53:58 -04:00
var websocket_provider_1 = require("./websocket-provider");
2019-08-02 02:10:58 -04:00
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
2019-05-14 18:48:48 -04:00
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;
}
2020-05-03 17:53:58 -04:00
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);
};
2019-05-14 18:48:48 -04:00
InfuraProvider.getApiKey = function (apiKey) {
2019-11-20 18:57:38 +09:00
var apiKeyObj = {
apiKey: defaultProjectId,
projectId: defaultProjectId,
projectSecret: null
};
2019-05-14 18:48:48 -04:00
if (apiKey == null) {
2019-11-20 18:57:38 +09:00
return apiKeyObj;
2019-05-14 18:48:48 -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-05-14 18:48:48 -04:00
};
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:
2019-08-02 02:10:58 -04:00
logger.throwError("unsupported network", logger_1.Logger.errors.INVALID_ARGUMENT, {
2019-05-14 18:48:48 -04:00
argument: "network",
value: network
});
}
2019-11-20 18:57:38 +09:00
var connection = {
url: ("https:/" + "/" + host + "/v3/" + apiKey.projectId)
};
if (apiKey.projectSecret != null) {
connection.user = "";
connection.password = apiKey.projectSecret;
}
return connection;
2019-05-14 18:48:48 -04:00
};
return InfuraProvider;
}(url_json_rpc_provider_1.UrlJsonRpcProvider));
exports.InfuraProvider = InfuraProvider;