Skip to content

Commit 50a277d

Browse files
authored
feat: added proxy support (#17)
* feat: added proxy support --------- Co-authored-by: agoltzman <106151463+agoltzman@users.noreply.github.com>
1 parent c016644 commit 50a277d

6 files changed

Lines changed: 174 additions & 55 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,9 @@ type FireblocksProviderConfig = {
147147
* Same as setting env var `DEBUG=fireblocks-web3-provider:error`
148148
*/
149149
enhancedErrorHandling?: boolean,
150+
/**
151+
* Proxy path in the format of `http(s)://user:pass@server`
152+
*/
153+
proxyPath?: string
150154
}
151155
```

package-lock.json

Lines changed: 114 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@
5555
},
5656
"dependencies": {
5757
"@ethersproject/units": "^5.7.0",
58+
"axios": "^1.6.2",
5859
"debug": "^4.3.4",
5960
"ethers": "^5.7.2",
60-
"fireblocks-sdk": "^3.1.4",
61+
"fireblocks-sdk": "^4.2.0",
62+
"https-proxy-agent": "^7.0.2",
6163
"web3-providers-http": "1.8.0"
6264
}
6365
}

src/provider.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import { DEBUG_NAMESPACE_ENHANCED_ERROR_HANDLING, DEBUG_NAMESPACE_REQUESTS_AND_R
99
import * as ethers from "ethers";
1010
import { NativeMetaTransaction__factory } from "./contracts/factories"
1111
import { _TypedDataEncoder } from "@ethersproject/hash";
12+
import { HttpsProxyAgent } from 'https-proxy-agent';
1213
import { formatJsonRpcRequest, formatJsonRpcResult } from "./jsonRpcUtils";
1314
import { version as SDK_VERSION } from "../package.json";
1415
import Debug from "debug";
16+
import { AxiosProxyConfig } from "axios";
1517
const HttpProvider = require("web3-providers-http");
1618
const logTransactionStatusChange = Debug(DEBUG_NAMESPACE_TX_STATUS_CHANGES);
1719
const 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
}

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FeeLevel } from "fireblocks-sdk";
2+
import { AxiosProxyConfig } from "axios";
23

34
export enum ChainId {
45
MAINNET = 1,
@@ -142,6 +143,11 @@ export type FireblocksProviderConfig = {
142143
* relayed via the provided vault account
143144
*/
144145
gaslessGasTankVaultId?: number,
146+
147+
/**
148+
* Proxy path in the format of `http(s)://user:pass@server`
149+
*/
150+
proxyPath?: string,
145151
}
146152

147153
export interface RequestArguments<T = any> {

0 commit comments

Comments
 (0)