Skip to content

Commit

Permalink
adds support to 'mail_StructArray' json example
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Nov 26, 2024
1 parent 970e6e5 commit b0af345
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
23 changes: 11 additions & 12 deletions typed/typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,27 +228,28 @@ func encodeType(typeName string, types map[string]TypeDefinition, revision *revi
if i != (len(typeDef.Parameters) - 1) {
buf.WriteString(",")
}
singleTypeName, _ := strings.CutSuffix(param.Type, "*")

if isBasicType(param.Type) {
if isBasicType(singleTypeName) {
continue
} else if _, ok = customTypesEncodeResp[param.Type]; !ok {
if isPresetType(param.Type) {
typeDef, ok := revision.Types().Preset[param.Type]
} else if _, ok = customTypesEncodeResp[singleTypeName]; !ok {
if isPresetType(singleTypeName) {
typeDef, ok := revision.Types().Preset[singleTypeName]
if !ok {
return result, fmt.Errorf("error trying to get the type definition of '%s'", param.Type)
return result, fmt.Errorf("error trying to get the type definition of '%s'", singleTypeName)
}
customTypesEncodeResp[param.Type], err = getEncodeType(param.Type, typeDef)
customTypesEncodeResp[singleTypeName], err = getEncodeType(singleTypeName, typeDef)
if err != nil {
return "", err
}

continue
}
customTypeDef, ok := types[param.Type]
customTypeDef, ok := types[singleTypeName]
if !ok {
return "", fmt.Errorf("can't parse type %s from types %v", param.Type, types)
return "", fmt.Errorf("can't parse type %s from types %v", singleTypeName, types)
}
customTypesEncodeResp[param.Type], err = getEncodeType(param.Type, customTypeDef)
customTypesEncodeResp[singleTypeName], err = getEncodeType(singleTypeName, customTypeDef)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -401,8 +402,6 @@ func encodeData(
} else {
localArr = append(localArr, rev.HashMerkleMethod(felts[i], felts[i+1]))
}
oi := utils.FeltArrToStringArr(localArr)
fmt.Print(oi)
}

return handleMerkleTree(localArr)
Expand Down Expand Up @@ -444,7 +443,7 @@ func encodeData(
localEncode = append(localEncode, resp)
}

if isMerkle[0] {
if len(isMerkle) != 0 {
return handleMerkleTree(localEncode), nil
}
return rev.HashMethod(localEncode...), nil
Expand Down
12 changes: 11 additions & 1 deletion typed/typed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestMain(m *testing.M) {
"example_baseTypes",
// "example_enum",
"example_presetTypes",
// "mail_StructArray",
"mail_StructArray",
"session_MerkleTree",
// "v1Nested",
}
Expand Down Expand Up @@ -294,6 +294,11 @@ func TestGetMessageHash(t *testing.T) {
Address: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
ExpectedMessageHash: "0x751fb7d98545f7649d0d0eadc80d770fcd88d8cfaa55590b284f4e1b701ef0a",
},
{
TypedData: typedDataExamples["mail_StructArray"],
Address: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
ExpectedMessageHash: "0x5914ed2764eca2e6a41eb037feefd3d2e33d9af6225a9e7fe31ac943ff712c",
},
}

for _, test := range testSet {
Expand Down Expand Up @@ -422,6 +427,11 @@ func TestEncodeType(t *testing.T) {
TypeName: "Session",
ExpectedEncode: `Session(key:felt,expires:felt,root:merkletree)`,
},
{
TypedData: typedDataExamples["mail_StructArray"],
TypeName: "Mail",
ExpectedEncode: `Mail(from:Person,to:Person,posts_len:felt,posts:Post*)Person(name:felt,wallet:felt)Post(title:felt,content:felt)`,
},
}
for _, test := range testSet {
encode, err := encodeType(test.TypeName, test.TypedData.Types, test.TypedData.Revision)
Expand Down

0 comments on commit b0af345

Please sign in to comment.