diff --git a/examples/ts/http-proxy/README.md b/examples/ts/http-proxy/README.md new file mode 100644 index 0000000000..11787ec1c5 --- /dev/null +++ b/examples/ts/http-proxy/README.md @@ -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` diff --git a/examples/ts/http-proxy/create-wallet.ts b/examples/ts/http-proxy/create-wallet.ts new file mode 100644 index 0000000000..b282b851f4 --- /dev/null +++ b/examples/ts/http-proxy/create-wallet.ts @@ -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 , 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(); diff --git a/examples/ts/http-proxy/package.json b/examples/ts/http-proxy/package.json new file mode 100644 index 0000000000..f77ff12592 --- /dev/null +++ b/examples/ts/http-proxy/package.json @@ -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" + } +} diff --git a/examples/ts/http-proxy/server.ts b/examples/ts/http-proxy/server.ts new file mode 100644 index 0000000000..98e6489ef0 --- /dev/null +++ b/examples/ts/http-proxy/server.ts @@ -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); +}); diff --git a/examples/ts/http-proxy/tsconfig.json b/examples/ts/http-proxy/tsconfig.json new file mode 100644 index 0000000000..d2702e156f --- /dev/null +++ b/examples/ts/http-proxy/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../../tsconfig.json", + "include": ["."], + "exclude": ["node_modules"], + "references": [ + { + "path": "../bitgo" + } + ] +} diff --git a/examples/ts/proxy/README.md b/examples/ts/proxy/README.md index 07f5ce7eb7..369003f4d2 100644 --- a/examples/ts/proxy/README.md +++ b/examples/ts/proxy/README.md @@ -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