-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmasterApiSpec.ts
More file actions
132 lines (115 loc) · 3.79 KB
/
masterApiSpec.ts
File metadata and controls
132 lines (115 loc) · 3.79 KB
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
import * as t from 'io-ts';
import {
apiSpec,
httpRoute,
httpRequest,
HttpResponse,
Method as HttpMethod,
} from '@api-ts/io-ts-http';
import {
createRouter,
TypedRequestHandler,
type WrappedRouter,
} from '@api-ts/typed-express-router';
import { Response } from '@api-ts/response';
import express from 'express';
import { BitGo } from 'bitgo';
import { BitGoRequest } from '../../types/request';
import { MasterExpressConfig } from '../../config';
import { handleGenerateWalletOnPrem } from '../generateWallet';
import { withResponseHandler } from '../../shared/responseHandler';
// Middleware functions
export function parseBody(req: express.Request, res: express.Response, next: express.NextFunction) {
req.headers['content-type'] = req.headers['content-type'] || 'application/json';
return express.json({ limit: '20mb' })(req, res, next);
}
export function prepareBitGo(config: MasterExpressConfig) {
const { env, customRootUri } = config;
const BITGOEXPRESS_USER_AGENT = `BitGoExpress/${process.env.npm_package_version}`;
return function prepBitGo(
req: express.Request,
res: express.Response,
next: express.NextFunction,
) {
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,
accessToken,
userAgent,
};
(req as BitGoRequest).bitgo = new BitGo(bitgoConstructorParams);
(req as BitGoRequest).config = config;
next();
};
}
// Response type for /generate endpoint
const GenerateWalletResponse: HttpResponse = {
// TODO: Get type from public types repo
200: t.any,
500: t.type({
error: t.string,
details: t.string,
}),
};
// Request type for /generate endpoint
const GenerateWalletRequest = {
label: t.string,
multisigType: t.union([t.undefined, t.literal('onchain'), t.literal('tss')]),
enterprise: t.string,
disableTransactionNotifications: t.union([t.undefined, t.boolean]),
isDistributedCustody: t.union([t.undefined, t.boolean]),
};
// API Specification
export const MasterApiSpec = apiSpec({
'v1.wallet.generate': {
post: httpRoute({
method: 'POST',
path: '/{coin}/wallet/generate',
request: httpRequest({
params: {
coin: t.string,
},
body: GenerateWalletRequest,
}),
response: GenerateWalletResponse,
description: 'Generate a new wallet',
}),
},
});
export type MasterApiSpec = typeof MasterApiSpec;
export type MasterApiSpecRouteHandler<
ApiName extends keyof MasterApiSpec,
Method extends keyof MasterApiSpec[ApiName] & HttpMethod,
> = TypedRequestHandler<MasterApiSpec, ApiName, Method>;
export type MasterApiSpecRouteRequest<
ApiName extends keyof MasterApiSpec,
Method extends keyof MasterApiSpec[ApiName] & HttpMethod,
> = BitGoRequest & Parameters<MasterApiSpecRouteHandler<ApiName, Method>>[0];
export type GenericMasterApiSpecRouteRequest = MasterApiSpecRouteRequest<any, any>;
// Create router with handlers
export function createMasterApiRouter(
cfg: MasterExpressConfig,
): WrappedRouter<typeof MasterApiSpec> {
const router = createRouter(MasterApiSpec);
// Add middleware to all routes
router.use(parseBody);
router.use(prepareBitGo(cfg));
// Generate wallet endpoint handler
router.post('v1.wallet.generate', [
withResponseHandler(async (req: GenericMasterApiSpecRouteRequest) => {
const result = await handleGenerateWalletOnPrem(req);
return Response.ok(result);
}),
]);
return router;
}