From 51a86c5e82027b539bf543d8ed1bdf5c54a0ab51 Mon Sep 17 00:00:00 2001 From: freehere107 Date: Fri, 26 Jul 2024 14:41:46 +0800 Subject: [PATCH] Add BitVec encode --- types/types.go | 11 +++++++++++ types/types_test.go | 5 ++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/types/types.go b/types/types.go index 28f2bbc..b66d66d 100644 --- a/types/types.go +++ b/types/types.go @@ -493,3 +493,14 @@ func (b *BitVec) Process() { func (b *BitVec) TypeStructString() string { return "BitVec" } + +func (b *BitVec) Encode(value string) string { + value = strings.TrimPrefix(value, "0b") + values := strings.Split(value, "_") + var u8a []byte + for _, v := range values { + b, _ := strconv.ParseUint(v, 2, 8) + u8a = append(u8a, byte(b)) + } + return Encode("Compact", len(u8a)) + utiles.BytesToHex(u8a) +} diff --git a/types/types_test.go b/types/types_test.go index e3d038c..4f0042b 100644 --- a/types/types_test.go +++ b/types/types_test.go @@ -112,7 +112,6 @@ func TestBalance(t *testing.T) { assert.Equal(t, raw, Encode("Balance", m.ProcessAndUpdateData("Balance"))) } -// func TestRegistration(t *testing.T) { raw := "04010000000200a0724e180900000000000000000000000d505552455354414b452d30310e507572655374616b65204c74641b68747470733a2f2f7777772e707572657374616b652e636f6d2f000000000d40707572657374616b65636f" m := ScaleDecoder{} @@ -352,8 +351,8 @@ func TestEmptyVec(t *testing.T) { func TestBitVec(t *testing.T) { m := ScaleDecoder{} r := []string{"0b00010111", "0b01111011", "0b00110011"} - for i, v := range []string{"0x0817", "0x087b", "0x0c33"} { - m.Init(scaleBytes.ScaleBytes{Data: utiles.HexToBytes(v)}, nil) + for i := range []string{"0x0817", "0x087b", "0x0c33"} { + m.Init(scaleBytes.ScaleBytes{Data: utiles.HexToBytes(utiles.AddHex(Encode("BitVec", r[i])))}, nil) assert.EqualValues(t, r[i], m.ProcessAndUpdateData("BitVec")) } }