Skip to content

Commit

Permalink
Adds revision.go file, new Revision field of TypedData
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Oct 21, 2024
1 parent 5494ea4 commit 0fdc98b
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 17 deletions.
101 changes: 101 additions & 0 deletions typedData/revision.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package typedData

import (
"encoding/json"
"fmt"

"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/curve"
)

var (
// There is also an array version of each type. The array is defined like this: 'type' + '*' (e.g.: "felt*", "bool*", "string*"...)
revision_0_basic_types []string = []string{
"felt",
"bool",
"string", //up to 31 ASCII characters
"selector",
"merkletree",
}

// Revision 1 includes all types from Revision 0 plus these. The only difference is that for Revision 1 "string" represents an
// arbitrary size string instead of having a 31 ASCII characters limit in Revision 0; for this limit, use the new type "shortstring" instead.
//
// There is also an array version of each type. The array is defined like this: 'type' + '*' (e.g.: "ClassHash*", "timestamp*", "shortstring*"...)
revision_1_basic_types []string = []string{
//TODO: enum?
"u128",
"i128",
"ContractAddress",
"ClassHash",
"timestamp",
"shortstring",
}
)

type Revision struct {
Domain String
HashMethod func(felts ...*felt.Felt) *felt.Felt
//TODO: hashMerkleMethod ?
Types RevisionTypes
}

type RevisionTypes struct {
Basic []string
Preset map[string]any
}

func NewRevision(version uint8) (rev Revision, err error) {
preset := make(map[string]any)

switch version {
case 0:
rev = Revision{
Domain: "StarkNetDomain",
HashMethod: curve.PedersenArray,
Types: RevisionTypes{
Basic: revision_0_basic_types,
Preset: preset,
},
}
return rev, nil
case 1:
preset, err = getRevisionV1PresetTypes()
if err != nil {
return rev, fmt.Errorf("error getting revision 1 preset types: %w", err)
}
rev = Revision{
Domain: "StarknetDomain",
HashMethod: curve.PoseidonArray,
Types: RevisionTypes{
Basic: append(revision_1_basic_types, revision_0_basic_types...),
Preset: preset,
},
}
return rev, nil
default:
return rev, fmt.Errorf("invalid revision version")
}
}

func getRevisionV1PresetTypes() (result map[string]any, err error) {
type RevV1PresetTypes struct {
NftId NftId
TokenAmount TokenAmount
U256 U256
}

var preset RevV1PresetTypes

bytes, err := json.Marshal(preset)
if err != nil {
return result, err
}

err = json.Unmarshal(bytes, &result)
if err != nil {
return result, err
}

return result, err
}
31 changes: 19 additions & 12 deletions typedData/typedData.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,29 @@ var (
)

type TypedData struct {
Types map[string]TypeDefinition
PrimaryType string
Domain Domain
Message map[string]any
Types map[string]TypeDefinition `json:"types"`
PrimaryType string `json:"primaryType"`
Domain Domain `json:"domain"`
Message map[string]any `json:"message"`
Revision Revision
}

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

type TypeDefinition struct {
Name string `json:"-"`
Encoding *big.Int
Name string `json:"-"`
Encoding *big.Int //TODO: maybe remove this
Parameters []TypeParameter
}

type TypeParameter struct {
Name string
Type string
Name string `json:"name"`
Type string `json:"type"`
Contains string `json:"contains,omitempty"`
}

Expand Down Expand Up @@ -147,11 +148,17 @@ func NewTypedData(types []TypeDefinition, primaryType string, domain Domain, mes
return td, fmt.Errorf("error unmarshalling the message: %w", err)
}

revision, err := NewRevision(domain.Revision)
if err != nil {
return td, fmt.Errorf("error getting revision: %w", err)
}

td = TypedData{
Types: typesMap,
PrimaryType: primaryType,
Domain: domain,
Message: messageMap,
Revision: revision,
}
if _, ok := td.Types[primaryType]; !ok {
return td, fmt.Errorf("invalid primary type: %s", primaryType)
Expand Down
10 changes: 5 additions & 5 deletions typedData/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ type (
Bool bool
String string
Selector string
U128 big.Int
I128 big.Int
U128 *big.Int
I128 *big.Int
ContractAddress string
ClassHash string
Timestamp U128
Shortstring string
)

type U256 struct {
Low U128
High U128
Low U128 `json:"low"`
High U128 `json:"high"`
}

type TokenAmount struct {
TokenAddress ContractAddress `json:"token_address"`
Amount U256
Amount U256 `json:"amount"`
}

type NftId struct {
Expand Down

0 comments on commit 0fdc98b

Please sign in to comment.