From f3c47db1825863f4d2f2d1c05dd6b3fbef2dd560 Mon Sep 17 00:00:00 2001 From: lwedge99 <116623885+lwedge99@users.noreply.github.com> Date: Tue, 14 Jan 2025 21:27:08 +0800 Subject: [PATCH] fix hexlify with odd length string --- package.json | 2 +- src.ts/utils/data.ts | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 416832abf8..a7ff92ecbd 100644 --- a/package.json +++ b/package.json @@ -132,5 +132,5 @@ "build-pub": "npm run build-all && npm run build-dist && npm publish --ignore-git-checks" }, "sideEffects": false, - "version": "6.13.1-patch.3" + "version": "6.13.1-patch.4" } diff --git a/src.ts/utils/data.ts b/src.ts/utils/data.ts index fd0c7a5e13..3e0525364e 100644 --- a/src.ts/utils/data.ts +++ b/src.ts/utils/data.ts @@ -30,15 +30,19 @@ function _getBytes(value: BytesLike, name?: string, copy?: boolean): Uint8Array if (copy) { return new Uint8Array(value); } return value; } - - if (typeof(value) === "string" && value.match(/^0x(?:[0-9a-f][0-9a-f])*$/i)) { - const result = new Uint8Array((value.length - 2) / 2); - let offset = 2; - for (let i = 0; i < result.length; i++) { - result[i] = parseInt(value.substring(offset, offset + 2), 16); - offset += 2; + if (typeof(value) === "string") { + if (value.length % 2 == 1) { + value = "0x0" + value.substring(2); + } + if (value.match(/^0x(?:[0-9a-f][0-9a-f])*$/i)) { + const result = new Uint8Array((value.length - 2) / 2); + let offset = 2; + for (let i = 0; i < result.length; i++) { + result[i] = parseInt(value.substring(offset, offset + 2), 16); + offset += 2; + } + return result; } - return result; } assertArgument(false, "invalid BytesLike value", name || "value", value);