From c16782e62a0f3544c0ac1e060bc01d0f9d24af97 Mon Sep 17 00:00:00 2001 From: samsamtrum Date: Mon, 22 Jun 2026 02:17:49 +0700 Subject: [PATCH] Reject negative bigint hexlify values --- src/sdk/common/utils/hexlify.ts | 4 ++++ test/hexlify.test.ts | 8 ++++++++ 2 files changed, 12 insertions(+) create mode 100644 test/hexlify.test.ts diff --git a/src/sdk/common/utils/hexlify.ts b/src/sdk/common/utils/hexlify.ts index f0001b3..be2b9a7 100644 --- a/src/sdk/common/utils/hexlify.ts +++ b/src/sdk/common/utils/hexlify.ts @@ -91,6 +91,10 @@ export function hexlifyValue(value: BytesLike | Hexable | number | bigint, optio } if (typeof (value) === "bigint") { + if (value < 0n) { + throw new Error(`Invalid Hexlify value - negative bigint value: ${value}`); + } + value = value.toString(16); if (value.length % 2) { return ("0x0" + value); } return "0x" + value; diff --git a/test/hexlify.test.ts b/test/hexlify.test.ts new file mode 100644 index 0000000..8bd2a03 --- /dev/null +++ b/test/hexlify.test.ts @@ -0,0 +1,8 @@ +import assert from 'node:assert/strict'; +import { hexlifyValue } from '../src/sdk/common/utils/hexlify.js'; + +assert.equal(hexlifyValue(0n), '0x00'); +assert.equal(hexlifyValue(15n), '0x0f'); +assert.equal(hexlifyValue(16n), '0x10'); +assert.throws(() => hexlifyValue(-1n), /negative bigint value/); +assert.throws(() => hexlifyValue(-16n), /negative bigint value/);