|
| 1 | +import * as t from 'io-ts'; |
| 2 | +import { |
| 3 | + apiSpec, |
| 4 | + httpRoute, |
| 5 | + httpRequest, |
| 6 | + HttpResponse, |
| 7 | + Method as HttpMethod, |
| 8 | +} from '@api-ts/io-ts-http'; |
| 9 | +import { |
| 10 | + createRouter, |
| 11 | + type WrappedRouter, |
| 12 | + TypedRequestHandler, |
| 13 | +} from '@api-ts/typed-express-router'; |
| 14 | +import { Response } from '@api-ts/response'; |
| 15 | +import express from 'express'; |
| 16 | +import { BitGoRequest } from '../../types/request'; |
| 17 | +import { EnclavedConfig } from '../../types'; |
| 18 | +import { postIndependentKey } from '../../api/enclaved/postIndependentKey'; |
| 19 | +import { signMultisigTransaction } from '../../api/enclaved/signMultisigTransaction'; |
| 20 | +import { prepareBitGo, responseHandler } from '../../shared/middleware'; |
| 21 | + |
| 22 | +// Request type for /key/independent endpoint |
| 23 | +const IndependentKeyRequest = { |
| 24 | + source: t.string, |
| 25 | + seed: t.union([t.undefined, t.string]), |
| 26 | +}; |
| 27 | + |
| 28 | +// Response type for /key/independent endpoint |
| 29 | +const IndependentKeyResponse: HttpResponse = { |
| 30 | + // TODO: Define proper response type |
| 31 | + 200: t.any, |
| 32 | + 500: t.type({ |
| 33 | + error: t.string, |
| 34 | + details: t.string, |
| 35 | + }), |
| 36 | +}; |
| 37 | + |
| 38 | +// Request type for /multisig/sign endpoint |
| 39 | +const SignMultisigRequest = { |
| 40 | + source: t.string, |
| 41 | + pub: t.string, |
| 42 | + txPrebuild: t.any, // TransactionPrebuild type from BitGo |
| 43 | +}; |
| 44 | + |
| 45 | +// Response type for /multisig/sign endpoint |
| 46 | +const SignMultisigResponse: HttpResponse = { |
| 47 | + // TODO: Define proper response type for signed multisig transaction |
| 48 | + 200: t.any, |
| 49 | + 500: t.type({ |
| 50 | + error: t.string, |
| 51 | + details: t.string, |
| 52 | + }), |
| 53 | +}; |
| 54 | + |
| 55 | +// API Specification |
| 56 | +export const EnclavedAPiSpec = apiSpec({ |
| 57 | + 'v1.multisig.sign': { |
| 58 | + post: httpRoute({ |
| 59 | + method: 'POST', |
| 60 | + path: '/{coin}/multisig/sign', |
| 61 | + request: httpRequest({ |
| 62 | + params: { |
| 63 | + coin: t.string, |
| 64 | + }, |
| 65 | + body: SignMultisigRequest, |
| 66 | + }), |
| 67 | + response: SignMultisigResponse, |
| 68 | + description: 'Sign a multisig transaction', |
| 69 | + }), |
| 70 | + }, |
| 71 | + 'v1.key.independent': { |
| 72 | + post: httpRoute({ |
| 73 | + method: 'POST', |
| 74 | + path: '/{coin}/key/independent', |
| 75 | + request: httpRequest({ |
| 76 | + params: { |
| 77 | + coin: t.string, |
| 78 | + }, |
| 79 | + body: IndependentKeyRequest, |
| 80 | + }), |
| 81 | + response: IndependentKeyResponse, |
| 82 | + description: 'Generate an independent key', |
| 83 | + }), |
| 84 | + }, |
| 85 | +}); |
| 86 | + |
| 87 | +export type EnclavedApiSpecRouteHandler< |
| 88 | + ApiName extends keyof typeof EnclavedAPiSpec, |
| 89 | + Method extends keyof (typeof EnclavedAPiSpec)[ApiName] & HttpMethod, |
| 90 | +> = TypedRequestHandler<typeof EnclavedAPiSpec, ApiName, Method>; |
| 91 | + |
| 92 | +export type EnclavedApiSpecRouteRequest< |
| 93 | + ApiName extends keyof typeof EnclavedAPiSpec, |
| 94 | + Method extends keyof (typeof EnclavedAPiSpec)[ApiName] & HttpMethod, |
| 95 | +> = BitGoRequest<EnclavedConfig> & Parameters<EnclavedApiSpecRouteHandler<ApiName, Method>>[0]; |
| 96 | + |
| 97 | +export type GenericEnclavedApiSpecRouteRequest = EnclavedApiSpecRouteRequest<any, any>; |
| 98 | + |
| 99 | +// Create router with handlers |
| 100 | +export function createKeyGenRouter(config: EnclavedConfig): WrappedRouter<typeof EnclavedAPiSpec> { |
| 101 | + const router = createRouter(EnclavedAPiSpec); |
| 102 | + // Add middleware |
| 103 | + router.use(express.json()); |
| 104 | + router.use(prepareBitGo(config)); |
| 105 | + |
| 106 | + // Independent key generation endpoint handler |
| 107 | + router.post('v1.key.independent', [ |
| 108 | + responseHandler<EnclavedConfig>(async (req) => { |
| 109 | + const typedReq = req as EnclavedApiSpecRouteRequest<'v1.key.independent', 'post'>; |
| 110 | + const result = await postIndependentKey(typedReq); |
| 111 | + return Response.ok(result); |
| 112 | + }), |
| 113 | + ]); |
| 114 | + |
| 115 | + // Multisig transaction signing endpoint handler |
| 116 | + router.post('v1.multisig.sign', [ |
| 117 | + responseHandler<EnclavedConfig>(async (req) => { |
| 118 | + const typedReq = req as EnclavedApiSpecRouteRequest<'v1.multisig.sign', 'post'>; |
| 119 | + const result = await signMultisigTransaction(typedReq); |
| 120 | + return Response.ok(result); |
| 121 | + }), |
| 122 | + ]); |
| 123 | + |
| 124 | + return router; |
| 125 | +} |
0 commit comments