-
Notifications
You must be signed in to change notification settings - Fork 269
/
clientRoutes.ts
executable file
·870 lines (785 loc) · 23.2 KB
/
clientRoutes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
/**
* @prettier
*/
import * as bodyParser from 'body-parser';
import * as bluebird from 'bluebird';
import * as url from 'url';
import * as debugLib from 'debug';
import { BitGo, Coin, Errors } from 'bitgo';
import * as _ from 'lodash';
import * as express from 'express';
// RequestTracer should be extracted into a separate npm package (along with
// the rest of the BitGoJS HTTP request machinery)
import { RequestTracer } from 'bitgo/dist/src/v2/internal/util';
import { Config } from './config';
import { ApiResponseError } from './errors';
const { version } = require('bitgo/package.json');
const pjson = require('../package.json');
const debug = debugLib('bitgo:express');
const BITGOEXPRESS_USER_AGENT = `BitGoExpress/${pjson.version} BitGoJS/${version}`;
declare module 'express-serve-static-core' {
export interface Request {
bitgo: BitGo;
}
}
function handlePing(req: express.Request, res: express.Response, next: express.NextFunction) {
return req.bitgo.ping();
}
function handlePingExpress(req: express.Request) {
return {
status: 'express server is ok!',
};
}
function handleLogin(req: express.Request) {
const username = req.body.username || req.body.email;
const body = req.body;
body.username = username;
return req.bitgo.authenticate(body);
}
function handleDecrypt(req: express.Request) {
return {
decrypted: req.bitgo.decrypt(req.body),
};
}
function handleEncrypt(req: express.Request) {
return {
encrypted: req.bitgo.encrypt(req.body),
};
}
/**
* @deprecated
* @param req
*/
function handleVerifyAddress(req: express.Request) {
return {
verified: req.bitgo.verifyAddress(req.body),
};
}
/**
* @deprecated
* @param req
*/
function handleCreateLocalKeyChain(req: express.Request) {
return req.bitgo.keychains().create(req.body);
}
/**
* @deprecated
* @param req
*/
function handleDeriveLocalKeyChain(req: express.Request) {
return req.bitgo.keychains().deriveLocal(req.body);
}
/**
* @deprecated
* @param req
*/
function handleCreateWalletWithKeychains(req: express.Request) {
return req.bitgo.wallets().createWalletWithKeychains(req.body);
}
/**
* @deprecated
* @param req
*/
function handleSendCoins(req: express.Request) {
return req.bitgo
.wallets()
.get({ id: req.params.id })
.then(function(wallet) {
return wallet.sendCoins(req.body);
})
.catch(function(err) {
err.status = 400;
throw err;
})
.then(function(result) {
if (result.status === 'pendingApproval') {
throw apiResponse(202, result);
}
return result;
});
}
/**
* @deprecated
* @param req
*/
function handleSendMany(req: express.Request) {
return req.bitgo
.wallets()
.get({ id: req.params.id })
.then(function(wallet) {
return wallet.sendMany(req.body);
})
.catch(function(err) {
err.status = 400;
throw err;
})
.then(function(result) {
if (result.status === 'pendingApproval') {
throw apiResponse(202, result);
}
return result;
});
}
/**
* @deprecated
* @param req
*/
function handleCreateTransaction(req: express.Request) {
return req.bitgo
.wallets()
.get({ id: req.params.id })
.then(function(wallet) {
return wallet.createTransaction(req.body);
})
.catch(function(err) {
err.status = 400;
throw err;
});
}
/**
* @deprecated
* @param req
*/
function handleSignTransaction(req: express.Request) {
return req.bitgo
.wallets()
.get({ id: req.params.id })
.then(function(wallet) {
return wallet.signTransaction(req.body);
});
}
/**
* @deprecated
* @param req
*/
function handleShareWallet(req: express.Request) {
return req.bitgo
.wallets()
.get({ id: req.params.id })
.then(function(wallet) {
return wallet.shareWallet(req.body);
});
}
/**
* @deprecated
* @param req
*/
function handleAcceptShare(req: express.Request) {
const params = req.body || {};
params.walletShareId = req.params.shareId;
return req.bitgo.wallets().acceptShare(params);
}
/**
* @deprecated
* @param req
*/
function handleApproveTransaction(req: express.Request) {
const params = req.body || {};
return req.bitgo
.pendingApprovals()
.get({ id: req.params.id })
.then(function(pendingApproval) {
if (params.state === 'approved') {
return pendingApproval.approve(params);
}
return pendingApproval.reject(params);
});
}
/**
* @deprecated
* @param req
*/
function handleConstructApprovalTx(req: express.Request) {
const params = req.body || {};
return req.bitgo
.pendingApprovals()
.get({ id: req.params.id })
.then(function(pendingApproval) {
return pendingApproval.constructApprovalTx(params);
});
}
/**
* @deprecated
* @param req
*/
function handleConsolidateUnspents(req: express.Request) {
return req.bitgo
.wallets()
.get({ id: req.params.id })
.then(function(wallet) {
return wallet.consolidateUnspents(req.body);
});
}
/**
* @deprecated
* @param req
*/
function handleFanOutUnspents(req: express.Request) {
return req.bitgo
.wallets()
.get({ id: req.params.id })
.then(function(wallet) {
return wallet.fanOutUnspents(req.body);
});
}
/**
* @deprecated
* @param req
*/
function handleCalculateMinerFeeInfo(req: express.Request) {
return req.bitgo.calculateMinerFeeInfo({
bitgo: req.bitgo,
feeRate: req.body.feeRate,
nP2shInputs: req.body.nP2shInputs,
nP2pkhInputs: req.body.nP2pkhInputs,
nP2shP2wshInputs: req.body.nP2shP2wshInputs,
nOutputs: req.body.nOutputs,
});
}
/**
* Builds the API's URL string, optionally building the querystring if parameters exist
* @param req
* @return {string}
*/
function createAPIPath(req: express.Request) {
let apiPath = '/' + req.params[0];
if (!_.isEmpty(req.query)) {
// req.params does not contain the querystring, so we manually add them here
const urlDetails = url.parse(req.url);
if (urlDetails.search) {
// "search" is the properly URL encoded query params, prefixed with "?"
apiPath += urlDetails.search;
}
}
return apiPath;
}
/**
* handle any other V1 API call
* @deprecated
* @param req
* @param res
* @param next
*/
function handleREST(req: express.Request, res: express.Response, next: express.NextFunction) {
const method = req.method;
const bitgo = req.bitgo;
const bitgoURL = bitgo.url(createAPIPath(req));
return redirectRequest(bitgo, method, bitgoURL, req, next);
}
/**
* handle any other V2 API call
* @param req
* @param res
* @param next
*/
function handleV2UserREST(req: express.Request, res: express.Response, next: express.NextFunction) {
const method = req.method;
const bitgo = req.bitgo;
const bitgoURL = bitgo.url('/user' + createAPIPath(req), 2);
return redirectRequest(bitgo, method, bitgoURL, req, next);
}
/**
* handle v2 address validation
* @param req
*/
function handleV2VerifyAddress(req: express.Request): { isValid: boolean } {
if (!_.isString(req.body.address)) {
throw new Error('Expected address to be a string');
}
if (req.body.supportOldScriptHashVersion !== undefined && !_.isBoolean(req.body.supportOldScriptHashVersion)) {
throw new Error('Expected supportOldScriptHashVersion to be a boolean.');
}
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
if (coin instanceof Coin.AbstractUtxoCoin) {
return {
isValid: coin.isValidAddress(req.body.address, !!req.body.supportOldScriptHashVersion),
};
}
return {
isValid: coin.isValidAddress(req.body.address),
};
}
/**
* handle address canonicalization
* @param req
*/
function handleCanonicalAddress(req: express.Request) {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
if (!['ltc', 'bch', 'bsv'].includes(coin.getFamily())) {
throw new Error('only Litecoin/Bitcoin Cash/Bitcoin SV address canonicalization is supported');
}
const address = req.body.address;
const fallbackVersion = req.body.scriptHashVersion; // deprecate
const version = req.body.version;
return (coin as Coin.Bch | Coin.Bsv | Coin.Ltc).canonicalAddress(address, version || fallbackVersion);
}
/**
* handle new wallet creation
* @param req
*/
async function handleV2GenerateWallet(req: express.Request) {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
const result = await coin.wallets().generateWallet(req.body);
// @ts-ignore
return result.wallet._wallet;
}
/**
* handle v2 approve transaction
* @param req
*/
async function handleV2PendingApproval(req: express.Request): Promise<any> {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
const params = req.body || {};
const pendingApproval = await coin.pendingApprovals().get({ id: req.params.id });
if (params.state === 'approved') {
return pendingApproval.approve(params);
}
return pendingApproval.reject(params);
}
/**
* create a keychain
* @param req
*/
function handleV2CreateLocalKeyChain(req: express.Request) {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
return coin.keychains().create(req.body);
}
/**
* handle wallet share
* @param req
*/
async function handleV2ShareWallet(req: express.Request) {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
const wallet = await coin.wallets().get({ id: req.params.id });
return wallet.shareWallet(req.body);
}
/**
* handle accept wallet share
* @param req
*/
async function handleV2AcceptWalletShare(req: express.Request) {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
const params = _.extend({}, req.body, { walletShareId: req.params.id });
return coin.wallets().acceptShare(params);
}
/**
* handle wallet sign transaction
*/
async function handleV2SignTxWallet(req: express.Request) {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
const wallet = await coin.wallets().get({ id: req.params.id });
return wallet.signTransaction(req.body);
}
/**
* handle sign transaction
* @param req
*/
function handleV2SignTx(req: express.Request) {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
return coin.signTransaction(req.body);
}
/**
* handle wallet recover token
* @param req
*/
async function handleV2RecoverToken(req: express.Request) {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
const wallet = await coin.wallets().get({ id: req.params.id });
return wallet.recoverToken(req.body);
}
/**
* handle wallet fanout unspents
* @param req
*/
async function handleV2ConsolidateUnspents(req: express.Request) {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
const wallet = await coin.wallets().get({ id: req.params.id });
return wallet.consolidateUnspents(req.body);
}
/**
* handle wallet fanout unspents
* @param req
*/
async function handleV2FanOutUnspents(req: express.Request) {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
const wallet = await coin.wallets().get({ id: req.params.id });
return wallet.fanoutUnspents(req.body);
}
/**
* handle wallet sweep
* @param req
*/
async function handleV2Sweep(req: express.Request) {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
const wallet = await coin.wallets().get({ id: req.params.id });
return wallet.sweep(req.body);
}
/**
* handle CPFP accelerate transaction creation
* @param req
*/
async function handleV2AccelerateTransaction(req: express.Request) {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
const wallet = await coin.wallets().get({ id: req.params.id });
return wallet.accelerateTransaction(req.body);
}
/**
* handle send one
* @param req
*/
async function handleV2SendOne(req: express.Request) {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
const reqId = new RequestTracer();
const wallet = await coin.wallets().get({ id: req.params.id, reqId });
req.body.reqId = reqId;
let result;
try {
result = await wallet.send(req.body);
} catch (err) {
err.status = 400;
throw err;
}
if (result.status === 'pendingApproval') {
throw apiResponse(202, result);
}
return result;
}
/**
* handle send many
* @param req
*/
async function handleV2SendMany(req: express.Request) {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
const reqId = new RequestTracer();
const wallet = await coin.wallets().get({ id: req.params.id, reqId });
req.body.reqId = reqId;
let result;
try {
result = await wallet.sendMany(req.body);
} catch (err) {
err.status = 400;
throw err;
}
if (result.status === 'pendingApproval') {
throw apiResponse(202, result);
}
return result;
}
/**
* handle any other API call
* @param req
* @param res
* @param next
*/
function handleV2CoinSpecificREST(req: express.Request, res: express.Response, next: express.NextFunction) {
const method = req.method;
const bitgo = req.bitgo;
try {
const coin = bitgo.coin(req.params.coin);
const coinURL = coin.url(createAPIPath(req));
return redirectRequest(bitgo, method, coinURL, req, next);
} catch (e) {
if (e instanceof Errors.UnsupportedCoinError) {
const queryParams = _.transform(
req.query,
(acc: string[], value, key) => {
for (const val of _.castArray(value)) {
acc.push(`${key}=${val}`);
}
},
[]
);
const baseUrl = bitgo.url(req.baseUrl.replace(/^\/api\/v2/, ''), 2);
const url = _.isEmpty(queryParams) ? baseUrl : `${baseUrl}?${queryParams.join('&')}`;
debug(`coin ${req.params.coin} not supported, attempting to handle as a coinless route with url ${url}`);
return redirectRequest(bitgo, method, url, req, next);
}
throw e;
}
}
/**
* Redirect a request using the bitgo request functions
* @param bitgo
* @param method
* @param url
* @param req
* @param next
*/
function redirectRequest(bitgo: BitGo, method: string, url: string, req: express.Request, next: express.NextFunction) {
switch (method) {
case 'GET':
return bitgo
.get(url)
.result()
.nodeify();
case 'POST':
return bitgo
.post(url)
.send(req.body)
.result()
.nodeify();
case 'PUT':
return bitgo
.put(url)
.send(req.body)
.result()
.nodeify();
case 'DELETE':
return bitgo
.del(url)
.send(req.body)
.result()
.nodeify();
}
// something has presumably gone wrong
next();
}
/**
*
* @param status
* @param result
* @param message
*/
function apiResponse(status: number, result: any, message?: string): ApiResponseError {
return new ApiResponseError(message, status, result);
}
const expressJSONParser = bodyParser.json({ limit: '20mb' });
/**
* Perform body parsing here only on routes we want
*/
function parseBody(req: express.Request, res: express.Response, next: express.NextFunction) {
// Set the default Content-Type, in case the client doesn't set it. If
// Content-Type isn't specified, Express silently refuses to parse the
// request body.
req.headers['content-type'] = req.headers['content-type'] || 'application/json';
return expressJSONParser(req, res, next);
}
/**
* Create the bitgo object in the request
* @param config
*/
function prepareBitGo(config: Config) {
const { env, customRootUri, customBitcoinNetwork } = config;
return function(req: express.Request, res: express.Response, next: express.NextFunction) {
// Get access token
let accessToken;
if (req.headers.authorization) {
const authSplit = req.headers.authorization.split(' ');
if (authSplit.length === 2 && authSplit[0].toLowerCase() === 'bearer') {
accessToken = authSplit[1];
}
}
const userAgent = req.headers['user-agent']
? BITGOEXPRESS_USER_AGENT + ' ' + req.headers['user-agent']
: BITGOEXPRESS_USER_AGENT;
const bitgoConstructorParams = {
env,
customRootURI: customRootUri,
customBitcoinNetwork,
accessToken,
userAgent,
};
req.bitgo = new BitGo(bitgoConstructorParams);
(req.bitgo as any)._promise.longStackSupport = true;
next();
};
}
/**
* Promise handler wrapper to handle sending responses and error cases
* @param promiseRequestHandler
*/
function promiseWrapper(promiseRequestHandler: Function) {
return function(req: express.Request, res: express.Response, next: express.NextFunction) {
debug(`handle: ${req.method} ${req.originalUrl}`);
bluebird
.try(promiseRequestHandler.bind(null, req, res, next))
.then(function(result: any) {
let status = 200;
if (result.__redirect) {
res.redirect(result.url);
status = 302;
} else if (result.__render) {
res.render(result.template, result.params);
} else {
res.status(status).send(result);
}
})
.catch(function(caught) {
let err;
if (caught instanceof Error) {
err = caught;
} else if (typeof caught === 'string') {
err = new Error('(string_error) ' + caught);
} else {
err = new Error('(object_error) ' + JSON.stringify(caught));
}
const message = err.message || 'local error';
// use attached result, or make one
let result = err.result || { error: message };
result = _.extend({}, result);
result.message = err.message;
const status = err.status || 500;
if (!(status >= 200 && status < 300)) {
console.log('error %s: %s', status, err.message);
}
if (status === 500) {
console.log(err.stack);
}
res.status(status).send(result);
})
.done();
};
}
export function setupRoutes(app: express.Application, config: Config) {
// When adding new routes to BitGo Express make sure that you also add the exact same routes to the server. Since
// some customers were confused when calling a BitGo Express route on the BitGo server, we now handle all BitGo
// Express routes on the BitGo server and return an error message that says that one should call BitGo Express
// instead.
// V1 routes should be added to www/config/routes.js
// V2 routes should be added to www/config/routesV2.js
// ping
// /api/v[12]/pingexpress is the only exception to the rule above, as it explicitly checks the health of the
// express server without running into rate limiting with the BitGo server.
app.get('/api/v[12]/ping', prepareBitGo(config), promiseWrapper(handlePing));
app.get('/api/v[12]/pingexpress', promiseWrapper(handlePingExpress));
// auth
app.post('/api/v[12]/user/login', parseBody, prepareBitGo(config), promiseWrapper(handleLogin));
app.post('/api/v[12]/decrypt', parseBody, prepareBitGo(config), promiseWrapper(handleDecrypt));
app.post('/api/v[12]/encrypt', parseBody, prepareBitGo(config), promiseWrapper(handleEncrypt));
app.post('/api/v[12]/verifyaddress', parseBody, prepareBitGo(config), promiseWrapper(handleVerifyAddress));
app.post(
'/api/v[12]/calculateminerfeeinfo',
parseBody,
prepareBitGo(config),
promiseWrapper(handleCalculateMinerFeeInfo)
);
app.post('/api/v1/keychain/local', parseBody, prepareBitGo(config), promiseWrapper(handleCreateLocalKeyChain));
app.post('/api/v1/keychain/derive', parseBody, prepareBitGo(config), promiseWrapper(handleDeriveLocalKeyChain));
app.post(
'/api/v1/wallets/simplecreate',
parseBody,
prepareBitGo(config),
promiseWrapper(handleCreateWalletWithKeychains)
);
app.post('/api/v1/wallet/:id/sendcoins', parseBody, prepareBitGo(config), promiseWrapper(handleSendCoins));
app.post('/api/v1/wallet/:id/sendmany', parseBody, prepareBitGo(config), promiseWrapper(handleSendMany));
app.post(
'/api/v1/wallet/:id/createtransaction',
parseBody,
prepareBitGo(config),
promiseWrapper(handleCreateTransaction)
);
app.post(
'/api/v1/wallet/:id/signtransaction',
parseBody,
prepareBitGo(config),
promiseWrapper(handleSignTransaction)
);
app.post('/api/v1/wallet/:id/simpleshare', parseBody, prepareBitGo(config), promiseWrapper(handleShareWallet));
app.post(
'/api/v1/walletshare/:shareId/acceptShare',
parseBody,
prepareBitGo(config),
promiseWrapper(handleAcceptShare)
);
app.put(
'/api/v1/pendingapprovals/:id/express',
parseBody,
prepareBitGo(config),
promiseWrapper(handleApproveTransaction)
);
app.put(
'/api/v1/pendingapprovals/:id/constructTx',
parseBody,
prepareBitGo(config),
promiseWrapper(handleConstructApprovalTx)
);
app.put(
'/api/v1/wallet/:id/consolidateunspents',
parseBody,
prepareBitGo(config),
promiseWrapper(handleConsolidateUnspents)
);
app.put('/api/v1/wallet/:id/fanoutunspents', parseBody, prepareBitGo(config), promiseWrapper(handleFanOutUnspents));
// any other API call
app.use('/api/v[1]/*', parseBody, prepareBitGo(config), promiseWrapper(handleREST));
// API v2
// create keychain
app.post(
'/api/v2/:coin/keychain/local',
parseBody,
prepareBitGo(config),
promiseWrapper(handleV2CreateLocalKeyChain)
);
// generate wallet
app.post('/api/v2/:coin/wallet/generate', parseBody, prepareBitGo(config), promiseWrapper(handleV2GenerateWallet));
// share wallet
app.post('/api/v2/:coin/wallet/:id/share', parseBody, prepareBitGo(config), promiseWrapper(handleV2ShareWallet));
app.post(
'/api/v2/:coin/walletshare/:id/acceptshare',
parseBody,
prepareBitGo(config),
promiseWrapper(handleV2AcceptWalletShare)
);
// sign transaction
app.post('/api/v2/:coin/signtx', parseBody, prepareBitGo(config), promiseWrapper(handleV2SignTx));
app.post('/api/v2/:coin/wallet/:id/signtx', parseBody, prepareBitGo(config), promiseWrapper(handleV2SignTxWallet));
app.post(
'/api/v2/:coin/wallet/:id/recovertoken',
parseBody,
prepareBitGo(config),
promiseWrapper(handleV2RecoverToken)
);
// send transaction
app.post('/api/v2/:coin/wallet/:id/sendcoins', parseBody, prepareBitGo(config), promiseWrapper(handleV2SendOne));
app.post('/api/v2/:coin/wallet/:id/sendmany', parseBody, prepareBitGo(config), promiseWrapper(handleV2SendMany));
// unspent changes
app.post(
'/api/v2/:coin/wallet/:id/consolidateunspents',
parseBody,
prepareBitGo(config),
promiseWrapper(handleV2ConsolidateUnspents)
);
app.post(
'/api/v2/:coin/wallet/:id/fanoutunspents',
parseBody,
prepareBitGo(config),
promiseWrapper(handleV2FanOutUnspents)
);
app.post('/api/v2/:coin/wallet/:id/sweep', parseBody, prepareBitGo(config), promiseWrapper(handleV2Sweep));
// CPFP
app.post(
'/api/v2/:coin/wallet/:id/acceleratetx',
parseBody,
prepareBitGo(config),
promiseWrapper(handleV2AccelerateTransaction)
);
// Miscellaneous
app.post('/api/v2/:coin/canonicaladdress', parseBody, prepareBitGo(config), promiseWrapper(handleCanonicalAddress));
app.post('/api/v2/:coin/verifyaddress', parseBody, prepareBitGo(config), promiseWrapper(handleV2VerifyAddress));
app.put(
'/api/v2/:coin/pendingapprovals/:id',
parseBody,
prepareBitGo(config),
promiseWrapper(handleV2PendingApproval)
);
// any other API v2 call
app.use('/api/v2/user/*', parseBody, prepareBitGo(config), promiseWrapper(handleV2UserREST));
app.use('/api/v2/:coin/*', parseBody, prepareBitGo(config), promiseWrapper(handleV2CoinSpecificREST));
}