Skip to content

Commit 6e353d3

Browse files
authored
fix: correct enum typed data hashing (#1281)
1 parent 0fce61e commit 6e353d3

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

__tests__/utils/typedData.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ describe('typedData', () => {
326326

327327
messageHash = getMessageHash(exampleEnum, exampleAddress);
328328
expect(messageHash).toMatchInlineSnapshot(
329-
`"0x6e61abaf480b1370bbf231f54e298c5f4872f40a6d2dd409ff30accee5bbd1e"`
329+
`"0x416b85b18063b1b3420ab709e9d5e35cb716691d397c5841ce7c5198ee30bf"`
330330
);
331331

332332
expect(spyPedersen).not.toHaveBeenCalled();

src/utils/typedData.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,13 @@ export function encodeValue(
357357
if (revision === Revision.ACTIVE) {
358358
const [variantKey, variantData] = Object.entries(data as TypedData['message'])[0];
359359

360-
const parentType = types[ctx.parent as string].find((t) => t.name === ctx.key);
361-
const enumType = types[(parentType as StarknetEnumType).contains];
360+
const parentType = types[ctx.parent as string].find((t) => t.name === ctx.key)!;
361+
const enumName = (parentType as StarknetEnumType).contains;
362+
const enumType = types[enumName];
362363
const variantType = enumType.find((t) => t.name === variantKey) as StarknetType;
363364
const variantIndex = enumType.indexOf(variantType);
364365

366+
const typeHash = getTypeHash(types, enumName, revision);
365367
const encodedSubtypes = variantType.type
366368
.slice(1, -1)
367369
.split(',')
@@ -372,7 +374,7 @@ export function encodeValue(
372374
});
373375
return [
374376
type,
375-
revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes]),
377+
revisionConfiguration[revision].hashMethod([typeHash, variantIndex, ...encodedSubtypes]),
376378
];
377379
} // else fall through to default
378380
return [type, getHex(data as string)];

0 commit comments

Comments
 (0)