From dbe2cd62d0d8f4125ace3c757e0c60bedafb4aa7 Mon Sep 17 00:00:00 2001 From: thiagodeev Date: Thu, 17 Oct 2024 02:01:44 -0300 Subject: [PATCH] Rename files and create types file --- {typed => typedData}/tests/baseExample.json | 0 typed/typed.go => typedData/typedData.go | 72 ++++++++++++++++++- .../typedData_test.go | 2 +- typedData/types.go | 31 ++++++++ 4 files changed, 101 insertions(+), 4 deletions(-) rename {typed => typedData}/tests/baseExample.json (100%) rename typed/typed.go => typedData/typedData.go (84%) rename typed/typed_test.go => typedData/typedData_test.go (99%) create mode 100644 typedData/types.go diff --git a/typed/tests/baseExample.json b/typedData/tests/baseExample.json similarity index 100% rename from typed/tests/baseExample.json rename to typedData/tests/baseExample.json diff --git a/typed/typed.go b/typedData/typedData.go similarity index 84% rename from typed/typed.go rename to typedData/typedData.go index b03f79fb..36b8feaa 100644 --- a/typed/typed.go +++ b/typedData/typedData.go @@ -1,4 +1,4 @@ -package typed +package typedData import ( "bytes" @@ -57,8 +57,8 @@ type Domain struct { } type TypeDefinition struct { - Name string `json:"-"` - Encoding *big.Int + Name string `json:"-"` + Encoding *big.Int // should be string Parameters []TypeParameter } @@ -309,6 +309,72 @@ func (td TypedData) EncodeType(typeName string) (enc string, err error) { return enc, nil } +func EncodeData(typedData TypedData, typeName string) (enc string, err error) { + // recebe types, type name + // + + customTypesEncodeResp := make(map[string]string) + + var encodeType func(typeName string, typeDef TypeDefinition) (result string, err error) + encodeType = func(typeName string, typeDef TypeDefinition) (result string, err error) { + var buf bytes.Buffer + + buf.WriteString(typeName) + buf.WriteString("(") + + var ok bool + + for i, param := range typeDef.Parameters { + buf.WriteString(fmt.Sprintf("%s:%s", param.Name, param.Type)) + if i != (len(typeDef.Parameters) - 1) { + buf.WriteString(",") + } + // e.g.: "felt" or "felt*" + if slices.Contains(REVISION_0_TYPES, param.Type) || slices.Contains(REVISION_0_TYPES, fmt.Sprintf("%s*", param.Type)) { + continue + } else if _, ok = customTypesEncodeResp[param.Type]; !ok { + var customTypeDef TypeDefinition + if customTypeDef, ok = typedData.Types[param.Type]; !ok { //OBS: this is wrong on V1 + return "", fmt.Errorf("can't parse type %s from types %v", param.Type, typedData.Types) + } + customTypesEncodeResp[param.Type], err = encodeType(param.Type, customTypeDef) + if err != nil { + return "", err + } + } + } + buf.WriteString(")") + + return buf.String(), nil + } + + var typeDef TypeDefinition + var ok bool + if typeDef, ok = typedData.Types[typeName]; !ok { + return "", fmt.Errorf("can't parse type %s from types %v", typeName, typedData.Types) + } + enc, err = encodeType(typeName, typeDef) + if err != nil { + return "", err + } + + // appends the custom types' encode + if len(customTypesEncodeResp) > 0 { + // sort the types + keys := make([]string, 0, len(customTypesEncodeResp)) + for key := range customTypesEncodeResp { + keys = append(keys, key) + } + slices.Sort(keys) + + for _, key := range keys { + enc = enc + customTypesEncodeResp[key] + } + } + + return enc, nil +} + func (typedData *TypedData) UnmarshalJSON(data []byte) error { var dec map[string]interface{} if err := json.Unmarshal(data, &dec); err != nil { diff --git a/typed/typed_test.go b/typedData/typedData_test.go similarity index 99% rename from typed/typed_test.go rename to typedData/typedData_test.go index b7304651..997bea5a 100644 --- a/typed/typed_test.go +++ b/typedData/typedData_test.go @@ -1,4 +1,4 @@ -package typed +package typedData import ( "encoding/json" diff --git a/typedData/types.go b/typedData/types.go new file mode 100644 index 00000000..619c059e --- /dev/null +++ b/typedData/types.go @@ -0,0 +1,31 @@ +package typedData + +import "math/big" + +type ( + Felt string + Bool bool + String string + Selector string + U128 big.Int + I128 big.Int + ContractAddress string + ClassHash string + Timestamp U128 + Shortstring string +) + +type U256 struct { + Low U128 + High U128 +} + +type TokenAmount struct { + TokenAddress ContractAddress `json:"token_address"` + Amount U256 +} + +type NftId struct { + CollectionAddress ContractAddress `json:"collection_address"` + TokenID U256 `json:"token_id"` +}