Skip to content

Commit 48e162d

Browse files
feat(express): migrate encrypt to typed routes
Ticket: WP-5398
1 parent a6f979b commit 48e162d

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

modules/express/src/clientRoutes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function handleDecrypt(req: ExpressApiRouteRequest<'express.decrypt', 'post'>) {
9696
};
9797
}
9898

99-
function handleEncrypt(req: express.Request) {
99+
function handleEncrypt(req: ExpressApiRouteRequest<'express.encrypt', 'post'>) {
100100
return {
101101
encrypted: req.bitgo.encrypt(req.body),
102102
};
@@ -1564,7 +1564,7 @@ export function setupAPIRoutes(app: express.Application, config: Config): void {
15641564
router.post('express.login', [prepareBitGo(config), typedPromiseWrapper(handleLogin)]);
15651565

15661566
router.post('express.decrypt', [prepareBitGo(config), typedPromiseWrapper(handleDecrypt)]);
1567-
app.post('/api/v[12]/encrypt', parseBody, prepareBitGo(config), promiseWrapper(handleEncrypt));
1567+
router.post('express.encrypt', [prepareBitGo(config), typedPromiseWrapper(handleEncrypt)]);
15681568
router.post('express.verifyaddress', [prepareBitGo(config), typedPromiseWrapper(handleVerifyAddress)]);
15691569
app.post(
15701570
'/api/v[12]/calculateminerfeeinfo',
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as t from 'io-ts';
2+
import { httpRoute, httpRequest, optional } from '@api-ts/io-ts-http';
3+
import { BitgoExpressError } from '../../schemas/error';
4+
5+
export const EncryptRequestBody = {
6+
input: t.string,
7+
password: optional(t.string),
8+
adata: optional(t.string),
9+
};
10+
11+
/**
12+
* Encrypt
13+
*
14+
* @operationId express.encrypt
15+
*/
16+
export const PostEncrypt = httpRoute({
17+
path: '/api/v[12]/encrypt',
18+
method: 'POST',
19+
request: httpRequest({
20+
body: EncryptRequestBody,
21+
}),
22+
response: {
23+
200: t.type({
24+
encrypted: t.string,
25+
}),
26+
404: BitgoExpressError,
27+
},
28+
});

modules/express/src/typedRoutes/api/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { GetPing } from './common/ping';
66
import { GetPingExpress } from './common/pingExpress';
77
import { PostLogin } from './common/login';
88
import { PostDecrypt } from './common/decrypt';
9+
import { PostEncrypt } from './common/encrypt';
910
import { PostVerifyAddress } from './common/verifyAddress';
1011
import { PostAcceptShare } from './v1/acceptShare';
1112
import { PostSimpleCreate } from './v1/simpleCreate';
@@ -25,6 +26,9 @@ export const ExpressApi = apiSpec({
2526
'express.decrypt': {
2627
post: PostDecrypt,
2728
},
29+
'express.encrypt': {
30+
post: PostEncrypt,
31+
},
2832
'express.verifyaddress': {
2933
post: PostVerifyAddress,
3034
},

modules/express/test/unit/typedRoutes/decode.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as assert from 'assert';
22
import * as t from 'io-ts';
33
import { DecryptRequestBody } from '../../../src/typedRoutes/api/common/decrypt';
4+
import { EncryptRequestBody } from '../../../src/typedRoutes/api/common/encrypt';
45
import { LoginRequest } from '../../../src/typedRoutes/api/common/login';
56
import { VerifyAddressBody } from '../../../src/typedRoutes/api/common/verifyAddress';
67
import { SimpleCreateRequestBody } from '../../../src/typedRoutes/api/v1/simpleCreate';
@@ -41,6 +42,46 @@ describe('io-ts decode tests', function () {
4142
password: 'password',
4243
});
4344
});
45+
it('express.encrypt', function () {
46+
// input is required field
47+
assert.throws(() =>
48+
assertDecode(t.type(EncryptRequestBody), {
49+
password: 'password',
50+
})
51+
);
52+
53+
// input must be a string
54+
assert.throws(() =>
55+
assertDecode(t.type(EncryptRequestBody), {
56+
input: 123,
57+
})
58+
);
59+
60+
// valid with just input
61+
assertDecode(t.type(EncryptRequestBody), {
62+
input: 'data to encrypt',
63+
});
64+
65+
// valid with input and password
66+
assertDecode(t.type(EncryptRequestBody), {
67+
input: 'data to encrypt',
68+
password: 'password',
69+
});
70+
71+
// valid with all fields
72+
assertDecode(t.type(EncryptRequestBody), {
73+
input: 'data to encrypt',
74+
password: 'password',
75+
adata: 'additional authenticated data',
76+
});
77+
78+
// password and adata are optional, should accept undefined
79+
assertDecode(t.type(EncryptRequestBody), {
80+
input: 'data to encrypt',
81+
password: undefined,
82+
adata: undefined,
83+
});
84+
});
4485
it('express.verifyaddress', function () {
4586
assert.throws(() =>
4687
assertDecode(t.type(VerifyAddressBody), {

0 commit comments

Comments
 (0)