Skip to content

Commit 8ec1e76

Browse files
committed
feat: add hex string check in encodePieceOfData function
- Introduced a new helper function isHexString to determine if a value is a valid hex string. - Updated the encodePieceOfData function to skip hashing for already hashed selectors, avoiding wrong struct hashes due to hashing the wrong value.
1 parent 4cf08ae commit 8ec1e76

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

typedData/typedData.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,12 +819,27 @@ func encodePieceOfData(typeName string, data any, rev *revision) (resp *felt.Fel
819819
case "selector":
820820
value := fmt.Sprintf("%v", data)
821821

822+
// To the case of the value being an already hashed selector, so we
823+
// skip the hashing step
824+
if isHexString(value) {
825+
return internalUtils.HexToFelt(value)
826+
}
827+
822828
return internalUtils.GetSelectorFromNameFelt(value), nil
823829
default:
824830
return resp, fmt.Errorf("invalid type '%s'", typeName)
825831
}
826832
}
827833

834+
func isHexString(str string) bool {
835+
res, err := regexp.MatchString("^0x[0-9a-fA-F]+$", str)
836+
if err != nil {
837+
return false
838+
}
839+
840+
return res
841+
}
842+
828843
// UnmarshalJSON implements the json.Unmarshaler interface for TypedData
829844
func (td *TypedData) UnmarshalJSON(data []byte) error {
830845
var dec map[string]json.RawMessage

0 commit comments

Comments
 (0)