From ce7dbe0562aff47acb31882901965bef2c07fc7d Mon Sep 17 00:00:00 2001 From: Jonathan LEI Date: Thu, 2 Jan 2025 02:22:14 +0800 Subject: [PATCH] fix: missing snip-12 enum type dependency --- src/utils/typedData.ts | 44 ++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/utils/typedData.ts b/src/utils/typedData.ts index fc9407c6c..6b9e44746 100644 --- a/src/utils/typedData.ts +++ b/src/utils/typedData.ts @@ -167,36 +167,46 @@ export function getDependencies( contains: string = '', revision: Revision = Revision.LEGACY ): string[] { + let dependencyTypes: string[] = [type]; + // Include pointers (struct arrays) if (type[type.length - 1] === '*') { - type = type.slice(0, -1); + dependencyTypes = [type.slice(0, -1)]; } else if (revision === Revision.ACTIVE) { // enum base if (type === 'enum') { - type = contains; + dependencyTypes = [contains]; } // enum element types else if (type.match(/^\(.*\)$/)) { - type = type.slice(1, -1); + dependencyTypes = type + .slice(1, -1) + .split(',') + .map((depType) => (depType[depType.length - 1] === '*' ? depType.slice(0, -1) : depType)); } } - if (dependencies.includes(type) || !types[type]) { - return dependencies; - } - - return [ - type, - ...(types[type] as StarknetEnumType[]).reduce( - (previous, t) => [ - ...previous, - ...getDependencies(types, t.type, previous, t.contains, revision).filter( - (dependency) => !previous.includes(dependency) - ), + return dependencyTypes + .filter((t) => !dependencies.includes(t) && types[t]) + .reduce( + // This comment prevents prettier from rolling everything here into a single line. + (p, depType) => [ + ...p, + ...[ + depType, + ...(types[depType] as StarknetEnumType[]).reduce( + (previous, t) => [ + ...previous, + ...getDependencies(types, t.type, previous, t.contains, revision).filter( + (dependency) => !previous.includes(dependency) + ), + ], + [] + ), + ].filter((dependency) => !p.includes(dependency)), ], [] - ), - ]; + ); } function getMerkleTreeType(types: TypedData['types'], ctx: Context) {