Skip to content

Commit d9d167b

Browse files
Merge pull request #7024 from BitGo/GNA-2162
fix(express): signPayload API to handle stringified payload as req
2 parents 0593fbc + 1423d8b commit d9d167b

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const isJsonString = (str: any): boolean => {
2+
try {
3+
JSON.parse(str);
4+
return true;
5+
} catch {
6+
return false;
7+
}
8+
};

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

Lines changed: 17 additions & 5 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,11 +42,7 @@ 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-
// TODO(GO-1015): unskip test
46-
return;
47-
48-
// eslint-disable-next-line no-unreachable
45+
it('should return a signed payload with type as object', async function () {
4946
const expectedResponse = {
5047
payload: JSON.stringify(payload),
5148
signature,
@@ -60,6 +57,21 @@ describe('Sign an arbitrary payload with trading account key', function () {
6057
} as unknown as Request;
6158
await handleV2OFCSignPayload(req).should.be.resolvedWith(expectedResponse);
6259
});
60+
it('should return a signed payload with type as json string', async function () {
61+
const expectedResponse = {
62+
payload: stringifiedPayload,
63+
signature,
64+
};
65+
const req = {
66+
bitgo: bitGoStub,
67+
body: {
68+
payload: stringifiedPayload,
69+
walletId,
70+
},
71+
query: {},
72+
} as unknown as Request;
73+
await handleV2OFCSignPayload(req).should.be.resolvedWith(expectedResponse);
74+
});
6375
});
6476

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

0 commit comments

Comments
 (0)