Skip to content

Commit e57eaf3

Browse files
committed
test: add nested enum tests
1 parent 0cffd40 commit e57eaf3

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"types": {
3+
"StarknetDomain": [
4+
{ "name": "name", "type": "shortstring" },
5+
{ "name": "version", "type": "shortstring" },
6+
{ "name": "chainId", "type": "shortstring" },
7+
{ "name": "revision", "type": "shortstring" }
8+
],
9+
"Example": [{ "name": "someEnum", "type": "enum", "contains": "EnumA" }],
10+
"EnumA": [
11+
{ "name": "Variant 1", "type": "()" },
12+
{ "name": "Variant 2", "type": "(u128,StructA)" }
13+
],
14+
"StructA": [{ "name": "nestedEnum", "type": "enum", "contains": "EnumB" }],
15+
"EnumB": [
16+
{ "name": "Variant A", "type": "()" },
17+
{ "name": "Variant B", "type": "(StructB*)" }
18+
],
19+
"StructB": [{ "name": "flag", "type": "bool" }]
20+
},
21+
"primaryType": "Example",
22+
"domain": {
23+
"name": "StarkNet Mail",
24+
"version": "1",
25+
"chainId": "1",
26+
"revision": "1"
27+
},
28+
"message": {
29+
"someEnum": {
30+
"Variant 2": [2, { "nestedEnum": { "Variant B": [[{ "flag": true }, { "flag": false }]] } }]
31+
}
32+
}
33+
}

__tests__/utils/typedData.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as starkCurve from '@scure/starknet';
33
import typedDataExample from '../../__mocks__/typedData/baseExample.json';
44
import exampleBaseTypes from '../../__mocks__/typedData/example_baseTypes.json';
55
import exampleEnum from '../../__mocks__/typedData/example_enum.json';
6+
import exampleEnumNested from '../../__mocks__/typedData/example_enumNested.json';
67
import examplePresetTypes from '../../__mocks__/typedData/example_presetTypes.json';
78
import typedDataStructArrayExample from '../../__mocks__/typedData/mail_StructArray.json';
89
import typedDataSessionExample from '../../__mocks__/typedData/session_MerkleTree.json';
@@ -66,6 +67,10 @@ describe('typedData', () => {
6667
expect(encoded).toMatchInlineSnapshot(
6768
`"\\"Example\\"(\\"someEnum1\\":\\"EnumA\\",\\"someEnum2\\":\\"EnumB\\")\\"EnumA\\"(\\"Variant 1\\"(),\\"Variant 2\\"(\\"u128\\",\\"u128*\\"),\\"Variant 3\\"(\\"u128\\"))\\"EnumB\\"(\\"Variant 1\\"(),\\"Variant 2\\"(\\"u128\\"))"`
6869
);
70+
encoded = encodeType(exampleEnumNested.types, 'Example', TypedDataRevision.ACTIVE);
71+
expect(encoded).toMatchInlineSnapshot(
72+
`"\\"Example\\"(\\"someEnum\\":\\"EnumA\\")\\"EnumA\\"(\\"Variant 1\\"(),\\"Variant 2\\"(\\"u128\\",\\"StructA\\"))\\"EnumB\\"(\\"Variant A\\"(),\\"Variant B\\"(\\"StructB*\\"))\\"StructA\\"(\\"nestedEnum\\":\\"EnumB\\")\\"StructB\\"(\\"flag\\":\\"bool\\")"`
73+
);
6974
});
7075

7176
test('should get right type hash', () => {
@@ -106,6 +111,10 @@ describe('typedData', () => {
106111
expect(typeHash).toMatchInlineSnapshot(
107112
`"0x393bf83422ca8626a2932696cfa0acb19dcad6de2fe84a2dd2ca7607ea5329a"`
108113
);
114+
typeHash = getTypeHash(exampleEnumNested.types, 'Example', TypedDataRevision.ACTIVE);
115+
expect(typeHash).toMatchInlineSnapshot(
116+
`"0x267f739fd83d30528a0fafb23df33b6c35ca0a5adbcfb32152721478fa9d0ce"`
117+
);
109118
});
110119

111120
test('should transform type selector', () => {
@@ -329,6 +338,11 @@ describe('typedData', () => {
329338
`"0x150a589bb56a4fbf4ee01f52e44fd5adde6af94c02b37e383413fed185321a2"`
330339
);
331340

341+
messageHash = getMessageHash(exampleEnumNested, exampleAddress);
342+
expect(messageHash).toMatchInlineSnapshot(
343+
`"0x6e70eb4ef625dda451094716eee7f31fa81ca0ba99d390885e9c7b0d64cd22"`
344+
);
345+
332346
expect(spyPedersen).not.toHaveBeenCalled();
333347
expect(spyPoseidon).toHaveBeenCalled();
334348
spyPedersen.mockRestore();

0 commit comments

Comments
 (0)