Skip to content

Commit 3f047b5

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

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

modules/express/src/clientRoutes.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,15 @@ export async function handleV2OFCSignPayloadInExtSigningMode(
589589
}
590590
}
591591

592+
const isJsonString = (str: any): boolean => {
593+
try {
594+
JSON.parse(str);
595+
return true;
596+
} catch {
597+
return false;
598+
}
599+
};
600+
592601
export async function handleV2OFCSignPayload(req: express.Request): Promise<{ payload: string; signature: string }> {
593602
const walletId = req.body.walletId;
594603
const payload = req.body.payload;
@@ -630,7 +639,7 @@ export async function handleV2OFCSignPayload(req: express.Request): Promise<{ pa
630639

631640
const walletPassphrase = bodyWalletPassphrase || getWalletPwFromEnv(wallet.id());
632641
const tradingAccount = wallet.toTradingAccount();
633-
const stringifiedPayload = JSON.stringify(req.body.payload);
642+
const stringifiedPayload = isJsonString(req.body.payload) ? req.body.payload : JSON.stringify(req.body.payload);
634643
const signature = await tradingAccount.signPayload({
635644
payload: stringifiedPayload,
636645
walletPassphrase,

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

Lines changed: 21 additions & 1 deletion
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,7 +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+
it('should return a signed payload with type as object', async function () {
4546
// TODO(GO-1015): unskip test
4647
return;
4748

@@ -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)