From 37c98f5baffcf92579000e7e62109965ab056d41 Mon Sep 17 00:00:00 2001 From: Ferran Borreguero Date: Tue, 11 Apr 2023 10:42:51 +0200 Subject: [PATCH] Add rest of types for eip-712 --- signing/eip712.go | 25 +++++++++++++++++++++---- signing/eip712_test.go | 25 +++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/signing/eip712.go b/signing/eip712.go index 5320e168..4ade5cb2 100644 --- a/signing/eip712.go +++ b/signing/eip712.go @@ -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" @@ -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) diff --git a/signing/eip712_test.go b/signing/eip712_test.go index 00c6871a..9ffd282b 100644 --- a/signing/eip712_test.go +++ b/signing/eip712_test.go @@ -1,6 +1,7 @@ package signing import ( + "math/big" "testing" "github.com/stretchr/testify/require" @@ -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"` @@ -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}, @@ -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()) +}