From 908ae59bc6d3e794fe4e4c83fa920ec57e735fbb Mon Sep 17 00:00:00 2001 From: freehere107 Date: Thu, 7 Mar 2024 18:07:56 +0800 Subject: [PATCH] Fix fixed array encode --- types/FixedArray.go | 2 +- types/FixedU8.go | 12 ++++++++++-- types/Uint.go | 2 ++ types/types_test.go | 7 +++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/types/FixedArray.go b/types/FixedArray.go index 68194d7..4f2815f 100644 --- a/types/FixedArray.go +++ b/types/FixedArray.go @@ -66,7 +66,7 @@ func (f *FixedArray) Encode(value interface{}) string { case reflect.String: valueStr := value.(string) if strings.HasPrefix(valueStr, "0x") { - return valueStr + return utiles.TrimHex(valueStr) } else { return utiles.BytesToHex([]byte(valueStr)) } diff --git a/types/FixedU8.go b/types/FixedU8.go index 3a4a7ef..bc3808c 100644 --- a/types/FixedU8.go +++ b/types/FixedU8.go @@ -20,8 +20,16 @@ func (s *FixedU8) Process() { } } -func (s *FixedU8) Encode(value string) string { - return utiles.TrimHex(value) +func (s *FixedU8) Encode(value interface{}) string { + switch value.(type) { + case string: + return utiles.TrimHex(value.(string)) + case []byte: + return utiles.TrimHex(utiles.BytesToHex(value.([]byte))) + default: + panic("type error,only support string or []byte") + } + return "" } func (s *FixedU8) TypeStructString() string { diff --git a/types/Uint.go b/types/Uint.go index b619d98..a2189ec 100644 --- a/types/Uint.go +++ b/types/Uint.go @@ -35,6 +35,8 @@ func (u *U8) Encode(value interface{}) string { i = int(v) case float64: i = int(v) + case uint8: + i = int(v) } return utiles.U8Encode(i) } diff --git a/types/types_test.go b/types/types_test.go index 871b7c0..3a2fefc 100644 --- a/types/types_test.go +++ b/types/types_test.go @@ -357,3 +357,10 @@ func TestBitVec(t *testing.T) { assert.EqualValues(t, r[i], m.ProcessAndUpdateData("BitVec")) } } + +func TestFixedArray(t *testing.T) { + ts := []string{"[u8; 16]", "[u8; 16]", "[u8;16]"} + for i, v := range []interface{}{[]byte{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, []byte{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, "0x02020202020202020202020202020202"} { + assert.Equal(t, Encode(ts[i], v), "02020202020202020202020202020202", "TestFixedArray Encode fail %s", ts[i]) + } +}