Skip to content

Commit

Permalink
Add rest of types for eip-712
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt committed Apr 11, 2023
1 parent 5c66b51 commit 37c98f5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
25 changes: 21 additions & 4 deletions signing/eip712.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ func decodeTypes(val reflect.Type, result *map[string][]*EIP712Type) string {
case reflect.Struct:
return decodeStructType(val, result)

case reflect.String:
return "string"

case reflect.Uint8:
return "uint8"

case reflect.Uint16:
return "uint16"

case reflect.Uint32:
return "uint32"

case reflect.Uint64:
return "uint64"

Expand Down Expand Up @@ -139,16 +151,21 @@ func structToMap(v reflect.Value) map[string]interface{} {
field := typ.Field(i)
fieldValue := v.Field(i)

if fieldValue.Kind() == reflect.Ptr {
fieldValue = fieldValue.Elem()
}

fieldName := field.Name
// use a tag as a name (if any)
if tagVal := field.Tag.Get("eip712"); tagVal != "" {
fieldName = tagVal
}

if field.Type == bigIntT {
result[fieldName] = fieldValue.Interface()
continue
}

if fieldValue.Kind() == reflect.Ptr {
fieldValue = fieldValue.Elem()
}

if fieldValue.Kind() == reflect.Struct {
// the field is a struct, handle recursively as another map
result[fieldName] = structToMap(fieldValue)
Expand Down
25 changes: 23 additions & 2 deletions signing/eip712_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package signing

import (
"math/big"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -9,6 +10,7 @@ import (

type Message struct {
A uint64 `eip712:"a"`
C *big.Int `eip712:"c"`
Msg1 *Message2 `eip712:"msg1"`
Msg2 []Message2 `eip712:"msg2"`
Msg3 [3]Message2 `eip712:"msg3"`
Expand All @@ -19,15 +21,16 @@ type Message2 struct {
Addr ethgo.Address `eip712:"addr"`
}

func TestBuildMessage(t *testing.T) {
func TestBuildMessage_Encode(t *testing.T) {
domain := &EIP712Domain{
Name: "name1",
}

b := NewEIP712MessageBuilder[Message](domain)
require.Equal(t, "Message(uint64 a,Message2 msg1,Message2[] msg2,Message2[3] msg3)Message2(uint64 b,address addr)", b.GetEncodedType())
require.Equal(t, "Message(uint64 a,uint256 c,Message2 msg1,Message2[] msg2,Message2[3] msg3)Message2(uint64 b,address addr)", b.GetEncodedType())

msg := &Message{
C: big.NewInt(1),
Msg1: &Message2{},
Msg2: []Message2{
{B: 1},
Expand All @@ -47,3 +50,21 @@ func TestBuildMessage(t *testing.T) {
_, err := typedMsg.Hash()
require.NoError(t, err)
}

func TestBuildMessage_BasicTypes(t *testing.T) {
domain := &EIP712Domain{
Name: "name1",
}

type Message struct {
A uint64
B uint32
C uint16
D uint8
E [32]byte
F string
}

b := NewEIP712MessageBuilder[Message](domain)
require.Equal(t, "Message(uint64 A,uint32 B,uint16 C,uint8 D,[32]byte E,string F)", b.GetEncodedType())
}

0 comments on commit 37c98f5

Please sign in to comment.