diff --git a/README.md b/README.md index 60c5920..432a2ad 100644 --- a/README.md +++ b/README.md @@ -85,9 +85,9 @@ type FireblocksProviderConfig = { */ rpcUrl?: string, /** - * Proxy configuration + * Proxy path in the format of `http(s)://user:pass@server` */ - proxyConfig?: AxiosProxyConfig + proxyPath?: string // ------------- Optional fields -------------- diff --git a/src/provider.ts b/src/provider.ts index c598e9e..1fcb9ef 100644 --- a/src/provider.ts +++ b/src/provider.ts @@ -13,6 +13,7 @@ import { HttpsProxyAgent } from 'https-proxy-agent'; import { formatJsonRpcRequest, formatJsonRpcResult } from "./jsonRpcUtils"; import { version as SDK_VERSION } from "../package.json"; import Debug from "debug"; +import { AxiosProxyConfig } from "axios"; const HttpProvider = require("web3-providers-http"); const logTransactionStatusChange = Debug(DEBUG_NAMESPACE_TX_STATUS_CHANGES); const logEnhancedErrorHandling = Debug(DEBUG_NAMESPACE_ENHANCED_ERROR_HANDLING); @@ -88,7 +89,7 @@ export class FireblocksWeb3Provider extends HttpProvider { undefined, { userAgent: this.getUserAgent(), - proxy: config.proxyConfig ?? undefined + proxy: config.proxyPath ? this.toAxiosProxyConfig(config.proxyPath) : undefined }); this.feeLevel = config.fallbackFeeLevel || FeeLevel.MEDIUM this.note = config.note ?? 'Created by Fireblocks Web3 Provider' @@ -104,13 +105,8 @@ export class FireblocksWeb3Provider extends HttpProvider { this.whitelistedPopulatedPromise = promiseToFunction(async () => { if (!this.oneTimeAddressesEnabled) return await this.populateWhitelisted() }) this.gaslessGasTankAddressPopulatedPromise = promiseToFunction(async () => { if (this.gaslessGasTankVaultId) return await this.populateGaslessGasTankAddress() }) - if (config.proxyConfig) { - const proxyConfig = config.proxyConfig!; - const protocol = 'http://'; - const creds = proxyConfig.auth ? proxyConfig.auth.username ? `${proxyConfig.auth.username}${proxyConfig.auth.password ? `:${proxyConfig.auth.password}@` : '@'}` : '' : ''; - const host = `${proxyConfig.host}:${proxyConfig.port}`; - const proxyPath = `${protocol}${creds}${host}`; - const proxyAgent = new HttpsProxyAgent(proxyPath); + if (config.proxyPath) { + const proxyAgent = new HttpsProxyAgent(config.proxyPath); this.agent = { http: proxyAgent, https: proxyAgent @@ -637,4 +633,21 @@ Available addresses: ${Object.values(this.accounts).join(', ')}.` public setExternalTxId(externalTxId: (() => string) | string | undefined) { this.externalTxId = externalTxId; } + + private toAxiosProxyConfig(path: string): AxiosProxyConfig { + const proxyUrl = new URL(path); + + if (proxyUrl.pathname != '/') { + throw 'Proxy with path is not supported by axios'; + } + return { + protocol: proxyUrl.protocol.replace(':', ''), + host: proxyUrl.hostname, + port: parseInt(proxyUrl.port), + auth: proxyUrl.username ? { + username: proxyUrl.username, + password: proxyUrl.password + } : undefined + } + } } diff --git a/src/types.ts b/src/types.ts index b0d1e84..babba17 100644 --- a/src/types.ts +++ b/src/types.ts @@ -73,9 +73,9 @@ export type FireblocksProviderConfig = { */ rpcUrl?: string, /** - * Proxy configuration + * Proxy path in the format of `http(s)://user:pass@server` */ - proxyConfig?: AxiosProxyConfig + proxyPath?: string // ------------- Optional fields -------------- diff --git a/test/utils.ts b/test/utils.ts index beda225..5726621 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -3,7 +3,6 @@ dotenv.config() import * as ethers from "ethers" import { FireblocksWeb3Provider, ChainId } from "../src" import Web3 from "web3"; -import { AxiosProxyConfig } from 'axios'; export function getFireblocksProviderForTesting(extraConfiguration?: any) { if (!process.env.FIREBLOCKS_API_PRIVATE_KEY_PATH || @@ -21,28 +20,11 @@ export function getFireblocksProviderForTesting(extraConfiguration?: any) { ...extraConfiguration }; - if(process.env.PROXY_HOST && process.env.PROXY_PORT){ - if(process.env.PROXY_PASS && !process.env.PROXY_USER){ - throw new Error("PROXY_PASS and PROXY_USER must bother be set, or only PROXY_USER must appear (if auth is required)") - } - - providerConfig["proxyConfig"] = { - host: process.env.PROXY_HOST, - port: parseInt(process.env.PROXY_PORT), - auth: undefined, - } as AxiosProxyConfig - - if(process.env.PROXY_USER){ - providerConfig["proxy"]["auth"] = { - username: process.env.PROXY_USER, - password: process.env.PROXY_PASS ?? undefined - } - } - - if(process.env.PROXY_UNTRUSTED_CERT) - process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; - } else if ((!process.env.PROXY_HOST && process.env.PROXY_PORT) || (process.env.PROXY_HOST && !process.env.PROXY_PORT)){ - throw new Error("PROXY_HOST and PROXY_PORT must both appear, or neither") + if (process.env.PROXY_PATH) { + providerConfig["proxyPath"] = process.env.PROXY_PATH + if (process.env.PROXY_UNTRUSTED_CERT) + process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; + } const provider = new FireblocksWeb3Provider(providerConfig)