-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmasterApiSpec.ts
More file actions
216 lines (198 loc) · 6.7 KB
/
Copy pathmasterApiSpec.ts
File metadata and controls
216 lines (198 loc) · 6.7 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
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
import {
apiSpec,
Method as HttpMethod,
httpRequest,
HttpResponse,
httpRoute,
} from '@api-ts/io-ts-http';
import { Response } from '@api-ts/response';
import {
createRouter,
TypedRequestHandler,
type WrappedRouter,
} from '@api-ts/typed-express-router';
import express from 'express';
import * as t from 'io-ts';
import { MasterExpressConfig } from '../../initConfig';
import { prepareBitGo, responseHandler } from '../../shared/middleware';
import { BitGoRequest } from '../../types/request';
import { handleGenerateWalletOnPrem } from '../generateWallet';
import { handleSendMany } from '../handleSendMany';
import { validateMasterExpressConfig } from '../middleware';
import { handleRecoveryWalletOnPrem } from '../recoveryWallet';
// 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,
}),
};
// Response type for /recovery endpoint
const RecoveryWalletResponse: HttpResponse = {
// TODO: Get type from public types repo
200: t.type({
txHex: t.string, // the full signed transaction hex
}),
500: t.type({
error: t.string,
details: t.string,
}),
};
// Request type for /recovery endpoint
const RecoveryWalletRequest = {
userPub: t.string,
backupPub: t.string,
walletContractAddress: t.string,
recoveryDestinationAddress: t.string,
apiKey: t.string,
coinSpecificParams: t.union([
t.undefined,
t.partial({
bitgoPub: t.union([t.undefined, t.string]),
ignoreAddressTypes: t.union([t.undefined, t.array(t.string)]),
}),
]),
};
// API Specification
export const MasterApiSpec = apiSpec({
'v1.wallet.generate': {
post: httpRoute({
method: 'POST' as const,
path: '/api/{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: '/api/{coin}/wallet/{walletId}/sendMany',
request: httpRequest({
params: {
walletId: t.string,
coin: t.string,
},
body: SendManyRequest,
}),
response: SendManyResponse,
description: 'Send many transactions',
}),
},
'v1.wallet.recovery': {
post: httpRoute({
method: 'POST',
path: '/api/{coin}/wallet/recovery',
request: httpRequest({
params: {
coin: t.string,
},
body: RecoveryWalletRequest,
}),
response: RecoveryWalletResponse,
description: 'Recover an existing 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));
router.use(validateMasterExpressConfig);
// 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);
}),
]);
router.post('v1.wallet.recovery', [
responseHandler<MasterExpressConfig>(async (req: express.Request) => {
const typedReq = req as GenericMasterApiSpecRouteRequest;
const result = await handleRecoveryWalletOnPrem(typedReq);
return Response.ok(result);
}),
]);
return router;
}