Skip to content

Commit

Permalink
Add BitVec encode
Browse files Browse the repository at this point in the history
  • Loading branch information
freehere107 committed Jul 26, 2024
1 parent 0031c15 commit 51a86c5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 11 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>", len(u8a)) + utiles.BytesToHex(u8a)
}
5 changes: 2 additions & 3 deletions types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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"))
}
}
Expand Down

0 comments on commit 51a86c5

Please sign in to comment.