|
| 1 | +import * as t from 'io-ts'; |
| 2 | +import { apiSpec, httpRoute, httpRequest, HttpResponse } from '@api-ts/io-ts-http'; |
| 3 | +import { createRouter, type WrappedRouter } from '@api-ts/typed-express-router'; |
| 4 | +import { Response } from '@api-ts/response'; |
| 5 | +import express, { Request } from 'express'; |
| 6 | +import { BitGo } from 'bitgo'; |
| 7 | +import { BitGoRequest, isBitGoRequest } from '../../types/request'; |
| 8 | +import { MasterExpressConfig } from '../../config'; |
| 9 | +import { handleGenerateWalletOnPrem } from '../generateWallet'; |
| 10 | +import { withResponseHandler } from '../../shared/responseHandler'; |
| 11 | + |
| 12 | +// Middleware functions |
| 13 | +export function parseBody(req: express.Request, res: express.Response, next: express.NextFunction) { |
| 14 | + req.headers['content-type'] = req.headers['content-type'] || 'application/json'; |
| 15 | + return express.json({ limit: '20mb' })(req, res, next); |
| 16 | +} |
| 17 | + |
| 18 | +export function prepareBitGo(config: MasterExpressConfig) { |
| 19 | + const { env, customRootUri } = config; |
| 20 | + const BITGOEXPRESS_USER_AGENT = `BitGoExpress/${process.env.npm_package_version}`; |
| 21 | + |
| 22 | + return function prepBitGo( |
| 23 | + req: express.Request, |
| 24 | + res: express.Response, |
| 25 | + next: express.NextFunction, |
| 26 | + ) { |
| 27 | + let accessToken; |
| 28 | + if (req.headers.authorization) { |
| 29 | + const authSplit = req.headers.authorization.split(' '); |
| 30 | + if (authSplit.length === 2 && authSplit[0].toLowerCase() === 'bearer') { |
| 31 | + accessToken = authSplit[1]; |
| 32 | + } |
| 33 | + } |
| 34 | + const userAgent = req.headers['user-agent'] |
| 35 | + ? BITGOEXPRESS_USER_AGENT + ' ' + req.headers['user-agent'] |
| 36 | + : BITGOEXPRESS_USER_AGENT; |
| 37 | + |
| 38 | + const bitgoConstructorParams = { |
| 39 | + env, |
| 40 | + customRootURI: customRootUri, |
| 41 | + accessToken, |
| 42 | + userAgent, |
| 43 | + }; |
| 44 | + |
| 45 | + (req as BitGoRequest).bitgo = new BitGo(bitgoConstructorParams); |
| 46 | + (req as BitGoRequest).config = config; |
| 47 | + |
| 48 | + next(); |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +// Response type for /generate endpoint |
| 53 | +const GenerateWalletResponse: HttpResponse = { |
| 54 | + // TODO: Get type from public types repo |
| 55 | + 200: t.any, |
| 56 | + 500: t.type({ |
| 57 | + error: t.string, |
| 58 | + details: t.string, |
| 59 | + }), |
| 60 | +}; |
| 61 | + |
| 62 | +// Request type for /generate endpoint |
| 63 | +const GenerateWalletRequest = { |
| 64 | + label: t.string, |
| 65 | + multisigType: t.union([t.undefined, t.literal('onchain'), t.literal('tss')]), |
| 66 | + enterprise: t.string, |
| 67 | + disableTransactionNotifications: t.union([t.undefined, t.boolean]), |
| 68 | + isDistributedCustody: t.union([t.undefined, t.boolean]), |
| 69 | +}; |
| 70 | + |
| 71 | +// API Specification |
| 72 | +export const EnclavedApiSpec = apiSpec({ |
| 73 | + 'v1.wallet.generate': { |
| 74 | + post: httpRoute({ |
| 75 | + method: 'POST', |
| 76 | + path: '/{coin}/wallet/generate', |
| 77 | + request: httpRequest({ |
| 78 | + params: { |
| 79 | + coin: t.string, |
| 80 | + }, |
| 81 | + body: GenerateWalletRequest, |
| 82 | + }), |
| 83 | + response: GenerateWalletResponse, |
| 84 | + description: 'Generate a new wallet', |
| 85 | + }), |
| 86 | + }, |
| 87 | +}); |
| 88 | + |
| 89 | +// Create router with handlers |
| 90 | +export function createEnclavedApiRouter( |
| 91 | + cfg: MasterExpressConfig, |
| 92 | +): WrappedRouter<typeof EnclavedApiSpec> { |
| 93 | + const router = createRouter(EnclavedApiSpec); |
| 94 | + |
| 95 | + // Add middleware to all routes |
| 96 | + router.use(parseBody); |
| 97 | + router.use(prepareBitGo(cfg)); |
| 98 | + |
| 99 | + // Generate wallet endpoint handler |
| 100 | + router.post('v1.wallet.generate', [ |
| 101 | + withResponseHandler(async (req: BitGoRequest | Request) => { |
| 102 | + if (!isBitGoRequest(req)) { |
| 103 | + throw new Error('Invalid request type'); |
| 104 | + } |
| 105 | + const result = await handleGenerateWalletOnPrem(req); |
| 106 | + return Response.ok(result); |
| 107 | + }), |
| 108 | + ]); |
| 109 | + |
| 110 | + return router; |
| 111 | +} |
0 commit comments