Skip to content

Commit 5b7fdf1

Browse files
committed
Merge branch 'develop' into beta
2 parents 8449968 + 83a59c3 commit 5b7fdf1

File tree

4 files changed

+80
-17
lines changed

4 files changed

+80
-17
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525

2626
- Library defaults to RPC 0.8 with the corresponding API changes, dropped RPC 0.6 support
2727

28+
## [6.24.1](https://github.com/starknet-io/starknet.js/compare/v6.24.0...v6.24.1) (2025-03-20)
29+
30+
### Bug Fixes
31+
32+
- repair snip-12 enum type nested dependency ([#1289](https://github.com/starknet-io/starknet.js/issues/1289)) ([1cd4219](https://github.com/starknet-io/starknet.js/commit/1cd4219df5e61a676bc58ba8c8673269695fc2db))
33+
2834
# [6.24.0](https://github.com/starknet-io/starknet.js/compare/v6.23.1...v6.24.0) (2025-03-12)
2935

3036
### Features
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
`"0x8eb4aeac64b707f3e843284c4258df6df1f0f7fd38dcffdd8a153a495cd351"`
108113
);
114+
typeHash = getTypeHash(exampleEnumNested.types, 'Example', TypedDataRevision.ACTIVE);
115+
expect(typeHash).toMatchInlineSnapshot(
116+
`"0x2143bb787fabace39d62e9acf8b6e97d9a369000516c3e6ffd963dc1370fc1a"`
117+
);
109118
});
110119

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

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

src/utils/typedData.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,36 +167,46 @@ export function getDependencies(
167167
contains: string = '',
168168
revision: Revision = Revision.LEGACY
169169
): string[] {
170+
let dependencyTypes: string[] = [type];
171+
170172
// Include pointers (struct arrays)
171173
if (type[type.length - 1] === '*') {
172-
type = type.slice(0, -1);
174+
dependencyTypes = [type.slice(0, -1)];
173175
} else if (revision === Revision.ACTIVE) {
174176
// enum base
175177
if (type === 'enum') {
176-
type = contains;
178+
dependencyTypes = [contains];
177179
}
178180
// enum element types
179181
else if (type.match(/^\(.*\)$/)) {
180-
type = type.slice(1, -1);
182+
dependencyTypes = type
183+
.slice(1, -1)
184+
.split(',')
185+
.map((depType) => (depType[depType.length - 1] === '*' ? depType.slice(0, -1) : depType));
181186
}
182187
}
183188

184-
if (dependencies.includes(type) || !types[type]) {
185-
return dependencies;
186-
}
187-
188-
return [
189-
type,
190-
...(types[type] as StarknetEnumType[]).reduce<string[]>(
191-
(previous, t) => [
192-
...previous,
193-
...getDependencies(types, t.type, previous, t.contains, revision).filter(
194-
(dependency) => !previous.includes(dependency)
195-
),
189+
return dependencyTypes
190+
.filter((t) => !dependencies.includes(t) && types[t])
191+
.reduce<string[]>(
192+
// This comment prevents prettier from rolling everything here into a single line.
193+
(p, depType) => [
194+
...p,
195+
...[
196+
depType,
197+
...(types[depType] as StarknetEnumType[]).reduce<string[]>(
198+
(previous, t) => [
199+
...previous,
200+
...getDependencies(types, t.type, previous, t.contains, revision).filter(
201+
(dependency) => !previous.includes(dependency)
202+
),
203+
],
204+
[]
205+
),
206+
].filter((dependency) => !p.includes(dependency)),
196207
],
197208
[]
198-
),
199-
];
209+
);
200210
}
201211

202212
function getMerkleTreeType(types: TypedData['types'], ctx: Context) {

0 commit comments

Comments
 (0)