diff --git a/modules/express/src/clientRoutes.ts b/modules/express/src/clientRoutes.ts index 4d945f1405..23029f98e6 100755 --- a/modules/express/src/clientRoutes.ts +++ b/modules/express/src/clientRoutes.ts @@ -90,7 +90,7 @@ function handleLogin(req: ExpressApiRouteRequest<'express.login', 'post'>) { return req.bitgo.authenticate(body); } -function handleDecrypt(req: express.Request) { +function handleDecrypt(req: ExpressApiRouteRequest<'express.decrypt', 'post'>) { return { decrypted: req.bitgo.decrypt(req.body), }; @@ -1563,7 +1563,7 @@ export function setupAPIRoutes(app: express.Application, config: Config): void { // auth router.post('express.login', [prepareBitGo(config), typedPromiseWrapper(handleLogin)]); - app.post('/api/v[12]/decrypt', parseBody, prepareBitGo(config), promiseWrapper(handleDecrypt)); + router.post('express.decrypt', [prepareBitGo(config), typedPromiseWrapper(handleDecrypt)]); app.post('/api/v[12]/encrypt', parseBody, prepareBitGo(config), promiseWrapper(handleEncrypt)); app.post('/api/v[12]/verifyaddress', parseBody, prepareBitGo(config), promiseWrapper(handleVerifyAddress)); app.post( diff --git a/modules/express/src/typedRoutes/api/common/decrypt.ts b/modules/express/src/typedRoutes/api/common/decrypt.ts new file mode 100644 index 0000000000..62a8a0ab1e --- /dev/null +++ b/modules/express/src/typedRoutes/api/common/decrypt.ts @@ -0,0 +1,27 @@ +import * as t from 'io-ts'; +import { httpRoute, httpRequest, optional } from '@api-ts/io-ts-http'; +import { BitgoExpressError } from '../../schemas/error'; + +export const DecryptRequestBody = { + input: t.string, + password: optional(t.string), +}; + +/** + * Decrypt + * + * @operationId express.decrypt + */ +export const PostDecrypt = httpRoute({ + path: '/api/v[12]/decrypt', + method: 'POST', + request: httpRequest({ + body: DecryptRequestBody, + }), + response: { + 200: t.type({ + decrypted: t.string, + }), + 404: BitgoExpressError, + }, +}); diff --git a/modules/express/src/typedRoutes/api/index.ts b/modules/express/src/typedRoutes/api/index.ts index 4c3f7dc663..a3a6d3ea76 100644 --- a/modules/express/src/typedRoutes/api/index.ts +++ b/modules/express/src/typedRoutes/api/index.ts @@ -5,6 +5,7 @@ import * as express from 'express'; import { GetPing } from './common/ping'; import { GetPingExpress } from './common/pingExpress'; import { PostLogin } from './common/login'; +import { PostDecrypt } from './common/decrypt'; export const ExpressApi = apiSpec({ 'express.ping': { @@ -16,6 +17,9 @@ export const ExpressApi = apiSpec({ 'express.login': { post: PostLogin, }, + 'express.decrypt': { + post: PostDecrypt, + }, }); export type ExpressApi = typeof ExpressApi; diff --git a/modules/express/test/unit/typedRoutes/decode.ts b/modules/express/test/unit/typedRoutes/decode.ts index ecbba27a2f..6c1d435126 100644 --- a/modules/express/test/unit/typedRoutes/decode.ts +++ b/modules/express/test/unit/typedRoutes/decode.ts @@ -1,5 +1,6 @@ import * as assert from 'assert'; import * as t from 'io-ts'; +import { DecryptRequestBody } from '../../../src/typedRoutes/api/common/decrypt'; import { LoginRequest } from '../../../src/typedRoutes/api/common/login'; export function assertDecode(codec: t.Type, input: unknown): T { @@ -25,4 +26,17 @@ describe('io-ts decode tests', function () { password: 'password', }); }); + it('express.decrypt', function () { + // input is required field + assert.throws(() => + assertDecode(t.type(DecryptRequestBody), { + password: 'hello', + }) + ); + + assertDecode(t.type(DecryptRequestBody), { + input: 'input', + password: 'password', + }); + }); });