Skip to content

Commit

Permalink
fix: correct enum typed data hashing (starknet-io#1281)
Browse files Browse the repository at this point in the history
  • Loading branch information
penovicp authored Dec 4, 2024
1 parent 0fce61e commit 6e353d3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion __tests__/utils/typedData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ describe('typedData', () => {

messageHash = getMessageHash(exampleEnum, exampleAddress);
expect(messageHash).toMatchInlineSnapshot(
`"0x6e61abaf480b1370bbf231f54e298c5f4872f40a6d2dd409ff30accee5bbd1e"`
`"0x416b85b18063b1b3420ab709e9d5e35cb716691d397c5841ce7c5198ee30bf"`
);

expect(spyPedersen).not.toHaveBeenCalled();
Expand Down
8 changes: 5 additions & 3 deletions src/utils/typedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(',')
Expand All @@ -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)];
Expand Down

0 comments on commit 6e353d3

Please sign in to comment.