From b0af345fad92a66f295eeda76094159ffd879bf6 Mon Sep 17 00:00:00 2001 From: thiagodeev Date: Tue, 26 Nov 2024 15:57:11 -0300 Subject: [PATCH] adds support to 'mail_StructArray' json example --- typed/typed.go | 23 +++++++++++------------ typed/typed_test.go | 12 +++++++++++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/typed/typed.go b/typed/typed.go index b2add74f..49d7875e 100644 --- a/typed/typed.go +++ b/typed/typed.go @@ -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 } @@ -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) @@ -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 diff --git a/typed/typed_test.go b/typed/typed_test.go index fa9a37d2..50925cc3 100644 --- a/typed/typed_test.go +++ b/typed/typed_test.go @@ -63,7 +63,7 @@ func TestMain(m *testing.M) { "example_baseTypes", // "example_enum", "example_presetTypes", - // "mail_StructArray", + "mail_StructArray", "session_MerkleTree", // "v1Nested", } @@ -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 { @@ -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)