Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions misc/safe-json/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ const JSONParse = (json: string) => {
*/
// prettier-ignore
// eslint-disable-next-line no-useless-escape
const numbersBiggerThanMaxInt = /(?<!")(?<=:)\b(\d{17,}|(?:[9](?:[1-9]07199254740991|0[1-9]7199254740991|00[8-9]199254740991|007[2-9]99254740991|007199[3-9]54740991|0071992[6-9]4740991|00719925[5-9]740991|007199254[8-9]40991|0071992547[5-9]0991|00719925474[1-9]991|00719925474099[2-9])))(?=[,\}\]]|$)/g;
const numbersBiggerThanMaxInt = /(?<!")(?<=:)-?(\d{17,}|(?:[9](?:[1-9]07199254740991|0[1-9]7199254740991|00[8-9]199254740991|007[2-9]99254740991|007199[3-9]54740991|0071992[6-9]4740991|00719925[5-9]740991|007199254[8-9]40991|0071992547[5-9]0991|00719925474[1-9]991|00719925474099[2-9])))(?=[,\}\]]|$)/g;
const serializedData = json.replace(numbersBiggerThanMaxInt, (match) => `"${match}n"`);
return JSON.parse(serializedData, (_, value) => {
const isCustomFormatBigInt = typeof value === "string" && value.match(/^\d+n$/);
const isCustomFormatBigInt = typeof value === "string" && value.match(/^-?\d+n$/);

if (isCustomFormatBigInt) return BigInt(value.substring(0, value.length - 1));

Expand Down
4 changes: 4 additions & 0 deletions misc/safe-json/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ describe("@walletconnect/safe-json", () => {
const result = safeJsonParse(safeJsonStringify({ bigint: BigInt(1) }));
chai.expect(result).to.deep.eq({ bigint: BigInt(1) });
});
it("should handle negative bigint", () => {
const result = safeJsonParse(safeJsonStringify({ amount: BigInt(-100) }));
chai.expect(result).to.deep.eq({ amount: BigInt(-100) });
});
it("should handle number inside string literal. Case 1", () => {
const nested = '{"x":"12345678901234567,"}';
const result = safeJsonParse(nested);
Expand Down
Loading