-
Notifications
You must be signed in to change notification settings - Fork 2
/
primitive.go
164 lines (145 loc) · 3.52 KB
/
primitive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package stafi_decoder
import (
"bytes"
"encoding/binary"
"io"
"math"
"github.com/itering/scale.go/utiles"
"github.com/shopspring/decimal"
)
type Compact struct {
ScaleDecoder
CompactLength int `json:"compact_length"`
CompactBytes []byte `json:"compact_bytes"`
}
func (c *Compact) ProcessCompactBytes() []byte {
compactByte := c.NextBytes(1)
var byteMod = 0
if len(compactByte) != 0 {
byteMod = int(compactByte[0]) % 4
}
if byteMod == 0 {
c.CompactLength = 1
} else if byteMod == 1 {
c.CompactLength = 2
} else if byteMod == 2 {
c.CompactLength = 4
} else {
c.CompactLength = 5 + ((int(compactByte[0]) - 3) / 4)
}
if c.CompactLength == 1 {
c.CompactBytes = compactByte
} else if utiles.IntInSlice(c.CompactLength, []int{2, 4}) {
c.CompactBytes = append(compactByte[:], c.NextBytes(c.CompactLength - 1)[:]...)
} else {
c.CompactBytes = c.NextBytes(c.CompactLength - 1)
}
return c.CompactBytes
}
func (c *Compact) Process() {
c.ProcessCompactBytes()
if c.SubType != "" {
s := ScaleDecoder{TypeString: c.SubType, Data: ScaleBytes{Data: c.CompactBytes}}
byteData := s.ProcessAndUpdateData(c.SubType)
if c.CompactLength <= 4 {
switch v := byteData.(type) {
case uint64:
c.Value = v / 4
case uint32:
c.Value = uint64(v / 4)
case int:
c.Value = uint64(v / 4)
case decimal.Decimal:
c.Value = v.Div(decimal.New(4, 0)).Floor()
case string:
c.Value = decimal.RequireFromString(v).Div(decimal.New(4, 0)).Floor()
default:
c.Value = byteData
}
} else {
c.Value = byteData
}
} else {
c.Value = c.CompactBytes
}
}
type CompactU32 struct {
Compact
Reader io.Reader
}
func (c *CompactU32) Init(data ScaleBytes, option *ScaleDecoderOption) {
c.TypeString = "Compact<u32>"
c.ScaleDecoder.Init(data, option)
}
func (c *CompactU32) Process() {
c.ProcessCompactBytes()
buf := &bytes.Buffer{}
c.Reader = buf
_, _ = buf.Write(c.CompactBytes)
b := make([]byte, 8)
_, _ = c.Reader.Read(b)
c.Value = int(binary.LittleEndian.Uint64(b))
if c.CompactLength <= 4 {
c.Value = int(c.Value.(int)) / 4
}
}
func (c *CompactU32) Encode(value int) ScaleBytes {
if value <= 63 {
bs := make([]byte, 4)
binary.LittleEndian.PutUint32(bs, uint32(value<<2))
c.Data.Data = bs[0:1]
} else if value <= 16383 {
bs := make([]byte, 4)
binary.LittleEndian.PutUint32(bs, uint32(value<<2)|1)
c.Data.Data = bs[0:2]
} else if value <= 1073741823 {
bs := make([]byte, 4)
binary.LittleEndian.PutUint32(bs, uint32(value<<2)|2)
c.Data.Data = bs
}
return c.Data
}
type Option struct {
ScaleDecoder
}
func (o *Option) Process() {
optionType := o.NextBytes(1)
if o.SubType != "" && utiles.BytesToHex(optionType) != "00" {
o.Value = o.ProcessAndUpdateData(o.SubType)
}
}
type Bytes struct {
ScaleDecoder
}
func (b *Bytes) Init(data ScaleBytes, option *ScaleDecoderOption) {
b.TypeString = "Vec<u8>"
b.ScaleDecoder.Init(data, option)
}
func (b *Bytes) Process() {
length := b.ProcessAndUpdateData("Compact<u32>").(int)
value := b.NextBytes(length)
if utiles.IsASCII(value) {
b.Value = string(value)
} else {
b.Value = utiles.BytesToHex(value)
}
}
type String struct {
ScaleDecoder
}
func (b *String) Process() {
length := b.ProcessAndUpdateData("Compact<u32>").(int)
value := b.NextBytes(length)
if utiles.IsASCII(value) {
b.Value = string(value)
} else {
b.Value = utiles.BytesToHex(value)
}
}
type BitVec struct {
Compact
}
func (b *BitVec) Process() {
length := b.ProcessAndUpdateData("Compact<u32>").(int)
b.Value = b.NextBytes(int(math.Ceil(float64(length) / 8)))
}