Skip to content

Commit

Permalink
chore: simplify enum and array handling
Browse files Browse the repository at this point in the history
  • Loading branch information
penovicp committed Jan 30, 2024
1 parent 8606094 commit d59d369
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
2 changes: 1 addition & 1 deletion __mocks__/typedData/example_enum.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"Example": [{ "name": "someEnum", "type": "enum", "contains": "MyEnum" }],
"MyEnum": [
{ "name": "Variant 1", "type": "()" },
{ "name": "Variant 2", "type": "(u128, u128*)" },
{ "name": "Variant 2", "type": "(u128,u128*)" },
{ "name": "Variant 3", "type": "(u128)" }
]
},
Expand Down
16 changes: 5 additions & 11 deletions src/utils/typedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export function encodeType(
? `(${targetType
.slice(1, -1)
.split(',')
.map((e) => (e ? esc(e.trim()) : e))
.map((e) => (e ? esc(e) : e))
.join(',')})`
: esc(targetType);
return `${esc(t.name)}:${typeString}`;
Expand Down Expand Up @@ -254,15 +254,9 @@ export function encodeValue(
}

if (type.endsWith('*')) {
const isStructArray = Object.keys(types)
.map((x) => `${x}*`)
.includes(type);

const mappingMethod: (entry: Record<string, unknown>) => string = isStructArray
? (entry) => getStructHash(types, type.slice(0, -1), entry, revision)
: (entry) => encodeValue(types, type.slice(0, -1), entry, undefined, revision)[1];

const hashes: string[] = (data as Array<TypedData['message']>).map(mappingMethod);
const hashes: string[] = (data as Array<TypedData['message']>).map(
(entry) => encodeValue(types, type.slice(0, -1), entry, undefined, revision)[1]
);
return [type, revisionConfiguration[revision].hashMethod(hashes)];
}

Expand All @@ -282,7 +276,7 @@ export function encodeValue(
.map((subtype, index) => {
if (!subtype) return subtype;
const subtypeData = (variantData as unknown[])[index];
return encodeValue(types, subtype.trim(), subtypeData, undefined, revision)[1];
return encodeValue(types, subtype, subtypeData, undefined, revision)[1];
});
return [
type,
Expand Down

0 comments on commit d59d369

Please sign in to comment.