From 6273f208f6f6e27790a976eccc0e7dc5603bfde6 Mon Sep 17 00:00:00 2001 From: FT <140458077+zeevick10@users.noreply.github.com> Date: Wed, 23 Jul 2025 20:40:23 +0200 Subject: [PATCH] Update decode-message.ts --- packages/utils/src/decode-message.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/utils/src/decode-message.ts b/packages/utils/src/decode-message.ts index 6ac00f8e1..5e13764e3 100644 --- a/packages/utils/src/decode-message.ts +++ b/packages/utils/src/decode-message.ts @@ -1,6 +1,4 @@ -import type { BigNumberish } from "ethers" -import { decodeBytes32String } from "ethers/abi" -import { toBeHex } from "ethers/utils" +import { BigNumberish, decodeBytes32String, toBeHex } from "ethers" /** * Typically used for decoding on-chain Semaphore messages. @@ -9,9 +7,20 @@ import { toBeHex } from "ethers/utils" * This function help devs converting bigint messages to text again. * If the original message was not text the output of this * function won't probably be human-readable text. + * + * Note: If the input is not a valid bytes32 string, the function will return null instead of throwing. + * This makes the function safer to use with untrusted or dynamic input. + * * @param message The Semaphore message as a bigint. - * @returns The Semaphore message as a text. + * @returns The Semaphore message as a text, or null if decoding fails. */ export default function decodeMessage(message: BigNumberish) { - return decodeBytes32String(toBeHex(message, 32)) + try { + // Attempt to decode the message as a bytes32 string. + // If the input is not valid, return null instead of throwing an error. + return decodeBytes32String(toBeHex(message, 32)) + } catch { + // Decoding failed (invalid input or not a valid bytes32 string) + return null + } }