2019-05-15 01:25:46 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { Network } from "@ethersproject/networks";
|
2019-11-19 12:00:05 +03:00
|
|
|
import { ConnectionInfo } from "@ethersproject/web";
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-08-02 01:04:06 +03:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
import { UrlJsonRpcProvider } from "./url-json-rpc-provider";
|
|
|
|
|
|
|
|
|
|
|
|
const defaultProjectId = "84842078b09946638c03157f83405213"
|
|
|
|
|
|
|
|
export class InfuraProvider extends UrlJsonRpcProvider {
|
2019-11-19 12:00:05 +03:00
|
|
|
readonly projectId: string;
|
|
|
|
readonly projectSecret: string;
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-19 12:00:05 +03:00
|
|
|
static getApiKey(apiKey: any): any {
|
|
|
|
const apiKeyObj: { apiKey: string, projectId: string, projectSecret: string } = {
|
2019-11-20 12:28:13 +03:00
|
|
|
apiKey: defaultProjectId,
|
2019-11-19 12:00:05 +03:00
|
|
|
projectId: defaultProjectId,
|
|
|
|
projectSecret: null
|
|
|
|
};
|
|
|
|
|
2019-11-20 12:28:13 +03:00
|
|
|
if (apiKey == null) { return apiKeyObj; }
|
|
|
|
|
2019-11-19 12:00:05 +03: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-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-19 12:00:05 +03:00
|
|
|
static getUrl(network: Network, apiKey: any): string | ConnectionInfo {
|
|
|
|
let host: string = null;
|
2019-05-15 01:25:46 +03:00
|
|
|
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 01:04:06 +03:00
|
|
|
logger.throwError("unsupported network", Logger.errors.INVALID_ARGUMENT, {
|
2019-05-15 01:25:46 +03:00
|
|
|
argument: "network",
|
|
|
|
value: network
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-19 12:00:05 +03:00
|
|
|
const connection: ConnectionInfo = {
|
|
|
|
url: ("https:/" + "/" + host + "/v3/" + apiKey.projectId)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (apiKey.projectSecret != null) {
|
|
|
|
connection.user = "";
|
|
|
|
connection.password = apiKey.projectSecret
|
|
|
|
}
|
|
|
|
|
|
|
|
return connection;
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
}
|