6
6
"os"
7
7
"testing"
8
8
9
+ "github.com/stretchr/testify/assert"
9
10
"github.com/stretchr/testify/require"
10
11
)
11
12
@@ -30,6 +31,7 @@ func TestMain(m *testing.M) {
30
31
"session_MerkleTree" ,
31
32
"v1Nested" ,
32
33
"allInOne" ,
34
+ "example_enumNested" ,
33
35
}
34
36
35
37
for _ , fileName := range fileNames {
@@ -82,63 +84,71 @@ func BMockTypedData(b *testing.B) (ttd TypedData) {
82
84
// - None
83
85
func TestGetMessageHash (t * testing.T ) {
84
86
type testSetType struct {
85
- TypedData TypedData
87
+ TypedDataName string
86
88
Address string
87
89
ExpectedMessageHash string
88
90
}
89
91
testSet := []testSetType {
90
92
{
91
- TypedData : typedDataExamples [ "baseExample" ] ,
93
+ TypedDataName : "baseExample" ,
92
94
Address : "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" ,
93
95
ExpectedMessageHash : "0x6fcff244f63e38b9d88b9e3378d44757710d1b244282b435cb472053c8d78d0" ,
94
96
},
95
97
{
96
- TypedData : typedDataExamples [ "example_array" ] ,
98
+ TypedDataName : "example_array" ,
97
99
Address : "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" ,
98
100
ExpectedMessageHash : "0x88edea26d6177a8bc545b2e73c960ab7ddd67b46237b386b514e50315ce0f4" ,
99
101
},
100
102
{
101
- TypedData : typedDataExamples [ "example_baseTypes" ] ,
103
+ TypedDataName : "example_baseTypes" ,
102
104
Address : "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" ,
103
105
ExpectedMessageHash : "0xdb7829db8909c0c5496f5952bcfc4fc894341ce01842537fc4f448743480b6" ,
104
106
},
105
107
{
106
- TypedData : typedDataExamples [ "example_presetTypes" ] ,
108
+ TypedDataName : "example_presetTypes" ,
107
109
Address : "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" ,
108
110
ExpectedMessageHash : "0x185b339d5c566a883561a88fb36da301051e2c0225deb325c91bb7aa2f3473a" ,
109
111
},
110
112
{
111
- TypedData : typedDataExamples [ "session_MerkleTree" ] ,
113
+ TypedDataName : "session_MerkleTree" ,
112
114
Address : "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" ,
113
115
ExpectedMessageHash : "0x751fb7d98545f7649d0d0eadc80d770fcd88d8cfaa55590b284f4e1b701ef0a" ,
114
116
},
115
117
{
116
- TypedData : typedDataExamples [ "mail_StructArray" ] ,
118
+ TypedDataName : "mail_StructArray" ,
117
119
Address : "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" ,
118
120
ExpectedMessageHash : "0x5914ed2764eca2e6a41eb037feefd3d2e33d9af6225a9e7fe31ac943ff712c" ,
119
121
},
120
122
{
121
- TypedData : typedDataExamples [ "v1Nested" ] ,
123
+ TypedDataName : "v1Nested" ,
122
124
Address : "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" ,
123
125
ExpectedMessageHash : "0x69b57cf0cd7c151c51f9616cc58a1f0a877fec28c8c15ff7537cf777c54a30d" ,
124
126
},
125
127
{
126
- TypedData : typedDataExamples [ "example_enum" ] ,
128
+ TypedDataName : "example_enum" ,
127
129
Address : "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" ,
128
- ExpectedMessageHash : "0x416b85b18063b1b3420ab709e9d5e35cb716691d397c5841ce7c5198ee30bf " ,
130
+ ExpectedMessageHash : "0x6e61abaf480b1370bbf231f54e298c5f4872f40a6d2dd409ff30accee5bbd1e " ,
129
131
},
130
132
{
131
- TypedData : typedDataExamples [ "allInOne" ] ,
133
+ TypedDataName : "allInOne" ,
132
134
Address : "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" ,
133
- ExpectedMessageHash : "0x300dc63cc85a15529bba5ed482009be716645fa9f2c64bd8716cf6f34767651" ,
135
+ ExpectedMessageHash : "0x8fa4e453de78c2762493760efd449a38eb46f85b2e02b116b77b3daa9075c8" ,
136
+ },
137
+ {
138
+ TypedDataName : "example_enumNested" ,
139
+ Address : "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" ,
140
+ ExpectedMessageHash : "0x691fc54567306a8ea5431130f1b98299e74a748ac391540a86736f20ef5f2b7" ,
134
141
},
135
142
}
136
143
137
144
for _ , test := range testSet {
138
- hash , err := test .TypedData .GetMessageHash (test .Address )
139
- require .NoError (t , err )
145
+ t .Run (test .TypedDataName , func (t * testing.T ) {
146
+ td := typedDataExamples [test .TypedDataName ]
147
+ hash , err := td .GetMessageHash (test .Address )
148
+ require .NoError (t , err )
140
149
141
- require .Equal (t , test .ExpectedMessageHash , hash .String ())
150
+ assert .Equal (t , test .ExpectedMessageHash , hash .String ())
151
+ })
142
152
}
143
153
}
144
154
@@ -177,42 +187,50 @@ func BenchmarkGetMessageHash(b *testing.B) {
177
187
// none
178
188
func TestGetTypeHash (t * testing.T ) {
179
189
type testSetType struct {
180
- TypedData TypedData
181
- TypeName string
182
- ExpectedHash string
190
+ TypedDataName string
191
+ TypeName string
192
+ ExpectedHash string
183
193
}
184
194
testSet := []testSetType {
185
195
{
186
- TypedData : typedDataExamples ["baseExample" ],
187
- TypeName : "StarkNetDomain" ,
188
- ExpectedHash : "0x1bfc207425a47a5dfa1a50a4f5241203f50624ca5fdf5e18755765416b8e288" ,
196
+ TypedDataName : "baseExample" ,
197
+ TypeName : "StarkNetDomain" ,
198
+ ExpectedHash : "0x1bfc207425a47a5dfa1a50a4f5241203f50624ca5fdf5e18755765416b8e288" ,
199
+ },
200
+ {
201
+ TypedDataName : "baseExample" ,
202
+ TypeName : "Mail" ,
203
+ ExpectedHash : "0x13d89452df9512bf750f539ba3001b945576243288137ddb6c788457d4b2f79" ,
189
204
},
190
205
{
191
- TypedData : typedDataExamples [ "baseExample" ] ,
192
- TypeName : "Mail " ,
193
- ExpectedHash : "0x13d89452df9512bf750f539ba3001b945576243288137ddb6c788457d4b2f79 " ,
206
+ TypedDataName : "example_baseTypes" ,
207
+ TypeName : "Example " ,
208
+ ExpectedHash : "0x1f94cd0be8b4097a41486170fdf09a4cd23aefbc74bb2344718562994c2c111 " ,
194
209
},
195
210
{
196
- TypedData : typedDataExamples [ "example_baseTypes" ] ,
197
- TypeName : "Example" ,
198
- ExpectedHash : "0x1f94cd0be8b4097a41486170fdf09a4cd23aefbc74bb2344718562994c2c111 " ,
211
+ TypedDataName : "example_presetTypes" ,
212
+ TypeName : "Example" ,
213
+ ExpectedHash : "0x1a25a8bb84b761090b1fadaebe762c4b679b0d8883d2bedda695ea340839a55 " ,
199
214
},
200
215
{
201
- TypedData : typedDataExamples [ "example_presetTypes" ] ,
202
- TypeName : "Example " ,
203
- ExpectedHash : "0x1a25a8bb84b761090b1fadaebe762c4b679b0d8883d2bedda695ea340839a55 " ,
216
+ TypedDataName : "session_MerkleTree" ,
217
+ TypeName : "Session " ,
218
+ ExpectedHash : "0x1aa0e1c56b45cf06a54534fa1707c54e520b842feb21d03b7deddb6f1e340c " ,
204
219
},
205
220
{
206
- TypedData : typedDataExamples [ "session_MerkleTree" ] ,
207
- TypeName : "Session " ,
208
- ExpectedHash : "0x1aa0e1c56b45cf06a54534fa1707c54e520b842feb21d03b7deddb6f1e340c " ,
221
+ TypedDataName : "example_enumNested" ,
222
+ TypeName : "Example " ,
223
+ ExpectedHash : "0x2143bb787fabace39d62e9acf8b6e97d9a369000516c3e6ffd963dc1370fc1a " ,
209
224
},
210
225
}
211
226
for _ , test := range testSet {
212
- hash , err := test .TypedData .GetTypeHash (test .TypeName )
213
- require .NoError (t , err )
227
+ t .Run (test .TypedDataName , func (t * testing.T ) {
228
+ td := typedDataExamples [test .TypedDataName ]
229
+ hash , err := td .GetTypeHash (test .TypeName )
230
+ require .NoError (t , err )
214
231
215
- require .Equal (t , test .ExpectedHash , hash .String ())
232
+ assert .Equal (t , test .ExpectedHash , hash .String ())
233
+ })
216
234
}
217
235
}
218
236
@@ -230,59 +248,67 @@ func TestGetTypeHash(t *testing.T) {
230
248
// none
231
249
func TestEncodeType (t * testing.T ) {
232
250
type testSetType struct {
233
- TypedData TypedData
251
+ TypedDataName string
234
252
TypeName string
235
253
ExpectedEncode string
236
254
}
237
255
testSet := []testSetType {
238
256
{
239
- TypedData : typedDataExamples [ "baseExample" ] ,
257
+ TypedDataName : "baseExample" ,
240
258
TypeName : "StarkNetDomain" ,
241
259
ExpectedEncode : "StarkNetDomain(name:felt,version:felt,chainId:felt)" ,
242
260
},
243
261
{
244
- TypedData : typedDataExamples [ "baseExample" ] ,
262
+ TypedDataName : "baseExample" ,
245
263
TypeName : "Mail" ,
246
264
ExpectedEncode : "Mail(from:Person,to:Person,contents:felt)Person(name:felt,wallet:felt)" ,
247
265
},
248
266
{
249
- TypedData : typedDataExamples [ "example_array" ] ,
267
+ TypedDataName : "example_array" ,
250
268
TypeName : "StarknetDomain" ,
251
269
ExpectedEncode : `"StarknetDomain"("name":"shortstring","version":"shortstring","chainId":"shortstring","revision":"shortstring")` ,
252
270
},
253
271
{
254
- TypedData : typedDataExamples [ "example_baseTypes" ] ,
272
+ TypedDataName : "example_baseTypes" ,
255
273
TypeName : "Example" ,
256
274
ExpectedEncode : `"Example"("n0":"felt","n1":"bool","n2":"string","n3":"selector","n4":"u128","n5":"i128","n6":"ContractAddress","n7":"ClassHash","n8":"timestamp","n9":"shortstring")` ,
257
275
},
258
276
{
259
- TypedData : typedDataExamples [ "example_presetTypes" ] ,
277
+ TypedDataName : "example_presetTypes" ,
260
278
TypeName : "Example" ,
261
279
ExpectedEncode : `"Example"("n0":"TokenAmount","n1":"NftId")"NftId"("collection_address":"ContractAddress","token_id":"u256")"TokenAmount"("token_address":"ContractAddress","amount":"u256")"u256"("low":"u128","high":"u128")` ,
262
280
},
263
281
{
264
- TypedData : typedDataExamples [ "session_MerkleTree" ] ,
282
+ TypedDataName : "session_MerkleTree" ,
265
283
TypeName : "Session" ,
266
284
ExpectedEncode : `Session(key:felt,expires:felt,root:merkletree)` ,
267
285
},
268
286
{
269
- TypedData : typedDataExamples [ "mail_StructArray" ] ,
287
+ TypedDataName : "mail_StructArray" ,
270
288
TypeName : "Mail" ,
271
289
ExpectedEncode : `Mail(from:Person,to:Person,posts_len:felt,posts:Post*)Person(name:felt,wallet:felt)Post(title:felt,content:felt)` ,
272
290
},
273
291
{
274
- TypedData : typedDataExamples [ "v1Nested" ] ,
292
+ TypedDataName : "v1Nested" ,
275
293
TypeName : "TransferERC721" ,
276
294
ExpectedEncode : `"TransferERC721"("MessageId":"felt","From":"Account1","To":"Account1","Nft_to_transfer":"Nft","Comment1":"string","Comment2":"string","Comment3":"string")"Account1"("Name":"string","Address":"felt")"Nft"("Collection":"string","Address":"felt","Nft_id":"felt","Negotiated_for":"Transaction")"Transaction"("Qty":"string","Unit":"string","Token_address":"felt","Amount":"felt")` ,
277
295
},
278
296
{
279
- TypedData : typedDataExamples [ "example_enum" ] ,
297
+ TypedDataName : "example_enum" ,
280
298
TypeName : "Example" ,
281
299
ExpectedEncode : `"Example"("someEnum1":"EnumA","someEnum2":"EnumB")"EnumA"("Variant 1":(),"Variant 2":("u128","u128*"),"Variant 3":("u128"))"EnumB"("Variant 1":(),"Variant 2":("u128"))` ,
282
300
},
301
+ {
302
+ TypedDataName : "example_enumNested" ,
303
+ TypeName : "Example" ,
304
+ ExpectedEncode : `"Example"("someEnum":"EnumA")"EnumA"("Variant 1":(),"Variant 2":("u128","StructA"))"EnumB"("Variant A":(),"Variant B":("StructB*"))"StructA"("nestedEnum":"EnumB")"StructB"("flag":"bool")` ,
305
+ },
283
306
}
284
307
for _ , test := range testSet {
285
- require .Equal (t , test .ExpectedEncode , test .TypedData .Types [test .TypeName ].EncoddingString )
308
+ t .Run (test .TypedDataName , func (t * testing.T ) {
309
+ td := typedDataExamples [test .TypedDataName ]
310
+ assert .Equal (t , test .ExpectedEncode , td .Types [test .TypeName ].EncoddingString )
311
+ })
286
312
}
287
313
}
288
314
@@ -300,47 +326,48 @@ func TestEncodeType(t *testing.T) {
300
326
// none
301
327
func TestGetStructHash (t * testing.T ) {
302
328
type testSetType struct {
303
- TypedData TypedData
304
- TypeName string
305
- Context []string
306
- ExpectedHash string
329
+ TypedDataName string
330
+ TypeName string
331
+ Context []string
332
+ ExpectedHash string
307
333
}
308
334
testSet := []testSetType {
309
335
{
310
- TypedData : typedDataExamples [ "baseExample" ] ,
311
- TypeName : "StarkNetDomain" ,
312
- ExpectedHash : "0x54833b121883a3e3aebff48ec08a962f5742e5f7b973469c1f8f4f55d470b07" ,
336
+ TypedDataName : "baseExample" ,
337
+ TypeName : "StarkNetDomain" ,
338
+ ExpectedHash : "0x54833b121883a3e3aebff48ec08a962f5742e5f7b973469c1f8f4f55d470b07" ,
313
339
},
314
340
{
315
- TypedData : typedDataExamples [ "example_baseTypes" ] ,
316
- TypeName : "Example" ,
317
- ExpectedHash : "0x75db031c1f5bf980cc48f46943b236cb85a95c8f3b3c8203572453075d3d39" ,
341
+ TypedDataName : "example_baseTypes" ,
342
+ TypeName : "Example" ,
343
+ ExpectedHash : "0x75db031c1f5bf980cc48f46943b236cb85a95c8f3b3c8203572453075d3d39" ,
318
344
},
319
345
{
320
- TypedData : typedDataExamples [ "example_presetTypes" ] ,
321
- TypeName : "Example" ,
322
- ExpectedHash : "0x74fba3f77f8a6111a9315bac313bf75ecfa46d1234e0fda60312fb6a6517667" ,
346
+ TypedDataName : "example_presetTypes" ,
347
+ TypeName : "Example" ,
348
+ ExpectedHash : "0x74fba3f77f8a6111a9315bac313bf75ecfa46d1234e0fda60312fb6a6517667" ,
323
349
},
324
350
{
325
- TypedData : typedDataExamples [ "session_MerkleTree" ] ,
326
- TypeName : "Session" ,
327
- ExpectedHash : "0x73602062421caf6ad2e942253debfad4584bff58930981364dcd378021defe8" ,
351
+ TypedDataName : "session_MerkleTree" ,
352
+ TypeName : "Session" ,
353
+ ExpectedHash : "0x73602062421caf6ad2e942253debfad4584bff58930981364dcd378021defe8" ,
328
354
},
329
355
{
330
- TypedData : typedDataExamples [ "v1Nested" ] ,
331
- TypeName : "TransferERC721" ,
332
- ExpectedHash : "0x11b5fb80dd88c3d8b6239b065def4ac9a79e6995b117ed5940a3a0734324b79" ,
356
+ TypedDataName : "v1Nested" ,
357
+ TypeName : "TransferERC721" ,
358
+ ExpectedHash : "0x11b5fb80dd88c3d8b6239b065def4ac9a79e6995b117ed5940a3a0734324b79" ,
333
359
},
334
360
{
335
- TypedData : typedDataExamples [ "example_enum" ] ,
336
- TypeName : "Example" ,
337
- ExpectedHash : "0x1551dc992033e2256a2f7ec849495d90f9759ebb535e3006d16e2b9e3b57b4c " ,
361
+ TypedDataName : "example_enum" ,
362
+ TypeName : "Example" ,
363
+ ExpectedHash : "0x1e1bb5d477e92cbf562b3b766c5c1e5f8590f2df868d4c8249c0db8416f8c37 " ,
338
364
},
339
365
}
340
366
for _ , test := range testSet {
341
- hash , err := test .TypedData .GetStructHash (test .TypeName , test .Context ... )
367
+ td := typedDataExamples [test .TypedDataName ]
368
+ hash , err := td .GetStructHash (test .TypeName , test .Context ... )
342
369
require .NoError (t , err )
343
370
344
- require .Equal (t , test .ExpectedHash , hash .String ())
371
+ assert .Equal (t , test .ExpectedHash , hash .String ())
345
372
}
346
373
}
0 commit comments