-
Notifications
You must be signed in to change notification settings - Fork 170
/
int.go
280 lines (228 loc) · 6.67 KB
/
int.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
//
// Copyright 2019 Centrifuge GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package types
import (
"encoding/json"
"fmt"
"math/big"
"github.com/centrifuge/go-substrate-rpc-client/v4/scale"
)
// I8 is a signed 8-bit integer
type I8 int8
// NewI8 creates a new I8 type
func NewI8(i int8) I8 {
return I8(i)
}
// UnmarshalJSON fills i with the JSON encoded byte array given by b
func (i *I8) UnmarshalJSON(b []byte) error {
var tmp int8
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
*i = I8(tmp)
return nil
}
// MarshalJSON returns a JSON encoded byte array of i
func (i I8) MarshalJSON() ([]byte, error) {
return json.Marshal(int8(i))
}
// I16 is a signed 16-bit integer
type I16 int16
// NewI16 creates a new I16 type
func NewI16(i int16) I16 {
return I16(i)
}
// UnmarshalJSON fills i with the JSON encoded byte array given by b
func (i *I16) UnmarshalJSON(b []byte) error {
var tmp int16
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
*i = I16(tmp)
return nil
}
// MarshalJSON returns a JSON encoded byte array of i
func (i I16) MarshalJSON() ([]byte, error) {
return json.Marshal(int16(i))
}
// I32 is a signed 32-bit integer
type I32 int32
// NewI32 creates a new I32 type
func NewI32(i int32) I32 {
return I32(i)
}
// UnmarshalJSON fills i with the JSON encoded byte array given by b
func (i *I32) UnmarshalJSON(b []byte) error {
var tmp int32
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
*i = I32(tmp)
return nil
}
// MarshalJSON returns a JSON encoded byte array of i
func (i I32) MarshalJSON() ([]byte, error) {
return json.Marshal(int32(i))
}
// I64 is a signed 64-bit integer
type I64 int64
// NewI64 creates a new I64 type
func NewI64(i int64) I64 {
return I64(i)
}
// UnmarshalJSON fills i with the JSON encoded byte array given by b
func (i *I64) UnmarshalJSON(b []byte) error {
var tmp int64
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
*i = I64(tmp)
return nil
}
// MarshalJSON returns a JSON encoded byte array of i
func (i I64) MarshalJSON() ([]byte, error) {
return json.Marshal(int64(i))
}
// I128 is a signed 128-bit integer, it is represented as a big.Int in Go.
type I128 struct {
*big.Int
}
// NewI128 creates a new I128 type
func NewI128(i big.Int) I128 {
return I128{&i}
}
// Decode implements decoding as per the Scale specification
func (i *I128) Decode(decoder scale.Decoder) error {
bs := make([]byte, 16)
err := decoder.Read(bs)
if err != nil {
return err
}
// reverse bytes, scale uses little-endian encoding, big.int's bytes are expected in big-endian
scale.Reverse(bs)
b, err := IntBytesToBigInt(bs)
if err != nil {
return err
}
// deal with zero differently to get a nil representation (this is how big.Int deals with 0)
if b.Sign() == 0 {
*i = I128{big.NewInt(0)}
return nil
}
*i = I128{b}
return nil
}
// Encode implements encoding as per the Scale specification
func (i I128) Encode(encoder scale.Encoder) error {
b, err := BigIntToIntBytes(i.Int, 16)
if err != nil {
return err
}
// reverse bytes, scale uses little-endian encoding, big.int's bytes are expected in big-endian
scale.Reverse(b)
return encoder.Write(b)
}
// I256 is a signed 256-bit integer, it is represented as a big.Int in Go.
type I256 struct {
*big.Int
}
// NewI256 creates a new I256 type
func NewI256(i big.Int) I256 {
return I256{&i}
}
// Decode implements decoding as per the Scale specification
func (i *I256) Decode(decoder scale.Decoder) error {
bs := make([]byte, 32)
err := decoder.Read(bs)
if err != nil {
return err
}
// reverse bytes, scale uses little-endian encoding, big.int's bytes are expected in big-endian
scale.Reverse(bs)
b, err := IntBytesToBigInt(bs)
if err != nil {
return err
}
// deal with zero differently to get a nil representation (this is how big.Int deals with 0)
if b.Sign() == 0 {
*i = I256{big.NewInt(0)}
return nil
}
*i = I256{b}
return nil
}
// Encode implements encoding as per the Scale specification
func (i I256) Encode(encoder scale.Encoder) error {
b, err := BigIntToIntBytes(i.Int, 32)
if err != nil {
return err
}
// reverse bytes, scale uses little-endian encoding, big.int's bytes are expected in big-endian
scale.Reverse(b)
return encoder.Write(b)
}
// BigIntToIntBytes encodes the given big.Int to a big endian encoded signed integer byte slice of the given byte
// length, using a two's complement if the big.Int is negative and returning an error if the given big.Int would be
// bigger than the maximum positive (negative) numbers the byte slice of the given length could hold
func BigIntToIntBytes(i *big.Int, bytelen int) ([]byte, error) {
res := make([]byte, bytelen)
maxNeg := big.NewInt(0).Exp(big.NewInt(2), big.NewInt(int64(bytelen*8-1)), nil)
maxPos := big.NewInt(0).Sub(maxNeg, big.NewInt(1))
if i.Sign() >= 0 {
if i.CmpAbs(maxPos) > 0 {
return nil, fmt.Errorf("cannot encode big.Int to []byte: given big.Int exceeds highest positive number "+
"%v for an int with %v bits", maxPos, bytelen*8)
}
bs := i.Bytes()
copy(res[len(res)-len(bs):], bs)
return res, nil
}
// negative, two's complement
if i.CmpAbs(maxNeg) > 0 {
return nil, fmt.Errorf("cannot encode big.Int to []byte: given big.Int exceeds highest negative number -"+
"%v for an int with %v bits", maxNeg, bytelen*8)
}
i = big.NewInt(0).Add(i, big.NewInt(1))
bs := i.Bytes()
copy(res[len(res)-len(bs):], bs)
// apply not to every byte
for j, b := range res {
res[j] = ^b
}
return res, nil
}
// IntBytesToBigInt decodes the given byte slice containing a big endian encoded signed integer to a big.Int, using a
// two's complement if the most significant bit is 1
func IntBytesToBigInt(b []byte) (*big.Int, error) {
if len(b) == 0 {
return nil, fmt.Errorf("cannot decode an empty byte slice")
}
if b[0]&0x80 == 0x00 {
// positive
return big.NewInt(0).SetBytes(b), nil
}
// negative, two's complement
t := make([]byte, len(b))
copy(t, b)
// apply not to every byte
for j, b := range b {
t[j] = ^b
}
res := big.NewInt(0).SetBytes(t)
res = res.Add(res, big.NewInt(1))
res = res.Neg(res)
return res, nil
}