Skip to content

Commit

Permalink
Implements typedData unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Oct 16, 2024
1 parent 64d2145 commit 4df1af7
Show file tree
Hide file tree
Showing 4 changed files with 397 additions and 4 deletions.
35 changes: 35 additions & 0 deletions typed/tests/baseExample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"types": {
"StarkNetDomain": [
{ "name": "name", "type": "felt" },
{ "name": "version", "type": "felt" },
{ "name": "chainId", "type": "felt" }
],
"Person": [
{ "name": "name", "type": "felt" },
{ "name": "wallet", "type": "felt" }
],
"Mail": [
{ "name": "from", "type": "Person" },
{ "name": "to", "type": "Person" },
{ "name": "contents", "type": "felt" }
]
},
"primaryType": "Mail",
"domain": {
"name": "StarkNet Mail",
"version": "1",
"chainId": 1
},
"message": {
"from": {
"name": "Cow",
"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
},
"to": {
"name": "Bob",
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
},
"contents": "Hello, Bob!"
}
}
75 changes: 71 additions & 4 deletions typed/typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package typed
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"regexp"
Expand Down Expand Up @@ -50,8 +51,8 @@ type TypedData struct {

type Domain struct {
Name string
Version string
ChainId string
Version json.Number
ChainId json.Number
Revision uint8 `json:"contains,omitempty"`
}

Expand Down Expand Up @@ -88,9 +89,9 @@ func (dm Domain) FmtDefinitionEncoding(field string) (fmtEnc []*big.Int) {
case "name":
processStrToBig(dm.Name)
case "version":
processStrToBig(dm.Version)
processStrToBig(dm.Version.String())
case "chainId":
processStrToBig(dm.ChainId)
processStrToBig(dm.ChainId.String())
}
return fmtEnc
}
Expand Down Expand Up @@ -300,3 +301,69 @@ func (td TypedData) EncodeType(typeName string) (enc string, err error) {

return enc, nil
}

func (typedData *TypedData) UnmarshalJSON(data []byte) error {
var dec map[string]interface{}
if err := json.Unmarshal(data, &dec); err != nil {
return err
}

// primaryType
rawPrimaryType, ok := dec["primaryType"]
if !ok {
return fmt.Errorf("invalid typedData json: missing field 'primaryType'")
}
primaryType, ok := rawPrimaryType.(string)
if !ok {
return fmt.Errorf("failed to unmarshal 'primaryType', it's not a string")
}

// domain
rawDomain, ok := dec["domain"]
if !ok {
return fmt.Errorf("invalid typedData json: missing field 'domain'")
}
bytesDomain, err := json.Marshal(rawDomain)
if err != nil {
return err
}
var domain Domain
if err := json.Unmarshal(bytesDomain, &domain); err != nil {
return err
}

// types
rawTypes, err := utils.UnwrapJSON(dec, "types")
if err != nil {
return err
}
var types []TypeDefinition
for key, value := range rawTypes {
bytesValue, err := json.Marshal(value)
if err != nil {
return err
}

var params []TypeParameter
if err := json.Unmarshal(bytesValue, &params); err != nil {
return err
}

typeDef := TypeDefinition{
Name: key,
Parameters: params,
}

types = append(types, typeDef)
}

resultTypedData, err := NewTypedData(types, primaryType, domain)
if err != nil {
return err
}

*typedData = resultTypedData
return nil

// TODO: implement typedMessage unmarshal
}
11 changes: 11 additions & 0 deletions typed/typed_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package typed

import (
"encoding/json"
"fmt"
"math/big"
"os"
"testing"

"github.com/NethermindEth/starknet.go/utils"
Expand Down Expand Up @@ -89,6 +91,15 @@ func MockTypedData() (ttd TypedData, err error) {
return ttd, err
}

func TestGeneral_Unmarshal(t *testing.T) {
content, err := os.ReadFile("./tests/baseExample.json")
require.NoError(t, err)

var typedData TypedData
err = json.Unmarshal(content, &typedData)
require.NoError(t, err)
}

// TestGeneral_GetMessageHash tests the GetMessageHash function.
//
// It creates a mock TypedData and sets up a test case for hashing a mail message.
Expand Down
Loading

0 comments on commit 4df1af7

Please sign in to comment.