@@ -161,7 +161,7 @@ impl TypedData {
161
161
return Err ( TypedDataError :: UnexpectedEnum ( name. to_owned ( ) ) ) ;
162
162
}
163
163
164
- self . encode_enum :: < H > ( type_hash , enum_def, obj_value) ?
164
+ self . encode_enum :: < H > ( enum_def, obj_value) ?
165
165
}
166
166
}
167
167
}
@@ -482,15 +482,20 @@ impl TypedData {
482
482
483
483
fn encode_enum < H > (
484
484
& self ,
485
- type_hash : Felt ,
486
485
enum_def : & EnumDefinition ,
487
486
value : & ObjectValue ,
488
487
) -> Result < Felt , TypedDataError >
489
488
where
490
489
H : TypedDataHasher ,
491
490
{
492
491
let mut hasher = H :: default ( ) ;
493
- hasher. update ( type_hash) ;
492
+
493
+ // Here we're NOT hashing the enum type hash. This is technically a SNIP-12 violation.
494
+ // Unfortunately, as the de-facto standard, starknet.js implemented it incorrectly. Despite
495
+ // the fix being merged (https://github.com/starknet-io/starknet.js/pull/1281) it's expected
496
+ // to never be released.
497
+ //
498
+ // Context: https://github.com/starknet-io/starknet.js/pull/1292
494
499
495
500
let mut value_field_iter = value. fields . iter ( ) ;
496
501
@@ -878,7 +883,56 @@ mod tests {
878
883
879
884
#[ test]
880
885
#[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test:: wasm_bindgen_test) ]
881
- fn test_message_hash_v1_with_enum ( ) {
886
+ fn test_message_hash_v1_with_simple_enum ( ) {
887
+ let raw = r###"{
888
+ "types": {
889
+ "StarknetDomain": [
890
+ { "name": "name", "type": "shortstring" },
891
+ { "name": "version", "type": "shortstring" },
892
+ { "name": "chainId", "type": "shortstring" },
893
+ { "name": "revision", "type": "shortstring" }
894
+ ],
895
+ "Example Message": [
896
+ { "name": "Value", "type": "enum", "contains": "My Enum" }
897
+ ],
898
+ "My Enum": [
899
+ { "name": "Variant 1", "type": "()" },
900
+ { "name": "Variant 2", "type": "(string)" },
901
+ { "name": "Variant 3", "type": "(u128)" }
902
+ ]
903
+ },
904
+ "primaryType": "Example Message",
905
+ "domain": {
906
+ "name": "Starknet Example",
907
+ "version": "1",
908
+ "chainId": "SN_MAIN",
909
+ "revision": "1"
910
+ },
911
+ "message": {
912
+ "Value": {
913
+ "Variant 2": ["tuple element"]
914
+ }
915
+ }
916
+ }"### ;
917
+
918
+ let data = serde_json:: from_str :: < TypedData > ( raw) . unwrap ( ) ;
919
+
920
+ assert_eq ! (
921
+ data. message_hash( Felt :: from_hex_unchecked( "0x1234" ) )
922
+ . unwrap( ) ,
923
+ // This expected hash was generated with starknet.js v6.24.0, due to the expectation
924
+ // that the following fixes, despite being merged, would never be released:
925
+ // - https://github.com/starknet-io/starknet.js/pull/1281
926
+ // - https://github.com/starknet-io/starknet.js/pull/1288
927
+ Felt :: from_hex_unchecked(
928
+ "0x05cb0569ef378e0c17c07c13cb86bc6e067f824ccffd79fd49d875ecc0296124"
929
+ )
930
+ ) ;
931
+ }
932
+
933
+ #[ test]
934
+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test:: wasm_bindgen_test) ]
935
+ fn test_message_hash_v1_with_enum_nested ( ) {
882
936
let raw = r###"{
883
937
"types": {
884
938
"StarknetDomain": [
@@ -931,8 +985,16 @@ mod tests {
931
985
assert_eq ! (
932
986
data. message_hash( Felt :: from_hex_unchecked( "0x1234" ) )
933
987
. unwrap( ) ,
988
+ // This expected hash was generated with starknet.js v6.24.0 patched with:
989
+ // - https://github.com/starknet-io/starknet.js/pull/1289
990
+ //
991
+ // Here a patched version is used as it's expected that the patch would eventually be
992
+ // released.
993
+ //
994
+ // See this thread for more context:
995
+ // - https://github.com/starknet-io/starknet.js/pull/1292
934
996
Felt :: from_hex_unchecked(
935
- "0x03745761c0f8ab5f0dbbba52b448f7db6ebfecbf74069073dcbf4fc5a6608125 "
997
+ "0x0470e6107a4d464e16d8f77ff673c06f6fbfe107fef1e496e53b10d3744afd42 "
936
998
)
937
999
) ;
938
1000
}
0 commit comments