From 30b657a2e9f5d0d44a2eac715fbffcb2c89c805b Mon Sep 17 00:00:00 2001 From: Archit Date: Tue, 13 Feb 2024 18:04:05 +0530 Subject: [PATCH] fix message parsing --- src/utils/index.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index da2bf73..30e140f 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -39,10 +39,18 @@ export const isValidSignature = (data: SetDataInput) => { r: Buffer.from(decodedSignature.substring(0, 64), "hex"), s: Buffer.from(decodedSignature.substring(64, 128), "hex"), }; - return elliptic.verify(keccak256(stringify(setData)), ecSignature, pubKey); + // this is to ensure that the signature is valid for both JSON and stringified data + // and for backward compatibility. + const result1 = elliptic.verify(keccak256(stringify(setData)), ecSignature, pubKey); + if (result1) return true; + return elliptic.verify(keccak256(JSON.stringify(setData)), ecSignature, pubKey); }; export const isValidLockSignature = (lockData: LockDataInput) => { const { key, signature, data } = lockData; - return elliptic.verify(keccak256(stringify(data)), signature, Buffer.from(key, "hex")); + // this is to ensure that the signature is valid for both JSON and stringified data + // and for backward compatibility. + const result = elliptic.verify(keccak256(Buffer.from(stringify(data), "utf8")), signature, Buffer.from(key, "hex")); + if (result) return result; + return elliptic.verify(keccak256(Buffer.from(JSON.stringify(data), "utf8")), signature, Buffer.from(key, "hex")); };