Skip to content

Commit

Permalink
fix: repair enum type lookup for typed data hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
penovicp committed Aug 17, 2024
1 parent b96e3d3 commit 36f8c3c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
16 changes: 13 additions & 3 deletions __mocks__/typedData/example_enum.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@
{ "name": "chainId", "type": "shortstring" },
{ "name": "revision", "type": "shortstring" }
],
"Example": [{ "name": "someEnum", "type": "enum", "contains": "MyEnum" }],
"MyEnum": [
"Example": [
{ "name": "someEnum1", "type": "enum", "contains": "EnumA" },
{ "name": "someEnum2", "type": "enum", "contains": "EnumB" }
],
"EnumA": [
{ "name": "Variant 1", "type": "()" },
{ "name": "Variant 2", "type": "(u128,u128*)" },
{ "name": "Variant 3", "type": "(u128)" }
],
"EnumB": [
{ "name": "Variant 1", "type": "()" },
{ "name": "Variant 2", "type": "(u128)" }
]
},
"primaryType": "Example",
Expand All @@ -21,8 +28,11 @@
"revision": "1"
},
"message": {
"someEnum": {
"someEnum1": {
"Variant 2": [2, [0, 1]]
},
"someEnum2": {
"Variant 1": []
}
}
}
6 changes: 3 additions & 3 deletions __tests__/utils/typedData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('typedData', () => {
);
encoded = encodeType(exampleEnum.types, 'Example', TypedDataRevision.ACTIVE);
expect(encoded).toMatchInlineSnapshot(
`"\\"Example\\"(\\"someEnum\\":\\"MyEnum\\")\\"MyEnum\\"(\\"Variant 1\\":(),\\"Variant 2\\":(\\"u128\\",\\"u128*\\"),\\"Variant 3\\":(\\"u128\\"))"`
`"\\"Example\\"(\\"someEnum1\\":\\"EnumA\\",\\"someEnum2\\":\\"EnumB\\")\\"EnumA\\"(\\"Variant 1\\":(),\\"Variant 2\\":(\\"u128\\",\\"u128*\\"),\\"Variant 3\\":(\\"u128\\"))\\"EnumB\\"(\\"Variant 1\\":(),\\"Variant 2\\":(\\"u128\\"))"`
);
});

Expand Down Expand Up @@ -94,7 +94,7 @@ describe('typedData', () => {
);
typeHash = getTypeHash(exampleEnum.types, 'Example', TypedDataRevision.ACTIVE);
expect(typeHash).toMatchInlineSnapshot(
`"0x380a54d417fb58913b904675d94a8a62e2abc3467f4b5439de0fd65fafdd1a8"`
`"0x8eb4aeac64b707f3e843284c4258df6df1f0f7fd38dcffdd8a153a495cd351"`
);
});

Expand Down Expand Up @@ -316,7 +316,7 @@ describe('typedData', () => {

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

expect(spyPedersen).not.toHaveBeenCalled();
Expand Down
4 changes: 2 additions & 2 deletions src/utils/typedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ export function encodeValue(
if (revision === Revision.ACTIVE) {
const [variantKey, variantData] = Object.entries(data as TypedData['message'])[0];

const parentType = types[ctx.parent as string][0] as StarknetEnumType;
const enumType = types[parentType.contains];
const parentType = types[ctx.parent as string].find((t) => t.name === ctx.key);
const enumType = types[(parentType as StarknetEnumType).contains];
const variantType = enumType.find((t) => t.name === variantKey) as StarknetType;
const variantIndex = enumType.indexOf(variantType);

Expand Down

0 comments on commit 36f8c3c

Please sign in to comment.