Skip to content

Commit

Permalink
Rename files and create types file
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Oct 17, 2024
1 parent 5459a56 commit dbe2cd6
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 4 deletions.
File renamed without changes.
72 changes: 69 additions & 3 deletions typed/typed.go → typedData/typedData.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package typed
package typedData

import (
"bytes"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion typed/typed_test.go → typedData/typedData_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package typed
package typedData

import (
"encoding/json"
Expand Down
31 changes: 31 additions & 0 deletions typedData/types.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit dbe2cd6

Please sign in to comment.