Skip to content

Commit

Permalink
chore: revert typed data enum fixes
Browse files Browse the repository at this point in the history
* revert "fix: rectify snip-12 violation by removing extra `:` in enum encoding (#1288)"
    - this reverts commit b903116
* revert "fix: correct enum typed data hashing (#1281)"
    - this reverts commit 6e353d3
  • Loading branch information
penovicp committed Jan 7, 2025
1 parent b903116 commit e8db599
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
6 changes: 3 additions & 3 deletions __tests__/utils/typedData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('typedData', () => {
);
encoded = encodeType(exampleEnum.types, 'Example', TypedDataRevision.ACTIVE);
expect(encoded).toMatchInlineSnapshot(
`"\\"Example\\"(\\"someEnum1\\":\\"EnumA\\",\\"someEnum2\\":\\"EnumB\\")\\"EnumA\\"(\\"Variant 1\\"(),\\"Variant 2\\"(\\"u128\\",\\"u128*\\"),\\"Variant 3\\"(\\"u128\\"))\\"EnumB\\"(\\"Variant 1\\"(),\\"Variant 2\\"(\\"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 @@ -104,7 +104,7 @@ describe('typedData', () => {
);
typeHash = getTypeHash(exampleEnum.types, 'Example', TypedDataRevision.ACTIVE);
expect(typeHash).toMatchInlineSnapshot(
`"0x393bf83422ca8626a2932696cfa0acb19dcad6de2fe84a2dd2ca7607ea5329a"`
`"0x8eb4aeac64b707f3e843284c4258df6df1f0f7fd38dcffdd8a153a495cd351"`
);
});

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

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

expect(spyPedersen).not.toHaveBeenCalled();
Expand Down
12 changes: 5 additions & 7 deletions src/utils/typedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ export function encodeType(
.split(',')
.map((e) => (e ? esc(e) : e))
.join(',')})`
: `:${esc(targetType)}`;
return `${esc(t.name)}${typeString}`;
: esc(targetType);
return `${esc(t.name)}:${typeString}`;
});
return `${esc(dependency)}(${dependencyElements})`;
})
Expand Down Expand Up @@ -357,13 +357,11 @@ 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 enumName = (parentType as StarknetEnumType).contains;
const enumType = types[enumName];
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);

const typeHash = getTypeHash(types, enumName, revision);
const encodedSubtypes = variantType.type
.slice(1, -1)
.split(',')
Expand All @@ -374,7 +372,7 @@ export function encodeValue(
});
return [
type,
revisionConfiguration[revision].hashMethod([typeHash, variantIndex, ...encodedSubtypes]),
revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes]),
];
} // else fall through to default
return [type, getHex(data as string)];
Expand Down

0 comments on commit e8db599

Please sign in to comment.