@@ -9,9 +9,11 @@ import { DEBUG_NAMESPACE_ENHANCED_ERROR_HANDLING, DEBUG_NAMESPACE_REQUESTS_AND_R
99import * as ethers from "ethers" ;
1010import { NativeMetaTransaction__factory } from "./contracts/factories"
1111import { _TypedDataEncoder } from "@ethersproject/hash" ;
12+ import { HttpsProxyAgent } from 'https-proxy-agent' ;
1213import { formatJsonRpcRequest , formatJsonRpcResult } from "./jsonRpcUtils" ;
1314import { version as SDK_VERSION } from "../package.json" ;
1415import Debug from "debug" ;
16+ import { AxiosProxyConfig } from "axios" ;
1517const HttpProvider = require ( "web3-providers-http" ) ;
1618const logTransactionStatusChange = Debug ( DEBUG_NAMESPACE_TX_STATUS_CHANGES ) ;
1719const logEnhancedErrorHandling = Debug ( DEBUG_NAMESPACE_ENHANCED_ERROR_HANDLING ) ;
@@ -87,7 +89,8 @@ export class FireblocksWeb3Provider extends HttpProvider {
8789 undefined ,
8890 {
8991 userAgent : this . getUserAgent ( ) ,
90- } )
92+ proxy : config . proxyPath ? this . toAxiosProxyConfig ( config . proxyPath ) : undefined
93+ } ) ;
9194 this . feeLevel = config . fallbackFeeLevel || FeeLevel . MEDIUM
9295 this . note = config . note ?? 'Created by Fireblocks Web3 Provider'
9396 this . externalTxId = config . externalTxId ;
@@ -101,6 +104,14 @@ export class FireblocksWeb3Provider extends HttpProvider {
101104 this . accountsPopulatedPromise = promiseToFunction ( async ( ) => { return await this . populateAccounts ( ) } )
102105 this . whitelistedPopulatedPromise = promiseToFunction ( async ( ) => { if ( ! this . oneTimeAddressesEnabled ) return await this . populateWhitelisted ( ) } )
103106 this . gaslessGasTankAddressPopulatedPromise = promiseToFunction ( async ( ) => { if ( this . gaslessGasTankVaultId ) return await this . populateGaslessGasTankAddress ( ) } )
107+
108+ if ( config . proxyPath ) {
109+ const proxyAgent = new HttpsProxyAgent ( config . proxyPath ) ;
110+ this . agent = {
111+ http : proxyAgent ,
112+ https : proxyAgent
113+ }
114+ }
104115 }
105116
106117 private parsePrivateKey ( privateKey : string ) : string {
@@ -622,4 +633,21 @@ Available addresses: ${Object.values(this.accounts).join(', ')}.`
622633 public setExternalTxId ( externalTxId : ( ( ) => string ) | string | undefined ) {
623634 this . externalTxId = externalTxId ;
624635 }
636+
637+ private toAxiosProxyConfig ( path : string ) : AxiosProxyConfig {
638+ const proxyUrl = new URL ( path ) ;
639+
640+ if ( proxyUrl . pathname != '/' ) {
641+ throw 'Proxy with path is not supported by axios' ;
642+ }
643+ return {
644+ protocol : proxyUrl . protocol . replace ( ':' , '' ) ,
645+ host : proxyUrl . hostname ,
646+ port : parseInt ( proxyUrl . port ) ,
647+ auth : proxyUrl . username ? {
648+ username : proxyUrl . username ,
649+ password : proxyUrl . password
650+ } : undefined
651+ }
652+ }
625653}
0 commit comments