Skip to content

Commit

Permalink
Added initial PocketProvider (#1052).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Oct 9, 2020
1 parent 5e0e3de commit a62d20d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/providers/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -78,6 +79,7 @@ function getDefaultProvider(network?: Network | string, options?: any): BaseProv
InfuraProvider,
JsonRpcProvider,
NodesmithProvider,
PocketProvider,
Web3Provider,

IpcProvider,
Expand Down Expand Up @@ -110,6 +112,7 @@ export {
InfuraWebSocketProvider,
JsonRpcProvider,
NodesmithProvider,
PocketProvider,
StaticJsonRpcProvider,
Web3Provider,
WebSocketProvider,
Expand Down
78 changes: 78 additions & 0 deletions packages/providers/src.ts/pocket-provider.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit a62d20d

Please # to comment.