Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WP-1185 fix SDK using proxy #4169

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/ts/http-proxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
This directory contains a very simple example and test for http proxy setup that allows
talking to BitGo's API backend through a http proxy.

This is in contrast to the [BitGo Express module](https://github.com/BitGo/BitGoJS/tree/master/modules/express) which implements the SDK on the
server that you host in order to provide specific SDK logic before proxying requests
to BitGo APIs for your convenience.

This is also different from the [proxy](../proxy) example, which allows developers to use local BitGo SDK methods with a non-BitGo back-end.
Please take the time to review your use case as you choose between these options.

## Setup + Usage
- Acquire a test environment account, enterprise and access token
- Fill in the `TODO` sections with the relevant credentials
- `npm install`
- `ts-node server.ts`
- In a separate shell: `ts-node create-wallet.ts`
38 changes: 38 additions & 0 deletions examples/ts/http-proxy/create-wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { BitGoAPI } from '@bitgo/sdk-api';
import { Tpolygon } from '@bitgo/sdk-coin-polygon'; // Replace with your given coin (e.g. Ltc, Tltc)

// This script emulates a front-end using the BitGo SDK to BitGo backend via a proxy.
// Set up the BitGo connection object.
const bitgo = new BitGoAPI({
// TODO: your developer access token to the BitGo platform API
accessToken: 'your-token',
// Set as prod/test as needed for whatever BitGo environment you want to use.
// This *must* match the BitGo platform API your proxy instance is using.
env: 'test',
// TODO: In your real setup this would be <your.proxy.url>, where you host the proxy server.
proxy: 'http://localhost:3000',
});
const coin = 'tpolygon';
bitgo.register(coin, Tpolygon.createInstance);

async function createTSSWalletSimple() {
const newWallet = await bitgo
.coin(coin)
.wallets()
.generateWallet({
label: 'hot multisig wallet ' + Math.floor(Date.now() / 1000),
// TODO: your wallet password
passphrase: 'VerySecurePassword1234',
// TODO: your enterprise ID
enterprise: 'your-enterprise-id',
multisigType: 'tss',
walletVersion: 3,
});
console.log(JSON.stringify(newWallet, undefined, 2));
}

async function main() {
await createTSSWalletSimple();
}

main();
19 changes: 19 additions & 0 deletions examples/ts/http-proxy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "sdk-proxy-poc",
"version": "1.0.0",
"description": "",
"main": "server.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "BitGo",
"license": "ISC",
"dependencies": {
"ts-node": "^10.8.1",
"typescript": "^4.7.3",
"typescript-cached-transpile": "^0.0.6",
"superagent": "4.1",
"proxy-agent": "^6.3.1",
"proxy": "2.1.1"
}
}
8 changes: 8 additions & 0 deletions examples/ts/http-proxy/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createProxy } from 'proxy';
import { AddressInfo } from 'net';

const proxy = createProxy();
proxy.listen(3000, () => {
const port = (proxy.address() as AddressInfo).port;
console.log('HTTP(s) proxy server listening on port %d', port);
});
10 changes: 10 additions & 0 deletions examples/ts/http-proxy/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../../tsconfig.json",
"include": ["."],
"exclude": ["node_modules"],
"references": [
{
"path": "../bitgo"
}
]
}
4 changes: 4 additions & 0 deletions examples/ts/proxy/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
This directory contains a very simple example proxy setup that allows developers to use local BitGo SDK methods with a non-BitGo back-end.

This is in contrast to the [BitGo Express module](https://github.com/BitGo/BitGoJS/tree/master/modules/express) which implements the SDK on the server that you host in order to provide specific SDK logic before proxying requests to BitGo APIs for your convenience.

This is not an example for using the BitGo SDK with a http proxy. For that, see the [http-proxy](../http-proxy) example.

Please take the time to review your use case as you choose between these options.

## Setup + Usage
Expand Down
2 changes: 1 addition & 1 deletion modules/sdk-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"debug": "3.1.0",
"eol": "^0.5.0",
"lodash": "^4.17.15",
"proxy-agent": "6.3.1",
"proxy-agent": "5.0.0",
"sanitize-html": "^2.11",
"secp256k1": "^4.0.2",
"secrets.js-grempe": "^1.1.0",
Expand Down
10 changes: 8 additions & 2 deletions modules/sdk-api/src/bitgoAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const TransactionBuilder = require('./v1/transactionBuilder');
let proxyAgent: any;
if (!isBrowser && !isWebWorker) {
debug('enabling proxy-agent');
proxyAgent = require('proxy-agent').ProxyAgent;
proxyAgent = require('proxy-agent');
}

const patchedRequestMethods = ['get', 'post', 'put', 'del', 'patch'] as const;
Expand Down Expand Up @@ -574,7 +574,13 @@ export class BitGoAPI implements BitGoBase {
// Proxy settings must still be respected however
const resultPromise = this.getAgentRequest('get', this.url('/client/constants'));
resultPromise.set('BitGo-SDK-Version', this._version);
const result = await (this._proxy ? resultPromise.proxy(this._proxy) : resultPromise);
if (this._proxy) {
const agent = new proxyAgent(this._proxy);
if (agent) {
resultPromise.agent(agent);
}
}
const result = await resultPromise;
BitGoAPI._constants[env] = result.body.constants;

if (result.body?.ttl && typeof result.body?.ttl === 'number') {
Expand Down
Loading
Loading