From 6e353d3d50226907ce6b5ad53309d55ed51c6874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petar=20Penovi=C4=87?= Date: Wed, 4 Dec 2024 23:21:34 +0100 Subject: [PATCH] fix: correct enum typed data hashing (#1281) --- __tests__/utils/typedData.test.ts | 2 +- src/utils/typedData.ts | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/__tests__/utils/typedData.test.ts b/__tests__/utils/typedData.test.ts index 945f12075..ccbdc3dc1 100644 --- a/__tests__/utils/typedData.test.ts +++ b/__tests__/utils/typedData.test.ts @@ -326,7 +326,7 @@ describe('typedData', () => { messageHash = getMessageHash(exampleEnum, exampleAddress); expect(messageHash).toMatchInlineSnapshot( - `"0x6e61abaf480b1370bbf231f54e298c5f4872f40a6d2dd409ff30accee5bbd1e"` + `"0x416b85b18063b1b3420ab709e9d5e35cb716691d397c5841ce7c5198ee30bf"` ); expect(spyPedersen).not.toHaveBeenCalled(); diff --git a/src/utils/typedData.ts b/src/utils/typedData.ts index 1d462283a..fc9407c6c 100644 --- a/src/utils/typedData.ts +++ b/src/utils/typedData.ts @@ -357,11 +357,13 @@ export function encodeValue( if (revision === Revision.ACTIVE) { const [variantKey, variantData] = Object.entries(data as TypedData['message'])[0]; - const parentType = types[ctx.parent as string].find((t) => t.name === ctx.key); - const enumType = types[(parentType as StarknetEnumType).contains]; + const parentType = types[ctx.parent as string].find((t) => t.name === ctx.key)!; + const enumName = (parentType as StarknetEnumType).contains; + const enumType = types[enumName]; const variantType = enumType.find((t) => t.name === variantKey) as StarknetType; const variantIndex = enumType.indexOf(variantType); + const typeHash = getTypeHash(types, enumName, revision); const encodedSubtypes = variantType.type .slice(1, -1) .split(',') @@ -372,7 +374,7 @@ export function encodeValue( }); return [ type, - revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes]), + revisionConfiguration[revision].hashMethod([typeHash, variantIndex, ...encodedSubtypes]), ]; } // else fall through to default return [type, getHex(data as string)];