UNPKG

6.08 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 if (typeof b !== "function" && b !== null)
11 throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12 extendStatics(d, b);
13 function __() { this.constructor = d; }
14 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15 };
16})();
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.PocketProvider = void 0;
19var properties_1 = require("@ethersproject/properties");
20var logger_1 = require("@ethersproject/logger");
21var _version_1 = require("./_version");
22var logger = new logger_1.Logger(_version_1.version);
23var url_json_rpc_provider_1 = require("./url-json-rpc-provider");
24// These are load-balancer-based application IDs
25var defaultApplicationIds = {
26 homestead: "6004bcd10040261633ade990",
27 ropsten: "6004bd4d0040261633ade991",
28 rinkeby: "6004bda20040261633ade994",
29 goerli: "6004bd860040261633ade992",
30};
31var PocketProvider = /** @class */ (function (_super) {
32 __extends(PocketProvider, _super);
33 function PocketProvider(network, apiKey) {
34 // We need a bit of creativity in the constructor because
35 // Pocket uses different default API keys based on the network
36 var _newTarget = this.constructor;
37 var _this = this;
38 if (apiKey == null) {
39 var n = (0, properties_1.getStatic)(_newTarget, "getNetwork")(network);
40 if (n) {
41 var applicationId = defaultApplicationIds[n.name];
42 if (applicationId) {
43 apiKey = {
44 applicationId: applicationId,
45 loadBalancer: true
46 };
47 }
48 }
49 // If there was any issue above, we don't know this network
50 if (apiKey == null) {
51 logger.throwError("unsupported network", logger_1.Logger.errors.INVALID_ARGUMENT, {
52 argument: "network",
53 value: network
54 });
55 }
56 }
57 _this = _super.call(this, network, apiKey) || this;
58 return _this;
59 }
60 PocketProvider.getApiKey = function (apiKey) {
61 // Most API Providers allow null to get the default configuration, but
62 // Pocket requires the network to decide the default provider, so we
63 // rely on hijacking the constructor to add a sensible default for us
64 if (apiKey == null) {
65 logger.throwArgumentError("PocketProvider.getApiKey does not support null apiKey", "apiKey", apiKey);
66 }
67 var apiKeyObj = {
68 applicationId: null,
69 loadBalancer: false,
70 applicationSecretKey: null
71 };
72 // Parse applicationId and applicationSecretKey
73 if (typeof (apiKey) === "string") {
74 apiKeyObj.applicationId = apiKey;
75 }
76 else if (apiKey.applicationSecretKey != null) {
77 logger.assertArgument((typeof (apiKey.applicationId) === "string"), "applicationSecretKey requires an applicationId", "applicationId", apiKey.applicationId);
78 logger.assertArgument((typeof (apiKey.applicationSecretKey) === "string"), "invalid applicationSecretKey", "applicationSecretKey", "[REDACTED]");
79 apiKeyObj.applicationId = apiKey.applicationId;
80 apiKeyObj.applicationSecretKey = apiKey.applicationSecretKey;
81 apiKeyObj.loadBalancer = !!apiKey.loadBalancer;
82 }
83 else if (apiKey.applicationId) {
84 logger.assertArgument((typeof (apiKey.applicationId) === "string"), "apiKey.applicationId must be a string", "apiKey.applicationId", apiKey.applicationId);
85 apiKeyObj.applicationId = apiKey.applicationId;
86 apiKeyObj.loadBalancer = !!apiKey.loadBalancer;
87 }
88 else {
89 logger.throwArgumentError("unsupported PocketProvider apiKey", "apiKey", apiKey);
90 }
91 return apiKeyObj;
92 };
93 PocketProvider.getUrl = function (network, apiKey) {
94 var host = null;
95 switch (network ? network.name : "unknown") {
96 case "homestead":
97 host = "eth-mainnet.gateway.pokt.network";
98 break;
99 case "ropsten":
100 host = "eth-ropsten.gateway.pokt.network";
101 break;
102 case "rinkeby":
103 host = "eth-rinkeby.gateway.pokt.network";
104 break;
105 case "goerli":
106 host = "eth-goerli.gateway.pokt.network";
107 break;
108 default:
109 logger.throwError("unsupported network", logger_1.Logger.errors.INVALID_ARGUMENT, {
110 argument: "network",
111 value: network
112 });
113 }
114 var url = null;
115 if (apiKey.loadBalancer) {
116 url = "https://" + host + "/v1/lb/" + apiKey.applicationId;
117 }
118 else {
119 url = "https://" + host + "/v1/" + apiKey.applicationId;
120 }
121 var connection = { url: url };
122 // Initialize empty headers
123 connection.headers = {};
124 // Apply application secret key
125 if (apiKey.applicationSecretKey != null) {
126 connection.user = "";
127 connection.password = apiKey.applicationSecretKey;
128 }
129 return connection;
130 };
131 PocketProvider.prototype.isCommunityResource = function () {
132 return (this.applicationId === defaultApplicationIds[this.network.name]);
133 };
134 return PocketProvider;
135}(url_json_rpc_provider_1.UrlJsonRpcProvider));
136exports.PocketProvider = PocketProvider;
137//# sourceMappingURL=pocket-provider.js.map
\No newline at end of file