Skip to content

Commit b18c879

Browse files
refactor(express): split encrypt into v1 and v2
TICKET: WCI-185
1 parent d067420 commit b18c879

6 files changed

Lines changed: 80 additions & 25 deletions

File tree

modules/express/src/clientRoutes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,7 @@ export function setupAPIRoutes(app: express.Application, config: Config): void {
17011701
router.post('express.login', [prepareBitGo(config), typedPromiseWrapper(handleLogin)]);
17021702

17031703
router.post('express.decrypt', [prepareBitGo(config), typedPromiseWrapper(handleDecrypt)]);
1704+
router.post('express.v1.encrypt', [prepareBitGo(config), typedPromiseWrapper(handleEncrypt)]);
17041705
router.post('express.encrypt', [prepareBitGo(config), typedPromiseWrapper(handleEncrypt)]);
17051706
router.post('express.verifyaddress', [prepareBitGo(config), typedPromiseWrapper(handleVerifyAddress)]);
17061707
router.post('express.v1.calculateminerfeeinfo', [

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ 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';
9+
import { PostV1Encrypt } from './v1/encrypt';
10+
import { PostV2Encrypt } from './v2/encrypt';
1011
import { PostVerifyAddress } from './common/verifyAddress';
1112
import { PostV1CalculateMinerFeeInfo } from './v1/calculateMinerFeeInfo';
1213
import { PostV2CalculateMinerFeeInfo } from './v2/calculateMinerFeeInfo';
@@ -87,8 +88,11 @@ export const ExpressDecryptApiSpec = apiSpec({
8788
});
8889

8990
export const ExpressEncryptApiSpec = apiSpec({
91+
'express.v1.encrypt': {
92+
post: PostV1Encrypt,
93+
},
9094
'express.encrypt': {
91-
post: PostEncrypt,
95+
post: PostV2Encrypt,
9296
},
9397
});
9498

modules/express/src/typedRoutes/api/common/encrypt.ts renamed to modules/express/src/typedRoutes/api/v1/encrypt.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@ import { httpRoute, httpRequest, optional } from '@api-ts/io-ts-http';
33
import { BitgoExpressError } from '../../schemas/error';
44

55
export const EncryptRequestBody = {
6+
/** Plaintext message which should be encrypted */
67
input: t.string,
8+
/** Password which should be used to encrypt message */
79
password: optional(t.string),
810
adata: optional(t.string),
911
};
1012

1113
/**
12-
* Encrypt
14+
* Encrypt message (v1)
1315
*
14-
* @operationId express.encrypt
15-
* @tag express
16+
* Symmetrically encrypt an arbitrary message with provided password
17+
*
18+
* @operationId express.v1.encrypt
19+
* @tag Express
20+
* @private
1621
*/
17-
export const PostEncrypt = httpRoute({
18-
path: '/api/v[12]/encrypt',
22+
export const PostV1Encrypt = httpRoute({
23+
path: '/api/v1/encrypt',
1924
method: 'POST',
2025
request: httpRequest({
2126
body: EncryptRequestBody,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as t from 'io-ts';
2+
import { httpRoute, httpRequest } from '@api-ts/io-ts-http';
3+
import { BitgoExpressError } from '../../schemas/error';
4+
import { EncryptRequestBody } from '../v1/encrypt';
5+
6+
/**
7+
* Encrypt message
8+
*
9+
* Symmetrically encrypt an arbitrary message with provided password
10+
*
11+
* @operationId express.encrypt
12+
* @tag Express
13+
* @public
14+
*/
15+
export const PostV2Encrypt = httpRoute({
16+
path: '/api/v2/encrypt',
17+
method: 'POST',
18+
request: httpRequest({
19+
body: EncryptRequestBody,
20+
}),
21+
response: {
22+
200: t.type({
23+
encrypted: t.string,
24+
}),
25+
404: BitgoExpressError,
26+
},
27+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +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';
4+
import { EncryptRequestBody } from '../../../src/typedRoutes/api/v1/encrypt';
55
import { LoginRequest } from '../../../src/typedRoutes/api/common/login';
66
import { VerifyAddressBody } from '../../../src/typedRoutes/api/common/verifyAddress';
77
import { VerifyAddressV2Body, VerifyAddressV2Params } from '../../../src/typedRoutes/api/v2/verifyAddress';

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

Lines changed: 35 additions & 17 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';
3-
import { EncryptRequestBody, PostEncrypt } from '../../../src/typedRoutes/api/common/encrypt';
3+
import { EncryptRequestBody, PostV1Encrypt } from '../../../src/typedRoutes/api/v1/encrypt';
4+
import { PostV2Encrypt } from '../../../src/typedRoutes/api/v2/encrypt';
45
import { assertDecode } from './common';
56
import 'should';
67
import 'should-http';
@@ -92,7 +93,7 @@ describe('Encrypt codec tests', function () {
9293
});
9394

9495
describe('EncryptResponse', function () {
95-
const EncryptResponse = PostEncrypt.response[200];
96+
const EncryptResponse = PostV1Encrypt.response[200];
9697

9798
it('should validate response with required field', function () {
9899
const validResponse = {
@@ -139,24 +140,41 @@ describe('Encrypt codec tests', function () {
139140
});
140141
});
141142

142-
describe('PostEncrypt route definition', function () {
143+
describe('PostV1Encrypt route definition', function () {
143144
it('should have the correct path', function () {
144-
assert.strictEqual(PostEncrypt.path, '/api/v[12]/encrypt');
145+
assert.strictEqual(PostV1Encrypt.path, '/api/v1/encrypt');
145146
});
146147

147148
it('should have the correct HTTP method', function () {
148-
assert.strictEqual(PostEncrypt.method, 'POST');
149+
assert.strictEqual(PostV1Encrypt.method, 'POST');
149150
});
150151

151152
it('should have the correct request configuration', function () {
152-
// Verify the route is configured with a request property
153-
assert.ok(PostEncrypt.request);
153+
assert.ok(PostV1Encrypt.request);
154154
});
155155

156156
it('should have the correct response types', function () {
157-
// Check that the response object has the expected status codes
158-
assert.ok(PostEncrypt.response[200]);
159-
assert.ok(PostEncrypt.response[404]);
157+
assert.ok(PostV1Encrypt.response[200]);
158+
assert.ok(PostV1Encrypt.response[404]);
159+
});
160+
});
161+
162+
describe('PostV2Encrypt route definition', function () {
163+
it('should have the correct path', function () {
164+
assert.strictEqual(PostV2Encrypt.path, '/api/v2/encrypt');
165+
});
166+
167+
it('should have the correct HTTP method', function () {
168+
assert.strictEqual(PostV2Encrypt.method, 'POST');
169+
});
170+
171+
it('should have the correct request configuration', function () {
172+
assert.ok(PostV2Encrypt.request);
173+
});
174+
175+
it('should have the correct response types', function () {
176+
assert.ok(PostV2Encrypt.response[200]);
177+
assert.ok(PostV2Encrypt.response[404]);
160178
});
161179
});
162180

@@ -191,7 +209,7 @@ describe('Encrypt codec tests', function () {
191209
result.body.should.have.property('encrypted');
192210
assert.strictEqual(result.body.encrypted, mockEncryptResponse);
193211

194-
const decodedResponse = assertDecode(PostEncrypt.response[200], result.body);
212+
const decodedResponse = assertDecode(PostV1Encrypt.response[200], result.body);
195213
assert.strictEqual(decodedResponse.encrypted, mockEncryptResponse);
196214
});
197215

@@ -213,7 +231,7 @@ describe('Encrypt codec tests', function () {
213231
result.body.should.have.property('encrypted');
214232
assert.strictEqual(result.body.encrypted, mockEncryptResponse);
215233

216-
const decodedResponse = assertDecode(PostEncrypt.response[200], result.body);
234+
const decodedResponse = assertDecode(PostV2Encrypt.response[200], result.body);
217235
assert.strictEqual(decodedResponse.encrypted, mockEncryptResponse);
218236
});
219237

@@ -235,7 +253,7 @@ describe('Encrypt codec tests', function () {
235253
assert.strictEqual(result.status, 200);
236254
assert.strictEqual(result.body.encrypted, mockEncryptResponse);
237255

238-
const decodedResponse = assertDecode(PostEncrypt.response[200], result.body);
256+
const decodedResponse = assertDecode(PostV1Encrypt.response[200], result.body);
239257
assert.strictEqual(decodedResponse.encrypted, mockEncryptResponse);
240258
});
241259

@@ -257,7 +275,7 @@ describe('Encrypt codec tests', function () {
257275
assert.strictEqual(result.status, 200);
258276
assert.strictEqual(result.body.encrypted, mockEncryptResponse);
259277

260-
const decodedResponse = assertDecode(PostEncrypt.response[200], result.body);
278+
const decodedResponse = assertDecode(PostV2Encrypt.response[200], result.body);
261279
assert.strictEqual(decodedResponse.encrypted, mockEncryptResponse);
262280
});
263281

@@ -279,7 +297,7 @@ describe('Encrypt codec tests', function () {
279297
assert.strictEqual(result.status, 200);
280298
assert.strictEqual(result.body.encrypted, mockLongEncrypted);
281299

282-
const decodedResponse = assertDecode(PostEncrypt.response[200], result.body);
300+
const decodedResponse = assertDecode(PostV1Encrypt.response[200], result.body);
283301
assert.strictEqual(decodedResponse.encrypted, mockLongEncrypted);
284302
});
285303

@@ -300,7 +318,7 @@ describe('Encrypt codec tests', function () {
300318
assert.strictEqual(result.status, 200);
301319
assert.strictEqual(result.body.encrypted, mockEncryptResponse);
302320

303-
const decodedResponse = assertDecode(PostEncrypt.response[200], result.body);
321+
const decodedResponse = assertDecode(PostV2Encrypt.response[200], result.body);
304322
assert.ok(decodedResponse);
305323
});
306324

@@ -321,7 +339,7 @@ describe('Encrypt codec tests', function () {
321339
assert.strictEqual(result.status, 200);
322340
assert.strictEqual(result.body.encrypted, mockEncryptResponse);
323341

324-
const decodedResponse = assertDecode(PostEncrypt.response[200], result.body);
342+
const decodedResponse = assertDecode(PostV1Encrypt.response[200], result.body);
325343
assert.ok(decodedResponse);
326344
});
327345
});

0 commit comments

Comments
 (0)