Skip to content

Commit 4bd5f9e

Browse files
committed
feat: add token enablement support in express
adds token enablement support in express. also adds examples for enabling tokens for SOL. Ticket: BG-60201
1 parent b517867 commit 4bd5f9e

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Enables tokens for a Solana wallet.
3+
*
4+
* This will create associated token addresses that belong to your wallet's root address.
5+
*
6+
* Copyright 2022, BitGo, Inc. All Rights Reserved.
7+
*/
8+
const BitGo = require('bitgo');
9+
const bitgo = new BitGo.BitGo({
10+
env: 'custom',
11+
customRootURI: '',
12+
});
13+
14+
// TODO: set your access token here
15+
// You can get this from User Settings > Developer Options > Add Access Token
16+
const accessToken = '';
17+
18+
// TODO: set the id of your wallet
19+
const walletId = '';
20+
21+
// TODO: set your passphrase for your new wallet here
22+
const passphrase = 'test_wallet_passphrase';
23+
24+
const coin = 'tsol';
25+
26+
// Enable tokens for your root wallet
27+
async function main() {
28+
bitgo.authenticateWithAccessToken({ accessToken });
29+
const wallet = await bitgo.coin(coin).wallets().get({ id: walletId });
30+
31+
const enableTokens = await wallet.sendTokenEnablements({
32+
enableTokens: [
33+
// TODO: provide name of tokens you want to enable
34+
{ name: 'tsol:usdc' },
35+
{ name: 'tsol:usdt' },
36+
],
37+
walletPassphrase: passphrase,
38+
});
39+
40+
console.log(JSON.stringify(enableTokens, undefined, 2));
41+
}
42+
43+
main().catch((e) => console.error(e));
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Enables tokens for a Solana wallet.
3+
*
4+
* This will create associated token addresses that belong to one of your wallet's receive addresses.
5+
*
6+
* Copyright 2022, BitGo, Inc. All Rights Reserved.
7+
*/
8+
const BitGo = require('bitgo');
9+
const bitgo = new BitGo.BitGo({
10+
env: 'custom',
11+
customRootURI: '',
12+
});
13+
14+
// TODO: set your access token here
15+
// You can get this from User Settings > Developer Options > Add Access Token
16+
const accessToken = '';
17+
18+
// TODO: set the id of your wallet
19+
const walletId = '';
20+
21+
// TODO: set the receive address (actual address, not the id) to enable tokens for
22+
const receiveAddress = '';
23+
24+
// TODO: set your passphrase for your new wallet here
25+
const passphrase = 'test_wallet_passphrase';
26+
27+
const coin = 'tsol';
28+
29+
// Enable tokens for a receive address
30+
async function main() {
31+
bitgo.authenticateWithAccessToken({ accessToken });
32+
const wallet = await bitgo.coin(coin).wallets().get({ id: walletId });
33+
34+
const enableTokens = await wallet.sendTokenEnablements({
35+
enableTokens: [
36+
// TODO: provide name of tokens you want to enable
37+
{ name: 'tsol:usdc', address: receiveAddress },
38+
{ name: 'tsol:usdt', address: receiveAddress },
39+
],
40+
walletPassphrase: passphrase,
41+
});
42+
43+
console.log(JSON.stringify(enableTokens, undefined, 2));
44+
}
45+
46+
main().catch((e) => console.error(e));

modules/express/src/clientRoutes.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,24 @@ export async function handleV2PrebuildAndSignTransaction(req: express.Request):
762762
return result;
763763
}
764764

765+
/**
766+
* Enables tokens on a wallet
767+
* @param req
768+
*/
769+
export async function handleV2EnableTokens(req: express.Request) {
770+
const bitgo = req.bitgo;
771+
const coin = bitgo.coin(req.params.coin);
772+
const reqId = new RequestTracer();
773+
const wallet = await coin.wallets().get({ id: req.params.id, reqId });
774+
req.body.reqId = reqId;
775+
try {
776+
return wallet.sendTokenEnablements(createSendParams(req));
777+
} catch (err) {
778+
err.status = 400;
779+
throw err;
780+
}
781+
}
782+
765783
/**
766784
* handle any other API call
767785
* @param req
@@ -1121,6 +1139,14 @@ export function setupAPIRoutes(app: express.Application, config: Config): void {
11211139
promiseWrapper(handleV2PrebuildAndSignTransaction)
11221140
);
11231141

1142+
// token enablement
1143+
app.post(
1144+
'/api/v2/:coin/wallet/:id/enableTokens',
1145+
parseBody,
1146+
prepareBitGo(config),
1147+
promiseWrapper(handleV2EnableTokens)
1148+
);
1149+
11241150
// unspent changes
11251151
app.post(
11261152
'/api/v2/:coin/wallet/:id/consolidateunspents',
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as sinon from 'sinon';
2+
3+
import 'should-http';
4+
import 'should-sinon';
5+
import '../../lib/asserts';
6+
7+
import * as express from 'express';
8+
9+
import { handleV2EnableTokens } from '../../../src/clientRoutes';
10+
11+
import { BitGo } from 'bitgo';
12+
13+
describe('Enable tokens', () => {
14+
it('should enable tokens', async () => {
15+
const walletStub = { sendTokenEnablements: async () => Promise.resolve('success') };
16+
const coinStub = {
17+
wallets: () => ({ get: () => Promise.resolve(walletStub) }),
18+
};
19+
const stubBitgo = sinon.createStubInstance(BitGo as any, { coin: coinStub });
20+
21+
const mockRequest = {
22+
bitgo: stubBitgo,
23+
params: {
24+
coin: 'tbtc',
25+
id: '23423423423423',
26+
},
27+
body: {
28+
enableTokens: [
29+
{ name: 'tsol:usdc' },
30+
{ name: 'tsol:usdt' },
31+
],
32+
},
33+
} as unknown as express.Request;
34+
35+
await handleV2EnableTokens(mockRequest).should.be.resolvedWith('success');
36+
});
37+
});

0 commit comments

Comments
 (0)