Skip to content

Commit

Permalink
refactor: ♻️ refactored proxy config to string
Browse files Browse the repository at this point in the history
  • Loading branch information
a0ngo committed Jan 4, 2024
1 parent 5291635 commit b870f66
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 35 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 --------------

Expand Down
29 changes: 21 additions & 8 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}
}
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 --------------

Expand Down
28 changes: 5 additions & 23 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand All @@ -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)
Expand Down

0 comments on commit b870f66

Please sign in to comment.