Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing snip-12 enum type dependency #1289

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions __mocks__/typedData/example_enumNested.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"types": {
"StarknetDomain": [
{ "name": "name", "type": "shortstring" },
{ "name": "version", "type": "shortstring" },
{ "name": "chainId", "type": "shortstring" },
{ "name": "revision", "type": "shortstring" }
],
"Example": [{ "name": "someEnum", "type": "enum", "contains": "EnumA" }],
"EnumA": [
{ "name": "Variant 1", "type": "()" },
{ "name": "Variant 2", "type": "(u128,StructA)" }
],
"StructA": [{ "name": "nestedEnum", "type": "enum", "contains": "EnumB" }],
"EnumB": [
{ "name": "Variant A", "type": "()" },
{ "name": "Variant B", "type": "(StructB*)" }
],
"StructB": [{ "name": "flag", "type": "bool" }]
},
"primaryType": "Example",
"domain": {
"name": "StarkNet Mail",
"version": "1",
"chainId": "1",
"revision": "1"
},
"message": {
"someEnum": {
"Variant 2": [2, { "nestedEnum": { "Variant B": [[{ "flag": true }, { "flag": false }]] } }]
}
}
}
14 changes: 14 additions & 0 deletions __tests__/utils/typedData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as starkCurve from '@scure/starknet';
import typedDataExample from '../../__mocks__/typedData/baseExample.json';
import exampleBaseTypes from '../../__mocks__/typedData/example_baseTypes.json';
import exampleEnum from '../../__mocks__/typedData/example_enum.json';
import exampleEnumNested from '../../__mocks__/typedData/example_enumNested.json';
import examplePresetTypes from '../../__mocks__/typedData/example_presetTypes.json';
import typedDataStructArrayExample from '../../__mocks__/typedData/mail_StructArray.json';
import typedDataSessionExample from '../../__mocks__/typedData/session_MerkleTree.json';
Expand Down Expand Up @@ -66,6 +67,10 @@ describe('typedData', () => {
expect(encoded).toMatchInlineSnapshot(
`"\\"Example\\"(\\"someEnum1\\":\\"EnumA\\",\\"someEnum2\\":\\"EnumB\\")\\"EnumA\\"(\\"Variant 1\\"(),\\"Variant 2\\"(\\"u128\\",\\"u128*\\"),\\"Variant 3\\"(\\"u128\\"))\\"EnumB\\"(\\"Variant 1\\"(),\\"Variant 2\\"(\\"u128\\"))"`
);
encoded = encodeType(exampleEnumNested.types, 'Example', TypedDataRevision.ACTIVE);
expect(encoded).toMatchInlineSnapshot(
`"\\"Example\\"(\\"someEnum\\":\\"EnumA\\")\\"EnumA\\"(\\"Variant 1\\"(),\\"Variant 2\\"(\\"u128\\",\\"StructA\\"))\\"EnumB\\"(\\"Variant A\\"(),\\"Variant B\\"(\\"StructB*\\"))\\"StructA\\"(\\"nestedEnum\\":\\"EnumB\\")\\"StructB\\"(\\"flag\\":\\"bool\\")"`
);
});

test('should get right type hash', () => {
Expand Down Expand Up @@ -106,6 +111,10 @@ describe('typedData', () => {
expect(typeHash).toMatchInlineSnapshot(
`"0x393bf83422ca8626a2932696cfa0acb19dcad6de2fe84a2dd2ca7607ea5329a"`
);
typeHash = getTypeHash(exampleEnumNested.types, 'Example', TypedDataRevision.ACTIVE);
expect(typeHash).toMatchInlineSnapshot(
`"0x267f739fd83d30528a0fafb23df33b6c35ca0a5adbcfb32152721478fa9d0ce"`
);
});

test('should transform type selector', () => {
Expand Down Expand Up @@ -329,6 +338,11 @@ describe('typedData', () => {
`"0x150a589bb56a4fbf4ee01f52e44fd5adde6af94c02b37e383413fed185321a2"`
);

messageHash = getMessageHash(exampleEnumNested, exampleAddress);
expect(messageHash).toMatchInlineSnapshot(
`"0x6e70eb4ef625dda451094716eee7f31fa81ca0ba99d390885e9c7b0d64cd22"`
);

expect(spyPedersen).not.toHaveBeenCalled();
expect(spyPoseidon).toHaveBeenCalled();
spyPedersen.mockRestore();
Expand Down
44 changes: 27 additions & 17 deletions src/utils/typedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>(
(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<string[]>(
// This comment prevents prettier from rolling everything here into a single line.
(p, depType) => [
...p,
...[
depType,
...(types[depType] as StarknetEnumType[]).reduce<string[]>(
(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) {
Expand Down
Loading