Added support for complex API keys including support for INFURA project secrets (#464, #651, #652).

This commit is contained in:
Richard Moore 2019-11-19 18:00:05 +09:00
parent 75895fa149
commit 1ec5804bd4
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651
6 changed files with 78 additions and 28 deletions

@ -18,8 +18,11 @@ const defaultApiKey = "_gg7wSSi0KMBsdKnGVfHDueq6xMB9EkC"
export class AlchemyProvider extends UrlJsonRpcProvider {
readonly apiKey: string;
static getApiKey(apiKey: string): string {
static getApiKey(apiKey: any): any {
if (apiKey == null) { return defaultApiKey; }
if (apiKey && typeof(apiKey) !== "string") {
logger.throwArgumentError("invalid apiKey", "apiKey", apiKey);
}
return apiKey;
}

@ -9,11 +9,14 @@ const logger = new Logger(version);
export class CloudflareProvider extends UrlJsonRpcProvider {
static getUrl(network: Network, apiKey?: string): string {
static getApiKey(apiKey: any): any {
if (apiKey != null) {
logger.throwArgumentError("apiKey not supported for cloudflare", "apiKey", apiKey);
}
return null;
}
static getUrl(network: Network, apiKey?: any): string {
let host = null;
switch (network.name) {
case "homestead":

@ -1,6 +1,7 @@
"use strict";
import { Network } from "@ethersproject/networks";
import { ConnectionInfo } from "@ethersproject/web";
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
@ -12,15 +13,40 @@ import { UrlJsonRpcProvider } from "./url-json-rpc-provider";
const defaultProjectId = "84842078b09946638c03157f83405213"
export class InfuraProvider extends UrlJsonRpcProvider {
get projectId(): string { return this.apiKey; }
readonly projectId: string;
readonly projectSecret: string;
static getApiKey(apiKey: string): string {
if (apiKey == null) { return defaultProjectId; }
return apiKey;
static getApiKey(apiKey: any): any {
const apiKeyObj: { apiKey: string, projectId: string, projectSecret: string } = {
apiKey: null,
projectId: defaultProjectId,
projectSecret: null
};
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;
}
static getUrl(network: Network, apiKey: string): string {
let host = null;
static getUrl(network: Network, apiKey: any): string | ConnectionInfo {
let host: string = null;
switch(network.name) {
case "homestead":
host = "mainnet.infura.io";
@ -44,6 +70,15 @@ export class InfuraProvider extends UrlJsonRpcProvider {
});
}
return "https:/" + "/" + host + "/v3/" + apiKey;
const connection: ConnectionInfo = {
url: ("https:/" + "/" + host + "/v3/" + apiKey.projectId)
};
if (apiKey.projectSecret != null) {
connection.user = "";
connection.password = apiKey.projectSecret
}
return connection;
}
}

@ -266,11 +266,11 @@ export class JsonRpcProvider extends BaseProvider {
if (!url) { url = "http:/" + "/localhost:8545"; }
if (typeof(url) === "string") {
this.connection = {
this.connection = Object.freeze({
url: url
};
});
} else {
this.connection = url;
this.connection = Object.freeze(shallowCopy(url));
}
this._nextId = 42;

@ -12,11 +12,14 @@ const defaultApiKey = "ETHERS_JS_SHARED";
export class NodesmithProvider extends UrlJsonRpcProvider {
static getApiKey(apiKey: string): string {
static getApiKey(apiKey: any): any {
if (apiKey && typeof(apiKey) !== "string") {
logger.throwArgumentError("invalid apiKey", "apiKey", apiKey);
}
return apiKey || defaultApiKey;
}
static getUrl(network: Network, apiKey?: string): string {
static getUrl(network: Network, apiKey?: any): string {
let host = null;
switch (network.name) {
case "homestead":

@ -2,6 +2,7 @@
import { Network, Networkish } from "@ethersproject/networks";
import { defineReadOnly, getStatic } from "@ethersproject/properties";
import { ConnectionInfo } from "@ethersproject/web";
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
@ -9,21 +10,29 @@ const logger = new Logger(version);
import { JsonRpcProvider, JsonRpcSigner } from "./json-rpc-provider";
type getUrlFunc = (network: Network, apiKey: string) => string | ConnectionInfo;
export abstract class UrlJsonRpcProvider extends JsonRpcProvider {
readonly apiKey: string;
constructor(network?: Networkish, apiKey?: string) {
constructor(network?: Networkish, apiKey?: any) {
logger.checkAbstract(new.target, UrlJsonRpcProvider);
// Normalize the Network and API Key
network = getStatic<(network: Networkish) => Network>(new.target, "getNetwork")(network);
apiKey = getStatic<(apiKey: string) => string>(new.target, "getApiKey")(apiKey);
const url = getStatic<(network: Network, apiKey: string) => string>(new.target, "getUrl")(network, apiKey);
const connection = getStatic<getUrlFunc>(new.target, "getUrl")(network, apiKey);
super(url, network);
super(connection, network);
defineReadOnly(this, "apiKey", apiKey);
if (typeof(apiKey) === "string") {
defineReadOnly(this, "apiKey", apiKey);
} else if (apiKey != null) {
Object.keys(apiKey).forEach((key) => {
defineReadOnly(this, key, apiKey[key]);
});
}
}
_startPending(): void {
@ -31,29 +40,26 @@ export abstract class UrlJsonRpcProvider extends JsonRpcProvider {
}
getSigner(address?: string): JsonRpcSigner {
logger.throwError(
return logger.throwError(
"API provider does not support signing",
Logger.errors.UNSUPPORTED_OPERATION,
{ operation: "getSigner" }
);
return null;
}
listAccounts(): Promise<Array<string>> {
return Promise.resolve([]);
}
/*
static getNetwork(network?: Networkish): Network {
return getNetwork((network == null) ? "homestead": network);
}
*/
// Return a defaultApiKey if null, otherwise validate the API key
static getApiKey(apiKey: string): string {
static getApiKey(apiKey: any): any {
return apiKey;
}
// Returns the url for the given network and API key
static getUrl(network: Network, apiKey: string): string {
// Returns the url or connection for the given network and API key. The
// API key will have been sanitized by the getApiKey first, so any validation
// or transformations can be done there.
static getUrl(network: Network, apiKey: any): string | ConnectionInfo {
return logger.throwError("not implemented; sub-classes must override getUrl", Logger.errors.NOT_IMPLEMENTED, {
operation: "getUrl"
});