-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmasterApiSpec.ts
More file actions
163 lines (148 loc) · 5.23 KB
/
Copy pathmasterApiSpec.ts
File metadata and controls
163 lines (148 loc) · 5.23 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
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
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 { BitGoRequest } from '../../types/request';
import { MasterExpressConfig } from '../../initConfig';
import { handleGenerateWalletOnPrem } from '../generateWallet';
import { prepareBitGo, responseHandler } from '../../shared/middleware';
import { handleSendMany } from '../handleSendMany';
// 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);
}
// 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]),
};
export const SendManyRequest = {
pubkey: t.string,
source: t.union([t.literal('user'), t.literal('backup')]),
recipients: t.array(t.any),
numBlocks: t.union([t.undefined, t.number]),
feeRate: t.union([t.undefined, t.number]),
feeMultiplier: t.union([t.undefined, t.number]),
maxFeeRate: t.union([t.undefined, t.number]),
minConfirms: t.union([t.undefined, t.number]),
enforceMinConfirmsForChange: t.union([t.undefined, t.boolean]),
targetWalletUnspents: t.union([t.undefined, t.number]),
message: t.union([t.undefined, t.string]),
minValue: t.union([t.undefined, t.union([t.number, t.string])]),
maxValue: t.union([t.undefined, t.union([t.number, t.string])]),
sequenceId: t.union([t.undefined, t.string]),
lastLedgerSequence: t.union([t.undefined, t.number]),
ledgerSequenceDelta: t.union([t.undefined, t.number]),
gasPrice: t.union([t.undefined, t.number]),
noSplitChange: t.union([t.undefined, t.boolean]),
unspents: t.union([t.undefined, t.array(t.string)]),
comment: t.union([t.undefined, t.string]),
otp: t.union([t.undefined, t.string]),
changeAddress: t.union([t.undefined, t.string]),
allowExternalChangeAddress: t.union([t.undefined, t.boolean]),
instant: t.union([t.undefined, t.boolean]),
memo: t.union([t.undefined, t.string]),
transferId: t.union([t.undefined, t.number]),
eip1559: t.union([t.undefined, t.any]),
gasLimit: t.union([t.undefined, t.number]),
custodianTransactionId: t.union([t.undefined, t.string]),
};
export const SendManyResponse: HttpResponse = {
// TODO: Get type from public types repo / Wallet Platform
200: t.any,
500: t.type({
error: t.string,
details: t.string,
}),
};
// API Specification
export const MasterApiSpec = apiSpec({
'v1.wallet.generate': {
post: httpRoute({
method: 'POST' as const,
path: '/{coin}/wallet/generate',
request: httpRequest({
params: {
coin: t.string,
},
body: GenerateWalletRequest,
}),
response: GenerateWalletResponse,
description: 'Generate a new wallet',
}),
},
'v1.wallet.sendMany': {
post: httpRoute({
method: 'POST',
path: '/{coin}/wallet/{walletId}/sendMany',
request: httpRequest({
params: {
walletId: t.string,
coin: t.string,
},
body: SendManyRequest,
}),
response: SendManyResponse,
description: 'Send many transactions',
}),
},
});
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', [
responseHandler<MasterExpressConfig>(async (req: express.Request) => {
const typedReq = req as GenericMasterApiSpecRouteRequest;
const result = await handleGenerateWalletOnPrem(typedReq);
return Response.ok(result);
}),
]);
router.post('v1.wallet.sendMany', [
responseHandler<MasterExpressConfig>(async (req: express.Request) => {
const typedReq = req as GenericMasterApiSpecRouteRequest;
const result = await handleSendMany(typedReq);
return Response.ok(result);
}),
]);
return router;
}