-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added initial PocketProvider (#1052).
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |