Skip to content

Commit 2a259a8

Browse files
fix(express): signPayload API to handle stringified payload as req
Ticket: GNA-2162
1 parent 465f4a2 commit 2a259a8

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

modules/express/src/clientRoutes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import { handleLightningWithdraw } from './lightning/lightningWithdrawRoutes';
6262
import createExpressRouter from './typedRoutes';
6363
import { ExpressApiRouteRequest } from './typedRoutes/api';
6464
import { TypedRequestHandler, WrappedRequest, WrappedResponse } from '@api-ts/typed-express-router';
65+
import { isJsonString } from './utils';
6566

6667
const { version } = require('bitgo/package.json');
6768
const pjson = require('../package.json');
@@ -630,7 +631,7 @@ export async function handleV2OFCSignPayload(req: express.Request): Promise<{ pa
630631

631632
const walletPassphrase = bodyWalletPassphrase || getWalletPwFromEnv(wallet.id());
632633
const tradingAccount = wallet.toTradingAccount();
633-
const stringifiedPayload = JSON.stringify(req.body.payload);
634+
const stringifiedPayload = isJsonString(req.body.payload) ? req.body.payload : JSON.stringify(req.body.payload);
634635
const signature = await tradingAccount.signPayload({
635636
payload: stringifiedPayload,
636637
walletPassphrase,

modules/express/src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as J from 'fp-ts/Json';
2+
3+
export const isJsonString = (str: any): boolean => {
4+
try {
5+
J.parse(str);
6+
return true;
7+
} catch {
8+
return false;
9+
}
10+
};

modules/express/test/unit/clientRoutes/signPayload.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe('Sign an arbitrary payload with trading account key', function () {
1818
},
1919
},
2020
};
21+
const stringifiedPayload = JSON.stringify(payload);
2122
const signature = 'signedPayload123';
2223
const walletId = 'myWalletId';
2324

@@ -41,9 +42,9 @@ describe('Sign an arbitrary payload with trading account key', function () {
4142
process.env['WALLET_myWalletId_PASSPHRASE'] = 'mypass';
4243
});
4344

44-
it('should return a signed payload', async function () {
45+
it('should return a signed payload with type as object', async function () {
4546
// TODO(GO-1015): unskip test
46-
return;
47+
// return;
4748

4849
// eslint-disable-next-line no-unreachable
4950
const expectedResponse = {
@@ -60,6 +61,25 @@ describe('Sign an arbitrary payload with trading account key', function () {
6061
} as unknown as Request;
6162
await handleV2OFCSignPayload(req).should.be.resolvedWith(expectedResponse);
6263
});
64+
it('should return a signed payload with type as json string', async function () {
65+
// TODO(GO-1015): unskip test
66+
// return;
67+
68+
// eslint-disable-next-line no-unreachable
69+
const expectedResponse = {
70+
payload: stringifiedPayload,
71+
signature,
72+
};
73+
const req = {
74+
bitgo: bitGoStub,
75+
body: {
76+
payload: stringifiedPayload,
77+
walletId,
78+
},
79+
query: {},
80+
} as unknown as Request;
81+
await handleV2OFCSignPayload(req).should.be.resolvedWith(expectedResponse);
82+
});
6383
});
6484

6585
describe('With the handler to sign an arbitrary payload in external signing mode', () => {

0 commit comments

Comments
 (0)