diff --git a/packages/providers/src.ts/index.ts b/packages/providers/src.ts/index.ts index 63b88582ce..0bccf69d37 100644 --- a/packages/providers/src.ts/index.ts +++ b/packages/providers/src.ts/index.ts @@ -26,6 +26,7 @@ import { IpcProvider } from "./ipc-provider"; import { InfuraProvider, InfuraWebSocketProvider } from "./infura-provider"; import { JsonRpcProvider, JsonRpcSigner } from "./json-rpc-provider"; import { NodesmithProvider } from "./nodesmith-provider"; +import { PocketProvider } from "./pocket-provider"; import { StaticJsonRpcProvider, UrlJsonRpcProvider } from "./url-json-rpc-provider"; import { Web3Provider } from "./web3-provider"; import { WebSocketProvider } from "./websocket-provider"; @@ -78,6 +79,7 @@ function getDefaultProvider(network?: Network | string, options?: any): BaseProv InfuraProvider, JsonRpcProvider, NodesmithProvider, + PocketProvider, Web3Provider, IpcProvider, @@ -110,6 +112,7 @@ export { InfuraWebSocketProvider, JsonRpcProvider, NodesmithProvider, + PocketProvider, StaticJsonRpcProvider, Web3Provider, WebSocketProvider, diff --git a/packages/providers/src.ts/pocket-provider.ts b/packages/providers/src.ts/pocket-provider.ts new file mode 100644 index 0000000000..373b3284ca --- /dev/null +++ b/packages/providers/src.ts/pocket-provider.ts @@ -0,0 +1,78 @@ +"use strict"; + +import { Network } from "@ethersproject/networks"; +import { ConnectionInfo } from "@ethersproject/web"; + +import { Logger } from "@ethersproject/logger"; +import { version } from "./_version"; +const logger = new Logger(version); + +import { UrlJsonRpcProvider } from "./url-json-rpc-provider"; + +const defaultApplicationId = "5f7f8547b90218002e9ce9dd"; + +export class PocketProvider extends UrlJsonRpcProvider { + readonly applicationId: string; + readonly applicationSecretKey: string; + + static getApiKey(apiKey: any): any { + const apiKeyObj: { applicationId: string, applicationSecretKey: string } = { + applicationId: defaultApplicationId, + applicationSecretKey: null + }; + + if (apiKey == null) { return apiKeyObj; } + + // Parse applicationId and applicationSecretKey + if (typeof (apiKey) === "string") { + apiKeyObj.applicationId = apiKey; + + } else if (apiKey.applicationSecretKey != null) { + logger.assertArgument((typeof (apiKey.applicationId) === "string"), + "applicationSecretKey requires an applicationId", "applicationId", apiKey.applicationId); + logger.assertArgument((typeof (apiKey.applicationSecretKey) === "string"), + "invalid applicationSecretKey", "applicationSecretKey", "[REDACTED]"); + + apiKeyObj.applicationId = apiKey.applicationId; + apiKeyObj.applicationSecretKey = apiKey.applicationSecretKey; + + } else if (apiKey.applicationId) { + apiKeyObj.applicationId = apiKey.applicationId; + } + + return apiKeyObj; + } + + static getUrl(network: Network, apiKey: any): ConnectionInfo { + let host: string = null; + switch (network ? network.name : "unknown") { + case "homestead": + host = "eth-mainnet.gateway.pokt.network"; + break; + default: + logger.throwError("unsupported network", Logger.errors.INVALID_ARGUMENT, { + argument: "network", + value: network + }); + } + + const connection: ConnectionInfo = { + url: (`https:/\/${ host }/v1/${ apiKey.applicationId }`), + }; + + // Initialize empty headers + connection.headers = {} + + // Apply application secret key + if (apiKey.applicationSecretKey != null) { + connection.user = ""; + connection.password = apiKey.applicationSecretKey + } + + return connection; + } + + isCommunityResource(): boolean { + return (this.applicationId === defaultApplicationId); + } +}