Skip to content

Commit

Permalink
example: a new example to show and test http proxy usage
Browse files Browse the repository at this point in the history
Ticket: WP-1185
  • Loading branch information
pengyuc-bitgo committed Dec 21, 2023
1 parent 1f194e2 commit 743c75d
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
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 get-me.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

0 comments on commit 743c75d

Please sign in to comment.