From b9924465a1f01ac4273638f3fa258b36192d2101 Mon Sep 17 00:00:00 2001 From: Petar Penovic Date: Tue, 12 Mar 2024 00:13:46 +0100 Subject: [PATCH] fix: expand encoding type for preset types --- __tests__/utils/typedData.test.ts | 6 +++--- src/utils/typedData.ts | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/__tests__/utils/typedData.test.ts b/__tests__/utils/typedData.test.ts index 9ede00d77..c09f0ba65 100644 --- a/__tests__/utils/typedData.test.ts +++ b/__tests__/utils/typedData.test.ts @@ -47,7 +47,7 @@ describe('typedData', () => { ); encoded = encodeType(examplePresetTypes.types, 'Example', TypedDataRevision.Active); expect(encoded).toMatchInlineSnapshot( - `"\\"Example\\"(\\"n0\\":\\"TokenAmount\\",\\"n1\\":\\"NftId\\")"` + `"\\"Example\\"(\\"n0\\":\\"TokenAmount\\",\\"n1\\":\\"NftId\\")\\"NftId\\"(\\"collection_address\\":\\"ContractAddress\\",\\"token_id\\":\\"u256\\")\\"TokenAmount\\"(\\"token_address\\":\\"ContractAddress\\",\\"amount\\":\\"u256\\")\\"u256\\"(\\"low\\":\\"u128\\",\\"high\\":\\"u128\\")"` ); encoded = encodeType(exampleEnum.types, 'Example', TypedDataRevision.Active); expect(encoded).toMatchInlineSnapshot( @@ -87,7 +87,7 @@ describe('typedData', () => { ); typeHash = getTypeHash(examplePresetTypes.types, 'Example', TypedDataRevision.Active); expect(typeHash).toMatchInlineSnapshot( - `"0x155de33c6a0cc7f2b8926afc7a71fc2ac31ffc26726aee5da0570c5d517a763"` + `"0x1a25a8bb84b761090b1fadaebe762c4b679b0d8883d2bedda695ea340839a55"` ); typeHash = getTypeHash(exampleEnum.types, 'Example', TypedDataRevision.Active); expect(typeHash).toMatchInlineSnapshot( @@ -279,7 +279,7 @@ describe('typedData', () => { messageHash = getMessageHash(examplePresetTypes, exampleAddress); expect(messageHash).toMatchInlineSnapshot( - `"0x26e7b8cedfa63cdbed14e7e51b60ee53ac82bdf26724eb1e3f0710cb8987522"` + `"0x185b339d5c566a883561a88fb36da301051e2c0225deb325c91bb7aa2f3473a"` ); messageHash = getMessageHash(exampleEnum, exampleAddress); diff --git a/src/utils/typedData.ts b/src/utils/typedData.ts index 3dfb99a5f..d1193b29c 100644 --- a/src/utils/typedData.ts +++ b/src/utils/typedData.ts @@ -188,14 +188,24 @@ export function encodeType( type: string, revision: Revision = Revision.Legacy ): string { - const [primary, ...dependencies] = getDependencies(types, type, undefined, undefined, revision); + const allTypes = + revision === Revision.Active + ? { ...types, ...revisionConfiguration[revision].presetTypes } + : types; + const [primary, ...dependencies] = getDependencies( + allTypes, + type, + undefined, + undefined, + revision + ); const newTypes = !primary ? [] : [primary, ...dependencies.sort()]; const esc = revisionConfiguration[revision].escapeTypeString; return newTypes .map((dependency) => { - const dependencyElements = types[dependency].map((t) => { + const dependencyElements = allTypes[dependency].map((t) => { const targetType = t.type === 'enum' && revision === Revision.Active ? (t as StarkNetEnumType).contains